Error Code 138 in Backtesting

 

 Hey Everyone!

    I am having an issue, my EA is Working Fine in Live Market But when i run it on the Strategy tester it is not opening any trade and return Error is 138.

  I have tried to Fix this issue by Reading many threads And i Have tried everything that i can get my Hands on, but the issue is still there.

It would be highly appreciated if Someone can help me out. I am New to this community and Autotrading.

 Here is my Whole Code.


void OnTick()
  {
double bbLower1 = iBands(NULL,0,bbPeriod,band1Std,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbMid = iBands(NULL,0,bbPeriod,band1Std,0,PRICE_CLOSE,0,0);
   double bbUpper1 = iBands(NULL,0,bbPeriod,band1Std,0,PRICE_CLOSE,MODE_UPPER,0);
   
   double bbLower4 = iBands(NULL,0,bbPeriod,band4Std,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbUpper4 = iBands(NULL,0,bbPeriod,band4Std,0,PRICE_CLOSE,MODE_UPPER,0);
     
   if  (!Checkorders(magicnumber) )
   {
   if(Ask < bbLower1)//buying
   {
       
      
      
      Alert("Price is bellow bbLower1, Sending buy orde;r");
     stopLossPrice = NormalizeDouble(bbLower4,Digits) ;
     takeProfitPrice = NormalizeDouble(bbMid,Digits);
      Alert("Entry Price = " + Ask);
      Alert("Stop Loss Price = " + stopLossPrice);
      Alert("Take Profit Price = " + takeProfitPrice);
      RefreshRates();
       ticket = OrderSend(NULL,OP_BUY,OptimalLotSize(maxRiskPrc,Ask,stopLossPrice),0,500,stopLossPrice,takeProfitPrice,NULL,magicnumber);
       if (ticket<0)
       {  Alert( "order Rejected;" + GetLastError() ) ;
       }
   
   }
   else if(Bid > bbUpper1)//shorting
   {
   
     
      Alert("Price is above bbUpper1, Sending short order");
       stopLossPrice =  NormalizeDouble(bbUpper4, Digits) ;
       takeProfitPrice = NormalizeDouble(bbMid,Digits);
      Alert("Entry Price = " + Bid);
      Alert("Stop Loss Price = " + stopLossPrice);
      Alert("Take Profit Price = " + takeProfitPrice);
      
      
           RefreshRates();
          ticket = OrderSend(NULL,OP_SELL,OptimalLotSize(maxRiskPrc,Bid,stopLossPrice),0,500,stopLossPrice,takeProfitPrice,NULL,magicnumber);
              
               if (ticket<0)
       {  Alert( "order Rejected;" + GetLastError() ) ;
       }
   }
    
   }   
   

     
       
  }
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
ticket = OrderSend(NULL,OP_BUY,OptimalLotSize(maxRiskPrc,Ask,stopLossPrice),0,500,stopLossPrice,takeProfitPrice,NULL,magicnumber);

Be careful with NULL.

  1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
  2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
  3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
  4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
  5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25
 
William Roeder:

Be careful with NULL.

  1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
  2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
  3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
  4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
  5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25

I Have Changed NULL to SYBMOL(), But still the error is here.

The thing is that this Code is working fine on live, but this error appers only in Backtest.

 
I also get requotes when trying to open with insufficient margin available.
 

You are buying at 0 with 500 slippage. Why don't you just buy at Ask?

Why do you do a RefreshRates() right before opening the order, so that your buying parameters do now belong to possibly outdated values?

 
lippmaje:

You are buying at 0 with 500 slippage. Why don't you just buy at Ask?

Why do you do a RefreshRates() right before opening the order, so that your buying parameters do now belong to possibly outdated values?

Thankyou my freind. My problem is solved, I changed 0 to Ask , and now the error is eliminated.