//@version=5
indicator('RSI & PSAR', overlay=true)
//-- RSI
myPeriod = input(defval=40, title='Period', group = 'RSI')
myThresholdUp = input(defval=70, title='Upper Threshold', group = 'RSI')
myThresholdDn = input(defval=30, title='Lower Threshold', group = 'RSI')
src = input.source(close, title=«Source», group = 'RSI')
myAlgoFlipToggle = input(defval=false, title='Imverse Algorthim', group = 'RSI')
myLabelToggle = input(defval=true, title='Show Labels', group = 'RSI')
myRSI = ta.rsi(src, myPeriod)
//-- Variables
buy = myAlgoFlipToggle? ta.falling(myRSI, 1) and ta.cross(myRSI, myThresholdDn): ta.rising(myRSI, 1) and ta.cross(myRSI, myThresholdUp)
sell = myAlgoFlipToggle? ta.rising(myRSI, 1) and ta.cross(myRSI, myThresholdUp): ta.falling(myRSI, 1) and ta.cross(myRSI, myThresholdDn)
var myPosition = 0
myPosition := buy == 1? 0: sell == 1 or myPosition[1] == 1? 1: 0
var isLong = false
var isShort = false
buySignal = not isLong and myLabelToggle? buy and myPosition[1] == 1? low — 0.005: na: na // Buy only if we are not already long
sellSignal = not isShort and myLabelToggle? sell and myPosition[1] == 0? high + 0.005: na: na // Sell only if we are not already short
if buySignal
isLong := true
isShort := false
isShort
if sellSignal
isLong := false
isShort := true
isShort
//-- Plots
plotshape(series=buySignal, style=shape.labelup, location=location.belowbar, color=#2a2e39, size=size.normal, editable=true, text='Long', textcolor=color.new(color.white, 0), title='Long Label')
plotshape(series=sellSignal, style=shape.labeldown, location=location.abovebar, color=#2a2e39, size=size.normal, editable=true, text='Short', textcolor=color.new(color.white, 0), title='Short Label')
barcolor(myPosition == 1? color.red: color.white)
//-- Alerts
alertcondition(buySignal, title='Long', message='')
alertcondition(sellSignal, title='Short', message='')
changeCond = myPosition != myPosition[1]
alertcondition(changeCond, title=«Direction Change», message="")