Digital Nivesh: Trend Tracker
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © digitalnivesh

//@version=4
study("Trend Tracker", shorttitle="TM", overlay=true, format=format.price, precision=2, resolution="")
shortTf = input(defval="3",type=input.resolution)
AP=input(5,"ATR Period")
tr1 = security(syminfo.tickerid, shortTf, sma(tr,AP))
close1 = security(syminfo.tickerid, shortTf, close)
low1  =  security(syminfo.tickerid, shortTf, low)
high1  =  security(syminfo.tickerid, shortTf, high)
period=input(20,"CCI period")
coeff=input(1,"ATR Multiplier")

ATR=sma(tr1,AP)
rMT = input(title="Resolution", type=input.resolution, defval="5")
src=close1//input(close)

upT=low1-ATR*coeff
downT=high1+ATR*coeff
MagicTrend=0.0
MagicTrend := cci(src,period)>=0 ? (upT<nz(MagicTrend[1]) ? nz(MagicTrend[1]) : upT) : (downT>nz(MagicTrend[1]) ? nz(MagicTrend[1]) : downT)
color1= cci(src,period)>=0 ? #0022FC : #FC0400
buySignalFirst = crossover(close1,MagicTrend)
sellSignalFirst = crossunder(close1,MagicTrend)

plot(MagicTrend, color=color1, linewidth=3)

// SECOND INDICATOR

Periods = input(title="ATR Period", type=input.integer, defval=10)
srcSecond = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=srcSecond-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=srcSecond+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1

dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1

mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)


plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
//plotshape(buySignalFirst and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
//plotshape(sellSignalFirst and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
