Second opinion needed, How to locate order with lowest price?

 

Stuck with code where I need to locate order that has smallest OrderOpenPrice().

Code selects latest open order (position)..why not order where OrderOpenPrice is closest to not dependent what time order was added or position?

double   bScanOpn;
double   bScanPenOpn;
double   TopPrice=0, MinPrice=100000;

if(tot!=0 && bChkOpn!=0)
{
   for(int btk=tot-1;btk>=0;btk--)
   {
      if(!OrderSelect(btk,SELECT_BY_POS,MODE_TRADES)==true)continue;
      {
         if(OrderSymbol()==Symbol())
         {
            if(OrderMagicNumber()==bMagic)
            {
               if(OrderOpenPrice()<MinPrice)
               {
                  if(OrderType()==OP_SELL)
                  {
                  bScanOpn=OrderOpenPrice();
                  }
               }
               if(OrderOpenPrice()>bChkOpn && OrderType()==OP_SELLSTOP)
               {
               bScanPenOpn=OrderOpenPrice();
               }
            }
         }
      }
   }
}
 
elanin:

Stuck with code where I need to locate order that has smallest OrderOpenPrice().

Code selects latest open order (position)..why not order where OrderOpenPrice is closest to not dependent what time order was added or position?

You have  {  }  braces that are not needed . . .

 

double   bScanOpn;
double   bScanPenOpn;
double   TopPrice=0, MinPrice=100000;

if(tot!=0 && bChkOpn!=0)
   {
   for(int btk=tot-1;btk>=0;btk--)
      {
      if(!OrderSelect(btk,SELECT_BY_POS,MODE_TRADES)==true) continue;
//      {   not needed
      if(OrderSymbol()==Symbol())
         {
         if(OrderMagicNumber()==bMagic)
            {
            if(OrderOpenPrice()<MinPrice)
               {
               if(OrderType()==OP_SELL)
                  {
                  bScanOpn=OrderOpenPrice();
                  }
               }
            if(OrderOpenPrice()>bChkOpn && OrderType()==OP_SELLSTOP)
               {
               bScanPenOpn=OrderOpenPrice();
               }
            }
         }
      }
   }
//  }  not needed

What is the value of   tot ?  it is not shown in this code . . .   there is no need to check if tot != 0  the for loop will do nothing if it is zero . . .  why use MinPrice ?  you said you wanted the lowest OpenPrice ?  not lower than MinPrice . . .  do you know what it is that you actually want ?

 
RaptorUK:

You have  {  }  braces that are not needed . . .

 

What is the value of   tot ?  it is not shown in this code . . .   there is no need to check if tot != 0  the for loop will do nothing if it is zero . . .  why use MinPrice ?  you said you wanted the lowest OpenPrice ?  not lower than MinPrice . . .  do you know what it is that you actually want ?


Sry this piece of code started to look form half day testing a little messy.

Total order number is mix of pending and open buy or sell order..total is between 50 to 200 orders depending situation.

I used MinPrice for hoping to find my way out of troubleshooting..and you are right there is no point using it. 

I hit the wall e.g when new SELL order is triggered with higher price than some older SELL orders and loop gets me latest triggered SELL order OrderOpenPrice, but I want to get OrderOpenPrice from SELL order that has lowest OrderOpenPrice form total SELL orders in order pool.

 
   if(TotalOpenEA > 0)
     {//1
      TotalOpenEA = 0;
      MinPrice=100000;  //lowest sell
      TopPrice=0;       //highest buy
      for(int i = OrdersTotal()-1; i >= 0 ; i--)
        {//2
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
         if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
         TotalOpenEA++;
         //---- check order type
         if(OrderType()==OP_BUY)
           {//3
           if(OrderOpenPrice()> TopPrice)TopPrice=OrderOpenPrice();
           }//3 
         if(OrderType()==OP_SELL)         
           {//4
            if(OrderOpenPrice()< MinPrice)MinPrice=OrderOpenPrice();
           }//4

        }//2
     }// 1                      
be careful to the wall
 
deVries:
be careful to the wall


deVries..I and the wall are grateful!

I did fast integration to my code and got this logic working as I expected. I am not 100% sure why your code works. Only thing at this point.. I (think) I didn't try was to use break;

Anyway I will take a closer look to fully understand what I missed before..back to "work" for me.

Reason: