order error 138. code attached help would be appreciated

 

So every time i try to backtest my code error 138 comes up and no trades are placed, no matter how low the timeframe. Would appreciate any feedback.



#property version   "1.00"

#property strict

#property show_inputs

#include  <customfunctions01.mqh>


int rsiPeriod = 7;

input double riskPerTrade = 0.01;

input int rsiLowerLevel = 20;

input int rsiUpperLevel = 80;


input int stoplosspips = 100;


input int takeprofitpips = 500;

input int breakeven = 40;

input int SlowMA = 200;

input int FastMA = 50;

input int StoUpperlevel = 80;

input int StoLowerLevel = 20;



input int ticket = 0;


int openOrderID;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

   Alert("");

   Alert("Starting Strategy johnstrategy");


   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   Alert("Stopping johnstrategy");

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

  

  


  

   

   double FastMA = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);

   double SlowMA= iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,1);

   double rsiValue = iRSI(NULL,0,rsiPeriod,PRICE_CLOSE,0);

   double currentRSI = iRSI(NULL,0,7,0,0);

   double previousRSI = iRSI(NULL,0,7,0,5);

   double Stovalue = iStochastic(NULL, 0, 14 , 3, 3, MODE_SMA, MODE_MAIN, 0, 0);

   double currentStovalue = iStochastic(NULL, 0, 14 , 3, 3, MODE_SMA, MODE_MAIN, 0, 0);

   double previousStovalue = iStochastic (NULL, 0, 14 , 3, 3, MODE_SMA, MODE_MAIN, 0, 1);

   double MACD = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);

   

 

  //if(!checkifopenordersbymagicnb(magicNB))&&//if no open orders try to enter new position

  

  if (isnewcandle())

   {

if (rsiValue<20 &&  FastMA>SlowMA&& currentStovalue< 20 && Bid<SlowMA) //buying

  

      {

         Print("MA's show a bullish market and rsiValue is lower than " + rsiLowerLevel+ " , Sending buy order");

       double stopLossPrice = Bid - stoplosspips * getpipvalue();

         double takeProfitPrice = Bid + takeprofitpips * getpipvalue();

         Print("Entry Price = " + Bid);

         Print("Stop Loss Price = " + stopLossPrice);

       Print("Take Profit Price = " + takeProfitPrice);

         

         double lotSize = optimallotsize(riskPerTrade,Bid,stopLossPrice);

         

        openOrderID = OrderSend(NULL,OP_BUY,lotSize,Bid,10,stopLossPrice,takeProfitPrice,NULL,magicNB);

        if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());

        

        

   

      }

   else if(rsiValue > 80  &&  SlowMA>FastMA&& currentStovalue>80  && Ask>SlowMA )//shorting

     

      {

         

      

      Print("MA's show a bearish market and rsiValue is above  " + rsiUpperLevel+ " , Sending short order");

 double stopLossPrice = Ask + stoplosspips * getpipvalue();

     double takeProfitPrice = Ask - takeprofitpips * getpipvalue();

      Print("Entry Price = " + Ask);

     Print("Stop Loss Price = " + stopLossPrice);

      Print("Take Profit Price = " + takeProfitPrice);

   

    double lotSize = optimallotsize(riskPerTrade,Ask,stopLossPrice);


    openOrderID = OrderSend(NULL,OP_SELL,lotSize,Ask,10,stopLossPrice,takeProfitPrice,NULL,magicNB);

    if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());


}








}

}




                    

 
Use mql4 section please.
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

  3. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  4.  openOrderID = OrderSend(NULL,OP_SELL,lotSize,Ask,10,stopLossPrice,takeProfitPrice,NULL,magicNB);

    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)