Download MetaTrader 5

Issues with OrderSelect method. Please Help.

To add comments, please log in or register
zobad.mahmood
12
zobad.mahmood 2017.01.10 09:32 

int Check_OrderIgnore()
{
   //If the previous order was a stop loss and  was buy order
   //Set the buyIgnore flag to true
   // Else if previous order was a sell and was a stoploss
   //Set the sellIgnore flag to tue
   //----------------------------------------------------------   
  if (OrdersTotal()<0) return 0;
  if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY))
   {
      //1000=1sec 60*minutes=seconds*1000
      int _sec= minutes*60;
      _sec=_sec*1000;    
      if (OrderType() == OP_BUY)  {  bool ClosedBySL = OrderClosePrice() <= OrderStopLoss();buyIgnore=true;Print("OP_BUY state: "+ClosedBySL);  }
      else if(OrderType() == OP_SELL)   
      { 
       bool ClosedBySL = OrderClosePrice() >= OrderStopLoss();
       if(ClosedBySL)
       {
         sellIgnore=false;/*Print("SellIgnore:"+ClosedBySL+"OP_SELL OrderStopLoss: "+OrderStopLoss()+" OrderClosePrice: "+OrderClosePrice());*/
       } 
      }    
     // if(OrderType()==OP_SELL && OrderClosePrice()>=OrderStopLoss()){Print("Sleeping...");Sleep(_sec);}
   /*if(OrderType() == OP_BUY && OrderClosePrice()==OrderStopLoss() )
   {
         buyIgnore=true;
         return _TRUE;    
   }
   else if(OrderType()== OP_SELL && OrderClosePrice()== OrderStopLoss())
   {
      sellIgnore=true;
   }*/
   }
   return 0;
}

Hi Guys,

I have just started using mql4. I was trying a simple algorithm for which I needed to check if the previous order was a stoploss. I searched around the documentation and found the OrderSelect method to do this and I coded the following code. My issue was that when I tested this method on the backtest it would never change the state of either buyIgnore or sellIgnore to true even though the previous order did hit stoploss. 

Please help me out guys.

Thanks.

Files:
RT4-V2.1.mq4 48 kb
honest_knave
Moderator
2404
honest_knave 2017.01.10 10:18  
if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY))

You are always checking the order with index 0 in the history pool.

You'll want to loop through all the order history and find the most recent one to check. Have a search and you'll find lots of information on this.

whroeder1
14752
whroeder1 2017.01.10 13:59  
You assume history is ordered by date, it's not. Could EA Really Live By Order_History Alone? (ubzen) - MQL4 forum
zobad.mahmood
12
zobad.mahmood 2017.01.10 14:48  
honest_knave:
if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY))

You are always checking the order with index 0 in the history pool.

You'll want to loop through all the order history and find the most recent one to check. Have a search and you'll find lots of information on this.

Hi honest_Knave

I managed to solve my problem.

Thank you 

zobad.mahmood
12
zobad.mahmood 2017.01.10 14:56  
whroeder1:
You assume history is ordered by date, it's not. Could EA Really Live By Order_History Alone? (ubzen) - MQL4 forum
Thank you for the reply, I have checked out the link you mentioned and its very helpful, thanks.
To add comments, please log in or register