OrdersTotal doesnt always work?

 

Hi, why in the following code does OrdersTotal() not always return an order? 

void OnTick()
{
   // Timer
   int hour = Hour();
   int timehour = 10;

   // Buy Switch   
   static bool buySwitch=false;     

   // Adjust Lot Size      
   double lotSize=LotSize;

   // Current order counts     
   int buyCount = 0;    
   
   for(int order = 0; order <= OrdersTotal() - 1; order++)  
   {
      bool select = OrderSelect(order,SELECT_BY_POS);  
                                        
      if(OrderMagicNumber() == MagicNumber && select)    
      {                                                                           
         if(OrderType() == OP_BUY) buyCount++;     
      }   
   }

   // Our order condition  
   if(buySwitch == false && hour==timehour)  
   { 
      // Open buy order                                                                                 
      gBuyTicket = OrderSend(_Symbol,OP_BUY,lotSize,Ask,Slippage,0,0,"Buy order",MagicNumber,0,clrGreen);
      buySwitch=true;
      
      // Add stop loss & take profit to Buy order
      if(gBuyTicket > 0 && (StopLoss > 0 || TakeProfit > 0))
      {
         bool select = OrderSelect(gBuyTicket,SELECT_BY_TICKET); 
         
         // Calculate stop loss & take profit
         double stopLoss = 0, takeProfit = 0;
         if(StopLoss > 0) stopLoss = OrderOpenPrice() - (StopLoss * _Point);
         if(TakeProfit > 0) takeProfit = OrderOpenPrice() + (TakeProfit * _Point);

         // Modify order
         bool modify = OrderModify(gBuyTicket,0,stopLoss,takeProfit,0);
      }
   }

   if(hour > timehour)
   {
      buySwitch=false;
   }
}

It seems if the trade is closed out to fast it misses the order?

Is there a way to make it return the order?

 
Stephen Reynolds:

Hi, why in the following code does OrdersTotal() not always return an order? 

It seems if the trade is closed out to fast it misses the order?

Is there a way to make it return the order?

I don't know what you mean.

OrdersTotal() does not return an order. It returns the number of open orders.

How do you know what it returns?

All your loop does it count the number of Buy orders and save it in the variable buycount.
Then your code does nothing with buycount , doesn't print the value or anything.

 
Keith Watford #:

I don't know what you mean.

OrdersTotal() does not return an order. It returns the number of open orders.

How do you know what it returns?

All your loop does it count the number of Buy orders and save it in the variable buycount.
Then your code does nothing with buycount , doesn't print the value or anything.

Sorry if i worded that wrong. Yes it returns the number of open orders thats what i mean.

On applying this code to tester on open test as shown here trade 1 OrdersTotal() doesnt return the open order 1st trade but on 2nd trade it does return the open order okay.         

Also if i use ontick test it returns okay. The problem is on open test. It isnt returning an open order?

Files:
trade_1.png  24 kb
trade2.png  20 kb
 
Stephen Reynolds #:

Sorry if i worded that wrong. Yes it returns the number of open orders thats what i mean.
On applying this code to tester on open test as shown here trade 1 OrdersTotal() doesnt return the open order 1st trade but on 2nd trade it does return the open order okay. 
Also if i use ontick test it returns okay. The problem is on open test. It isnt returning an open order?

Hello,

Btw, OrdersTotal() is about index numbering, not the number of open positions.
So, if you have 1 open position, then that position has index number 0.
When you have 2 open positions, the second position is numbered index 1.

It's like what you coded in the loop :

for(int order = 0; order <= OrdersTotal() - 1; order++)  
{
 
Stephen Reynolds #:

Sorry if i worded that wrong. Yes it returns the number of open orders thats what i mean.

On applying this code to tester on open test as shown here trade 1 OrdersTotal() doesnt return the open order 1st trade but on 2nd trade it does return the open order okay.         

Also if i use ontick test it returns okay. The problem is on open test. It isnt returning an open order?

OrdersTotal() returns 0 because there are no open trades as you can see in your log.

Trade #1 has been closed by SL and #2 by TP before  OrdersTotal() is called.

 
Yohana Parmi #:

Hello,

Btw, OrdersTotal() is about index numbering, not the number of open positions.
So, if you have 1 open position, then that position has index number 0.
When you have 2 open positions, the second position is numbered index 1.

It's like what you coded in the loop :

OrdersTotal() returns the number of open orders (including pending orders).

Knowing the number of open orders allows the coder to run the loop using the orders' index numbers.

 
Keith Watford #:

OrdersTotal() returns the number of open orders (including pending orders).
Knowing the number of open orders allows the coder to run the loop using the orders' index numbers.

Yes, you're absolutely right.
- thanks.