Inconsistent result when looking for the oldest of currently open long orders

 

Hi, I need to point out the first long order that occurs after a pause of no open long orders (this EA trades multiple orders in the same direction).

The code below tries to find this order, and put a marker on it (for debugging) .

The EA places its trades as usual, and in the Start function I call:

bool dummy = FirstInRow();

This should be pretty simple, and the code below gets it right most of the time, but sometimes it does not... Why ?

This is what it looks like when correct:

But sometimes I get this (the 2nd Vline should not be there):

//----------------------------------------------------------------------------------
bool FirstInRow()
   {
   if(OrderCount(1) < 1) return(0);//Currently no open long orders so get out and save time!
   
   int TempTime = TimeCurrent() + 99999999;//No orders can have this opentime
   for (int i = OrdersTotal() - 1; i >= 0; i--)
      {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
         {
         if (OrderType() == OP_BUY)
            {                                    
            if (OrderOpenTime() < TempTime) TempTime = OrderOpenTime();//Found an older order              
            }
         }   
      }
//----------      
            
   DrawVline(Aqua, TempTime );//Marker will be at the opentime of the oldest open order
   return (FALSE);
   }
//----------------------------------------------------------------------------------

//--------------- OrderCount -----------------------------------
int OrderCount(int Dir)//Denne er testet 100% OK!
   {
   int OpenLongs = 0;  
   int OpenShorts = 0; 
   int Count;   
   for(int Order = OrdersTotal() - 1; Order >= 0; Order--)
      {
      OrderSelect(Order, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
         {         
         if ((OrderType() == OP_BUY)) OpenLongs++;//Found a long order, count it !               
         if ((OrderType() == OP_SELL)) OpenShorts++;//Found a short order, count it !                         
         }
      }    
   if(Dir ==1) Count = OpenLongs;//Longs was requested
   if(Dir == -1) Count = OpenShorts;//Shorts was requested          
   return (Count);
   }
//----------------------------------------------------------

//-----------------------------------------------------------
void DrawVline (color Color, datetime time)
   {//Typical Use: DrawVline(Yellow, Time[i]); //DrawVline(Yellow, Time[0]);
   static int cntr;
   static double PrevVlineTime;
   if (time == PrevVlineTime) return(0);//Still on same bar
      ObjectCreate( "UpLine"+cntr, OBJ_VLINE, 0, time, 0);
      ObjectSet("UpLine"+cntr, OBJPROP_COLOR, Color);
      ObjectSet("UpLine"+cntr, OBJPROP_BACK,true);
      cntr++;
      PrevVlineTime = time;//Store bar time of last drawn line
   return(0);
   }     
//----------------------------------------------------------- 
 
DayTrader:


But sometimes I get this (the 2nd Vline should not be there):

I don't see a problem with your picture . . . . assuming I understand what you are trying to do.

Let me explain . . .

When the 1st order has opened it is the oldest so it gets marked with an Aqua line . . . then the 2nd order opens, the 1st is still the oldest so no new aqua line is drawn. Then the 3rd order opens and the 1st is still the oldest . . no new aqua line is drawn. The 3rd trade closes. Then the 1st order closes . . . at this point in time the 2nd order is now the oldest open order so it gets marked with an aqua line . . . shortly after this it to closes.

 

Damn you're right! I was thinking along a linear time line and it didn't strike me that a marker could be placed back in time.

Thanks!

 
DayTrader:

Damn you're right! I was thinking along a linear time line and it didn't strike me that a marker could be placed back in time.

Thanks!

You are most welcome. :-)
Reason: