//
// @author LazyBear
// List of all my indicators:
// https://docs.google.com/document/d/15AGCufJZ8CIUvwFJ9W-IKns88gkWOKBCvByMEvm5MLo/edit?usp=sharing
//
study(title="Volatility-Based Trailing Stop [LazyBear]", shorttitle="TS", overlay=true)
mult = input (3.0, title="ATR Multiplier")
length = input(10, title="ATR Period")
type=input(1, title="Style? (0 => HighLow, 1 => Close)")
ticksize=input(1)
whitepoint = input(1)
yellowpoint = input(8)
WD=tostring(whitepoint)+"D"
YD=tostring(yellowpoint)+"D"
tid = tickerid
closeWD = security(tid, WD, close)
closeYD = security(tid, YD, close)
highWD = security(tid, WD, high)
highYD = security(tid, YD, high)
lowWD = security(tid, WD, low)
lowYD = security(tid, YD, low)
funcX(xtype, xlength, xmult, xticksize, xclose, xhigh, xlow) =>
uu=xtype == 1 ? xclose : xhigh
ll=xtype == 1 ? xclose : xlow
satr=atr(xlength)
svs = xlow+ceil(xmult*satr/xticksize)*xticksize
lvs = xhigh-ceil(xmult*satr/xticksize)*xticksize
shortvs=na(shortvs[1]) ? svs : iff(uu>shortvs[1], svs , min(svs,shortvs[1]))
longvs=na(longvs[1]) ? lvs : iff(ll<longvs[1], lvs, max(lvs,longvs[1]))
longswitch=iff (uu>=shortvs[1] and uu[1]<shortvs[1] , 1 , 0)
shortswitch=iff (ll<=longvs[1] and ll[1]>longvs[1] , 1 , 0)
direction= iff(na(direction[1]), 0,
iff (direction[1]<=0 and longswitch, 1,
iff (direction[1]>=0 and shortswitch, -1, direction[1])))
pc=direction>0?longvs:shortvs
pc=funcX(type, length, mult, ticksize, closeWD, highWD, lowWD)
plot(pc, title=WD, color=white, style=circles, linewidth=4)
pcd8=funcX(type, length, mult, ticksize, closeYD, highYD, lowYD)
plot(pcd8, title=YD, color=yellow, style=line, linewidth=2)
plotshape(crossover(close,pc), color=lime, style=shape.arrowup, text="buy")
plotshape(crossunder(close,pc), color=red, style=shape.arrowdown, text="sell")