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

Back to topics list To post a new topic, please log in or register
avatar
127
wemersonrv 2016.03.10 14: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;
}

 

 

 

avatar
12983
WHRoeder 2016.03.10 15: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.
avatar
872
eevviill 2016.03.10 15:33 #
 
LastOrderBar = Bars
better
LastOrderBar = Time[0]
avatar
127
wemersonrv 2016.03.10 16: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; 
}


Back to topics list  

To add comments, please log in or register