Convert this to a strategy

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RickSimpson
//@version=4

study(title="Rain On Me V2", shorttitle='ROM V2', overlay=true, max_bars_back=300)

//Inputs

showbs     = input(defval=true,  title="Show ATR?")
showvptbs  = input(defval=false, title="Show VPT?")
showsar    = input(defval=false, title="Show PSAR?")
showstrd   = input(defval=false, title="Show SuperTrend?")
showsr     = input(defval=true,  title="Show Support/Resistance Lines?")
showdivs   = input(defval=true,  title="Show Divergences?")
showbb     = input(defval=false, title="Show Bollinger?")
showichi   = input(defval=true,  title="Show Ichimoku?")
showtrline = input(defval=true,  title="Show Trendline?")
showfib    = input(defval=true,  title="Show Fibonacci?")
showpan    = input(defval=false, title="Show Fibonacci Info Panel?")

//ATR

//Inputs

atrper  = input(defval=14, title="ATR Period")
atrmult = input(defval=2,  title="ATR Multiplier", type=input.float, minval=0.5, maxval=100, step=0.1)

//Settings

emasrc     = hlc3
emalen     = 200
useemacond = false
emaline    = ema(emasrc, emalen)
ratr       = atr(atrper)
nl         = atrmult * ratr

//Calculation

ratrts   = float(na)
ratrts  := iff(close > nz(ratrts[1], 0) and close[1] > nz(ratrts[1], 0), max(nz(ratrts[1]), close - nl), iff(close < nz(ratrts[1], 0) and close[1] < nz(ratrts[1], 0), min(nz(ratrts[1]), close + nl), iff(close > nz(ratrts[1], 0), close - nl, close + nl)))
indir    = int(na)
indir   := iff(close[1] < nz(ratrts[1], 0) and close > nz(ratrts[1], 0), 1, iff(close[1] > nz(ratrts[1], 0) and close < nz(ratrts[1], 0) and (useemacond ? close < emaline : true), -1, nz(indir[1], 0)))
posbear  = false
posbear := nz(posbear[1], false)
posbull  = false
posbull := nz(posbull[1], false)
atrsell  = not posbear and indir == -1
atrbuy   = not posbull and indir ==  1

if atrsell
    posbull := false
    posbear := true
    posbear

if atrbuy
    posbull := true
    posbear := false
    posbear

//Plotting

plotshape(showbs ? atrsell : na, title="ATR Sell", style=shape.labeldown, location=location.abovebar, size=size.tiny, text="atr",  textcolor=color.white, color=color.red,   transp=0)
plotshape(showbs ? atrbuy  : na, title="ATR Buy",  style=shape.labelup,   location=location.belowbar, size=size.tiny, text="atr",  textcolor=color.white, color=color.green, transp=0)

//Alerts

alertcondition(atrsell, title="Sell",  message='Sell')
alertcondition(atrbuy,  title="Buy",   message='Buy')

//VPT

//Inputs

vptper   = input(defval=100, title="VPT Period",     minval=1)
vptmult  = input(defval=1,   title="VPT Multiplier", minval=1, maxval=100, step=0.1)

//Settings

source     = close
hl         = ((high - low) * 100)
oc         = ((close - open) * 100)
vol        = (volume / hl)
spreadv    = (oc * vol)
VPT        = spreadv + cum(spreadv)
window_len = 28

//Calculation

v_len        = 14
price_spread = stdev(high - low, window_len)

vop      = spreadv + cum(spreadv)
smooth   = sma(vop, v_len)
v_spread = stdev(vop - smooth, window_len)
shadow   = (vop - smooth) / v_spread * price_spread

out = shadow > 0 ? high + shadow : low + shadow

vptlen = 10
vpt    = ema(out, vptlen)

uplv = vpt - (vptmult * atr(vptper))
dnlv = vpt + (vptmult * atr(vptper))

uptrd   = 0.0
uptrd  := close[1] > uptrd[1] ? max(uplv, uptrd[1]) : uplv
dntrd   = 0.0
dntrd  := close[1] < dntrd[1] ? min(dnlv, dntrd[1]) : dnlv

trd  = 0
trd := close > dntrd[1] ? 1: close < uptrd[1] ? -1 : nz(trd[1], 1)

stlv = trd == 1 ? uptrd : dntrd

vptsell = crossunder(close, stlv)
vptbuy  = crossover(close,  stlv)

//Plotting

plotshape(showvptbs ? vptsell : na, title="VPT Sell", style=shape.labeldown, location=location.abovebar, size=size.tiny, text="vpt", textcolor=color.white, color=color.red,   transp=0)
plotshape(showvptbs ? vptbuy  : na, title="VPT Buy",  style=shape.labelup,   location=location.belowbar, size=size.tiny, text="vpt", textcolor=color.white, color=color.green, transp=0)

//Alerts

alertcondition(vptsell, title="VPT Sell", message='VPT Sell')
alertcondition(vptbuy,  title="VPT Buy",  message='VPT Buy')

//PSAR

//Inputs

start     = input(defval=0.02, title="PSAR Start",     step=0.001)
increment = input(defval=0.02, title="PSAR Increment", step=0.001)
maximum   = input(defval=0.2,  title="PSAR Maximum",   step=0.01)

//Calculation

psarper   = 14
psarmult  = 2

psaruplvl = hl2 - (psarmult * atr(psarper))
psardnlvl = hl2 + (psarmult * atr(psarper))

psaruptrd    = 0.0
psaruptrd   := close[1] > psaruptrd[1]   ? max(psaruplvl, psaruptrd[1])   : psaruplvl
psardowntrd  = 0.0
psardowntrd := close[1] < psardowntrd[1] ? min(psardnlvl, psardowntrd[1]) : psardnlvl

s = sar(start, increment, maximum)

psartrd  = 0
psartrd := close > psardowntrd[1] ? 1 : close < psaruptrd[1] ? -1 : nz(psartrd[1], 1)

psarstline = psartrd == 1 ? psaruptrd : psardowntrd
length = 3

f_ema(_src, _length)=>
    _length_adjusted = _length < 1 ? 1 : _length
    _multiplier      = 2 / (_length_adjusted + 1)
    _return          = 0.00
    _return         := na(_return[1]) ? _src : ((_src - _return[1]) * _multiplier) + _return[1]

psarpos = f_ema(psarstline, length)

psaruptrd1    = 0.0
psaruptrd1   := close[1]  > psarpos[1] ? max(psaruplvl, psaruptrd1[1])   : psaruplvl
psardowntrd1  = 0.0
psardowntrd1 := close [1] < psarpos[1] ? min(psardnlvl, psardowntrd1[1]) : psardnlvl

trend1  = 0
trend1 :=close > psarpos[1] ? 1: close < psarpos[1] ? -1 : nz(trend1[1], 1)

psarstline1 = trend1 == 1 ? psaruptrd1 : psardowntrd1

//Settings

psarsell = crossunder(close, psarstline1) and s > close
psarbuy  = crossover(close,  psarstline1) and s < close

//Plotting

plotshape(showsar ? psarsell : na, title="PSAR Sell", text="psar", textcolor=color.white, color=color.red,   style=shape.labeldown, location=location.abovebar)
plotshape(showsar ? psarbuy  : na, title="PSAR Buy",  text="psar", textcolor=color.white, color=color.green, style=shape.labelup,   location=location.belowbar)

//Alerts

alertcondition(psarsell, title="PSAR Sell", message='PSAR Sell')
alertcondition(psarbuy,  title="PSAR Buy",  message='PSAR Buy')

//SuperTrend

//Inputs

strdper      = input(defval=100, title ="SuperTrend Period",     minval=1)
strdmult     = input(defval=1,   title ="SuperTrend Multiplier", minval=0, maxval=100, step=0.01)

//Settings

strdsrc      = close
strdfastlen  = 1
strdslowlen  = 5
strdcurvelen = 50

calc(sFast, sSlow, lFast, lSlow, lC)=>
    fastC = sma(sFast, lFast)
    slowC = sma(sSlow, lSlow)
    cC = fastC + slowC
    sma(cC, lC)

strdclosecalc = calc(close, close, strdfastlen, strdslowlen, strdcurvelen)

trdc = strdclosecalc / 2

//Calculation

trduplvl = trdc - (strdmult * atr(strdper))
trddnlvl = trdc + (strdmult * atr(strdper))

trduptr    = 0.0
trduptr   := close[1] > trduptr[1]   ? max(trduplvl,   trduptr[1]) : trduplvl
trddowntr  = 0.0
trddowntr := close[1] < trddowntr[1] ? min(trddnlvl, trddowntr[1]) : trddnlvl

trdtrend  = 0
trdtrend := close > trddowntr[1] ? 1: close < trduptr[1] ? -1 : nz(trdtrend[1], 1)

trdstline = trdtrend == 1 ? trduptr : trddowntr

strdsell = crossunder(close, trdstline)
strdbuy  = crossover(close,  trdstline)

//Plotting

plot(showstrd ? trdstline[1] : na, color=trdtrend == 1 ? color.green : color.red , title="SuperTrend", style=plot.style_cross, linewidth=2)
plotshape(showstrd ? crossunder(close, trdstline) : na,  title="SuperTrend Sell",  text="st", textcolor=color.white,  style=shape.labeldown, location=location.abovebar, color=color.red,   size=size.tiny, transp=0)
plotshape(showstrd ? crossover(close,  trdstline) : na,  title="SuperTrend Buy",   text="st", textcolor=color.white,  style=shape.labelup,   location=location.belowbar, color=color.green, size=size.tiny, transp=0)

//Alerts

alertcondition(strdsell, title="SuperTrend Sell", message='SuperTrend Sell')
alertcondition(strdbuy,  title="SuperTrend Buy",  message='SuperTrend Buy')

//S/R Lines

//Settings

srpct    = 300
srpctg   = srpct / 100 * sma(volume, 50)
srspi    = volume > sma(volume, 50) + srpctg
srlpv    = 8
srstrt   = 1
srend    = 0
srbst    = 1
srbd     = 0
srlsrsrc = false
srsrc    = close

srup = pivothigh(iff(srlsrsrc,srsrc,high), srlpv,srlpv)
srdn = pivotlow(iff(srlsrsrc,srsrc,low),   srlpv,srlpv)

//Calculation

srn  = bar_index
srx1 = valuewhen(not na(srup), srn, srstrt)
sry1 = valuewhen(not na(srdn), srn, srbst)
srx2 = valuewhen(not na(srup), srn, srend)
sry2 = valuewhen(not na(srdn), srn, srbd)

line srupper = showsr ? line.new(srn[srn - srx1 + srlpv], srup[srn - srx1], srn[srn - srx2 + srlpv],srup[srn - srx2],
  extend     = extend.right, color=color.purple, width=3)   : na
line srlower = showsr ? line.new(srn[srn - sry1 + srlpv], srdn[srn - sry1], srn[srn - sry2 + srlpv],srdn[srn - sry2],
  extend     = extend.right, color=color.aqua,   width=3)   : na
line.delete(srupper[1])
line.delete(srlower[1])

//Plotting

plot(showsr ? srup : na, title="High Line", color=color.purple, linewidth=3, style=plot.style_circles, transp=0, offset=-srlpv)
plot(showsr ? srdn : na, title="Low Line",  color=color.aqua,   linewidth=3, style=plot.style_circles, transp=0, offset=-srlpv)
plot(showsr ? srup : na, title="High",      color=color.purple, transp=0, offset=-srlpv)
plot(showsr ? srdn : na, title="Low",       color=color.aqua,   transp=0, offset=-srlpv)

//Divergences

//Inputs

divtype = input(title="Divergences Type", defval='MACD', options=['MACD','VOLUME','RSI','CCI'])

//Settings

highdivsrc      = high
lowdivsrc       = low
macddivsrc      = close
macddivfast     = 12
macddivslow     = 26
macddivsmooth   = 9
volumedivlength = 5
rsidivlength    = 5
ccidivlength    = 5

//Calculation

f_top_fractal(_src) =>
    _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and 
       _src[2] > _src[0]
f_bot_fractal(_src) =>
    _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and 
       _src[2] < _src[0]
f_fractalize(_src) =>
    f_bot_fractal__1 = f_bot_fractal(_src)
    f_top_fractal(_src) ? 1 : f_bot_fractal__1 ? -1 : 0

f_macd(_src, _fast, _slow, _smooth) =>
    _fast_ma = sma(_src, _fast)
    _slow_ma = sma(_src, _slow)
    _macd    = _fast_ma - _slow_ma
    _signal  = ema(_macd, _smooth)
    _hist    = _macd - _signal
    _hist

f_volume(_smooth) =>
    _return = sma(cum(close == high and close == low or high == low ? 0 : (2 * close - low - high) / (high - low) * volume), _smooth)
    _return

osch = float(na)
oscl = float(na)
if divtype == 'MACD'
    osch := f_macd(macddivsrc, macddivfast, macddivslow, macddivsmooth)
    oscl := f_macd(macddivsrc, macddivfast, macddivslow, macddivsmooth)
    oscl
if divtype == 'VOLUME'
    osch := f_volume(volumedivlength)
    oscl := f_volume(volumedivlength)
    oscl
if divtype == 'RSI'
    osch := rsi(highdivsrc, rsidivlength)
    oscl := rsi(lowdivsrc,  rsidivlength)
if divtype == 'CCI'
    osch := cci(highdivsrc, ccidivlength)
    oscl := cci(lowdivsrc,  ccidivlength)  

fract = f_fractalize(osch) > 0 ? osch[2] : na
fracb = f_fractalize(oscl) < 0 ? oscl[2] : na

hprv = valuewhen(fract, osch[2], 0)[2]
hp   = valuewhen(fract, high[2], 0)[2]
lprv = valuewhen(fracb, oscl[2], 0)[2]
lp   = valuewhen(fracb, low[2], 0)[2]

bearishdiv = fract and high[2] > hp and osch[2] < hprv
bullishdiv = fracb and low[2]  < lp and oscl[2] > lprv

//Plotting

plot(title="Bearish Divergence", series=(showdivs and fracb) ? low[2]  : na, color=bullishdiv ? color.lime : na, linewidth=2, offset=-2)
plot(title="Bullish Divergence", series=(showdivs and fract) ? high[2] : na, color=bearishdiv ? color.red  : na, linewidth=2, offset=-2)

//Alerts

alertcondition(bearishdiv, title="Bearish Divergence", message='Bearish Divergence')
alertcondition(bullishdiv, title="Bullish Divergence", message='Bullish Divergence')

//Moving Average

//Inputs

maperiods1 = input(defval=7,     title="MA1 Period")
ma1type    = input(defval="wma", title="MA1 Type", options=["sma","ema","rma","wma","vwma"])
ma1src     = input(defval=close, title="MA1 Source")
maperiods2 = input(defval=21,    title="MA2 Period")
ma2type    = input(defval="wma", title="MA2 Type", options=["sma","ema","rma","wma","vwma"])
ma2src     = input(defval=close, title="MA2 Source")
maperiods3 = input(defval=50,    title="MA3 Period")
ma3type    = input(defval="sma", title="MA3 Type", options=["sma","ema","rma","wma","vwma"])
ma3src     = input(defval=close, title="MA3 Source")
hidema1    = input(defval=false, title="Hide MA1")
hidema2    = input(defval=false, title="Hide MA2")
hidema3    = input(defval=true,  title="Hide MA3")

//Settings

color_1  = color.new(color.black, 100)
ma1color = hidema1 ? color_1 : color.aqua
color_2  = color.new(color.black, 100)
ma2color = hidema2 ? color_2 : color.orange
color_3  = color.new(color.black, 100)
ma3color = hidema3 ? color_3 : color.purple

ma1 = sma(ma1src, maperiods1)

if ma1type == "ema"
    ma1 := ema(ma1src, maperiods1)
if ma1type == "rma"
    ma1 := rma(ma1src, maperiods1)
if ma1type == "wma"
    ma1 := wma(ma1src, maperiods1)
if ma1type == "vwma"
    ma1 := vwma(ma1src, maperiods1)
    
ma2 = sma(ma2src, maperiods2)

if ma2type == "ema"
    ma2 := ema(ma2src, maperiods2)
if ma2type == "rma"
    ma2 := rma(ma2src, maperiods2)
if ma2type == "wma"
    ma2 := wma(ma2src, maperiods2)
if ma2type == "vwma"
    ma2 := vwma(ma2src, maperiods2)
    
ma3 = sma(ma3src, maperiods3)

if ma3type == "ema"
    ma3 := ema(ma3src, maperiods3)
if ma3type == "rma"
    ma3 := rma(ma3src, maperiods3)
if ma3type == "wma"
    ma3 := wma(ma3src, maperiods3)
if ma3type == "vwma"
    ma3 := vwma(ma3src, maperiods3)

//Plotting

plot(ma1, title="MA1", color=ma1color, linewidth=2)
plot(ma2, title="MA2", color=ma2color, linewidth=2)
plot(ma3, title="MA3", color=ma3color, linewidth=2)

//Alerts

alertcondition(crossover(ma1, ma2) or crossunder(ma1, ma2), title="MA1 and MA2 Cross", message='MA1 and MA2 Cross')

//Fibonacci (Original Code Author @ QuantNomad - Ultimate Pivot Points Alerts at https://www.tradingview.com/script/l53FXt9D-QuantNomad-Ultimate-Pivot-Points-Alerts/)

//Settings

is_newbar(res) =>
    ch = 0
    if(res == 'Y')
        t  = year(time('D'))
        ch := change(t) != 0 ? 1 : 0
    else
        t = time(res)
        ch := change(t) != 0 ? 1 : 0
    ch

nround(x) => 
    n = round(x / syminfo.mintick) * syminfo.mintick
    
RoundToTick( _price) => round(_price / syminfo.mintick) * syminfo.mintick
 
Round( _val, _decimals) => 
    _p = pow(10, _decimals)
    round(abs(_val) * _p) / _p * sign(_val)

is_srnewbar(res) =>
    ch = 0
    if(res == 'Y')
        t  = year(time('D'))
        ch := change(t) != 0 ? 1 : 0
    else
        t = time(res)
        ch := change(t) != 0 ? 1 : 0   

//Inputs

pp_period = input(defval="Day", title="Fibonacci Period", type=input.string, options=['Day','Week','Month','Year'])

//Constants

pp_type                = "Fibonacci"
show_historical_levels = false
show_level_value       = true

//Calculation

pp_res = pp_period == 'Day' ? 'D' : pp_period == 'Week' ? 'W' : pp_period == 'Month' ? 'M' : 'Y' 

open_cur  = 0.0
open_cur := is_newbar(pp_res) ? open : open_cur[1]
popen     = 0.0
popen    := is_newbar(pp_res) ? open_cur[1] : popen[1]
high_cur  = 0.0
high_cur := is_newbar(pp_res) ? high : max(high_cur[1], high)
phigh     = 0.0
phigh    := is_newbar(pp_res) ? high_cur[1] : phigh[1]
low_cur   = 0.0
low_cur  := is_newbar(pp_res) ? low : min(low_cur[1], low)
plow      = 0.0
plow     := is_newbar(pp_res) ? low_cur[1] : plow[1]
pclose    = 0.0
pclose   := is_newbar(pp_res) ? close[1] : pclose[1]

//Pivots

PP = 0.0
R1 = 0.0, R2 = 0.0, R3 = 0.0, R4 = 0.0, R5 = 0.0, R6 = 0.0, R7 = 0.0, R8 = 0.0, R9 = 0.0, R10 = 0.0
S1 = 0.0, S2 = 0.0, S3 = 0.0, S4 = 0.0, S5 = 0.0, S6 = 0.0, S7 = 0.0, S8 = 0.0, S9 = 0.0, S10 = 0.0
  
if (pp_type == "Fibonacci")
    PP  := (phigh + plow + pclose) / 3
    R1  := PP + (phigh - plow) * 0.235
    S1  := PP - (phigh - plow) * 0.235
    R2  := PP + (phigh - plow) * 0.382
    S2  := PP - (phigh - plow) * 0.382
    R3  := PP + (phigh - plow) * 0.5
    S3  := PP - (phigh - plow) * 0.5
    R4  := PP + (phigh - plow) * 0.618
    S4  := PP - (phigh - plow) * 0.618
    R5  := PP + (phigh - plow) * 0.728
    S5  := PP - (phigh - plow) * 0.728
    R6  := PP + (phigh - plow) * 1.000
    S6  := PP - (phigh - plow) * 1.000
    R7  := PP + (phigh - plow) * 1.235
    S7  := PP - (phigh - plow) * 1.235
    R8  := PP + (phigh - plow) * 1.328
    S8  := PP - (phigh - plow) * 1.328
    R9  := PP + (phigh - plow) * 1.5
    S9  := PP - (phigh - plow) * 1.5
    R10 := PP + (phigh - plow) * 1.618
    S10 := PP - (phigh - plow) * 1.618

bars_sinse  = 0
bars_sinse := is_newbar(pp_res) ? 0 : bars_sinse[1] + 1

//Fibonacci Info Panel

srposition         = R6
h                  = srposition
info_label_off     = 50
info_label_size    = size.small
info_panel_x       = timenow + round(change(time)*info_label_off)
info_panel_y       = h
info_title         = "-=-=- Fibonacci Info Panel -=-=-"
info_div           = "\n\n------------------------------"
info_current_close = "\n\nCurrent Close : " + tostring(close)
srpp = "\n\ PP : " + tostring(RoundToTick(PP)) 
srr1 = "\n\ R1 : " + tostring(RoundToTick(R2))
srr2 = "\n\ R2 : " + tostring(RoundToTick(R4))
srr3 = "\n\ R3 : " + tostring(RoundToTick(R6))
srs1 = "\n\ S1 : " + tostring(RoundToTick(S2))
srs2 = "\n\ S2 : " + tostring(RoundToTick(S4))
srs3 = "\n\ S3 : " + tostring(RoundToTick(S6))
info_text  = info_title + info_current_close + srr3 + srr2 + srr1 + srpp + srs1 + srs2 + srs3
info_panel = showpan ? label.new(x=info_panel_x, y=info_panel_y, text=info_text, xloc=xloc.bar_time, yloc=yloc.price, color=color.black, style=label.style_labelup, textcolor=color.white, size=info_label_size) : na
label.delete(info_panel[1])

//Plotting Pivots Levels

vpp_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], PP,  bar_index, PP, color=color.gray,   style = line.style_dashed, extend = extend.right) : na
vs1_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S1,  bar_index, S1, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs2_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S2,  bar_index, S2, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs3_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S3,  bar_index, S3, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs4_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S4,  bar_index, S4, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs5_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S5,  bar_index, S5, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs6_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S6,  bar_index, S6, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs7_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S7,  bar_index, S7, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs8_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S8,  bar_index, S8, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs9_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], S9,  bar_index, S9, color=color.red,    style =  line.style_solid, extend = extend.right) : na
vs10_p = showfib ? line.new(bar_index[min(bars_sinse, 300)], S10, bar_index, S10, color=color.red,   style =  line.style_solid, extend = extend.right) : na
vr1_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R1,  bar_index, R1, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr2_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R2,  bar_index, R2, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr3_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R3,  bar_index, R3, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr4_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R4,  bar_index, R4, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr5_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R5,  bar_index, R5, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr6_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R6,  bar_index, R6, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr7_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R7,  bar_index, R7, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr8_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R8,  bar_index, R8, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr9_p  = showfib ? line.new(bar_index[min(bars_sinse, 300)], R9,  bar_index, R9, color=color.green,  style =  line.style_solid, extend = extend.right) : na
vr10_p = showfib ? line.new(bar_index[min(bars_sinse, 300)], R10, bar_index, R10, color=color.green, style =  line.style_solid, extend = extend.right) : na

//Delete Previous Lines

if (not is_newbar(pp_res) or not show_historical_levels)
    line.delete(vpp_p[1])
    line.delete(vs1_p[1])
    line.delete(vs2_p[1])
    line.delete(vs3_p[1])
    line.delete(vs4_p[1])
    line.delete(vs5_p[1])
    line.delete(vs6_p[1])
    line.delete(vs7_p[1])
    line.delete(vs8_p[1])
    line.delete(vs9_p[1])
    line.delete(vs10_p[1])
    line.delete(vr1_p[1])
    line.delete(vr2_p[1])
    line.delete(vr3_p[1])
    line.delete(vr4_p[1])
    line.delete(vr5_p[1])
    line.delete(vr6_p[1])
    line.delete(vr7_p[1])
    line.delete(vr8_p[1])
    line.delete(vr9_p[1])
    line.delete(vr10_p[1])

//Plotting Labels

label_vpp  = showfib ? label.new(bar_index, PP,  text=show_level_value ? ("                                  0      "  + " " +    tostring(nround(PP)))  : "P",   style= label.style_none, textcolor = color.gray)  : na
label_vs1  = showfib ? label.new(bar_index, S1,  text=show_level_value ? ("                                  0.235      " + " " + tostring(nround(S1)))  : "S1",  style= label.style_none, textcolor = color.red)   : na
label_vs2  = showfib ? label.new(bar_index, S2,  text=show_level_value ? ("                                  0.382      " + " " + tostring(nround(S2)))  : "S2",  style= label.style_none, textcolor = color.red)   : na
label_vs3  = showfib ? label.new(bar_index, S3,  text=show_level_value ? ("                                  0.5      " + " " +   tostring(nround(S3)))  : "S3",  style= label.style_none, textcolor = color.red)   : na
label_vs4  = showfib ? label.new(bar_index, S4,  text=show_level_value ? ("                                  0.618      " + " " + tostring(nround(S4)))  : "S4",  style= label.style_none, textcolor = color.red)   : na
label_vs5  = showfib ? label.new(bar_index, S5,  text=show_level_value ? ("                                  0.728      " + " " + tostring(nround(S5)))  : "S5",  style= label.style_none, textcolor = color.red)   : na
label_vs6  = showfib ? label.new(bar_index, S6,  text=show_level_value ? ("                                  1.000      " + " " + tostring(nround(S6)))  : "S6",  style= label.style_none, textcolor = color.red)   : na
label_vs7  = showfib ? label.new(bar_index, S7,  text=show_level_value ? ("                                  1.235      " + " " + tostring(nround(S7)))  : "S7",  style= label.style_none, textcolor = color.red)   : na
label_vs8  = showfib ? label.new(bar_index, S8,  text=show_level_value ? ("                                  1.328      " + " " + tostring(nround(S8)))  : "S8",  style= label.style_none, textcolor = color.red)   : na
label_vs9  = showfib ? label.new(bar_index, S9,  text=show_level_value ? ("                                  1.5      " + " " +   tostring(nround(S9)))  : "S9",  style= label.style_none, textcolor = color.red)   : na
label_vs10 = showfib ? label.new(bar_index, S10, text=show_level_value ? ("                                  1.618      " + " " + tostring(nround(S10))) : "S10", style= label.style_none, textcolor = color.red)   : na
label_vr1  = showfib ? label.new(bar_index, R1,  text=show_level_value ? ("                                  0.235      " + " " + tostring(nround(R1)))  : "R1",  style= label.style_none, textcolor = color.green) : na
label_vr2  = showfib ? label.new(bar_index, R2,  text=show_level_value ? ("                                  0.382      " + " " + tostring(nround(R2)))  : "R2",  style= label.style_none, textcolor = color.green) : na
label_vr3  = showfib ? label.new(bar_index, R3,  text=show_level_value ? ("                                  0.5      " + " " +   tostring(nround(R3)))  : "R3",  style= label.style_none, textcolor = color.green) : na
label_vr4  = showfib ? label.new(bar_index, R4,  text=show_level_value ? ("                                  0.618      " + " " + tostring(nround(R4)))  : "R4",  style= label.style_none, textcolor = color.green) : na
label_vr5  = showfib ? label.new(bar_index, R5,  text=show_level_value ? ("                                  0.728      " + " " + tostring(nround(R5)))  : "R5",  style= label.style_none, textcolor = color.green) : na
label_vr6  = showfib ? label.new(bar_index, R6,  text=show_level_value ? ("                                  1.000      " + " " + tostring(nround(R6)))  : "R6",  style= label.style_none, textcolor = color.green) : na
label_vr7  = showfib ? label.new(bar_index, R7,  text=show_level_value ? ("                                  1.235      " + " " + tostring(nround(R7)))  : "R7",  style= label.style_none, textcolor = color.green) : na
label_vr8  = showfib ? label.new(bar_index, R8,  text=show_level_value ? ("                                  1.328      " + " " + tostring(nround(R8)))  : "R8",  style= label.style_none, textcolor = color.green) : na
label_vr9  = showfib ? label.new(bar_index, R9,  text=show_level_value ? ("                                  1.5      " + " " +   tostring(nround(R9)))  : "R9",  style= label.style_none, textcolor = color.green) : na
label_vr10 = showfib ? label.new(bar_index, R10, text=show_level_value ? ("                                  1.618      " + " " + tostring(nround(R10))) : "R10", style= label.style_none, textcolor = color.green) : na

//Delete Previous Labels  
    
label.delete(label_vpp[1])
label.delete(label_vs1[1])
label.delete(label_vs2[1])
label.delete(label_vs3[1])
label.delete(label_vs4[1])
label.delete(label_vs5[1])
label.delete(label_vs6[1])
label.delete(label_vs7[1])
label.delete(label_vs8[1])
label.delete(label_vs9[1])
label.delete(label_vs10[1])
label.delete(label_vr1[1])
label.delete(label_vr2[1])
label.delete(label_vr3[1])
label.delete(label_vr4[1])
label.delete(label_vr5[1])
label.delete(label_vr6[1])
label.delete(label_vr7[1])
label.delete(label_vr8[1])
label.delete(label_vr9[1])
label.delete(label_vr10[1])

//Alerts

alertcondition(not is_newbar(pp_res) and crossover(close,  S2), "Fib Bear Trend Crossover 0.382",  "Fib Bear Trend Crossover 0.382")
alertcondition(not is_newbar(pp_res) and crossover(close,  S3), "Fib Bear Trend Crossover 0.5",    "Fib Bear Trend Crossover 0.5")
alertcondition(not is_newbar(pp_res) and crossover(close,  S4), "Fib Bear Trend Crossover 0.618",  "Fib Bear Trend Crossover 0.618")
alertcondition(not is_newbar(pp_res) and crossover(close,  R2), "Fib Bull Trend Crossover 0.382",  "Fib Bull Trend Crossover 0.382")
alertcondition(not is_newbar(pp_res) and crossover(close,  R3), "Fib Bull Trend Crossover 0.5",    "Fib Bull Trend Crossover 0.5")
alertcondition(not is_newbar(pp_res) and crossover(close,  R4), "Fib Bull Trend Crossover 0.618",  "Fib Bull Trend Crossover 0.618")

alertcondition(not is_newbar(pp_res) and crossunder(close, S2), "Fib Bear Trend Crossunder 0.382", "Fib Bear Trend Crossunder 0.382")
alertcondition(not is_newbar(pp_res) and crossunder(close, S3), "Fib Bear Trend Crossunder 0.5",   "Fib Bear Trend Crossunder 0.5")
alertcondition(not is_newbar(pp_res) and crossunder(close, S4), "Fib Bear Trend Crossunder 0.618", "Fib Bear Trend Crossunder 0.618")
alertcondition(not is_newbar(pp_res) and crossunder(close, R2), "Fib Bull Trend Crossunder 0.382", "Fib Bull Trend Crossunder 0.382")
alertcondition(not is_newbar(pp_res) and crossunder(close, R3), "Fib Bull Trend Crossunder 0.5", "Fib Bull Trend Crossunder 0.5")
alertcondition(not is_newbar(pp_res) and crossunder(close, R4), "Fib Bull Trend Crossunder 0.618", "Fib Bull Trend Crossunder 0.618")

//Bollinger

//Inputs

srcbb = input(defval=close, title="Bollinger Source")
lenbb = input(defval=20,    title="Bollinger SMA", minval=1)
mult  = input(defval=2,     title="Bollinger SD",  minval=0.001, maxval=50)
tol2  = input(defval=0,     title="Bollinger Color Tolerance", minval=0, type=input.float)

//Calculation

basis = sma(srcbb, lenbb)
dev   = mult * stdev(srcbb, lenbb)
upper = basis + dev
lower = basis - dev

//Plotting

pl1 = plot(showbb and upper ? upper : na, title="BB UP",  color=color.black, linewidth=3)
pl2 = plot(showbb and lower ? lower : na, title="BB LOW", color=color.black, linewidth=3)
fill(pl1, pl2)

//Ichimoku

//Inputs

conversionPeriods   = input(defval=9,   title="Ichimoku Conversion Line Periods", minval=1)
basePeriods         = input(defval=103, title="Ichimoku Base Line Periods",       minval=1)
laggingSpan2Periods = input(defval=52,  title="Ichimoku Lagging Span 2 Periods",  minval=1)
displacement        = input(defval=26,  title="Ichimoku Displacement",            minval=1)

//Settings

donchian(len) => avg(lowest(len), highest(len))
conversionLine = donchian(conversionPeriods)
baseline       = donchian(basePeriods)
leadline1      = avg(conversionLine, baseline)
leadline2      = donchian(laggingSpan2Periods)

//Plotting

plot(baseline, title="BaseLine", color=color.new(#991515, 0))
plot(showichi ? close : na, title="Lagging Span/Barney", color=color.new(#9C27B0, 100), linewidth=0, offset=-displacement)
p1ichi=plot(showichi ? leadline1 : na, title="Lead 1", color=color.new(color.green, 100), offset=displacement)
p2ichi=plot(showichi ? leadline2 : na, title="Lead 2", color=color.new(color.red, 100),   offset=displacement)
fill(p1ichi, p2ichi, title="Ichimoku Cloud", color=leadline1 > leadline2 ? color.new(color.aqua, 65) : color.new(color.purple, 65))

//Trendline

//Inputs

tdres = input(defval="", title="Trend Line Resolution", type=input.resolution)

//MTF Settings

tdsrc        = hl2
isrepainting = false
tdsrc1       = security(syminfo.tickerid, tdres, tdsrc[isrepainting ? 0 : barstate.isrealtime ? 1 : 0])[isrepainting ? 0 : barstate.isrealtime ? 0 : 1]

//Settings

pival   = 2 * asin(1)
tdlper  = 0.0
tdsm    = ((4 * tdsrc1) + (3 * nz(tdsrc1[1])) + (2 * nz(tdsrc1[2])) + nz(tdsrc1[3])) / 10
tdrd    = ((0.0962 * tdsm) + (0.5769 * nz(tdsm[2])) - (0.5769 * nz(tdsm[4])) - (0.0962 * nz(tdsm[6]))) * ((0.075 * nz(tdlper[1])) + 0.54)

tda1    = ((0.0962 * tdrd) + (0.5769 * nz(tdrd[2])) - (0.5769 * nz(tdrd[4])) - (0.0962 * nz(tdrd[6]))) * ((0.075 * nz(tdlper[1])) + 0.54)
tdl1    = nz(tdrd[3])

tdlt    = ((0.0962 * tdl1) + (0.5769 * nz(tdl1[2])) - (0.5769 * nz(tdl1[4])) - (0.0962 * nz(tdl1[6]))) * ((0.075 * nz(tdlper[1])) + 0.54)
tdlz    = ((0.0962 * tda1) + (0.5769 * nz(tda1[2])) - (0.5769 * nz(tda1[4])) - (0.0962 * nz(tda1[6]))) * ((0.075 * nz(tdlper[1])) + 0.54)

tdl2    = tdl1 - tdlz
tdl2   := (0.2 * tdl2) + (0.8 * nz(tdl2[1]))
tda2    = tda1 + tdlt
tda2   := (0.2 * tda2) + (0.8 * nz(tda2[1]))

re      = (tdl2 * nz(tdl2[1])) + (tda2 * nz(tda2[1]))
re     := (0.2 * re) + (0.8 * nz(re[1]))
im      = (tdl2 * nz(tda2[1])) - (tda2 * nz(tdl2[1]))
im     := (0.2 * im) + (0.8 * nz(im[1]))

tdlper := im != 0 and re != 0 ? 2 * pival / atan(im / re) : 0
tdlper := min(max(tdlper, 0.67 * nz(tdlper[1])), 1.5 * nz(tdlper[1]))
tdlper := min(max(tdlper, 6), 50)
tdlper := (0.2 * tdlper) + (0.8 * nz(tdlper[1]))

//Calculation

tdsmtdlper  = 0.0
tdsmtdlper := (0.33 * tdlper) + (0.67 * nz(tdsmtdlper[1]))

dctdlper = ceil(tdsmtdlper + 0.5), iTrend = 0.0
for i    = 0 to dctdlper - 1
    iTrend := iTrend + nz(tdsrc1[i])
iTrend := dctdlper > 0 ? iTrend / dctdlper : iTrend

tdlineauto = ((4 * iTrend) + (3 * nz(iTrend[1])) + (2 * nz(iTrend[2])) + nz(iTrend[3])) / 10

sig  = tdsm > tdlineauto ? 1 : tdsm < tdlineauto ? -1 : 0

tdcl = sig > 0 ? color.new(color.lime, 0) : sig < 0 ? color.new(color.red, 0) : na

//Plotting

plot(showtrline ? tdlineauto : na, title="TD", color=tdcl, linewidth=3)

//Alerts

alertcondition(crossunder(sig, 0), title="Trend Line Turned Red",   message='Trend Line Turned Red')
alertcondition(crossover(sig,  0), title="Trend Line Turned Green", message='Trend Line Turned Green')