IIP
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © isitporn

//@version=5
indicator('IIP', overlay=true)
//Define each MA term
ma1 = ta.sma(close, input(1, 'Close'))
ma3 = ta.sma(close, input(3, 'MA(3:Shortest term)'))
ma5 = ta.sma(close, input(5, 'MA(5:More Shorter term)'))
ma7 = ta.sma(close, input(7, 'MA(7:Shorter term)'))
ma9 = ta.sma(close, input(9, 'MA(9:Short term)'))
ma20 = ta.sma(close, input(20, 'MA(20:Middium term'))
ma60 = ta.sma(close, input(60, 'MA(60:Long term'))
ma100 = ta.sma(close, input(100, 'MA(100:Longer term)'))
ma300 = ta.sma(close, input(300, 'MA(300:Longest term)'))

//Plot each MA no chart
plot(ma1, color=color.new(color.gray, 0), title='close', linewidth=1)
plot(ma3, color=color.new(color.fuchsia, 0), title='MA(3:shortest term)', linewidth=2)
plot(ma5, color=color.new(color.red, 0), title='MA(5:More Shorter term)', linewidth=4)
plot(ma7, color=color.new(color.black, 0), title='MA(7:Shorter term)', linewidth=3)
plot(ma9, color=color.new(color.olive, 0), title='MA(9:short term)', linewidth=3)
plot(ma20, color=color.new(color.green, 0), title='MA(20:Middium term)', linewidth=4)
plot(ma60, color=color.new(color.blue, 0), title='MA(60:Long term)', linewidth=3)
plot(ma100, color=color.new(color.purple, 0), title='MA(100:Longer term)', linewidth=3)
plot(ma300, color=color.new(color.orange, 0), title='MA(300:Longest term)', linewidth=2)

//Definition of Perfect Order and Reverse Perfect Order 
stagePO = ma5 > ma20 and ma20 > ma60
stageRPO = ma5 < ma20 and ma20 < ma60

//Decide back round colour in each stage
//  stagePO=1 ? bgcolor(color=color.blue, transp=90, title="PPP Background Colour")
//  : stageRPO=1 ? bgcolor(color=color.red, transp=90, title="GPPP Background Colour")
//  : na
bgcolor(color=stagePO ? color.blue : stageRPO ? color.red : na, title='PO or RPO BackGroundColour', transp=90)

//Put labels on 5 days, 20 days, 60 days and 100 days before today on chart
l = label.new(bar_index[4], na, '-5', color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)

label.delete(l[1])

m = label.new(bar_index[19], na, '-20', color=color.green, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)

label.delete(m[1])

n = label.new(bar_index[59], na, '-60', color=color.blue, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)

label.delete(n[1])

o = label.new(bar_index[99], na, '-100', color=color.purple, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)

label.delete(o[1])

// close price is over or under MA100? (Stron or Weak?)
//S = close > ma100 ? true : na
//W = close < ma100 ? true : na
//bar_index > ma100 ? strong = true : bar index < ma100 ? weak =  : na
//plotshape(S, title="Strong_Term",location=location.bottom, style=shape.labelup, color=color.purple, size=size.tiny)
//plotshape(W, title="Weak_Term", location=location.top, style=shape.labeldown, color=color.purple, size=size.tiny)

// Crossover and crossunder MA5 and MA20
sma_goldencross = ta.crossover(ma5, ma20)
sma_deadcross = ta.crossunder(ma5, ma20)

plotshape(sma_goldencross, title='GC Point', location=location.abovebar, style=shape.triangleup, color=color.new(color.green, 0), size=size.small)
plotshape(sma_deadcross, title='DC Point', location=location.belowbar, style=shape.triangledown, color=color.new(color.red, 0), size=size.small)


//Draw line on first bar_index in every month
chosenColor(c_) =>
    c_ == 'aqua' ? color.aqua : c_ == 'black' ? color.black : c_ == 'blue' ? color.blue : c_ == 'fuchsia' ? color.fuchsia : c_ == 'gray' ? color.gray : c_ == 'green' ? color.green : c_ == 'lime' ? color.lime : c_ == 'maroon' ? color.maroon : c_ == 'navy' ? color.navy : c_ == 'olive' ? color.olive : c_ == 'orange' ? color.orange : c_ == 'purple' ? color.purple : c_ == 'red' ? color.red : c_ == 'silver' ? color.silver : c_ == 'teal' ? color.teal : c_ == 'white' ? color.white : c_ == 'yellow' ? color.yellow : color.black
my_color_1 = input.string(title='Color', defval='navy', options=['aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'orange', 'purple', 'red', 'silver', 'teal', 'white', 'yellow'])

p = timeframe.ismonthly ? '12M' : 'M'
vline(BarIndex, Color, LineStyle, LineWidth) =>
    return_1 = line.new(BarIndex, low - ta.tr, BarIndex, high + ta.tr, xloc.bar_index, extend.both, Color, LineStyle, LineWidth)
    return_1
if ta.change(time(p))
    vline(bar_index, chosenColor(my_color_1), line.style_dotted, 1)


