You have to track the condition with a variable.
- You start by declaring a boolean variable and initialise it to "false".
It should be either a globally scoped variable or a static local variable. - At the start of every new bar, you reset that variable to "false".
- When you close an order (or detect that it was closed), you set the variable to "true".
- Every time you need to place an order, you first check it:
If it is "true" you abort the placing of an order,
if not, proceed with placing the order.
Or you can check if there was any closed trade at the same bar already before it opens a new trade.
So before your function which opens new trades add
if(!isClosedTradeOnBar()) {//open trades}
And this function should be like that:
bool isClosedTradeOnBar(){ HistorySelect(currentBarTime,TimeCurrent()); //look for trades only from current bar for (int i = HistoryDealsTotal()-1; i >= 0; i--) { ulong ticket=HistoryDealGetTicket(i); //you can check magic and symbol here to find only trades from the EA if(HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT){ return(true);//closed trade on this bar } } return(true); }
Hi,
Thank you for the replies. I tried the first method with this:
bool NewBar() { static datetime previous_time = 0; datetime current_time = iTime(Symbol(), Period(), 0); if (previous_time!=current_time){ previous_time = current_time; return(true); } return(false); } If (NewBar()){//open trades}
It worked, but now some trades that were supposed to open did not. I can't figure out why.
Regarding the second method, how should currentBarTime be defined?
You have to track the condition with a variable.
- You start by declaring a boolean variable and initialise it to "false".
It should be either a globally scoped variable or a static local variable. - At the start of every new bar, you reset that variable to "false".
- When you close an order (or detect that it was closed), you set the variable to "true".
- Every time you need to place an order, you first check it:
If it is "true" you abort the placing of an order,
if not, proceed with placing the order.
Simpler approach.
Create a static or global datetime variable. In OnTick, if there is a position open, update the variable with bar time. Do not open if variable equals bar time.
Hi,
Thank you all for the suggestions. The easiest way I found was this:
void OnTick() { // Open trades on a New Bar Only static datetime Current = WRONG_VALUE; datetime Previous = Current; Current = iTime( _Symbol, _Period, 0 ); bool NewBar = ( Current != Previous ); if(NewBar) { //open trades }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I'm not a programmer, but want to learn and made a very basic EA using some EA creator. The problem is that each time the TP/SL is hit, while the Entry condition is still met on that same candle, it opens a new trade, which most of the time is not the optimal entry - see attached.
How to tell the EA to not open a new position if another was closed on the same candle?