Download MetaTrader 5

Finding what type of order was last opened.......a difficulty.

To add comments, please log in or register
davidb012
67
davidb012  

Hi,

I'm trying to code to find whether the last order was a buy or a sell. Seems simple.

My typical code would be: 

datetime LastOpenTime;
int LastOrderTyp;

double LastOrder()                                                                                                                // To find type of last order
 {
  LastOpenTime = 0; LastOrderTyp = 9;
  
  for(int ct1 = OrdersTotal()-1; ct1 >= 0; ct1--)
    {
     OrderSelect(ct1, SELECT_BY_POS, MODE_TRADES);                                                                                // Cycle through Orders
     
     if ((OrderSymbol() == Symbol())                         &&                                                                   // with this symbol, and
         (OrderMagicNumber() == MagicNumber))                                                                                     // with correct magic number      
          {
           if(OrderOpenTime() > LastOpenTime)
            {
             LastOpenTime = OrderOpenTime();
             LastOrderTyp = OrderType();                                                                                          // OP_BUY=0, OP_SELL=1
            }
          }
     }                   
  return(0);
 } 

 

Unfortunately, I had ignored the fact that the last order may by now have been closed, and would not be found in the pool MODE_TRADES, but in HISTORY.

How can I efficiently look through both open and closed trades, to find whether the last opened order was a Buy or Sell?

(Alternative I guess, would be to create some kind of static flag, updated when an order is opened to indicate order type. Is this a better solution?) .

Any suggestions appreciated 

whroeder1
15596
whroeder1  
davidb012: (Alternative I guess, would be to create some kind of static flag, updated when an order is opened to indicate order type.
EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV/file) of ticket numbers required.
davidb012
67
davidb012  
WHRoeder:
EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV/file) of ticket numbers required.

Yes, that's what I feared about having a static flag, rather than an OrderSelect loop.

 So, just trying to figure out how to efficiently search through both open and closed orders (MODE_TRADES and HISTORY)

To add comments, please log in or register