Cannot Get 2nd OrderOpenPrice(), Please Help

 

Hi coders, I need your help for my learning process.

I'm using a Multi-Symbol EA which it can run sizeable pairs and it was written by someone else.

My Mission :-
1) To get the 1st OrderOpenPrice
2) To get the 2nd OrderOpenPrice
3) Finally, get the difference MathAbs( 1 - 2 )

I have written the following codes :-

//+------------------------------------------------------------------+ Get 1st Order
   datetime Old1Tm=TimeCurrent();   // Get 1st Ord's Open Time
   double   Old1TmPx=0;             // Get 1st Ord's OpPx

   for(int i=OrdersTotal()-1; i>=0; i--) {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==sym &&
   (OrderMagicNumber()==mnBUY || OrderMagicNumber()==mnSEL)) {

   if((OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderOpenTime()<=Old1Tm) {
      Old1Tm=OrderOpenTime();
      Old1TmPx=OrderOpenPrice();}}}
//+------------------------------------------------------------------+ Get 2nd Order
   datetime Old2Tm=TimeCurrent();  // Get 2nd Ord's Open Time
   double   Old2TmPx=0;            // Get 2nd Ord's OpPx

   for(int i=OrdersTotal()-1; i>=0; i--) {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==sym &&
   (OrderMagicNumber()==mnBUY || OrderMagicNumber()==mnSEL)) {

   if((OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderOpenTime()<=Old2Tm && Old2Tm>Old1Tm) {
      Old2Tm=OrderOpenTime();
      Old2TmPx=OrderOpenPrice();}}}

   if(sym=="AUDNZD" || sym=="NZDUSD" || sym=="USDCAD") {
   Print(sym+" /Old1TmPx "+DoubleToStr(Old1TmPx,cDigits)+" ("+TimeToStr(Old1Tm)+
   ") /Old2TmPx "+DoubleToStr(Old2TmPx,cDigits)+" ("+TimeToStr(Old2Tm)+") /Pips "+
   DoubleToStr(MathAbs(Old1TmPx-Old2TmPx)/cPnt/10,3));}


For the (1 - 1st OrderOpenPrice), I can prettily get the correct output, the only problem is to get the (2 - 2nd OrderOpenPrice), I failed to get the correct output.

I've 3 pairs that with open orders :-
1) AUDNZD, total 2 open orders :-
  1st OrderOpenPrice : 1.07610 (correct)
  2nd OrderOpenPrice : 1.07590 (but EA output was incorrect, exactly the same as 1st OrderOpenPrice)

2) NZDUSD, total 2 open orders :-
  1st OrderOpenPrice : 0.61787 (correct)
  2nd OrderOpenPrice : 0.61794 (but EA output was incorrect, exactly the same as 1st OrderOpenPrice)

3) USDCAD, total 4 open orders :-
  1st OrderOpenPrice : 1.36799 (correct)
  2nd OrderOpenPrice : 1.36779 (but EA output was incorrect, exactly the same as 1st OrderOpenPrice)




FYI, I suspect when come to the 2nd loop, the Old1Tm turns into 0 (datetime), therefore the output for 2nd OrderOpenPrice will always be the same as the 1st OrderOpenPrice.


Is there any way to get the correct output for 2nd OrderOpenPrice ? FYI, I do not want to use Array as it would be make my strategy very much more complicated. Secondly, my EA is a Multi-Symbol EA, using Array is not so suitable for my case, therefore I would like to avoid it.

Could you please check my loops, is there any way to improve the loops so that I can get the correct output for 2nd OrderOpenPrice, thank you.

 

Hi

Here on the second loop you need to also check if the oldest trade is not this already found Old1Trade as you tried to do.But you need to compare Ordertime and not Old2Tm, because it’s not yet updated (when you compare times). So simply add this small change:

datetime Old2Tm=TimeCurrent();  // Get 2nd Ord's Open Time
   double   Old2TmPx=0;            // Get 2nd Ord's OpPx

   for(int i=OrdersTotal()-1; i>=0; i--) {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==sym &&
   (OrderMagicNumber()==mnBUY || OrderMagicNumber()==mnSEL)) {

   if((OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderOpenTime()<=Old2Tm && OrderOpenTime()>Old1Tm) {
      Old2Tm=OrderOpenTime();
      Old2TmPx=OrderOpenPrice();}}}

Best Regards

 
Marzena Maria Szmit #:

Hi

Here on the second loop you need to also check if the oldest trade is not this already found Old1Trade as you tried to do.But you need to compare Ordertime and not Old2Tm, because it’s not yet updated (when you compare times). So simply add this small change:

Best Regards

Thanks so much Marzena, yes, I tested, this is the correct solution.