However as soon as I add this code, the compiler starts saying that there are { in the wrong place and always too many or too few. When I remove the code if (iTime(NULL, 0, 0) everything is fine again...
//Hi,//add the following line to your declarations headerstaticdatetime _lastBarTime = 0; // DateTime of the last new bar which opened//Next add this line to your init function:
_lastBarTime = iTime(Symbol(),_period(),0);
//Now add the condition to check for a new bar which is:if(NewBar()==true) {
... do stuff here
}
//And dont forget to include the NewBar function which goes like this:bool NewBar() {
if (iTime(Symbol(),Period(),0) != _lastBarTime) {
_lastBarTime = iTime(Symbol(), Period(),0);
return (true);
} elsereturn (false);
}
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
Have you tried EA generator software to build EA? May be useful for you.
This simple script detects the start of a new BAR but it does only works if the price/volume has changed during the BAR period.
Maybe you can use it.
// NEW BAR CHECK.//------------------// Rates structure for BAR data.MqlRates TikData[1];
CopyRates(Symbol(), Period(), 1, 1, TikData); // Copy the data of the last COMPLETE bar to the rates structure;staticdouble dBar_Open;
staticdouble dBar_High;
staticdouble dBar_Low;
staticdouble dBar_Close;
staticlong lBar_Volume;
staticdatetime nBar_Time;
// Boolean confirmation for a new BAR.bool bStart_NewBar = false;
// Check of de prijs is veranderd tov de vorige BAR. if(TikData[0].open != dBar_Open || TikData[0].high != dBar_High || TikData[0].low != dBar_Low || TikData[0].close != dBar_Close || TikData[0].tick_volume
!= lBar_Volume || TikData[0].time != nBar_Time || prev_calculated == 0)
{
bStart_NewBar = true; // New bar detected // reset data
dBar_Open = TikData[0].open;
dBar_High = TikData[0].high;
dBar_Low = TikData[0].low;
dBar_Close = TikData[0].close;
lBar_Volume = TikData[0].tick_volume;
nBar_Time = TikData[0].time;
}
if(bStart_NewBar == true) // Check new BAR
{
// your code.
}
MetaQuotes Language 5 (MQL5) is a high-level language designed for developing technical indicators, trading robots and utility applications, which automate financial trading. MQL5 has been developed by MetaQuotes Software Corp. for their trading platform. The language syntax is very close to C++ enabling programmers to develop applications in...
Snelle Moda: This simple script detects the start of a new
BAR but it does only works if the price/volume has changed during the BAR period.
Simple?
For a new bar test, Bars is
unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate
prices and
The == operand. - MQL4 programming forum.) Always
use
time. I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple
times.
New
candle - MQL4 programming forum
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I believe the right code is:
if (iTime(NULL, 0, 0)
However as soon as I add this code, the compiler starts saying that there are { in the wrong place and always too many or too few. When I remove the code if (iTime(NULL, 0, 0) everything is fine again...
Any suggestions?
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
Hi
This simple script detects the start of a new BAR but it does only works if the price/volume has changed during the BAR period.
Maybe you can use it.
Awesome! It works for me like a charm!
Thanks for your input.
How can a newcomer learn to create an EA?
Hi @Olawale Daniel
Please see: https://www.mql5.com/en/docs
And you can also try codebase for example code: https://www.mql5.com/en/code
Hi
This simple script detects the start of a new BAR but it does only works if the price/volume has changed during the BAR period.
Maybe you can use it.
prev_calculated undeclared identified, where and how to define it
Simple? For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
New candle - MQL4 programming forum