//@version=4
study("TN Alerts v13", overlay=true)
// This indicator is designed to run with period setting "week"
// Settings Inputs Section
tsPercent= input(defval = 20, title = "Trailing Stop")/100
wma4_sw = input(true, title="Display wma4")
wma62_sw = input(true, title="Display wma62")
TSswitch = input(true, title="Display Stoploss")
BGswitch = input(true, title="Display BG Color")
REswitch = input(true, title="Display Re-entry?")
// Calculation Section
enterlong = (wma(close,62)>wma(close,62)[1]) and (close>wma(close,4)) and (close[1]>wma(close,4)[1]) and (close>close[1])
entershort = (wma(close,62)<wma(close,62)[1]) and (close<wma(close,4)) and (close[1]<wma(close,4)[1]) and (close<close[1])
trail_top = float(na)
trail_top := (close>trail_top[1]) or (close<trail_top[1]*(1-tsPercent)) or (entershort)?close:na(trail_top[1])?close:trail_top[1]
plot(trail_top)
trail_bot = float(na)
trail_bot := (close<trail_bot[1]) or (close>trail_bot[1]*(1+tsPercent)) or (enterlong)?close:na(trail_bot[1])?close:trail_bot[1]
exitlong = trail_top<trail_top[1]
exitshort = trail_bot>trail_bot[1]
inlong = bool(na)
inlong := enterlong?true:(entershort or exitlong)?false:inlong[1]
inshort = bool(na)
inshort := entershort?true:(enterlong or exitshort)?false:inshort[1]
// Output section
plot(wma4_sw ? wma(close,4) : na, title="WMA 4", color=color.blue, linewidth=2)
plot(wma62_sw ? wma(close,62) : na, title="WMA 62", color=color.purple, linewidth=2)
plot(inlong and TSswitch?trail_top * (1-tsPercent):na, title="Trading Stop Long", linewidth=4, style=plot.style_circles,color=color.red)
plot(inshort and TSswitch?trail_bot * (1+tsPercent):na, title="Trading Stop Short", linewidth=4, style=plot.style_circles,color=color.green)
bgcolor(BGswitch and inlong ? color.new(color.green, 85) : na, title="Background Long Period")
bgcolor(BGswitch and inshort ? color.new(color.red, 85) : na, title="Background Short Period")
plotshape(enterlong and not inlong[1], title="Buy Signal", color=color.green, style=shape.arrowup, text="Buy", location=location.belowbar, offset=0, size=size.small)
plotshape(entershort and not inshort[1], title="Sell Signal", color=color.red, style=shape.arrowdown, text="Sell", location=location.abovebar, offset=0, size=size.small)
plotshape(exitlong and inlong[1] and not entershort, title="Stoploss Long Signal", color=color.black, style=shape.triangledown, text="Buy Exit", location=location.abovebar, offset=0, size=size.small)
plotshape(exitshort and inshort[1] and not enterlong, title="Stoploss Short Signal", color=color.black, style=shape.triangleup, text="Sell Exit", location=location.belowbar, offset=0, size=size.small)
plotshape(REswitch and enterlong and inlong and inlong[1], title="Re-entry Long signal" ,color=color.aqua, style=shape.arrowup, text="RE", location=location.belowbar, offset=0, size=size.small)
plotshape(REswitch and entershort and inshort and inshort[1], title="Re-entry Short signal" ,color=color.silver, style=shape.arrowdown, text="RES", location=location.abovebar, offset=0, size=size.small)
// Alert section Create 'Alert' see also https://uk.tradingview.com/pine-script-reference/v4/#fun_alertcondition
alertcondition(enterlong and not inlong[1] or exitshort and inshort[1] , title="There is a Buy- or STOPLOSS SHORT Signal", message="Buy/STOPLOSS SHORT signal! \n{{exchange}}:{{ticker}}, price= {{'Source'}}\n{{time}}, volume= {{volume}}")
alertcondition(entershort and not inshort[1] or exitlong and inlong[1] , title="There is a Sell- or STOPLOSS LONG Signal", message="Sell/STOPLOSS LONG signal! \n{{exchange}}:{{ticker}}, price={{'Source'}}\n{{time}}, volume= {{volume}}")
plot(close)