EA working well in strategy tester, placing a first buy on demo/live, not trading afterwards

 

Hello Everyone,

I'm developing my first EA.

I got to write one, and it works perfectly well on the strategy tester

My EA does the following:

I specify a pair of currencies, say currency1 and currency2.

When it starts, the EA buys immediately the currency1.

Afterwards, it checks the conditions A & B. If either condition is true, the EA sells currency1 and buys currency2. 

From there on, it will check which currency is the opened-buy position, checks conditions and then accordingly switches from one to the other, so I'm always bought on one of the two currencies. 

1) On the strategy tester, it does everything I want. No warning messages. All trades clear and clean. All prints and alerts printed as they should.

2) On the demo/live account: it does the first immediate buy. (so, yes, autotrading is on, my demo account accepts EAs performing orders, there is money available to buy)

3) But then... nothing. I changed the conditions so the switch (sell + buy) would be as often as possible, and not a single trade was performed. Only the first buy.

What could be the issue?

Below, I'll post the code that might be the problem.


 MqlTradeRequest request;  // To be used for sending our trade requests
MqlTradeResult result;    // To be used to get our trade results
ZeroMemory(request);     // Initialization of mrequest structure

// FIRST BUY =>  The EA will immediately make a buy when it is started
             if(PositionsTotal() == 0)  
                
               {
             ZeroMemory(request);  
                  request.action=TRADE_ACTION_DEAL;
                  request.symbol=currency1;
                  request.volume=shares1;
                  request.magic=magic_number;
                  request.type=ORDER_TYPE_BUY;
                  request.type_filling=ORDER_FILLING_IOC;
               request.price=SymbolInfoDouble(currency1,SYMBOL_ASK);
                  request.deviation=5;
                  OrderSend(request,result);    
 // get the result code
         if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
           {
            Alert("A SELL order has been successfully placed with Ticket#:",result.order,"!!");
           }
         else
           {
            Alert("The SELL order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
           ZeroMemory(request);  
                 request.action=TRADE_ACTION_DEAL;
                  request.symbol="BBDC4";
                  request.volume=1000;
                  request.magic=magic_number;
                  request.type=ORDER_TYPE_BUY;
                  request.type_filling=ORDER_FILLING_FOK;
               request.price=SymbolInfoDouble(currency1,SYMBOL_ASK);
               request.deviation=5;
                  OrderSend(request,result);    
 // get the result code
         if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
           {
            Alert("A SELL order has been successfully placed with Ticket#:",result.order,"!!");
           }
         else
           {
            Alert("The SELL order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
           
                             
               }
               else {
// FROM NOW ON, THE EA WILL CHECK WHICH CURRENCY IS BUY-OPENED, CHECK THE CONDITIONS AND MAKE THE SWITCH IF CONDITIONS ARE MET.
               
                     int totalorders = PositionsTotal();                                                        
                     for(int i=0;i<totalorders;i++)
                     {
                     
                        if(PositionSelectByTicket(PositionGetTicket(i)))
                       {
                           if(magic_number == PositionGetInteger(POSITION_MAGIC)&&currency1 == PositionGetString(POSITION_SYMBOL))
                           {
                             if (X1>=c1/100 || Y1>=c2/100)                                   // SELL 1 BUY 2, both conditions
                             {
                         ZeroMemory(request);  
                             request.action=TRADE_ACTION_DEAL;
                             request.magic=magic_number;
                             request.position=PositionGetInteger(POSITION_TICKET);
                             request.symbol=PositionGetString(POSITION_SYMBOL);
                             request.volume=PositionGetDouble(POSITION_VOLUME);
                             request.price=SymbolInfoDouble(currency1,SYMBOL_BID);          
                             request.type_filling=ORDER_FILLING_IOC;
                             request.type=ORDER_TYPE_SELL;
                             OrderSend(request,result);
                              
         // get the result code
         if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
           {
            Alert("A SELL order has been successfully placed with Ticket#:",result.order,"!!");
           }
         else
           {
            Alert("The SELL order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
                         ZeroMemory(request);  
                             request.action=TRADE_ACTION_DEAL;
                             request.symbol=currency2;
                             request.volume=shares2;
                             request.magic=magic_number;
                             request.type=ORDER_TYPE_BUY;
                             request.type_filling=ORDER_FILLING_IOC;
                             request.price=SymbolInfoDouble(currency2,SYMBOL_ASK);
                             request.deviation=5;
                             OrderSend(request,result);
                             
 // get the result code
         if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",result.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
                                        
Print ("==================sell 1 buy 2: TRIGGERED AT "+bid1+"-"+ask2);
                              }
                           }
                           
                           else if (magic_number == PositionGetInteger(POSITION_MAGIC)&&currency2 == PositionGetString(POSITION_SYMBOL))
                           {
                             if (X2>=c1/100 || Y2>= c2/100)                               // SELL 2 BUY 1
                             {
                            
                             ZeroMemory(request);  
                             // MqlTradeRequest request={0};
                             request.action=TRADE_ACTION_DEAL;
                             request.magic=magic_number;
                             request.position=PositionGetInteger(POSITION_TICKET);
                             request.symbol=PositionGetString(POSITION_SYMBOL);
                             request.volume=PositionGetDouble(POSITION_VOLUME);
                             request.price=SymbolInfoDouble(currency1,SYMBOL_BID);              
                             request.type_filling=ORDER_FILLING_IOC;
                             request.type=ORDER_TYPE_SELL;
                             // MqlTradeResult result={0};
                             OrderSend(request,result);
                             last_trade_price_currency2=SymbolInfoDouble(currency2,SYMBOL_BID);
   // get the result code
         if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
           {
            Alert("A SELL order has been successfully placed with Ticket#:",result.order,"!!");
           }
         else
           {
            Alert("The SELL order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }                          
                            ZeroMemory(request);
                             request.action=TRADE_ACTION_DEAL;
                             request.symbol=currency1;
                             request.volume=shares1;
                             request.magic=magic_number;
                             request.type=ORDER_TYPE_BUY;
                             request.type_filling=ORDER_FILLING_IOC;
                             request.price=SymbolInfoDouble(currency1,SYMBOL_ASK);
                             request.deviation=5;
                             OrderSend(request,result);
                             
 // get the result code
         if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",result.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }                             
Print ("==================sell 2 buy 1: TRIGGERED AT "+bid2+"-"+ask1);
                             }
                        }
                }
}

Any lights? 

I appreciate your time!

Thanks

 

Are you experiencing this on the Live Account?

Results may differ between the strategy tester (backtest) and live account because of the length of data used and the conditions of your strategy. How long have you tried it on the live account?

Leave the EA to run on the live account for about a week or two. Then download the same data for the week or two and run the strategy test with the EA. If the results differ then we can begin to look at the code logic.

 

Hello Obike,

Thanks for your reply.

I did precisely that. I left it running many days -- more than one week -- and NO trades on the live account, whereas on the strategy tester the conditions were triggered multiple times. 

So the comparison is pretty simple: NO trades versus MANY...

Reason: