error code 138 even after using RefreshRates

 

Hi everyone, I encountered error code 138 when backtesting. I've looked on the internet that RefreshRates() can fix this. However after using it the problem still exist. Here's my code:

if(newBar && !CheckIfOpenOrdersByMagicNumber(MagicNumber))
     {
      //+----------------------------BUY-----------------------------+
      if(CheckOpenPosition(1)==1)
        {
         RefreshRates();
         double stoploss      = Bid-(Stoploss_Multiplier*ATR());

         double lotSize       = NormalizeDouble(OptimalLotSize(Risk_Per_Trade,Ask,stoploss)/lotType,2);
         if(lotSize < MarketInfo(Symbol(),MODE_MINLOT))
            lotSize = MarketInfo(Symbol(),MODE_MINLOT);

         if(((AccountStopoutMode() == 1)                                                     &&
             (AccountFreeMarginCheck(Symbol(), OP_BUY, lotSize) > AccountStopoutLevel()))    ||
            ((AccountStopoutMode() == 0)                                                    &&
             ((AccountEquity() / (AccountEquity() - AccountFreeMarginCheck(Symbol(), OP_BUY, lotSize)) * 100) > AccountStopoutLevel())))

            ticket              = OrderSend(Symbol(),OP_BUY,lotSize,Ask,slippage,stoploss,0,"Strong BUY",MagicNumber);

        }
      //+----------------------------SELL----------------------------+
      if(CheckOpenPosition(1)==2)
        {
         RefreshRates();
         double stoploss      = Ask+(Stoploss_Multiplier*ATR());

         double lotSize       = NormalizeDouble(OptimalLotSize(Risk_Per_Trade,Bid,stoploss)/lotType,2);
         if(lotSize < MarketInfo(Symbol(),MODE_MINLOT))
            lotSize = MarketInfo(Symbol(),MODE_MINLOT);

         if(((AccountStopoutMode() == 1)                                                     &&
             (AccountFreeMarginCheck(Symbol(), OP_SELL, lotSize) > AccountStopoutLevel()))   ||
            ((AccountStopoutMode() == 0)                                                    &&
             ((AccountEquity() / (AccountEquity() - AccountFreeMarginCheck(Symbol(), OP_SELL, lotSize)) * 100) > AccountStopoutLevel())))

            ticket              = OrderSend(Symbol(),OP_SELL,lotSize,Bid,slippage,stoploss,0,"Strong SELL",MagicNumber);

        }
     }
   else
     {
      double lots          = OrderLots();
      
      //double trailingStop = Stoploss_Multiplier*ATR();
      if(NewBar() && OrderSelect(ticket,SELECT_BY_TICKET))
        {
         //+--------------------------------------------------------for BUY order--------------------------------------------------------------+
         if(OrderSymbol() == Symbol() && OrderType()==OP_BUY)
           {
            //+--------------------------------------------------------Closing Order--------------------------------------------------------------+
            if(CheckClosePosition(1)==1)
              {
               RefreshRates();
               bool resCO = OrderClose(ticket,lots,Bid,slippage);
               Print("Buy order #",OrderTicket(), " closed");
               if(!resCO)
                 {
                  Print("Error in Closing Order. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Closing Order successfully for ticket: ", OrderTicket());
              }

            //+---------------------------------------------------trailing stop loss Plan---------------------------------------------------------+
            double trailingStop = Bid-Stoploss_Multiplier*ATR();
            if(newBar && OrderStopLoss()<trailingStop)
              {
               bool resSL = OrderModify(ticket,OrderOpenPrice(),trailingStop,OrderTakeProfit(),0);
               if(!resSL)
                 {
                  Print("Error in Order Modify. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Order modify successfully for ticket: ", OrderTicket());
              }
            else
               return;

           }

         //+----------------------------------------------------------for SELL order-------------------------------------------------------------+
         if(OrderSymbol() == Symbol() && OrderType()==OP_SELL)
           {
            //+--------------------------------------------------------Closing Order-------------------------------------------------------------+
            if(CheckClosePosition(1)==2)
              {
               RefreshRates();
               bool resCO = OrderClose(ticket,lots,Bid,slippage);
               Print("Sell order #",OrderTicket(), " closed");
               if(!resCO)
                 {
                  Print("Error in Closing Order. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Closing Order successfully for ticket: ", OrderTicket());
              }

            //+-----------------------------------------------------trailing stop loss Plan---------------------------------------------------------+
            double trailingStop = Ask+Stoploss_Multiplier*ATR();
            if(newBar && OrderStopLoss()>trailingStop)
              {
               bool resSL = OrderModify(ticket,OrderOpenPrice(),trailingStop,OrderTakeProfit(),0);
               if(!resSL)
                 {
                  Print("Error in OrderModify. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Order modify successfully for ticket: ", OrderTicket());
              }
            else
               return;

           }
        }
     }

after looking into backtest journal I found that some of the order (image attached) closed but instead of closing it still modify the stoploss of the trade, I think the order isn't really closed. Does anyone knows why error 138 still occur even after using RefreshRates()?

 
Luandre Ezra:

Hi everyone, I encountered error code 138 when backtesting. I've looked on the internet that RefreshRates() can fix this. However after using it the problem still exist. Here's my code:

after looking into backtest journal I found that some of the order (image attached) closed but instead of closing it still modify the stoploss of the trade, I think the order isn't really closed. Does anyone knows why error 138 still occur even after using RefreshRates()?

//+----------------------------------------------------------for SELL order-------------------------------------------------------------+
         if(OrderSymbol() == Symbol() && OrderType()==OP_SELL)
           {
            //+--------------------------------------------------------Closing Order-------------------------------------------------------------+
            if(CheckClosePosition(1)==2)
              {
               RefreshRates();
               bool resCO = OrderClose(ticket,lots,Bid,slippage);
               Print("Sell order #",OrderTicket(), " closed");
               if(!resCO)
                 {
                  Print("Error in Closing Order. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Closing Order successfully for ticket: ", OrderTicket());
              }

You can't Close Sell Orders on the Bid Price.

 
Jeremie Courchesne #:

You can't Close Sell Orders on the Bid Price.

Thanks for your help.

 
Luandre Ezra:

Hi everyone, I encountered error code 138 when backtesting. I've looked on the internet that RtefreshRates() can fix this. However after using it the problem still exist. Here's my code:

after looking into backtest journal I found that some of the order (image attached) closed but instead of closing it still modify the stoploss of the trade, I think the order isn't really closed. Does anyone knows why error 138 still occur even after using RefreshRates()?

90%
 
Luandre Ezra: I found that some of the order (image attached) closed but instead of closing it still modify the stoploss of the trade, I think the order isn't really closed.

You select by ticket but do not verify that the order is still open.

Reason: