Skip to content
All library documents

Calculating Rolling ATR Averages with Data-Quality Safeguards

Article Strategy library · Author: 作手君TradeMan

Summary

This short programming example illustrates how to derive a rolling average series from an indicator output, using ATR as its motivating example. It retrieves recent market records, computes an indicator, retains a trailing portion of the data, and averages successive windows of configurable length. The loop updates calculations when a new bar arrives and logs the latest average.

The example also demonstrates two defensive ideas: allowing for insufficient indicator history by excluding an initial segment, and replacing nonpositive or otherwise invalid values with zero before averaging. It offers no comparison with a standard ATR moving average, no validation results, and no discussion of how zero substitution may bias the calculated series. The snippet’s indicator call and comments do not fully specify whether the returned data is ATR in the intended form, so users would need to confirm the platform API and validate results before relying on it in a trading system.

Key ideas

  • The example calculates a configurable rolling mean over indicator values.
  • It refreshes the calculation when the latest record time advances.
  • An initial portion of the series is excluded to account for insufficient history.
  • Nonpositive or invalid values are replaced with zero, which can affect the average.
  • The snippet provides no validation of the indicator call or computed results.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.