static datetime preTime=0,curTime; bool newBar=preTime!=(curTime=iTime(Symbol(),Period(),0)); //--- if(newBar && HAGreenCandleCurrentPeriod(1)) { Alert("New Bar & Green Candle"); preTime=curTime; } //--- if(newBar && !HAGreenCandleCurrentPeriod(1)) { Alert("New Bar & Red Candle"); preTime=curTime; } //---
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.
New candle - MQL4 programming forum #3 2014.04.04I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum 2011.05.06Your code bool isNewBar = timeCur != timePre; if(isNewBar) { return true; // Once per bar } return false;
Simplified return timeCur != timePre;
- www.mql5.com
Hi I already use your suggestion Ernst and it works in green and red candle but instead working in every new bar it is working in every tick
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- static datetime preTime=iTime(Symbol(),Period(),0),curTime; bool newBar=preTime!=(curTime=iTime(Symbol(),Period(),0)); //--- if(newBar && HAGreenCandleCurrentPeriod(1)) { Alert("New Bar & Green Candle"); preTime=curTime; } //--- if(newBar && !HAGreenCandleCurrentPeriod(1)) { Alert("New Bar & Red Candle"); preTime=curTime; } //--- } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double HeikenAshiOpenCurrentPeriod(int shift = 0) { return iCustom(Symbol(),PERIOD_CURRENT,"Heiken Ashi","Red","Lime","Red","Lime",2,shift); } //--- double HeikenAshiCloseCurrentPeriod(int shift = 0) { return iCustom(Symbol(),PERIOD_CURRENT,"Heiken Ashi","Red","Lime","Red","Lime",3,shift); } //--- bool HAGreenCandleCurrentPeriod(int shift = 0) { return (HeikenAshiOpenCurrentPeriod(shift) < HeikenAshiCloseCurrentPeriod(shift)); } //+------------------------------------------------------------------+
Working. Please show your code.
I never knew that function can only be called once per tick, thank you for the knowledge.
Working. Please show your code.
static datetime preTime=iTime(Symbol(),Period(),0),curTime; bool newBar=preTime!=(curTime=iTime(Symbol(),Period(),0));
if(newBar && HAGreenCandleCurrentPeriod(1)) { Alert("New Bar & Green Candle"); preTime=curTime; } //--- if(newBar && !HAGreenCandleCurrentPeriod(1)) { Alert("New Bar & Red Candle"); preTime=curTime;
I never knew that function can only be called once per tick, thank you for the knowledge.
It's working know Ernst. I put the code above OnInit maybe that's why it doesn't work. Quick question, it seems that the code below doesn't works like I want. In the code I put the Heiken Ashi Candle shifted by 1, but every time the new bar coming the EA would return the candle color of the current candle. This code should be works when new bar is created then look for the finish bar color which is the previous candle. Is this happened because of the newBar variable?No, it happens because this:
bool HAGreenCandleCurrentPeriod(int shift = 0) { if(HeikenAshiOpenCurrentPeriod() < HeikenAshiCloseCurrentPeriod()) return true; return false; }
should be like this:
//--- bool HAGreenCandleCurrentPeriod(int shift = 0) { return (HeikenAshiOpenCurrentPeriod(shift) < HeikenAshiCloseCurrentPeriod(shift)); }
I never knew that function can only be called once per tick, thank you for the knowledge.
A function can be called multiple times in the same tick.
If a function such as IsNewBar() is called and returns true because it is the first tick of a new bar
if it is called again during the same tick it will return false.
So it can be called as many times as you like in a tick, but it can only ever return true the first time that it is called in the tick.
A function can be called multiple times in the same tick.
If a function such as IsNewBar() is called and returns true because it is the first tick of a new bar
if it is called again during the same tick it will return false.
So it can be called as many times as you like in a tick, but it can only ever return true the first time that it is called in the tick.
if(NewBar() && HAGreenCandleCurrentPeriod(1)) Alert("New Bar & Green Candle"); if(NewBar() && !HAGreenCandleCurrentPeriod(1)) Alert("New Bar & Red Candle");
hmm becoming a little bit confuse here. what do you mean by calling it again on the same tick? In my code, did this means calling it again?
- 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 tried to working on the code that is detecting new bar function. My code consist of static datetime in a NewBar() function and the problem of this is that if I use that function twice the second will never working. this is my code:
from that code I want to call the function in this code:
the second if will never be working because of the static datetime in the NewBar() Function. Is there a work around to deal with this?