Hello,
I made a simple EA but to speed up optimization I want to make the EA opena new trade only when the indicators are right and only when a new bar starts.
The EA has to wait until the bar closes and then opens a new trade only when the new bar opens.
Could someone tell me the code to use? I believe it will be something like if bar opens but I am sure and I can't find the info online.
Thank you
//------------------------------------- NUEVA VELA ------------------------------------- bool nuevaVela(string simb= NULL, ENUM_TIMEFRAMES marcoTmp= PERIOD_CURRENT) { static datetime nuevoTiempo=0; datetime horaVela= iTime(simb, marcoTmp, 0); bool resp= false; if(nuevoTiempo!=horaVela) { nuevoTiempo= horaVela; resp= true; } return(resp); }
datetime iTime(string simb, ENUM_TIMEFRAMES marcoTmp, int nVela= 0) { datetime aValor[1]; int nCopiado=CopyTime(simb, marcoTmp, nVela, 1, aValor); if(nCopiado!=1) return(0); return(aValor[0]); //si el valor devuelto es 0, falló la función }
The above code reports the start of a new bar.
add in this if (Volume == 0) { }
int counter=0; void OnTick() { if (NewBar()) { counter++; Comment("Bar "+counter); } } bool NewBar() { static datetime time0; // memory datetime Time[]; ArraySetAsSeries(Time,true); CopyTime(Symbol(),0,0,1,Time); if (time0<Time[0]) { time0=Time[0]; return true; } return false; }This even works without ArraySetAsSeries(), I don't really know if it's needed.
This even works without ArraySetAsSeries(), I don't really know if it's needed.
Hello,
I made a simple EA but to speed up optimization I want to make the EA opena new trade only when the indicators are right and only when a new bar starts.
The EA has to wait until the bar closes and then opens a new trade only when the new bar opens.
Could someone tell me the code to use? I believe it will be something like if bar opens but I am sure and I can't find the info online.
Thank you
I use this:
int start() { //---- if(IsNewCandle()) { // add your code return(0); } return(0); } //+------------------------------------------------------------------+ //insuring its a new candle function //+------------------------------------------------------------------+ bool IsNewCandle() { static int BarsOnChart=0; if (Bars == BarsOnChart) return (false); BarsOnChart = Bars; return(true); }
Please don't reply inside quote.
Your code is for mql4 only. Doesn't work with mql5.
Number of bars isn't a reliable way to detect a new bar. (What happens when max bars in chart is reached ?)
Please don't reply inside quote.
Your code is for mql4 only. Doesn't work with mql5.
Number of bars isn't a reliable way to detect a new bar. (What happens when max bars in chart is reached ?)
Thank you very much for everyone who replied with solutions to my problem.
I tried all the code you people gave me.
Some of them don't work, it gives error after error, and the others don't make the EA open trades only when a new bar opens, the EA continues to make trades as soon as the indicators are right.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I made a simple EA but to speed up optimization I want to make the EA opena new trade only when the indicators are right and only when a new bar starts.
The EA has to wait until the bar closes and then opens a new trade only when the new bar opens.
Could someone tell me the code to use? I believe it will be something like if bar opens but I am sure and I can't find the info online.
Thank you