Newbie needs help determining bar shift since last closed order

 

Hi,

I’m new to mql4 and new to programming. I was hopping, someone could help me with this little problem I encountered. I need an EA to determine bar shift since the last closed order. This is what I came up with …

int BarsSinceLastOrder()
   {
   if(OrdersHistoryTotal()<1)
      {
      int BSLO=BSLOAcc;
      }
   else
      {   
      int HistoryTotal=OrdersHistoryTotal();
      for(int i=HistoryTotal+1;i==HistoryTotal;i--)
         {
         OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
            {
            datetime POCT=OrderCloseTime();
            }
         }
      for(int n=0;;n++)
         {
         if(iTime(NULL,0,n)==POCT)
         n=BSLO;
         }
      }
   
   return();
   }

Somehow it doesn’t seem right, and of course compilation shows an error: 'HistoryTotal' - variable expected

I'd appreciate any constructive comment or advice for a newbie. Thanx in advance.

 
DrLukas:

Hi,

I’m new to mql4 and new to programming. I was hopping, someone could help me with this little problem I encountered. I need an EA to determine bar shift since the last closed order. This is what I came up with …

Somehow it doesn’t seem right, and of course compilation shows an error: 'HistoryTotal' - variable expected

I'd appreciate any constructive comment or advice for a newbie. Thanx in advance.

Hi DrLukas,

1. Your "variable expected" err occurs because you use old and obsolete function of MQL - possibly a function used before MQL4. Search for it in MetaEditor or Click here and search for function name HistoryTotal. To solve this, just rename that variable into something else, for example HistoryTotals, there I add letter 's' after Total, and you won't have that err again.

2. In order to get that last closed order, you need to find the biggest closed time. To do that you need to compare close time of each closed order against another.

int HistoryTotals = OrdersHistoryTotal();
datetime Last_Closed_Time;
for (int i = HistoryTotals; i >= 0; i--)                   // ==>> this is good programming habit : count down all orders to zero
   {
   if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == true // ==>> only if OrderSelect() return is true !, your code doesn't do that !
       && OrderType() <= OP_SELL 
       && OrderSymbol() == Symbol() 
       && OrderMagicNumber() == MagicNumber)
       {
       if (OrderCloseTime() > Last_Closed_Time) Last_Closed_Time = OrderCloseTime(); // ==>> save the biggest closed time
       }
   }

3. Use iBarShift() - click here - to find how far bar is shifted since the last closed order.

int Bar_Shift = iBarShift (Symbol(), Period(), Last_Closed_Time, True);

:D

 
for (int i = HistoryTotals; i >= 0; i--)                   // ==>> this is good programming habit : count down all orders to zero
   {
   if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == true /
  1. positions are relative to zero. There will never be an order at OrderSelect(OrdersHistoryTotal(), ...
  2. you would never code IF ( (2+2 == 4) == True ) would you? IF(bool) or IF(!bool) is sufficient.
  3. You don't need braces around a single statement.
    for(int iPos = OrdersHistoryTotal-1; iPos >= 0; iPos--)
    if( OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)      // Only my orders w/
    &&  OrderMagicNumber()  == magic.number                 // my magic number  
    &&  OrderType()         <= OP_SELL//Avoid cr/bal forum.mql4.com/32363#325360
    &&  OrderSymbol()       == chart.symbol                 // and my pair.
    &&  OrderCloseTime() > Last_Closed_Time) 
    ){  Last_Closed_Time = OrderCloseTime(); }
Checking symbol handles the case where the human forgot to change the MN when putting on another chart.
Reason: