Download MetaTrader 5

maximum buy or sell orders per bar

To add comments, please log in or register
Millions of traders download codes from MQL5 library. Share your app with them!
dergham
30
dergham 2016.07.26 20:42 

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 ?

Osama Shaban
1448
Osama Shaban 2016.07.26 23:50  

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 :) 

dergham
30
dergham 2016.07.27 03:15  
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
Ernst Van Der Merwe
4736
Ernst Van Der Merwe 2016.07.27 08:46  

Use continue instead of break.

          if(OrderOpenTime()<iTime(NULL,0,0))continue;
dergham
30
dergham 2016.07.27 09:28  

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
Marco vd Heijden
Moderator
4708
Marco vd Heijden 2016.07.27 10:04  
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.
dergham
30
dergham 2016.07.29 00:31  
Marco vd Heijden:
You can also approach it from the other side.
thank you very much
To add comments, please log in or register