Selecting the 2nd oldest opened order

 

Hi,

For an EA, i would like to select the 2nd oldest opened order on a symbol.

I have try many things but i still can't do it.

Does anyone can help me ?

Thanks in advance for your help!

Regards,

TAAD

 

is that code correct ?

int secondorder(string symbol)
{
int order=0;
int SecondOrder=0;
datetime first=0;
for(int i=OrdersTotal()-1;i>=0;i--)
   {
   OrderSelect(i,SELECT_BY_POS);
   if(OrderSymbol()==Symbol() && ( OrderType()==OP_BUY || OrderType()==OP_SELL ))
   {
   if(first==0 || OrderOpenTime()<first)
      {
      SecondOrder=order;
      first=OrderOpenTime();
      order=OrderTicket();
      }
   }
   }
return (SecondOrder);
}
 

Not sure if your code works or not, but for a second opinion, I think I would tackle it like this...

int secondorder(string symbol)      //<--- personally, I would pass magic or void seeing as you don't actually use string symbol
{
int mypool[];
int secondticket count;
for(int i=0;i>=OrdersTotal()-1;i++)
   {
   OrderSelect(i,SELECT_BY_POS);
   if (OrderSymbol()==Symbol() && ( OrderType()==OP_BUY || OrderType()==OP_SELL )) // and filter on magic here
      {
      count++;
      ArrayResize(mypool,count);
      mypool[count-1]=OrderTicket();
      }
   }
if (count>1)
   {
   ArraySort(mypool,count,0,MODE_ASCEND)
   secondticket=mypool[1];
   }

return (secondticket);

I haven't tested it, but I think it should get the job done. Let me know how it works out

V

EDIT: Sorry, made about a milliuon edits to the code after posting it! Doh!

 

thanks for your help but that doesn't work!

fonction always returns 0

I had modified it like this :

nt secondorder(string symbol)      //<--- personally, I would pass magic or void seeing as you don't actually use string symbol
{
int mypool[];
int secondticket=0,count=0;
for(int i=0;i>=OrdersTotal()-1;i++)
   {
   OrderSelect(i,SELECT_BY_POS);
   if (OrderSymbol()==Symbol() && ( OrderType()==OP_BUY || OrderType()==OP_SELL )) // and filter on magic here
      {
      count++;
      ArrayResize(mypool,count);
      mypool[count-1]=OrderTicket();
      }
   }
if (count>1)
   {
   ArraySort(mypool,count,0,MODE_ASCEND);
   secondticket=mypool[1];
   }

return (secondticket);
}
 

Sorry, that's what I get for not checking... couple of bugs but tested it now and this works fine...

int secondorder(int magic)
   {
   int mypool[];
   int secondticket, count;
   for(int i=0;i<=OrdersTotal()-1;i++)
      {
      OrderSelect(i,SELECT_BY_POS);
      if (OrderSymbol()!=Symbol() || OrderType()>1 || OrderMagicNumber()!=magic)
         {
         continue;
         }
      count++;
      ArrayResize(mypool,count);
      mypool[count-1]=OrderTicket();
      }
   if (count>1)
      {
      ArraySort(mypool,count,0,MODE_ASCEND);
      secondticket=mypool[1];
      }
   return (secondticket);
   }


I think you would be better passing magic than string symbol, particularly as there is no need for it so I adjusted it to suit. Easy enough to put back. Obviously, it only returns (a non zero) value if there are 2 or more orders in the pool.

hth

V

 
Thanks for your help !
Reason: