How to get just the buy open orders from a mix of open orders into an array?

 

I'd like to get the buy open orders of several orders into an array (like below):

OpenPrices:

BUY: 1.62181 (ticket:1)

Sell: 1.61282 (ticket: 2)

Buy: 1.59635 (ticket: 3)

Buy: 1.63566 (Ticket: 4)

Sell: 1.62332 (Ticket: 5)


I am trying to extract the Buy Open Prices to BuyOpenPriceArray[], such that:

BuyOpenPriceArray[0]: 1.63566 (Ticket:4)

BuyOpenPriceArray[1]: 1.59635 (Ticket:3)

BuyOpenPriceArray[2]: 1.62181 (Ticket:1)


And only BuyOpenPriceArray[2] is filled with changing the three Buy OpenPrices, one after another. [0] and [1] are not filled. What did I do wrong?


void OnTick()
 {

   double BuyOpenPriceArray[];
   int BuyTicketNumberArray[];
   int NumberOfBuyOrders=OrderTypeCounter(1);
   int NumberOfSellOrders=OrderTypeCounter(2);

   
   
   if((Open[1]<Close[1]) && OrdersTotal()==6) //Opens 4 orders (For OrdersTotal()== 0 to 3)
   {
      EnterTrade(OP_SELL, LotSize, clrDarkKhaki, magic);
   }
   if((Open[1]>Close[1]) && OrdersTotal()==5)
   {   
      EnterTrade(OP_SELL, LotSize, clrDarkKhaki, magic);
   }
   if((Open[1]<Close[1]) && OrdersTotal()==4) //Opens 4 orders (For OrdersTotal()== 0 to 3)
   {
      EnterTrade(OP_BUY, LotSize, clrAqua, magic);
   }
   if((Open[1]>Close[1]) && OrdersTotal()==3)
   {   
      EnterTrade(OP_BUY, LotSize, clrAqua, magic);
   }
   if((Open[1]<Close[1]) && OrdersTotal()==2) //Opens 4 orders (For OrdersTotal()== 0 to 3)
   {
      EnterTrade(OP_SELL, LotSize, clrDarkKhaki, magic);
   }
   if((Open[1]>Close[1]) && OrdersTotal()==1)
   {   
      EnterTrade(OP_SELL, LotSize, clrDarkKhaki, magic);
   }
   if((Open[1]<Close[1]) && OrdersTotal()==0) //Opens 4 orders (For OrdersTotal()== 0 to 3)
   {
      EnterTrade(OP_BUY, LotSize, clrAqua, magic);
   }

   
//------
    
   
   for(int i=0;i<=OrdersTotal()-1;i++)
   {  
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if(OrderMagicNumber()==magic)
         {
            if(OrderType()==OP_BUY) //Newest Buy order is LARGEST Open Price
            { 
               for(int j=NumberOfBuyOrders-1;j>=0;j++) 
               {

                  ArrayResize(BuyOpenPriceArray,NumberOfBuyOrders+1);
                  BuyOpenPriceArray[j]=OrderOpenPrice();
                  
                  Print("j is: ",j, "BuyOpenPriceArray[j]: ", BuyOpenPriceArray[j]);
                  
                  break;
               }
            }  
         }
      }
   }
   
   } 
 
              for(int j=NumberOfBuyOrders-1;j>=0;j++) {
                  ArrayResize(BuyOpenPriceArray,NumberOfBuyOrders+1);
                  BuyOpenPriceArray[j]=OrderOpenPrice();

You are storing the current selected order into all array elements.

Drop your loop. Enlarge the array and store once.

 
William Roeder #:

You are storing the current selected order into all array elements.

Drop your loop. Enlarge the array and store once.

Thank you for answering. I am a complete newbie.

By enlarging the array, you mean "ArrayResize", which I already did?

And for some reason, if I get rid of the "j" loop, the NumberOfBuyOrders function shows 0. With the loop, the NumberOfBuyOrders works as it should. 

It seems I need to take a closer look at that function. 


If you can give me a little code snippet to fix exactly how to fix it, I will greatly appreciate it. But I will take a closer look and post back later. It took me more than 36 hours of tinkering... before I posted here. But I will keep trying.

 

I just modified the code to get rid of the "j" Loop. 

And I managed to fix the NumberOfBuyOrders Problem.

But, only j = 3 (BuyOpenPriceArray[3]) is filled with OpenPrices. None others are filled with prices.

Here's the revised code, please help:

for(int i=0;i<=OrdersTotal()-1;i++)
   {  
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if(OrderMagicNumber()==magic)
         {
            if(OrderType()==OP_BUY) //Newest Buy order is LARGEST Open Price
            { 
                  int j=NumberOfBuyOrders;

                  Print("NumberOfBuyOrders: ", NumberOfBuyOrders);
                  Print("j value: ",j);
                  Print("OrdersTotal: ",OrdersTotal());

                  ArrayResize(BuyOpenPriceArray,NumberOfBuyOrders+1);
                  BuyOpenPriceArray[j]=OrderOpenPrice();
                  
                  Print("j is: ",j, "BuyOpenPriceArray[j]: ", BuyOpenPriceArray[j]);
                  
                  j--;
                  
               
            }  
         }
      }
   }
 
You enlarge the array to NumberOfBuyOrders+1 and then store. Where do you increase the variable?
 
William Roeder #:
You enlarge the array to NumberOfBuyOrders+1 and then store. Where do you increase the variable?
I am afraid I don't quite understand you (please forgive me, I am a complete newbie). Isn't this how I should enlarge the array: 
ArrayResize(BuyOpenPriceArray,NumberOfBuyOrders+1)

And with each iteration of the loop, more Buy orders are discovered and thus NumberOfBuyOrders+1 also increases automatically? 

 
YHMQL5 #: I am afraid I don't quite understand you
What part of “Where do you increase the variable” is unclear?