Search code examples
pine-scriptpine-script-v5

Pine script with three conditions not executing any thrades


I have the following script which should do the following:
Condition 1 = during specific trading hours
Condition 2 = MA cross
Condition 3 = if within the next x candles after Condition 2 = true, RSI crosses given band (and only then), execute trade. If RSI crosses after x candles, signal is invalid. If RSI crossed before Condition 2, signal is invalid.

Somehow, I can't bring all conditions to work together. Either it's executing without waiting for condition 3 (right after Condition 2, or as for the below code, it is not executing trades at all.

Thanks to anyone who can help. I have tried finding an answer for days...

//@version=5
    strategy("MS + RSI cross", overlay=true)

// User Inputs
// Group SMA
sma_group = "SMA"
sma1_length = input.int(10, title="SMA 1 Length", group=sma_group)
sma2_length = input.int(35, title="SMA 2 Length", group=sma_group)

// Group RSI
rsi_group = "RSI"
rsi_length = input.int(8, title="RSI Length", group=rsi_group)
rsi_source = input.source(close, title="RSI Source", group=rsi_group)
rsi_upper_band = input.float(55, title="RSI Upper Band", group=rsi_group)
rsi_lower_band = input.float(45, title="RSI Lower Band", group=rsi_group)
rsi_confirmation_window = input.int(3, title="RSI Confirmation Window", group=rsi_group)

// Group Trading Hours
trading_hours_group = "TRADING TIME"
start_trading_hour = input.int(9, title="Start Trading Hour", group=trading_hours_group)
stop_trading_hour = input.int(21, title="Stop Trading Hour", group=trading_hours_group)

// Group SL/TP
sl_tp_group = "SL & TP"
sl_points = input.int(300, title="SL (points)", group=sl_tp_group)
tp_points = input.int(450, title="TP (points)", group=sl_tp_group)

// Calculating moving averages
sma1 = ta.sma(close, sma1_length)
sma2 = ta.sma(close, sma2_length)

// Calculating RSI
rsi_value = ta.rsi(rsi_source, rsi_length)

// Trading Conditions
in_trading_hours = hour >= start_trading_hour and hour <= stop_trading_hour

// Long conditions
long_sma_cross = ta.crossover(sma1, sma2) and in_trading_hours
long_rsi_cross = false
bars_since_long_sma_cross = ta.barssince(long_sma_cross)
if bars_since_long_sma_cross > 0 and bars_since_long_sma_cross <= rsi_confirmation_window
long_rsi_cross := ta.crossover(rsi_value, rsi_upper_band)

// Visualize the rsi cross for debugging
bgcolor(ta.crossover(rsi_value, rsi_upper_band) ? color.new(#37b027, 81): na)

// Short conditions
short_sma_cross = ta.crossunder(sma1, sma2) and in_trading_hours
short_rsi_cross = false
bars_since_short_sma_cross = ta.barssince(short_sma_cross)
if bars_since_short_sma_cross > 0 and bars_since_short_sma_cross <= rsi_confirmation_window
    short_rsi_cross := ta.crossunder(rsi_value, rsi_lower_band)

// Visualize the rsi cross for debugging    
bgcolor(ta.crossunder(rsi_value, rsi_lower_band) ? color.new(#c74949, 76): na)

// Entry and Exit Logic
if (long_sma_cross and long_rsi_cross)
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit Long", "Long", stop=close - sl_points  * syminfo.mintick, limit=close + tp_points * syminfo.mintick)

if (short_sma_cross and short_rsi_cross)
    strategy.entry("Short", strategy.short)
    strategy.exit("Exit Short", "Short", stop=close + sl_points * syminfo.mintick, limit=close - tp_points * syminfo.mintick)

// Plotting
plot(sma1, color=color.rgb(243, 135, 33), title="SMA 1")
plot(sma2, color=color.rgb(235, 213, 213), title="SMA 2")
plotshape(long_sma_cross and long_rsi_cross, style=shape.labelup, location=location.belowbar, color=color.green, size=size.small, title="Long Signal")
plotshape(short_sma_cross and short_rsi_cross, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.small, title="Short Signal")

Solution

  • You have a errors in your conditions. You want to run the rsi crossover check on each bar, and then use a boolean to mark it as true when you're other 2 conditions are met. This will work

    //@version=5
    strategy("MS + RSI cross", overlay=true, process_orders_on_close = true)
    
    // User Inputs
    // Group SMA
    sma_group = "SMA"
    sma1_length = input.int(10, title="SMA 1 Length", group=sma_group)
    sma2_length = input.int(35, title="SMA 2 Length", group=sma_group)
    
    // Group RSI
    rsi_group = "RSI"
    rsi_length = input.int(8, title="RSI Length", group=rsi_group)
    rsi_source = input.source(close, title="RSI Source", group=rsi_group)
    rsi_upper_band = input.float(55, title="RSI Upper Band", group=rsi_group)
    rsi_lower_band = input.float(45, title="RSI Lower Band", group=rsi_group)
    rsi_confirmation_window = input.int(3, title="RSI Confirmation Window", group=rsi_group)
    
    // Group Trading Hours
    trading_hours_group = "TRADING TIME"
    start_trading_hour = input.int(9, title="Start Trading Hour", group=trading_hours_group)
    stop_trading_hour = input.int(21, title="Stop Trading Hour", group=trading_hours_group)
    
    // Group SL/TP
    sl_tp_group = "SL & TP"
    sl_points = input.int(300, title="SL (points)", group=sl_tp_group)
    tp_points = input.int(450, title="TP (points)", group=sl_tp_group)
    
    // Calculating moving averages
    sma1 = ta.sma(close, sma1_length)
    sma2 = ta.sma(close, sma2_length)
    
    // Calculating RSI
    rsi_value = ta.rsi(rsi_source, rsi_length)
    
    // Trading Conditions
    in_trading_hours = hour >= start_trading_hour and hour <= stop_trading_hour
    
    // Long conditions
    long_sma_cross = ta.crossover(sma1, sma2) and in_trading_hours
    bars_since_long_sma_cross = ta.barssince(long_sma_cross)
    long_rsi_cross = ta.crossover(rsi_value, rsi_upper_band)
    gol =false
    
    if bars_since_long_sma_cross > 0 and bars_since_long_sma_cross <= rsi_confirmation_window
        gol := true
    
    // Visualize the rsi cross for debugging
    bgcolor(ta.crossover(rsi_value, rsi_upper_band) ? color.new(#37b027, 81): na)
    
    // Short conditions
    short_sma_cross = ta.crossunder(sma1, sma2) and in_trading_hours
    goS = false
    bars_since_short_sma_cross = ta.barssince(short_sma_cross)
    short_rsi_cross = ta.crossunder(rsi_value, rsi_lower_band)
    
    if bars_since_short_sma_cross > 0 and bars_since_short_sma_cross <= rsi_confirmation_window
        goS := true
    
    // Visualize the rsi cross for debugging    
    bgcolor(ta.crossunder(rsi_value, rsi_lower_band) ? color.new(#c74949, 76): na)
    
    // Entry and Exit Logic
    if (long_sma_cross and long_rsi_cross)
        strategy.entry("Long", strategy.long)
        strategy.exit("Exit Long", "Long", stop=close - sl_points  * syminfo.mintick, limit=close + tp_points * syminfo.mintick)
    
    if (short_sma_cross and short_rsi_cross)
        strategy.entry("Short", strategy.short)
        strategy.exit("Exit Short", "Short", stop=close + sl_points * syminfo.mintick, limit=close - tp_points * syminfo.mintick)
    
    // Plotting
    plot(sma1, color=color.rgb(243, 135, 33), title="SMA 1")
    plot(sma2, color=color.rgb(235, 213, 213), title="SMA 2")
    plotshape(long_sma_cross and long_rsi_cross, style=shape.labelup, location=location.belowbar, color=color.green, size=size.small, title="Long Signal")
    plotshape(short_sma_cross and short_rsi_cross, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.small, title="Short Signal")