Your topic has been moved to the section: Expert Advisors and Automated Trading
Please always use the CODE button (Alt-S) when inserting code.
Please review the following code example ...
Detecting the start of a new bar or candle
Fernando Carreiro, 2022.04.24 00:38
Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
datetime dt; int candleNro; int counter = 0; int OnInit() { dt = Time[0]; return(INIT_SUCCEEDED); } void OnTick() { if(Isnewbar()){ counter++; } } bool Isnewbar(){ if(Time[0] !=dt){ dt = Time[0]; candleNro++; return true; } return false; }
The code you provided seems to have a logical error in the OnInit() function. The candleNro variable is not initialized, and it may lead to unpredictable behavior in your program. Ensure you initialize candleNro properly in the OnInit() function:
int OnInit() { dt = Time[0]; candleNro = 0; // Initialize candleNro return INIT_SUCCEEDED; }
Additionally, the condition in the Isnewbar() function checks if Time[0] is not equal to dt , and if true, it increments candleNro . However, the candleNro variable is not used in the OnTick() function where you increment the counter . If you intend to use candleNro in your logic, make sure to include it appropriately.
Here's an updated version of your code:
datetime dt; int candleNro; int counter = 0; int OnInit() { dt = Time[0]; candleNro = 0; // Initialize candleNro return INIT_SUCCEEDED; } void OnTick() { if (Isnewbar()) { counter++; // You can use candleNro in your logic here if needed. } } bool Isnewbar() { if (Time[0] != dt) { dt = Time[0]; candleNro++; return true; } return false; }
Make sure you include the necessary logic involving candleNro in your OnTick() function or adjust the code based on your specific requirements.
I can go inside the if block even when the condition is met. The IsNewBar() function works properly but for some reason the condition in the if() block is never met. The var counter never increments. Any idea??
Improperly formatted code edited by moderator.
Give it a try, you'll like it.
input bool G_TickToFramesSwitch = true; input string G_TickToFramesSymbol = "EURUSD"; input ENUM_TIMEFRAMES G_TickToFramesFrames = PERIOD_M5; datetime G_OldFramesTime = D'1970.01.01 00:00:00'; void OnTick() { if (!TickToFrames(AutoSymbol(G_TickToFramesSymbol), G_TickToFramesFrames, G_OldFramesTime)) { return; } CallYouStrategy(); } bool TickToFrames(string P_Symbol, ENUM_TIMEFRAMES P_Frames, datetime &P_OldTime) { bool L_TimeLoop = P_OldTime == iTime(P_Symbol, P_Frames, 0); if(!L_TimeLoop || !G_TickToFramesSwitch) { P_OldTime = iTime(P_Symbol, P_Frames, 0); return (true); } else { return (false); } }
- 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 can go inside the if block even when the condition is met. The IsNewBar() function works properly but for some reason the condition in the if() block is never met. The var counter never increments. Any idea??