Download MetaTrader 5

Order Close error 138 BACKTEST

To add comments, please log in or register
d.bignotti
105
d.bignotti  

Hello,

 

i'm getting this error only in BACKTESTING, here's a pic. 

  

 

As you can see i'm trying to close a Basket when a when an opposite signal is generated.

 

Here's the OrderClose() code for Sell orders, the same for Buy orders

for(int i=0; i<OrdersTotal(); i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            if(OrderSymbol()==_Symbol)
               if(OrderMagicNumber()==MagicNumber)
                  if(OrderType()==OP_SELL)
                     if(!OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),MaxSlippage,clrNONE))
                       {
                        err=GetLastError();
                        MessageBox(Symbol()+", Could not close the order due to error "+(string)err+" "+ErrorDescription(err),"OrderSend Error",0x00000010);
                       }
         else
            i--;
        }

 

 Also i paste the OrderSend maybe the problem is there

 

int  TicketNumber=OrderSend(Symbol(),OP_BUY,CalcLotsVolume(),NormalizePrice(Ask),MaxSlippage,0,0,"QuantumEA",MagicNumber,0,clrBlue);

   if(TicketNumber==-1)
     {
      err=GetLastError();
      MessageBox(Symbol()+", Could not open the order due to error "+(string)err+" "+ErrorDescription(err),"OrderSend Error",0x00000010);
      if(err==ERR_TRADE_NOT_ALLOWED)MessageBox("YOU NEED TO ENABLE YOUR AUTOTRADING BUTTON!","Auto Trading",0x00000010);
     }

 

Anyone know where's the problem? 

Vasyl Nosal
18832
Vasyl Nosal  
OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),MaxSlippage,clrNONE)
OrderClose(OrderTicket(),OrderLots(),Ask,MaxSlippage,clrNONE)
whroeder1
15304
whroeder1  
d_bignotti:
if(OrderType()==OP_SELL)
if(!OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),MaxSlippage,clrNONE))
  1. You can't close at the open price, only the current market price.
  2. For a sell that is the Ask. You can also use OrderClosePrice and be direction independent.
  3. Is MaxSlippage in points? Or have you adjusted for 4/5 digit brokers?
d.bignotti
105
d.bignotti  
eevviill:

And

WHRoeder:
  1. You can't close at the open price, only the current market price.
  2. For a sell that is the Ask. You can also use OrderClosePrice and be direction independent.
  3. Is MaxSlippage in points? Or have you adjusted for 4/5 digit brokers?

Thnak you, the problem was i didn't use ask/bid for closing price, such stupid.

 

Max Slippage is in point.

I have this for pisps in the initialization function.

   Pips=Point;// 0.0001 and 0.01 on old broker or .00001 and .001 on new broker
   if(Digits==5 || Digits==3)Pips*=10;//0.00001 becomes 0.0001 and 0.001 becomes 0.01

 

but when i try to multiply Maxslippage*Pips i get "Possible loss of data due to type conversion" i can't get rid of it! 

Carl Schreiber
6968
Carl Schreiber  

Do a type-cast like

(double)Maxslippage*Pips; // my crystal ball advised me that Maxslippage might have defined as int

d.bignotti
105
d.bignotti  
gooly:

Do a type-cast like

(double)Maxslippage*Pips; // my crystal ball advised me that Maxslippage might have defined as int

 

MaxSlippage is int Type even if i TypeCasto to double as you said i get the same error, that's why i can't get rid of it, i already tried that.

 

OrderClose(OrderTicket(),OrderLots(),Bid,(double)MaxSlippage*Pips,clrNONE)
Keith Watford
Moderator
10359
Keith Watford  
input int Slippage=5;
int Maxslippage;
   MaxSlippage=Slippage;
   Pips=Point;// 0.0001 and 0.01 on old broker or .00001 and .001 on new broker
   if(Digits==5 || Digits==3)   //0.00001 becomes 0.0001 and 0.001 becomes 0.01
     {
     Pips*=10;
     MaxSlippage*=10;
     }


.

d.bignotti
105
d.bignotti  
GumRai:


.

All right! Thank you
whroeder1
15304
whroeder1  
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
d.bignotti
105
d.bignotti  
WHRoeder:
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum


Ok, i'll modify and post everything, but know i'm using a for loop from 0 to OrdersTotal(), incrementing everytime, and at the end i--, isn't the same thing?

 

anyway, something like that?

 

   for(int i=OrdersTotal()-1; i>=0; i --)//Count down, from orders Total to 0
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==_Symbol)
            if(OrderMagicNumber()==MagicNumber)
               if(OrderType()==OP_BUY)
                  if(!OrderClose(OrderTicket(),OrderLots(),NormalizePrice(Bid),MaxSlippage,clrNONE))
                    {
                     int err=GetLastError();
                     MessageBox(Symbol()+", Could not close the order due to error "+(string)err+" "+ErrorDescription(err),"OrderSend Error",0x00000010);
                    }
     }
whroeder1
15304
whroeder1  
d_bignotti: i'm using a for loop from 0 to OrdersTotal(), incrementing everytime, and at the end i--, isn't the same thing?
You didn't understand Loops and Closing or Deleting Orders - MQL4 forum. It's also about what if another unrelated order gets closed.
12
To add comments, please log in or register