Preventing Repeated Indicator Recalculation on the Current Bar
Summary
The article addresses a MetaTrader 4 problem in indicators whose calculations depend recursively on values from earlier bars. Recalculating only the newest bar can fail because the current bar changes on each tick and recursive state may no longer match the state expected by the calculation. Recalculating every bar avoids that inconsistency but can waste substantial computing resources.
Using a multi-stage moving average as an example, the article demonstrates storing recursive intermediate values in indicator buffers so that the necessary history can be updated incrementally. It then describes a more general approach: retain the internal state needed to restart calculations, restore it before processing uncounted bars, and save the relevant state at chosen loop positions. It cautions that state and bar indices must be handled carefully when the chart changes, and recommends preserving bar time when stored indices may shift. This is a programming method for improving indicator correctness and efficiency, not a trading signal; the approach requires identifying every persistent variable and adapting the restoration logic to each indicator’s calculation structure.
Key ideas
- Indicators with recursive calculations rely on intermediate values from earlier bars, so recalculating only the newest bar can produce incorrect results.
- Recalculating all bars on every update may correct the state but consume excessive resources.
- Storing recursive intermediate values in indicator buffers supports incremental calculation of the necessary bars.
- A general alternative is to save and restore calculation state around the update loop, including bar timing where indices can change.
- The technique must be tailored to the indicator’s variables and loops and does not provide a trading strategy by itself.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.