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;
}