GP - Long Short Scanner
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gokhanpirnal

//@version=5
indicator("GP - Long Short Scanner",shorttitle = 'GP - Long Short Scanner', overlay = true, max_labels_count=500)

long = input(title='TSI Long Length', defval=1)
short = input(title='TSI Short Length', defval=1)
signal = input(title='TSI Signal Length', defval=2)
len = input.int(2, minval=1, title='RSI Length')
priceopen = open
double_smoothopen(srcopen, long, short) =>
    fist_smoothopen = ta.ema(srcopen, long)
    ta.ema(fist_smoothopen, short)
pcopen = ta.change(priceopen)
double_smoothed_pcopen = double_smoothopen(pcopen, long, short)
double_smoothed_abs_pcopen = double_smoothopen(math.abs(pcopen), long, short)
tsi_valueopen = 100 * (double_smoothed_pcopen / double_smoothed_abs_pcopen)

srcopen = input(open, 'Source')
upopen = ta.rma(math.max(ta.change(srcopen), 0), len)
downopen = ta.rma(-math.min(ta.change(srcopen), 0), len)
rsiopen = downopen == 0 ? 100 : upopen == 0 ? 0 : 100 - 100 / (1 + upopen / downopen)

totalopen = tsi_valueopen + ta.ema(tsi_valueopen, signal) + rsiopen

pricehigh = high
double_smoothhigh(srchigh, long, short) =>
    fist_smoothhigh = ta.ema(srchigh, long)
    ta.ema(fist_smoothhigh, short)
pchigh = ta.change(pricehigh)
double_smoothed_pchigh = double_smoothhigh(pchigh, long, short)
double_smoothed_abs_pchigh = double_smoothhigh(math.abs(pchigh), long, short)
tsi_valuehigh = 100 * (double_smoothed_pchigh / double_smoothed_abs_pchigh)

srchigh = input(high, 'Source')
uphigh = ta.rma(math.max(ta.change(srchigh), 0), len)
downhigh = ta.rma(-math.min(ta.change(srchigh), 0), len)
rsihigh = downhigh == 0 ? 100 : uphigh == 0 ? 0 : 100 - 100 / (1 + uphigh / downhigh)

totalhigh = tsi_valuehigh + ta.ema(tsi_valuehigh, signal) + rsihigh

pricelow = low
double_smoothlow(srclow, long, short) =>
    fist_smoothlow = ta.ema(srclow, long)
    ta.ema(fist_smoothlow, short)
pclow = ta.change(pricelow)
double_smoothed_pclow = double_smoothlow(pclow, long, short)
double_smoothed_abs_pclow = double_smoothlow(math.abs(pclow), long, short)
tsi_valuelow = 100 * (double_smoothed_pclow / double_smoothed_abs_pclow)

srclow = input(low, 'Source')
uplow = ta.rma(math.max(ta.change(srclow), 0), len)
downlow = ta.rma(-math.min(ta.change(srclow), 0), len)
rsilow = downlow == 0 ? 100 : uplow == 0 ? 0 : 100 - 100 / (1 + uplow / downlow)

totallow = tsi_valuelow + ta.ema(tsi_valuelow, signal) + rsilow

priceclose = close
double_smoothclose(srcclose, long, short) =>
    fist_smoothclose = ta.ema(srcclose, long)
    ta.ema(fist_smoothclose, short)
pcclose = ta.change(priceclose)
double_smoothed_pcclose = double_smoothclose(pcclose, long, short)
double_smoothed_abs_pcclose = double_smoothclose(math.abs(pcclose), long, short)
tsi_valueclose = 100 * (double_smoothed_pcclose / double_smoothed_abs_pcclose)

srcclose = input(close, 'Source')
upclose = ta.rma(math.max(ta.change(srcclose), 0), len)
downclose = ta.rma(-math.min(ta.change(srcclose), 0), len)
rsiclose = downclose == 0 ? 100 : upclose == 0 ? 0 : 100 - 100 / (1 + upclose / downclose)

totalclose = (tsi_valueclose + ta.ema(tsi_valueclose, signal) + rsiclose)

//col1=(totalclose[0]>totalopen[0]?color.rgb(0,138,138,0):color.rgb(255,70,70,0))
// plotcandle(totalopen, totalhigh, totallow, totalclose, title='Title', color=col1,wickcolor=col1,bordercolor=col1)

a = array.from(totalopen, totalhigh, totallow, totalclose)
mindata = array.min(a, 0)
maksdata = array.max(a, 0)

totalmin=(mindata[0]+totalclose[0])
totalmaks=(maksdata[0]+totalclose[0])

longsart1=(totalmin[1]<=(-390)?1:0)
longsart2=(mindata[1]==totalclose[1]?1:0)
longsart3=(mindata[0]<totalclose[0]?1:0)

shortsart1=(totalmaks[1]>=587?1:0)
shortsart2=(maksdata[1]==totalclose[1]?1:0)
shortsart3=(maksdata[0]>totalclose[0]?1:0)

minplot=(longsart1+longsart2+longsart3)
maksplot=(shortsart1+shortsart2+shortsart3)

// plot(minplot[0],title ='Single Long Line',color=#00ff00 )
// plot(maksplot,title="Single Short Line",color=#00ff00,linewidth = 1)

EnterLong=(minplot[0]>2.9)
EnterShort=(maksplot[0]>2.9)

plotshape(EnterLong, title='Long Alarm', text='Long', style=shape.labelup, location=location.belowbar, color=color.rgb(0,255,0,0), size=size.tiny, textcolor=color.new(#000000, 0))
plotshape(EnterShort, title='Short Alarm', text='Short', style=shape.labeldown, location=location.abovebar, color=color.rgb(255,0,0,0), size=size.tiny, textcolor=color.new(#ffffff, 0))

alertcondition(EnterShort, title="SHORT Alarm", message="SHORT Alarm")
alertcondition(EnterLong , title="LONG Alarm" , message="LONG Alarm")
