Ls
2024-10-03

//@version=5
indicator(title='LONG SHORT ASR', overlay=true)
src = input(defval=close, title='Source')
per = input.int(defval=100, minval=1, title='Sampling Period')
mult = input.float(defval=3.0, minval=0.1, title='Range Multiplier')
smoothrng(x, t, m) =>
    wper = t * 2 - 1
    avrng = ta.ema(math.abs(x - x ), t)
    smoothrng = ta.ema(avrng, wper) * m
    smoothrng
smrng = smoothrng(src, per, mult)
rngfilt(x, r) =>
    rngfilt = x
    rngfilt := x > nz(rngfilt ) ? x - r < nz(rngfilt ) ? nz(rngfilt ) : x - r : x + r > nz(rngfilt ) ? nz(rngfilt ) : x + r
    rngfilt
filt = rngfilt(src, smrng)
upward = 0.0
upward := filt > filt  ? nz(upward ) + 1 : filt < filt  ? 0 : nz(upward )
downward = 0.0
downward := filt < filt  ? nz(downward ) + 1 : filt > filt  ? 0 : nz(downward )
hband = filt + smrng
lband = filt - smrng
filtcolor = upward > 0 ? color.lime : downward > 0 ? color.red : color.orange
barcolor = src > filt and src > src  and upward > 0 ? color.lime : src > filt and src < src  and upward > 0 ? color.green : src < filt and src < src  and downward > 0 ? color.red : src < filt and src > src  and downward > 0 ? color.maroon : color.orange
filtplot = plot(filt, color=filtcolor, linewidth=3, title='Range Filter')
hbandplot = plot(hband, color=color.new(color.aqua, 100), title='High Target')
lbandplot = plot(lband, color=color.new(color.fuchsia, 100), title='Low Target')
fill(hbandplot, filtplot, color=color.new(color.aqua, 90), title='High Target Range')
fill(lbandplot, filtplot, color=color.new(color.fuchsia, 90), title='Low Target Range')
barcolor(barcolor)
longCond = bool(na)
shortCond = bool(na)
longCond := src > filt and src > src  and upward > 0 or src > filt and src < src  and upward > 0
shortCond := src < filt and src < src  and downward > 0 or src < filt and src > src  and downward > 0
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni 
longCondition = longCond and CondIni  == -1
shortCondition = shortCond and CondIni  == 1
plotshape(longCondition, title='Buy Signal', text='LONG', textcolor=color.new(color.white, 0), style=shape.labelup, size=size.normal, location=location.belowbar, color=color.new(color.green, 0))
plotshape(shortCondition, title='Sell Signal', text='SHORT', textcolor=color.new(color.white, 0), style=shape.labeldown, size=size.normal, location=location.abovebar, color=color.new(color.red, 0))
alertcondition(longCondition, title='Buy Alert', message='BUY')
alertcondition(longCondition, title='Buy Alert', message='BUY')
alertcondition(longCondition, title='Buy Alert', message='BUY')
alertcondition(shortCondition, title='Sell Alert', message='SELL')
