OrderClose() error 138 during backtest

 

I am getting error 138 when backtesting this robot. I am not sure what the problem is.

 Am I supposed to apply RefreshRates() somewhere?

 Please take a look at my code, any help would be appreciated.

//+------------------------------------------------------------------+
//|                                              Ichimoku Custom.mq4 |
//|                                                           Luke   |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Luke"
#property link      ""
#include <stdlib.mqh>
#include <stderror.mqh>
#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4



//---- input parameters
extern int       StopLoss=0;
extern int       TakeProfit=0;
extern double    Lots=0.1;
extern double    UserTrailingStop;

 //---------------------------------- Order Counting


   
//------ Initialization function
int init()
   {
      
     return(0);
   }
//------ Deinitialization function
int deinit()
   {
   return(0);
   }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   double tenkanSen = iIchimoku(NULL, 0, 9, 26, 52, MODE_TENKANSEN, 0);
   double kijunSen = iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 0);
   double senkouA = iIchimoku(NULL, 0, 9, 26, 52, MODE_SENKOUSPANA, 26);
   double senkouB = iIchimoku(NULL, 0, 9, 26, 52, MODE_SENKOUSPANB, 26);
   double openSen = iOpen(NULL, 0, 0);
   double chinkou = iIchimoku(NULL, 0, 9, 26, 52, MODE_CHINKOUSPAN, 0);
   double closeSen = iClose(NULL, 0, 0);
   double highSen = iHigh(NULL, 0, 26);
   double RSIsen = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);
   string symbol;
   int order = SIGNAL_NONE;
   int total, type, ticket;
      
   double SL,TP;   
   
   
 //--------------------------------------------------------------------------------//
 //                                                                                //
 //                           Define Buy & Sell                                    //
 //                                                                                //
 //--------------------------------------------------------------------------------//
   if (tenkanSen > kijunSen && openSen > senkouA && openSen > senkouB && chinkou > highSen && RSIsen > 50) order = SIGNAL_BUY; // Buy Parameters
   if (order==1) // If there is a BUY signal
      {
      if (StopLoss!=0)    SL=Bid-StopLoss*Point;   else SL=0;  // define SL       
      if (TakeProfit!=0)  TP=Bid+TakeProfit*Point; else TP=0;  // Define TP
      if(OrderCount(OrdersTotal())==0)
              {
              if(OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL,TP)==True) Print("BUY order opened : ",OrderOpenPrice());
         
                              }
        }
   if (tenkanSen < kijunSen && openSen < senkouA && openSen < senkouB && chinkou < highSen && RSIsen < 50) order = SIGNAL_SELL; // Sell Parameters
   if (order==2) // ??????? ???????, ??????? ???????
      {
      if (StopLoss!=0)    SL=Ask+StopLoss*Point;   else SL=0;         
      if (TakeProfit!=0)  TP=Ask-TakeProfit*Point; else TP=0; 
      if(OrderCount(OrdersTotal())==0){        
          if(OrderSend(Symbol(),OP_SELL,Lots,Bid,3,SL,TP)==True) Print("SELL order opened : ",OrderOpenPrice());
          
                        }
      }
// if(tenkanSen < kijunSen || openSen < kijunSen) order = SIGNAL_CLOSEBUY;
// if(tenkanSen > kijunSen || openSen > kijunSen) order = SIGNAL_CLOSESELL;
 //--------------------------------------------------------------------------------//
 //                                                                                //
 //                           Close Position                                       //
 //                                                                                //
 //--------------------------------------------------------------------------------//
 

  
   if(OrderCount(OrdersTotal())>0)
   {
      OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
        {
         if(type==0) // if long position is open
         {
            if(tenkanSen < kijunSen || openSen < kijunSen) order = SIGNAL_CLOSEBUY; // Should BUY be closed?
            if(order == 3)
            {
               if(OrderClose(GetTicket(ticket),OrderLots(),Bid,3,Red)==True) Print("Buy closed at: ",OrderClosePrice());
               return(0); // exit
             
            }
         if(type==1) // if short position is open
           {
            if(tenkanSen > kijunSen || openSen > kijunSen) order = SIGNAL_CLOSESELL; // should SELL be closed?
            if(order == 4)
                 {
              if(OrderClose(GetTicket(ticket),OrderLots(),Ask,3,Red)==True) Print("SELL closed at: ",OrderClosePrice()); // close position
                 return(0); // exit
                 }
            }
         if(type!=1 && type!=0) Print("Error closing order : ", GetLastError());
         
         
        }
      
     
   } // closes loop

//----

   return(0);
}
//+------------------------------------------------------------------+
}

int OrderCount(int total) // Count number of open orders
   {
    int cnt;
    if(total>0) cnt++;
    return(cnt);
    }
int GetTicket(int ticket) // Provide most recent ticket
   {
      RefreshRates();
      ticket = OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
      return(ticket);
   }
 

No apply a slippage to you OrderSend, and change it:

int ticket;

ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL,TP)

if (ticket>0)
{
   Print("BUY order opened : ",OrderOpenPrice());
}

Same for sell part.


OrderSend is not a Bool functions, it's "int" so you can't write

if(orderSend(....) == true)

 
Kane59:

No apply a slippage to you OrderSend, and change it:

Same for sell part.

It's not a Slippage issue . . .  this is being tested in the Strategy Tester,  there is no slippage . . . . and the issue was . .  "OrderClose() error 138 during backtest"

 

The problem is your variable  type . . .  you never set it so it's always 0 . . .  even for a  OP_SELL

 

I have applied this code to the buy and sell parameters. But even when type was defined, error 138 is still occurring.

 

if(OrderCount(OrdersTotal())==0)
         {        
          ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,0,SL,TP);
          type = OP_SELL;
          if (ticket>0)
          {
          Print("SELL order opened : ",OrderOpenPrice());
          }
                        }
 
Although if I define it as a static variable there is no error code at all, but the order will still not close.
 
listep:
Although if I define it as a static variable there is no error code at all, but the order will still not close.


Read the documentation for the functions you are trying to use,  how do you expect this to work ?

int GetTicket(int ticket) // Provide most recent ticket
   {
      RefreshRates();

      ticket = OrderSelect(0,SELECT_BY_POS,MODE_TRADES);  // <---------  NO,  read the Documentation for OrderSelect.

      return(ticket);
   }
 
RaptorUK:


Read the documentation for the functions you are trying to use,  how do you expect this to work ?

 

 


You're completely right. This is my first EA so I imagine I've made a lot of mistakes similar to this, I'm going to go back and audit.

 

Thanks for the help! 

 
listep:


Thanks for the help! 

You are welcome :-)
Reason: