selecting specific pending orders

 

I know how to select a market order like this :


for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{


But can some one please tell me how to select a specific pending order... For example :


I have 6 pending buys above price... and 6 pending sells below price...I want to select the lowest pending buy entry price and the highest pending sell entry price


so that I can subtract the pending sell entry price from the pending buy entry price and calcualte the size of the gap between them.


Does any one know how to do this??

 
23510:

I have 6 pending buys above price... and 6 pending sells below price...I want to select the lowest pending buy entry price and the highest pending sell entry price

Presumably something like the following. It's just a question of running through the order list, tracking the highest/lowest entries.


   // Open price and ticket number of lowest pending buy order found

   // (LowestBuyPrice starts at a very big number so that any order 

   // we find is lower)

   double LowestBuyPriceSoFar = 999999;

   int TicketOfLowestBuy = 0;

   

   // Open price and ticket number of highest pending sell order found

   double HighestSellPriceSoFar = 0;

   int TicketOfHighestSell = 0;

   

   // Loop through all open and pending orders 

   for (int iOrder = 0; iOrder < OrdersTotal(); iOrder++) {

      

      // Select the nth order

      OrderSelect(iOrder, SELECT_BY_POS);

      

      // Make sure that the order applies to this EA

      if (OrderSymbol() == Symbol() && OrderMagicNumber() == <your magic number>) {

         

         // Different handling for buys and sells. This code looks at ALL pending 

         // orders - both stops and limits. Stating the obvious: if there are both 

         // buy limits and buy stops, then this code will always return the

         // details of a buy limit order. Vice versa for sells. And, for the avoidance

         // of doubt, buys and sells are treated independently: the lowest buy

         // could be above/below the highest sell depending on the nature of your system.

         

         switch (OrderType()) {

            case OP_BUYSTOP:

            case OP_BUYLIMIT:

               // Is the trigger price of this buy order lower than

               // the lowest we've found so far?

               if (LowestBuyPriceSoFar > OrderOpenPrice()) {

                  LowestBuyPriceSoFar = OrderOpenPrice();

                  TicketOfLowestBuy = OrderTicket();

               }

               break;

            

            case OP_SELLSTOP:

            case OP_SELLLIMIT:

               // Is the trigger price of this sell order higher than

               // the highest we've found so far?

               if (HighestSellPriceSoFar < OrderOpenPrice()) {

                  HighestSellPriceSoFar = OrderOpenPrice();

                  TicketOfHighestSell = OrderTicket();

               }         

               break;

         }

      }

   }


   // TicketOfLowestBuy and TicketOfHighestSell now contain the ticket numbers

   // of the highest/lowest pending orders. Either can contain zero if there

   // are no pending buy/sell orders. If non-zero, then HighestSellPriceSoFar

   // and/or LowestBuyPriceSoFar will contain the entry price(s) of the order(s).


 

jjc,

Thank you I will try that. I want to use it in the grid EA I am coding.

thanx

 

jjc,

the code works for giving me the highest pending sell order but returns a 0.00000 for the lowest pending buy order and this is strange because there are 6 pending buys so there must be a value

 
23510:

jjc,

the code works for giving me the highest pending sell order but returns a 0.00000 for the lowest pending buy order and this is strange because there are 6 pending buys so there must be a value

It ought to be impossible for LowestBuyPriceSoFar to contain zero. If no pending buys are found, LowestBuyPriceSoFar should remain at its starting value of 999,999 (and TicketOfLowestBuy should contain zero). The only way for LowestBuyPriceSoFar to be zero ought to be if there's a buy-limit with a trigger price of zero, but I don't think any brokers are daft enough to allow that.


But it sounds like I've made a mistake somewhere. I suggest you use Print() to log which orders the code is finding, and what their open prices are, and work out from that where the bug is.

 
thanx I did do a print and thats how i found out that the value its returning is 0.0000, I will try to source the error like you suggest...if I cant figure it out i will post here again...thanx for the advice
 
23510:
thanx I did do a print and thats how i found out that the value its returning is 0.0000, I will try to source the error like you suggest...if I cant figure it out i will post here again...thanx for the advice

jjc,

thank you for all your help! I have gone back and re-read all your advice and found my mistakes...I have managed to make my grid EA work exactly as i wanted...thanx again for all your great advice!!! I really appreciate it.

Reason: