Pine Script

Momentum Trading Pine Script
This Pine Script strategy identifies momentum using the VIDYA and RSI indicators. It includes a configurable trailing stop loss to protect profits. Copy and paste this code into your TradingView Pine Editor, add it to your chart, and then create an alert for it using the settings from the "Settings" page.

//@version=5
strategy("Momentum VIDYA TSL Strategy", overlay=true, default_qty_value=10)

// VIDYA calculation
vidya(source, length, rvi_length) =>
    alpha = 2 / (length + 1)
    k = ta.stdev(ta.change(source), rvi_length) > 0 ? ta.stdev(ta.change(source), rvi_length) / ta.highest(ta.stdev(ta.change(source), rvi_length), rvi_length) : 0
    vidya_val = 0.0
    vidya_val := source * alpha * k + nz(vidya_val[1], source) * (1 - alpha * k)
    vidya_val

// --- Inputs ---
vidyaLength = input.int(14, "VIDYA Length")
rsiLength = input.int(14, "RSI Length")
rsiBuyThreshold = input.int(60, "RSI Buy Threshold")
rsiSellThreshold = input.int(40, "RSI Sell Threshold")

tslActivatePercentage = input.float(2.0, "Trailing Stop Activation Profit %", step = 0.5)
tslPercentage = input.float(5.0, "Trailing Stop Loss %", step = 0.5)


// --- Indicators ---
vidyaValue = vidya(close, vidyaLength, vidyaLength)
rsiValue = ta.rsi(close, rsiLength)

// --- Strategy Conditions ---
isBullishMomentum = rsiValue > rsiBuyThreshold
isLosingMomentum = rsiValue < rsiSellThreshold

longCondition = ta.crossover(close, vidyaValue) and isBullishMomentum and strategy.position_size == 0
exitOnMomentumLoss = ta.crossunder(close, vidyaValue) or isLosingMomentum

// --- Trailing Stop Loss Logic ---
var float tsl_price = na

// Activate TSL when in profit
if (strategy.position_size > 0)
    // Check if profit target for activation is met
    profitTargetMet = (high / strategy.position_avg_price - 1) * 100 >= tslActivatePercentage
    
    // Set initial TSL price once profit target is met
    if (profitTargetMet and na(tsl_price))
        tsl_price := high * (1 - tslPercentage / 100)
    
    // If TSL is active, update it with the highest high
    if not na(tsl_price)
        new_tsl_price = high * (1 - tslPercentage / 100)
        tsl_price := math.max(tsl_price, new_tsl_price, na.val)
else
    // Reset TSL price when not in a position
    tsl_price := na

// --- Strategy Execution ---
// Entry
if (longCondition)
    strategy.entry("Buy", strategy.long, alert_message = "BUY {{ticker}} QTY {{strategy.order.contracts}} @ {{close}}")

// Exit
if strategy.position_size > 0
    // Exit based on momentum loss
    strategy.close("Buy", when=exitOnMomentumLoss, comment="Exit on Momentum Loss", alert_message="SELL {{ticker}} QTY {{strategy.order.contracts}} @ {{close}}")
    // Exit based on Trailing Stop Loss
    if not na(tsl_price)
        strategy.exit("TSL", from_entry="Buy", stop=tsl_price, alert_message="SELL {{ticker}} QTY {{strategy.order.contracts}} @ {{close}}")

// --- Plotting ---
plot(vidyaValue, "VIDYA", color=color.blue)
plot(tsl_price, "TSL", color=color.orange, style=plot.style_linebr, linewidth=2)
hline(rsiBuyThreshold, "RSI Buy Level", color=color.new(color.green, 50))
hline(rsiSellThreshold, "RSI Sell Level", color=color.new(color.red, 50))