Simple EA Not Working in Testing Mode But Only in Backtest

 

Hello,


I have made simple EA to test MQL4 and the strategy does not working in test but only in backtest.

This is really strange because this is really a simple coding EA....

The signals are sends this is ok but the orders does not open.


if(OrdersTotal() == 0)
{
if(iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0) > iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0))

{

OrderSend(Symbol(),OP_SELL,1,Bid,3,Bid + 50 * Point,Bid - 50 * Point,"My order",22,0,Green);

Print("OK Sell : ",Bid + 50 * Point);
return(0);
}
}



I have no error in testing mode.

In backtesting mode i have error 130 but

only for few orders.


It works without SL and TP but i need to

leave them.


I tried with many broker in case the broker

don't accept SL and TP without modify order

but i have the same result.


Do you have any solution?


Thank you

 
anzaisensei:

Hello,


I have made simple EA to test MQL4 and the strategy does not working in test but only in backtest.

This is really strange because this is really a simple coding EA....

The signals are sends this is ok but the orders does not open.


if(OrdersTotal() == 0)
{
if(iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0) > iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0))

{

OrderSend(Symbol(),OP_SELL,1,Bid,3,Bid + 50 * Point,Bid - 50 * Point,"My order",22,0,Green);

Print("OK Sell : ",Bid + 50 * Point);
return(0);
}
}



I have no error in testing mode.

In backtesting mode i have error 130 but

only for few orders.


It works without SL and TP but i need to

leave them.


I tried with many broker in case the broker

don't accept SL and TP without modify order

but i have the same result.


Do you have any solution?


Thank you


if(OrdersTotal() == 0)      //wrong way to check if there is no trade
{
if(iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0) > iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0))


  int Ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,30,0,0,"My order",22,0,Color);
          if(Ticket>0)
            {
             if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES))
                {
                 Print("OK Sell : ",OrderOpenPrice());
                 OrderModify(Ticket,OrderOpenPrice(),Bid + 100 * Point,Bid - 100 * Point,0,Red);
                }
            }  

}
If you have on your account any trade pending manual opened whatever the EA can't open a new trade
 
OrderSend(Symbol(),OP_SELL,1,Bid,3,Bid + 50 * Point,Bid - 50 * Point,"My order",22,0,Green);
  1. Always test your return codes so you find out WHY!
  2. For sells, you open at the Bid, your stops are relative to the ASK.
  3. On 5 digit brokers you must adjust TP, SL, AND slippage.
  4. On ECN brokers you must open first and THEN set stops.
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){                                                 OptParameters();
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    if (ticket < 0)
       Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET))
       Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
       Alert("OrderModify failed: ", GetLastError());
     */

 

Thank you

It works with OrderModify after opening an OrderSend position.

I also adjust SL and TP with :

pips2dbl    = Point*10


thanks for help.

 
anzaisensei:

Thank you

It works with OrderModify after opening an OrderSend position.

I also adjust SL and TP with :


thanks for help.


That is still not enough

the error is

if(OrdersTotal() == 0)

Look for EAtrades so that you can open also when if(OrdersTotal() > 0)

     int sells=0;
     int buys=0; 
     for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) 
       if (OrderSelect(iPos, SELECT_BY_POS)                        // Only my orders w/
        &&  OrderMagicNumber()  == MagicNumber                  // my magic number
        &&  OrderSymbol()       == Symbol())                    //chart.symbol    my pair.
      {
       ...........
       .........
       .......
      }
Reason: