maximum buy or sell orders per bar

 

I am trying to limit the number of same type orders  .for example if buy order is closed don't open another one in same bar .unfortunately the next code doesn't work 

extern int max_same_order=1;
  void OnTick()
 int closed_buys=0,closed_sells=0;
  for(i=OrdersTotal()-1;i>=0;i--)
           {
            OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
            if(OrderOpenTime()>=iTime(NULL,0,0))
             {if (OrderType()==OP_BUY)closed_buys++;else if(OrderType()==OP_SELL)closed_sells++;}
           if(OrderOpenTime()<iTime(NULL,0,0))break;
           }
                     //trading orders
         if (closed_buys<max_same_order/* && other conditions*/)
                 //buy
    if (closed_sells<max_same_order/* && other conditions*/)
                 //sell

the code has no effect and many orders same type opened in same bar one after the close of one. what is wrong with it ?

 

You need to check the existence of an order to prevent opening a new order and at the same time to let the EA open orders only

at the open of new bars only. This way, only one order will opened on the bar if other conditions are met, otherwise, that bar is skipped.

 

bool ExistPositionsBuy() {
        for (int i=0; i<OrdersTotal(); i++) {
                if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
                        if (OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber) {
                                return(True);
                        }
                } 
        } 
        return(false);
}

 

An other solution is to compare the existence of orders within the bar number :)  

bool ExistBuyBarPositions(int a) {
        for (int i=0; i<OrdersTotal(); i++) {
                if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
                        if (OrderSymbol()==Symbol() && OrderType()==OP_BUY && Bars ==a && OrderMagicNumber()==MagicNumber) {
                                return(True);
                        }
                } 
        } 
        return(false);
}

 

 The above functions are custom functions I used to use in my own codes and do minor changes to make them work the way I want.

So, as you are a programmer, you will understand their function and can tweak them to let them work for you the way you want :) 

 
Osama Shaban:

You need to check the existence of an order to prevent opening a new order and at the same time to let the EA open orders only

at the open of new bars only. This way, only one order will opened on the bar if other conditions are met, otherwise, that bar is skipped.

 

 

An other solution is to compare the existence of orders within the bar number :)  

 

 The above functions are custom functions I used to use in my own codes and do minor changes to make them work the way I want.

So, as you are a programmer, you will understand their function and can tweak them to let them work for you the way you want :) 

thank you Osama,I am preventing the ea from opening 2 orders at the same time.my problem is to prevent it from opening one trade after a similar one closed at same candle
 

Use continue instead of break.

          if(OrderOpenTime()<iTime(NULL,0,0))continue;
 

I searched a lot in this forum and outside

I tried the solutions here https://forum.mql4.com/54407

I tried but everything fails,may be I apply wrongly

finally I make a very simple solution

datetime last_buy_time,last_sell_time;
void OnTick()
  {
if(last_buy_time<Time[0]) /* with other conditions*/{/*buy;*/last_buy_time=TimeCurrent();}
//same for sell

 I know it is very unprofessional but it is the only solution that worked with me 

thanks for all of you 

EA for partial closing of open trade (Rafael Rodriguez) - MQL4 forum
EA for partial closing of open trade (Rafael Rodriguez) - MQL4 forum
  • www.mql5.com
EA for partial closing of open trade (Rafael Rodriguez) - MQL4 forum
 
datetime time;

if(time!=iTime(Symbol(),PERIOD_CURRENT,0))
  {
   //Do Something...

   time=iTime(Symbol(),PERIOD_CURRENT,0);// save new value
  }
You can also approach it from the other side.
 
Marco vd Heijden:
You can also approach it from the other side.
thank you very much
Reason: