PINE LIBRARY
已更新 MovingAverages

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:
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®
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.
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.
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.
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.
發行說明
v2Removed thumbnail code form library.
Pine腳本庫
秉持TradingView一貫精神,作者已將此Pine代碼以開源函式庫形式發佈,方便我們社群中的其他Pine程式設計師重複使用。向作者致敬!您可以在私人專案或其他開源發表中使用此函式庫,但在公開發表中重用此代碼須遵守社群規範。
Discord: discord.gg/bPAPhwUeud
Website: liquid-trader.com
Website: liquid-trader.com
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。
Pine腳本庫
秉持TradingView一貫精神,作者已將此Pine代碼以開源函式庫形式發佈,方便我們社群中的其他Pine程式設計師重複使用。向作者致敬!您可以在私人專案或其他開源發表中使用此函式庫,但在公開發表中重用此代碼須遵守社群規範。
Discord: discord.gg/bPAPhwUeud
Website: liquid-trader.com
Website: liquid-trader.com
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。