To implement the RSI2 strategy

 avatar
3169
plain_text
a year ago
1.6 kB
10
Indexable
//@version=5
strategy("RSI2 Strategy", overlay=true)

// Input parameters
sma200_length = input.int(200, title="SMA 200 Length")
sma5_length = input.int(5, title="SMA 5 Length")
rsi_length = input.int(2, title="RSI Length")
rsi_buy_threshold = input.float(5, title="RSI Buy Threshold", minval=0, maxval=100)
rsi_sell_threshold = input.float(95, title="RSI Sell Threshold", minval=0, maxval=100)

// Calculating indicators
sma200 = ta.sma(close, sma200_length)
sma5 = ta.sma(close, sma5_length)
rsi2 = ta.rsi(close, rsi_length)

// Plotting moving averages
plot(sma200, color=color.red, title="SMA 200")
plot(sma5, color=color.fuchsia, title="SMA 5")  // Use `color.fuchsia` instead of `color.pink`

// Generating signals
buy_signal = rsi2 < rsi_buy_threshold and close > sma200
sell_signal = rsi2 > rsi_sell_threshold and close < sma200

// Plotting signals on the chart
plotshape(series=buy_signal, location=location.belowbar, color=color.green, style=shape.labelup, text="Buy", title="Buy Signal")
plotshape(series=sell_signal, location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell", title="Sell Signal")

// Strategy execution
if (buy_signal)
    strategy.entry("Buy", strategy.long)
if (sell_signal)
    strategy.entry("Sell", strategy.short)

// Optional: Stop-loss mechanism based on SMA5
use_stop_loss = input.bool(true, title="Use Stop Loss?")
if (use_stop_loss)
    long_stop = close < sma5
    short_stop = close > sma5
    if (long_stop)
        strategy.close("Buy")
    if (short_stop)
        strategy.close("Sell")
Editor is loading...
Leave a Comment