Hi everyone. Why does the function IsNewbarOnTimeframe return true when the program is just loaded (at the middle of the current bar)? Why not wait until the current bar (first bar) ends before returning true just like the subsequent bars? How can one code that the function should be false until the first bar ends? See the the function below. For instance, it returns true when one recompiles the program.Thank you.
- Trailing candle stoploss
- Opening One trade at the same bar...
- is a new variable/constant required once it is used for a separate comparison ?
macpee:
Hi everyone. Why does the function IsNewbarOnTimeframe return true when the program is just loaded (at the middle of the current bar)? Why not wait until the current bar (first bar) ends before returning true just like the subsequent bars? How can one code that the function should be false until the first bar ends? See the the function below. For instance, it returns true when one recompiles the program.Thank you.
Hi everyone. Why does the function IsNewbarOnTimeframe return true when the program is just loaded (at the middle of the current bar)? Why not wait until the current bar (first bar) ends before returning true just like the subsequent bars? How can one code that the function should be false until the first bar ends? See the the function below. For instance, it returns true when one recompiles the program.Thank you.
static datetime TickTime = 0; if(TickTime==0) TickTime=iTime(Symbol(),timeframe,0);
You should also make sure that the time-frame is fully updated.
Thanks. I guess you mean the following:
bool IsNewBarOnTimeframe(int timeframe) { static datetime TickTime = 0; datetime BarOpenTime = iTime(Symbol(),timeframe,0); if(TickTime==0) { TickTime=iTime(Symbol(),timeframe,0); return true; } else { return false; } }
Or rather
bool IsNewBarOnTimeframe(int timeframe) { static datetime TickTime = 0; if(TickTime==0) { TickTime=iTime(Symbol(),timeframe,0); return true; } else { return false; } }
I am trying this on M1 chart but it is not printing anything.
void OnTick() { if (IsNewBarOnTimeframe(Period()) == true) { Print ("This is a new bar"); } } bool IsNewBarOnTimeframe(int timeframe) { static datetime TickTime = 0; if(TickTime==0) { TickTime=iTime(Symbol(),timeframe,0); return true; } else { return false; } }
This...
void OnTick() { if (IsNewBarOnTimeframe(Period()) == true) { Print ("This is a new bar"); } } bool IsNewBarOnTimeframe(int timeframe) { static datetime TickTime = 0; datetime BarOpenTime = iTime(Symbol(),timeframe,0); if (TickTime == 0) { return false; } else { TickTime = BarOpenTime; return true; } }
I think this one is doing fine
bool IsNewBarOnTimeframe(int timeframe) { static datetime TickTime = 0; datetime BarOpenTime = iTime(Symbol(),timeframe,0); if (TickTime != BarOpenTime) { if (TickTime != 0) { TickTime = BarOpenTime; return true; } else { TickTime = BarOpenTime; return false; } } else { return false; } }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register