I am using this custom MQL5 Multi-Timeframe indicator and i get different results each time i compile the code. For example look at the two photos and you will spot the difference in bars. Why does this happen and how can i fix this ?
The indicator's .mq5 file is attached
Synchronizing several same-symbol charts on different timeframes
Dmitriy Gizlyk, 2018.06.06 13:30
When making trading decisions, we often have to analyze charts on several timeframes. At the same time, these charts often contain graphical objects. Applying the same objects to all charts is inconvenient. In this article, I propose to automate cloning of objects to be displayed on charts.In multi-timeframe indicators, it's not reliable to use "prev_calculated - 1" for start index in the calculation loop because "prev_calculated" refers to the processed data on the current timeframe. Some data can be missing if you're not starting from an index far back enough to accommodate the data of the higher timeframe, and it causes signal distortions.
It should be something like "prev_calculated - 1 - offset_bars" if you want to use this prev_calculated variable.
A daring optimization would look like this for a MTF indicator:
input ENUM_TIMEFRAMES Ext_TF = PERIOD_H2; //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { int offset_bars = GetRequiredBars(_Period, Ext_TF); int start = (prev_calculated == 0) ? 0 : prev_calculated - offset_bars - 1; for(int i = start; i<rates_total; i++) { // calculate indicator } return(rates_total); } int GetRequiredBars(ENUM_TIMEFRAMES current_tf, ENUM_TIMEFRAMES higher_tf) { int current_TF_seconds = PeriodSeconds(current_tf); int higher_TF_seconds = PeriodSeconds(higher_tf); int bars_per_htf_candle = higher_TF_seconds / current_TF_seconds; return bars_per_htf_candle; }
- 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 am using this custom MQL5 Multi-Timeframe indicator and i get different results each time i compile the code. For example look at the two photos and you will spot the difference in bars. Why does this happen and how can i fix this ?
The indicator's .mq5 file is attached