Download MetaTrader 5

Limit to one order per candle and max orders allowed... Not working!

To add comments, please log in or register
Having troubles understanding the code? Articles can help you!
Wemerson Guimaraes
560
Wemerson Guimaraes 2016.03.10 13:50 

Hi...

I need to limit number of opened orders in an asset AND limit one order per candle too.

See my code... it's allowing more than one orders per candle. I fixed it to allow 2 orders, but only one per candle:


input bool LimitBar=true; // Limitar 1 ordem por vela?
input int MaxOrders = 2;

int LastOrderBar; // Store the last candle with opened order


void OnTick(){
 
   //... code above, not important
   //... MySignalPoint --> Is the point i allow the buy/swll
   //... Not here because is too large and is not on the scope of this thread

   // If LimitBar is TRUE, check if last order was in the current bar
   // If LimitBar is FALSE, set true to bar_none_order to allow more orders on same bar
   bool bar_none_order = ( LimitBar && LastOrderBar != Bars ) || ( !LimitBar );

   // If MaxOrders = 0, no_max_orders must be defined as TRUE, to not limit number of orders
   // If MaxOrders > 0, then check if opened orders of current the asset is smaller than MaxOrders, and if smaller, 
   //		set no_max_orders too, because need to allow new orders till reach the limit.
   bool no_max_orders = MaxOrders == 0 ||( CountOpenOrders() < MaxOrders );

   // Here i check if all conditions are true to allow opening orders
   if( bar_none_order && no_max_orders && MySignalPoint){
      Neworder(cmd);   // BUY/SELL
      LastOrderBar = Bars; // Here i store the last bar that has an order opened
   }

}


int CountOpenOrders()
{
   int count=0;

   for( int i=OrdersTotal()-1; i>=0; i--)
   {
      if( OrderSelect( i, SELECT_BY_POS ) )
      {
         if( OrderSymbol() == Symbol() && OrderMagicNumber() == MyMagicNumber ){
            count++; 
         }
      }
   }
   
   return count;
}
whroeder1
14466
whroeder1 2016.03.10 14:27  
  1. This can be
    bool bar_none_order = ( LimitBar && LastOrderBar != Bars ) || ( !LimitBar );
    simplified to
    bool bar_none_order = !LimitBar || LastOrderBar != Bars;

  2. Print out your variables and find out why.
Vasyl Nosal
18534
Vasyl Nosal 2016.03.10 14:33  
LastOrderBar = Bars
better
LastOrderBar = Time[0]
Wemerson Guimaraes
560
Wemerson Guimaraes 2016.03.10 15:20  

Found it !

Here was right... the code i typed here was ok... but on my code, i forgot to store the position of new order

Right code 

 

Then i fix it and change to eevviill suggestion and works fine. Ah... and make the WHRoeder change suggestion too:

input bool LimitBar=true; // Limitar 1 ordem por vela?
input int MaxOrders = 2;

datetime LastOrderBar; // Store the last candle with opened order


void OnTick(){
 
   //... code above, not important
   //... MySignalPoint --> Is the point i allow the buy/swll
   //... Not here because is too large and is not on the scope of this thread

   // If LimitBar is TRUE, check if last order was in the current bar
   // If LimitBar is FALSE, set true to bar_none_order to allow more orders on same bar
   bool bar_none_order = !LimitBar || LastOrderBar != Time[0];

   // If MaxOrders = 0, no_max_orders must be defined as TRUE, to not limit number of orders
   // If MaxOrders > 0, then check if opened orders of current the asset is smaller than MaxOrders, and if smaller, 
   //		set no_max_orders too, because need to allow new orders till reach the limit.
   bool no_max_orders = MaxOrders == 0 ||( CountOpenOrders() < MaxOrders );

   // Here i check if all conditions are true to allow opening orders
   if( bar_none_order && no_max_orders && MySignalPoint){
      Neworder(cmd);   // BUY/SELL
      LastOrderBar = Time[0]; // Here i store the last bar that has an order opened
   }

}


int CountOpenOrders()
{
   int count=0;

   for( int i=OrdersTotal()-1; i>=0; i--)
   {
      if( OrderSelect( i, SELECT_BY_POS ) )
      {
         if( OrderSymbol() == Symbol() && OrderMagicNumber() == MyMagicNumber ){
            count++; 
         }
      }
   }
   
   return count; 
}
To add comments, please log in or register