OPEN-SOURCE SCRIPT
Actualizado

TrigWave Suite [InvestorUnknown]

7 376
The TrigWave Suite combines Sine-weighted, Cosine-weighted, and Hyperbolic Tangent moving averages (HTMA) with a Directional Movement System (DMS) and a Relative Strength System (RSS).

Hyperbolic Tangent Moving Average (HTMA)
  • The HTMA smooths the price by applying a hyperbolic tangent transformation to the difference between the price and a simple moving average. It also adjusts this value by multiplying it by a standard deviation to create a more stable signal.


// Function to calculate Hyperbolic Tangent
tanh(x) =>
    e_x                 = math.exp(x)
    e_neg_x             = math.exp(-x)
    (e_x - e_neg_x) / (e_x + e_neg_x)


// Function to calculate Hyperbolic Tangent Moving Average
htma(src, len, mul) =>
    tanh_src            = tanh((src - ta.sma(src, len)) * mul) * ta.stdev(src, len) + ta.sma(src, len)
    htma                = ta.sma(tanh_src, len)


Sine-Weighted Moving Average (SWMA)
  • The SWMA applies sine-based weights to historical prices. This gives more weight to the central data points, making it responsive yet less prone to noise.


// 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) * src
    swma


Cosine-Weighted Moving Average (CWMA)
  • The CWMA uses cosine-based weights for data points, which produces a more stable trend-following behavior, especially in low-volatility markets.


f_Cosine_Weighted_MA(series float src, simple int length) =>
    var float[] cosine_weights = array.new_float(0)
    array.clear(cosine_weights)  // Clear the array before recalculating weights
    for i = 0 to length - 1
        weight = math.cos((math.pi * (i + 1)) / length) + 1  // Shift by adding 1
        array.push(cosine_weights, weight)


    // Normalize the weights
    sum_weights = array.sum(cosine_weights)
    for i = 0 to length - 1
        norm_weight = array.get(cosine_weights, i) / sum_weights
        array.set(cosine_weights, i, norm_weight)


    // Calculate Cosine-Weighted Moving Average
    cwma = 0.0
    if bar_index >= length
        for i = 0 to length - 1
            cwma := cwma + array.get(cosine_weights, i) * src
    cwma


Directional Movement System (DMS)
  • DMS is used to identify trend direction and strength based on directional movement. It uses ADX to gauge trend strength and combines +DI and -DI for directional bias.


// Function to calculate Directional Movement System
f_DMS(simple int dmi_len, simple int adx_len) =>
    up = ta.change(high)
    down = -ta.change(low)
    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    trur = ta.rma(ta.tr, dmi_len)
    plus = fixnan(100 * ta.rma(plusDM, dmi_len) / trur)
    minus = fixnan(100 * ta.rma(minusDM, dmi_len) / trur)
    sum = plus + minus
    adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adx_len)


    dms_up = plus > minus and adx > minus
    dms_down = plus < minus and adx > plus
    dms_neutral = not (dms_up or dms_down)


    signal = dms_up ? 1 : dms_down ? -1 : 0


Relative Strength System (RSS)
  • RSS employs RSI and an adjustable moving average type (SMA, EMA, or HMA) to evaluate whether the market is in a bullish or bearish state.


// Function to calculate Relative Strength System
f_RSS(rsi_src, rsi_len, ma_type, ma_len) =>
    rsi = ta.rsi(rsi_src, rsi_len)


    ma = switch ma_type
        "SMA" => ta.sma(rsi, ma_len)
        "EMA" => ta.ema(rsi, ma_len)
        "HMA" => ta.hma(rsi, ma_len)


    signal = (rsi > ma and rsi > 50) ? 1 : (rsi < ma and rsi < 50) ? -1 : 0


ATR Adjustments
  • To minimize false signals, the HTMA, SWMA, and CWMA signals are adjusted with an Average True Range (ATR) filter:


// Calculate ATR adjusted components for HTMA, CWMA and SWMA
float atr                           = ta.atr(atr_len)


float htma_up                       = htma + (atr * atr_mult)
float htma_dn                       = htma - (atr * atr_mult)


float swma_up                       = swma + (atr * atr_mult)
float swma_dn                       = swma - (atr * atr_mult)


float cwma_up                       = cwma + (atr * atr_mult)
float cwma_dn                       = cwma - (atr * atr_mult)


  • This adjustment allows for better adaptation to varying market volatility, making the signal more reliable.


Signals and Trend Calculation
  • The indicator generates a Trend Signal by aggregating the output from each component. Each component provides a directional signal that is combined to form a unified trend reading. The trend value is then converted into a long (1), short (-1), or neutral (0) state.


Backtesting Mode and Performance Metrics
  • The Backtesting Mode includes a performance metrics table that compares the Buy and Hold strategy with the TrigWave Suite strategy. Key statistics like Sharpe Ratio, Sortino Ratio, and Omega Ratio are displayed to help users assess performance. Note that due to labels and plotchar use, automatic scaling may not function ideally in backtest mode.


Alerts and Visualization
  • Trend Direction Alerts: Set up alerts for long and short signals
  • Color Bars and Gradient Option: Bars are colored based on the trend direction, with an optional gradient for smoother visual feedback.


Important Notes
  • Customization: Default settings are experimental and not intended for trading/investing purposes. Users are encouraged to adjust and calibrate the settings to optimize results according to their trading style.
  • Backtest Results Disclaimer: Please note that backtest results are not indicative of future performance, and no strategy guarantees success.
Notas de prensa
Updated the code to pinescript v6, added backtesting library v2 with more backtesting functions and removed old backtesting functions from the code
Notas de prensa
Updated the code to pinescript v6, added backtesting library v2 with more backtesting functions and removed old backtesting functions from the code

Exención de responsabilidad

La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.