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

- www.mql5.com
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.
You can also approach it from the other side.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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
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 ?