PINE LIBRARY
Обновлено

MovingAverages

163
Library "MovingAverages"
A collection of O(1) numerically stable moving averages that support anchors and fractional lengths up to 100k bars.

Pine Script has a robust set of moving averages suitable for a majority of cases, making these alternatives useful only if you need anchoring, fractional lengths, or more than 5k bars. Included are the classic SMA, EMA, RMA, WMA, VWMA, VWAP, HMA, SWMA, Linear Regression, and ATR. The common parameters are:
  • source (float): Series of values to process.
  • length (simple float): Number of bars. Optional.
  • anchor (bool): The condition that triggers a calculation reset. Optional.
  • parity (simple bool): Sets if built-in function should be used. Optional.
Other DSP filter adaptations include One Euro, Laguerre, Super Smoother, and Holt, as well as rate limiting functions such as Smooth Damp and Slew Rate Limiter.

ANCHORING
This is the libraries first and primary benefit. Akin to the built-in VWAP, anchoring is managed by passing a series bool into the function. For sessional anchoring, the included new_session() returns true on the first bar of intraday sessions, and stabilize_anchor() helps reduce near-anchor volatility. When no length is provided, the series continues indefinitely until a new anchor is set. Values during the warmup period are returned.
Pine Script®
source = close length = 9.5 anchor = ma.new_session() // Assumes library is imported as "ma" swma = ma.swma(source, length, anchor).stabilize_anchor(source, length, anchor)

STREAMING UPDATES
Rather than naively using loops to recalculate the whole series on each bar, linear interpolation (aka. "lerping") is used to incrementally update and translate between values. The canonical formula being: a + (b - a) * t. This formula is effectively an EMA, but it's applicable to nearly all averaging equations. Coupling this technique with a circular buffer captures 3 of the 5 benefits this library offers: O(1) computation, fractional lengths, and 100k bars.


NUMERIC STABILITY
The last benefit is how the library minimizes floating point errors. When possible, Pine Script functions are used for mathematical parity. Otherwise Kahan summation error compensation is used when calculating an average. Not only does this keep custom implementations stable throughout the series, it also helps keep them within 1.0e-10 of the built-in functions. Automatically defaulting to the built-in functions can be disabled by setting parity to false.
Информация о релизе
v2
Removed thumbnail code form library.

Отказ от ответственности

Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.