0

I'm trying to code a lookback input to check if my RSI oversold condition happened anywhere within the last 4 candlesticks to trigger a buy signal.(And to have it adjustable would be ideal to retest strategies). I just don't know how to plug it into my "long" strategy ?. I keep getting "and" error? Can't seem to figure this one out and any help would be so awesome!

This worked actually... lookback = input(4, "lookback") long = (rsiOversold[lookback]) and open > close[1] and high1 - low1 >= 24 * syminfo.mintick

Big Thanks LoL,

Paul

//@version=4
strategy(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, overlay = false, process_orders_on_close = true)

//Get Values
len = input(10, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
overbought = input(title="OB",defval=70)
oversold = input(title="OS",defval=30)
lookback = input(4, "Lookback")


//RSI Value
rsiValue = rsi(src, len)
rsiOverbought = barssince(rsiValue > overbought) ==1
rsiOversold = barssince(rsiValue < oversold) ==1

long = rsiOversold, lookback and open > close[1] and high*1 - low*1  >= 24 * syminfo.mintick
exitLong = close < low[1]

short = rsiOverbought and open < close[1] and high*1 - low*1  >= 24 * syminfo.mintick
exitShort = high > high[1]

bgcolor(barssince(rsiValue > overbought) == 1 ? color.red : na)
bgcolor(barssince(rsiValue < oversold) == 1 ? color.green : na)

//Alerts
alertcondition(rsiOverbought, title = "Overbought", message = "{{close}}")
alertcondition(rsiOversold, title = "Oversold", message = "{{close}}")
    
// Plotted Values    
plot(rsi, "RSI", color=#7E57C2)
band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")

//Entry and Exit
strategy.entry("long", strategy.long, 1, when = long)

strategy.close("long", when = exitLong)

strategy.entry("short", strategy.short, 1, when = short)

strategy.close("short", when = exitShort) 
Paul Cas
  • 39
  • 7

1 Answers1

1
lookback = input(4, "Lookback")
rsiOverboughtWithLookback = barssince(rsiValue > overbought) < lookback
bgcolor(rsiOverboughtWithLookback ? color.lime : na)

The condition will remain true/ color the background lime, if your RSI oversold condition happened anywhere within the last 4 candlesticks.

enter image description here

bbnm
  • 1,034
  • 1
  • 4
  • 14
  • Hey thanks for answering this question. Is there a way to check if the candlestick size I'm looking for occurred after the RSI oversold signal was triggered. That's what I'm trying to get out of this code. Like as if to catch a missed opportunity after the RSI is on the move. I hope this explains more clearly what I'm going for. And thanks for the help again big time! I will be sure to check mark your answer. – Paul Cas Sep 03 '21 at 23:25
  • And this is another question I posted if you have time. https://stackoverflow.com/questions/69051347/how-to-enter-trade-by-crossover-with-a-specific-candle-height-or-tick-size-above. . Just wanted to say I love my RSI indicator now thanks to your help. – Paul Cas Sep 03 '21 at 23:32
  • You'll need to have the condition for checking the candlestick size first and add an RSI oversold condition with a lookback period if you desire. – bbnm Sep 04 '21 at 00:17
  • I will try that and see thanks. So it's the order in how you place the commands that makes the lookback function for a specific value. – Paul Cas Sep 04 '21 at 00:28