OPEN-SOURCE SCRIPT
已更新

Sine-Weighted MA ATR [InvestorUnknown]

9 436
The Sine-Weighted MA ATR is a technical analysis tool designed to emphasize recent price data using sine-weighted calculations, making it particularly well-suited for analyzing cyclical markets with repetitive patterns. The indicator combines the Sine-Weighted Moving Average (SWMA) and a Sine-Weighted Average True Range (SWATR) to enhance price trend detection and volatility analysis.

Sine-Weighted Moving Average (SWMA):
  • Unlike traditional moving averages that apply uniform or exponentially decaying weights, the SWMA applies Sine weights to the price data.
  • Emphasis on central data points: The Sine function assigns more weight to the middle of the lookback period, giving less importance to the beginning and end points. This helps capture the main trend more effectively while reducing noise from recent volatility or older data.

Pine Script®
// Function to calculate the Sine-Weighted Moving Average f_Sine_Weighted_MA(series float src, simple int length) => var float[] sine_weights = array.new_float(0) array.clear(sine_weights) // Clear the array before recalculating weights for i = 0 to length - 1 weight = math.sin((math.pi * (i + 1)) / length) array.push(sine_weights, weight) // Normalize the weights sum_weights = array.sum(sine_weights) for i = 0 to length - 1 norm_weight = array.get(sine_weights, i) / sum_weights array.set(sine_weights, i, norm_weight) // Calculate Sine-Weighted Moving Average swma = 0.0 if bar_index >= length for i = 0 to length - 1 swma := swma + array.get(sine_weights, i) * close swma

快照

Sine-Weighted ATR:
  • This is a variation of the Average True Range (ATR), which measures market volatility. Like the SWMA, the ATR is smoothed using Sine-based weighting, where central values are more heavily considered compared to the extremities. This improves sensitivity to changes in volatility while maintaining stability in highly volatile markets.


Pine Script®
// Function to calculate the Sine-Weighted ATR f_Sine_Weighted_ATR(simple int length) => var float[] sine_weights_atr = array.new_float(0) array.clear(sine_weights_atr) for i = 0 to length - 1 weight = math.sin((math.pi * (i + 1)) / length) array.push(sine_weights_atr, weight) // Normalize the weights sum_weights_atr = array.sum(sine_weights_atr) for i = 0 to length - 1 norm_weight_atr = array.get(sine_weights_atr, i) / sum_weights_atr array.set(sine_weights_atr, i, norm_weight_atr) // Calculate Sine-Weighted ATR using true ranges swatr = 0.0 tr = ta.tr(true) // True Range if bar_index >= length for i = 0 to length - 1 swatr := swatr + array.get(sine_weights_atr, i) * tr swatr


ATR Bands:
  • Upper and lower bands are created by adding/subtracting the Sine-Weighted ATR from the SWMA. These bands help identify overbought or oversold conditions, and when the price crosses these levels, it may generate long or short trade signals.


Pine Script®
// - - - - - CALCULATIONS - - - - - //{ bar b = bar.new() float src = b.calc_src(swma_src) float swma = f_Sine_Weighted_MA(src, ma_length) // Use normal ATR or Sine-Weighted ATR based on input float atr = atr_type == "Normal ATR" ? ta.atr(atr_len) : f_Sine_Weighted_ATR(atr_len) // Calculate upper and lower bands using ATR float swma_up = swma + (atr * atr_mult) float swma_dn = swma - (atr * atr_mult) float src_l = b.calc_src(src_long) float src_s = b.calc_src(src_short) // Signal logic for crossovers and crossunders var int signal = 0 if ta.crossover(src_l, swma_up) signal := 1 if ta.crossunder(src_s, swma_dn) signal := -1 //}


Signal Logic:
  • Long/Short Signals are triggered when the price crosses above or below the Sine-Weighted ATR bands


Backtest Mode and Equity Calculation
To evaluate its effectiveness, the indicator includes a backtest mode, allowing users to test its performance on historical data:
  • Backtest Equity: A detailed equity curve is calculated based on the generated signals over a user-defined period (startDate to endDate).
  • Buy and Hold Comparison: Alongside the strategy’s equity, a Buy-and-Hold equity curve is plotted for performance comparison.


Alerts
The indicator includes built-in alerts for both long and short signals, ensuring users are promptly notified when market conditions meet the criteria for an entry or exit.
發行說明
Added option to use Custom Timeframes on current chart for SWMA and ATR.

Pine Script®
simple bool custom_tf = input.bool(false, "Custom Timeframes", group = G2) simple string swma_tf = input.timeframe("", "SWMA Timeframe", group = G2) simple string atr_tf = input.timeframe("", "ATR Timeframe", group = G2) float swma = custom_tf ? request.security("", swma_tf, f_Sine_Weighted_MA(src, ma_length)) : f_Sine_Weighted_MA(src, ma_length) // Use normal ATR or Sine-Weighted ATR based on input float atr = custom_tf ? request.security("", atr_tf,(atr_type == "Normal ATR" ? ta.atr(atr_len) : f_Sine_Weighted_ATR(atr_len))) : (atr_type == "Normal ATR" ? ta.atr(atr_len) : f_Sine_Weighted_ATR(atr_len))
發行說明
Fixed issue with custom SWMA source not being passed correctly into the calculation.
發行說明
Updated the code to pinescript v6, added backtesting library v2 with more backtesting functions and removed old backtesting functions from the code

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。