Need help! EA won't open any trades during Back testing!

Discussion in 'Expert Advisors / Trading Robots' started by SaadUllah, Mar 5, 2019.

  1. SaadUllah

    SaadUllah Forum Newbie

    Joined:
    Jan 6, 2019
    Messages:
    1
    Likes Received:
    0
    Hi There,

    I am a novice in coding and Trading. I tried to build an EA with the little knowledge i have with the help of internet.

    My concept is to open a Buy trade with following logic (If 2 Bull candles appeared followed by a bear candle --> Buy and reverse is true for Sell Order)

    I tired to build the EA with this simple logic. The EA compiles with out any error or warning but when I Back tested it runs and complete the test but do not generate any trade!

    I also tried attaching it to a chart and running it for a day but still no trade occurred! Would please take a look at the code below -

    Code:
    #property copyright "Copyright 2019, SAAD"
    #property link      "SAAD"
    #property version   "1.00"
    #property strict
    
    input double Lots          =0.1;
    //+------------------------------------------------------------------+
    //| Expert initialization function                                   |
    //+------------------------------------------------------------------+
    int OnInit()
      {
    //---
       Print("EA Loaded");
    //---
       return(INIT_SUCCEEDED);
      }
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
      {
    //---
       Print("EA Loaded");   
      } 
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick(void)
      {
    //+------------------------------------------------------------------+
    //| Variable Definition                                              |
    //+------------------------------------------------------------------+
        
       double bid   =MarketInfo(Symbol(),MODE_BID); // Request for the value of Bid
       double ask   =MarketInfo(Symbol(),MODE_ASK); // Request for the value of Ask
       double point =MarketInfo(Symbol(),MODE_POINT);//Request for Point
       int    ticket;
       double c0c    = Close[0]; Print(c0c);
       double c0o    = Open[0]; Print(c0o);
       double c1c    = Close[1];
       double c1o    = Open[1];
       double c2o    = Open[2];
       double c2c    = Close[2];
       double c1L    = Low[1];
       double c1H    = High[1];       
       double closeBuyTrade   = c1L;
       double closeSellTrade  = c1H;   
       static int buySig = 0;
       static int sellSig = 0;
        
    // Generate Buy Signal
             if (c2c<c2o && c1c>c1o && c0c>c0o){
                                           buySig = 1;
                                           } else {
                                                    buySig = 0;
                                                    return;}
    // Generate Sell Signal
             if (c2c>c2o && c1c<c1o && c0c<c0o){
                                           sellSig = 1;
                                           } else {
                                                    sellSig = 0;
                                                    return;}
                      
    
     //--- check for long position (BUY) possibility
          if(c0c>c0o)
            {
             ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,closeBuyTrade,closeBuyTrade,"renko sample",16384,0,Green);
             if(ticket>0)
               {
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                   SendNotification(Symbol() + "BUY order opened : " + DoubleToString(OrderOpenPrice()));
               }
             else
                SendNotification("Error opening BUY order : " + DoubleToString(GetLastError()));
             return;
            }
     //--- check for Short position (SELL) possibility
          if(c0c<c0o)   
            {
             ticket=OrderSend(Symbol(),OP_SELL,Lots,Ask,0,closeSellTrade,closeSellTrade,"renko sample",16384,0,Red);
             if(ticket>0)
               {
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                   SendNotification(Symbol() + "Sell order opened : " + DoubleToString(OrderOpenPrice()));
               }
             else
                SendNotification("Error opening Sell order : " + DoubleToString(GetLastError()));
             return;
            }       
     
      }
    
     
  2. TheForexGuy

    TheForexGuy Forex Mentor

    Joined:
    Dec 9, 2014
    Messages:
    3,360
    Likes Received:
    3,060
    Why don't you see the reason why the trade won't open.

    Code:
             if(ticket>0)
               {
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                   SendNotification(Symbol() + "BUY order opened : " + DoubleToString(OrderOpenPrice()));
               }
             else
                SendNotification("Error opening BUY order : " + DoubleToString(GetLastError()));
             return;
            }
    You are using SendNotification for debugging? That's not going to work in the backtester.

    Find out what the error response is...

    Code:
    ResetLastError();
    
    ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,closeBuyTrade,closeBuyTrade,"renko sample",16384,0,Green);
    
    if( ticket <= 0 ) {
         printf("Ordersend BUY failed | Error code: %i ", GetLastError());
    }
     

Share This Page