SST-V2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © toyikata04

//@version=4


study(title="SST-V2", overlay = true)

a = input(2,     title = "sensitivity")
c = input(1,    title = "ATR Period")


h = input(false, title = "Base On Heikin Ashi Candles")

xATR  = atr(c)
nLoss = a * xATR

src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = false) : close


//INPUT

xATRTS = 0.0

xATRTS:= iff(src > nz(xATRTS[1], 0) and src[1] > nz(xATRTS[1], 0), max(nz(xATRTS[1]), src - nLoss),
   iff(src < nz(xATRTS[1], 0) and src[1] < nz(xATRTS[1], 0), min(nz(xATRTS[1]), src + nLoss), 
   iff(src > nz(xATRTS[1], 0), src - nLoss, src + nLoss)))
 
pos = 0   
pos :=	iff(src[1] < nz(xATRTS[1], 0) and src > nz(xATRTS[1], 0), 1,
   iff(src[1] > nz(xATRTS[1], 0) and src < nz(xATRTS[1], 0), -1, nz(pos[1], 0))) 
   
xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue 

ema   = ema(src,1)
above = crossover(ema, xATRTS)
below = crossover(xATRTS, ema)

BL  = src > xATRTS and above 
SS = src < xATRTS and below

barBL  = src > xATRTS 
barSS = src < xATRTS 

plotshape(BL,  title = "LONG",  text = 'LONG',  style = shape.labelup,   location = location.belowbar, color= color.blue, textcolor = color.white, transp = 0, size = size.tiny)
plotshape(SS, title = "SHORT", text = 'SHORT', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, transp = 0, size = size.tiny)

barcolor(barBL  ? color.rgb(255, 255, 255) : na)
barcolor(barSS ? color.rgb(3, 3, 3)   : na)


//EMA
EMA1 = input(200, minval=1)
EMA2 = input(34, minval=1)

xPrice = close
xEMA2 = ema(xPrice, EMA1)
xEMA1 = ema(xPrice, EMA2)

plot(xEMA2, color=#9c27b0, title="EMA")
plot(xEMA1, color=#250dff, title="EMA")


