Добрый день,программисты! Подскажите,пожалуйста,почему советник открывает покупки с шагом,а продажи разом? Спасибо
extern double Lots = 0.1;
extern int TakeProfit = 5;
extern int Step = 5;
extern double Multiplier = 2;
extern int Slippage = 5;
extern int Magic = 123;
extern int Averaging_Level = 5;
extern int Total = 10;
int ticket;
double price, TP, lastlot;
int init()
{
if (Digits == 3 || Digits == 5)
{
TakeProfit *=10;
Step *=10;
Slippage *=10;
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
//int deinit()
// {
// }
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
int start()
{
double Balance = AccountBalance();
double Profit = AccountProfit();
double Equity = AccountEquity();
// double Margin = AccountMargin();
double FreeMargin = fabs(AccountFreeMargin());
double MarginCheck = AccountFreeMarginCheck(_Symbol, OP_BUY, Lots);
double Margin = fabs(FreeMargin-MarginCheck);
double Punkt = Point*10/Ask*Lots*100000;
int Leverage = AccountLeverage();
int Kontrakt = (Lots*100000);
int STOP_OUT = AccountStopoutLevel();
int MARGIN_CALL = AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL);
double MC_ostatok = (Margin*MARGIN_CALL)/100;
double Stop_ostatok = (Margin*STOP_OUT)/100;
double Uroven = (Equity/Margin )*100;
int MC_dist = (Balance - MC_ostatok);
int Stop_dist = (Balance - Stop_ostatok);
int BUY = CountBuyOrder();
int SELL = CountSellOrder();
Comment(
"Баланс = ",DoubleToString(Balance,2),"\n"
"Прибыль = ",DoubleToString(Profit,2),"\n"
"Средства = ",DoubleToString(Equity,2),"\n"
"Маржа = ",DoubleToString(Margin,2),"\n"
"Свободная Маржа = ",DoubleToString(FreeMargin,2),"\n"
// "MarginChek = ",MarginCheck,"\n"
// "Размер контракта = ",CONTRACT_SIZE,"\n"
// "Тарифный контракт = ",RateContract,"\n"
"Уровень = ",DoubleToStr(Uroven,2) + " %\n"
"Уровень MARGIN CALL = ",DoubleToStr(MARGIN_CALL,0) + " %\n"
"Уровень Stop Out = " + DoubleToStr(STOP_OUT,0) + " %\n"
"МС_остаток = ",DoubleToString(MC_ostatok,2),"\n"
"Стоп_остаток = ",DoubleToString(Stop_ostatok,2),"\n"
"Контракт = ",DoubleToString(Kontrakt,0),"\n"
"Пункт = ",DoubleToString(Punkt),"\n"
"MC_пункт = ",DoubleToString(MC_dist,2),"\n"
"Stop_пункт = ",DoubleToString(Stop_dist,2),"\n"
"BUY = ",DoubleToString(CountBuyOrder(),0),"\n"
"SELL = ",DoubleToString(CountSellOrder(),0),"\n"
"Плечо = ",DoubleToString(Leverage,0));
if( CountTrades() == 0)
{
double ima = iMA(Symbol(), PERIOD_H1, 14, 0, MODE_SMA, PRICE_CLOSE, 1);
if (Ask > ima)
{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "", Magic, 0, Blue);
if (ticket > 0)
{
TP = NormalizeDouble(Ask + TakeProfit* Point, Digits);
OrderModify(ticket, OrderOpenPrice(),0, TP, 0 );
}
}
else if (Bid < ima)
{
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "", Magic, 0, Red);
if (ticket > 0)
{
TP = NormalizeDouble(Bid - TakeProfit* Point, Digits);
OrderModify(ticket, OrderOpenPrice(),0, TP, 0 );
}
}
}
if( CountTrades() < Averaging_Level)
{
int order_type = FindLastOrderType();
if (order_type == OP_BUY)
{
price = FindLastPrice(OP_BUY);
if(Ask <= price - Step*Point)
{
lastlot = FindLastLots(OP_BUY);
// lastlot = NormalizeDouble(lastlot * Multiplier, 2);
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "", Magic, 0, Blue);
if( ticket >0 )
ModifyOrders(OP_BUY);
}
}
else if (order_type == OP_SELL)
{
price = FindLastPrice(OP_SELL);
if(Bid <= price + Step*Point)
{
lastlot = FindLastLots(OP_SELL);
// lastlot = NormalizeDouble(lastlot * Multiplier, 2);
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "", Magic, 0, Red);
if( ticket >0 )
ModifyOrders(OP_SELL);
}
}
}
// if(Averaging_Level > 0 && CountTrades() >= Averaging_Level-1) lastlot = NormalizeDouble(lastlot, 2);
// else lastlot = NormalizeDouble(Lots * MathPow(Multiplier, CountTrades()), 2);
if( CountTrades() >= Averaging_Level && CountTrades() < Total )
{
int order_type = FindLastOrderType();
if (order_type == OP_BUY)
{
price = FindLastPrice(OP_BUY);
if(Ask <= price - Step*Point)
{
lastlot = FindLastLots(OP_BUY);
lastlot = NormalizeDouble(lastlot * Multiplier, 2);
ticket = OrderSend(Symbol(), OP_BUY, lastlot, Ask, Slippage, 0, 0, "", Magic, 0, Blue);
if( ticket >0 )
ModifyOrders(OP_BUY);
}
}
else if (order_type == OP_SELL)
{
price = FindLastPrice(OP_SELL);
if(Bid <= price + Step*Point)
{
lastlot = FindLastLots(OP_SELL);
lastlot = NormalizeDouble(lastlot * Multiplier, 2);
ticket = OrderSend(Symbol(), OP_SELL, lastlot, Bid, Slippage, 0, 0, "", Magic, 0, Red);
if( ticket >0 )
ModifyOrders(OP_SELL);
}
}
}
return(0);
}
//+------------------------------------------------------------------+
void ModifyOrders(int otype)
{
double avgprice = 0,
order_lots = 0;
price = 0;
for (int i = OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
{
price += OrderOpenPrice() * OrderLots();
order_lots += OrderLots();
}
}
}
avgprice = NormalizeDouble(price / order_lots, Digits);
if(otype == OP_BUY) TP = NormalizeDouble(avgprice + TakeProfit*Point,Digits);
if(otype == OP_SELL) TP = NormalizeDouble(avgprice - TakeProfit*Point,Digits);
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if( OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
OrderModify(OrderTicket(),OrderOpenPrice(), 0, TP, 0);
}
}
}
//+------------------------------------------------------------------+
double FindLastLots(int otype)
{
double oldlots;
int oldticket;
ticket = 0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
{
oldticket = OrderTicket();
if(oldticket > ticket)
{
oldlots = OrderLots();
ticket = oldticket;
}
}
}
}
return(oldlots);
}
//+------------------------------------------------------------------+
double FindLastPrice(int otype)
{
double oldopenprice;
int oldticket;
ticket = 0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
{
oldticket = OrderTicket();
if(oldticket > ticket)
{
oldopenprice = OrderOpenPrice();
ticket = oldticket;
}
}
}
}
return(oldopenprice);
}
//+------------------------------------------------------------------+
int CountTrades()
{
int count = 0;
for (int i = OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if( OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
int FindLastOrderType()
{
for (int i= OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if( OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
return(OrderType());
}
}
return(-1);
}
//+------------------------------------------------------------------+
int CountSellOrder() // сколько в рынке ордеров на SELL
{
int count = 0;
for (int i=OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS))
{
if (OrderMagicNumber() == Magic && OrderSymbol() == Symbol())
{
if (OrderType() == OP_SELL) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
int CountBuyOrder() // сколько в рынке ордеров на BUY
{
int count = 0;
for (int i=OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS))
{
if (OrderMagicNumber() == Magic && OrderSymbol() == Symbol())
{
if (OrderType() == OP_BUY) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+