Questions from Beginners MQL4 MT4 MetaTrader 4 - page 176

 

Guys, help me fix the EA. Instead of checking orders, it will buy without stopping, at the first signal, until it spends the entire deposit. Thank you all in advance

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
extern double Lots            = 1;
extern int    TakeProfit      = 300;
extern int    StopLoss        = 50;
extern int    Magic           = 111;
extern int    Slippage        = 3;
//+------------------------------------------------------------------+
extern string TMA             = "Параметры индикатора TMA";
extern string TimeFrame       = "current time frame";
extern int    HalfLength      = 56;
extern int    Price           = PRICE_CLOSE;
extern double ATRMultiplier   = 2.0;
extern int    ATRPeriod       = 100;
extern bool   Interpolate     = true;
//+------------------------------------------------------------------+
double PriceHigh,PriceLow,SL,TP,OrderMagicNumber;
int ticket;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits==3 || Digits==5)
     {
      TakeProfit *= 10;
      StopLoss   *= 10;
      Slippage   *= 10;
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   PriceHigh= iCustom(NULL,0,"TMA_Fair",TimeFrame,HalfLength,Price,ATRMultiplier,ATRPeriod,Interpolate,1,0);
   PriceLow = iCustom(NULL,0,"TMA_Fair",TimeFrame,HalfLength,Price,ATRMultiplier,ATRPeriod,Interpolate,2,0);

   if(CountSell()==0 && Bid>=PriceHigh)
     {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"ТМА",Magic,0,Red);
      if(ticket>0)
        {
         SL = NormalizeDouble(Bid + StopLoss*Point, Digits);
         TP = NormalizeDouble(Bid - TakeProfit*Point, Digits);
         if(OrderSelect(ticket,SELECT_BY_TICKET))
            if(!OrderModify(ticket,OrderOpenPrice(),SL,TP,0))
               Print("Ошибка модификации ордера на продажу");
        }
      else Print("Ошибка открытия ордера на продажу");
     }
   if(CountBuy()==0 && Ask<=PriceLow)
     {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"ТМА",Magic,0,Blue);
      if(ticket>0)
        {
         TP = NormalizeDouble(Ask + TakeProfit*Point, Digits);
         SL = NormalizeDouble(Ask - StopLoss*Point, Digits);
         if(OrderSelect(ticket,SELECT_BY_TICKET))
            if(!OrderModify(ticket,OrderOpenPrice(),SL,TP,0))
               Print("Ошибка модификации ордера на покупку");
        }
      else Print("Ошибка открытия ордера на покупку");
     }
   if(Ask<=PriceLow && CountSell()>0)
     {
      for(int i=OrdersTotal() -1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderMagicNumber()==Magic && OrderType()==OP_SELL)
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Black))
                  Print("Ошибка закрытия ордера на продажу");
           }
        }
     }
   if(Bid>=PriceHigh && CountBuy()>0)
     {
      for(i=OrdersTotal() -1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderMagicNumber()==Magic && OrderType()==OP_BUY)
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Black))
                  Print("Ошибка закрытия ордера на покупку");
           }
        }
     }
  }
//+------------------------------------------------------------------+
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber && Magic && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber && Magic && OrderType()==OP_BUY)
            count++;
        }
     }
//+------------------------------------------------------------------+
 
Zelimhannahal00:

Guys, help me fix the EA. Instead of checking orders, it will buy without stopping, at the first signal, until it spends the entire deposit. Thank you all in advance.

The code can be formatted normally right in the code editor using the code styler: Ctrl + <

I formatted it for you - now it's normally readable.

 
Zelimhannahal00:

Guys, help me fix the EA. Instead of checking orders, it will buy without stopping, at the first signal, until it spends the entire deposit. Thank you all in advance.

You have identified it correctly, the error is in the check positions function

//+------------------------------------------------------------------+
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType()==OP_SELL)
            count++;
        }
     }

   return(count);
  }
//+------------------------------------------------------------------+
int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType()==OP_BUY)
            count++;
        }
     }
//+----------------------

I have highlighted in red, fixed.

I have corrected it.

 
Alekseu Fedotov:

You have correctly identified the error in the position checking functions

red highlighted, corrected.

That's about it.

Fixed it, it's still the same. The problem is still there.
 
Zelimhannahal00:
Fixed it, it's still the same. The problem is still there.
//+------------------------------------------------------------------+
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType()==OP_BUY)
            count++;
        }
     }
return(count);


did you copy all the code in there?

 
Vladislav Andruschenko:


Did you copy all the code in there?

I didn't copy all the code by mistake. It's ok there return(count);

}

That's how the code ends, as it should. There are no errors and warnings at all when compiling the code. I must have made a mistake in the functions, I cannot figure out where. I fixed it as I was told above. I also have no errors, but the adviser itself works the same way (the first signal, it buys for the entire deposit, given lots, such as 1 lot set and he buys 1 lot of deals 200 or more, as long as the deposit is enough. But the signal is correct, buys also correctly, only a lot)

 
Zelimhannahal00:

I didn't copy the whole code by mistake. It's fine return(count);

}

That's how the code ends, as it should. There are no errors and warnings at all when compiling. I must have made a mistake in the functions, I cannot figure out where. I fixed it as I was told above. I also have no errors, but the adviser itself works the same way (the first signal, it buys for the entire deposit, given lots, such as 1 lot set and he buys 1 lot of deals 200 or more, as long as the deposit is enough. But the signal is correct, buys also correctly, but a lot)

I have checked and corrected it.

Files:
ProjectName.mq4  11 kb
 
How do you find the number of candles between dates?
 
Alexander Fedosov:
How to find number of candles between dates?

if the TF is synchronised then any CopyXXXX() function :

Referencing the start and end dates of the required time interval

intCopyTime(
stringsymbol_name,// symbol name
ENUM_TIMEFRAMEStimeframe,// period
datetimestart_time,// from which date
datetimestop_time,// till what date
datetimetime_array[]//array to copy opening time
);

Returned value

Number of copied array elements or -1 in case of error.


you can use iBarShift() to find 2 bars by time


 
Igor Makanu:

if the TF is synchronised then any CopyXXXX() function :

Referencing the start and end dates of the required time interval

intCopyTime(
stringsymbol_name,// symbol name
ENUM_TIMEFRAMEStimeframe,// period
datetimestart_time,//from what date
datetimestop_time,// till what date
datetimetime_array[]//array to copy opening time
);

Returned value

Number of copied array elements or -1 in case of error.


That's odd... I don't remember there being these features in mt4 before. Were they added to fives too?
Reason: