3 EMAs with Visible ANGLE
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Papadaulat

//@version=4
//Format 1st EMA
study("3 EMAs with Visible ANGLE", overlay = true)
len1 = input(5, minval=1, title="1st EMA Length", group = "1st EMA  ------")
res1 = input(title="Resolution", type=input.resolution, defval="")
src1 = input(close, title="Source")
dev1 = input(0.00,  minval=-5, title="Deviation %", step=0.05)
offset1 = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
turnon1 = input(true, title="Turn on/off 1st Ema")
allowRepainting1 = input(true, "Allow Repainting?")

//Format 2ndt EMA
len2 = input(18, minval=1, title="2nd EMA Length", group = "2nd EMA ------")
res2 = input(title="Resolution", type=input.resolution, defval="")
src2 = input(close, title="Source")
dev2 = input(0.00,  minval=-5, title="Deviation %",step=0.05)
offset2 = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
turnon2 = input(true, title="Turn on/off 2nd Ema")
allowRepainting2 = input(true, "Allow Repainting?")

//Format 3rd EMA
len3 = input(44, minval=1, title="3rd EMA Length", group = "3rd EMA  ------")
res3 = input(title="Resolution", type=input.resolution, defval="")
src3 = input(close, title="Source")
dev3 = input(0.00,  minval=-5, title="Deviation %",step=0.05)
offset3 = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
turnon3 = input(true, title="Turn on/off 3rd Ema")
allowRepainting3 = input(true, "Allow Repainting?")

extend = input(30, minval=0, title="Lines Extension Value", group = "Extension Lines  ------")
extendonoff1 = input(true, title="Turn on/off 1st Line Extension")
extendonoff2 = input(true, title="Turn on/off 2nd Line Extension")
extendonoff3 = input(true, title="Turn on/off 3rd Line Extension")
//------------------------------------------------------------------------------------------------------------------//

factorm1 = 1 - dev1/100
factorm2 = 1 - dev2/100
factorm3 = 1 - dev3/100

maa = ema(src1, len1)
MA1 = allowRepainting1 ? security(syminfo.tickerid, res1, maa) : security(syminfo.tickerid, res1, maa[1], barmerge.lookahead_on)
ma1 = MA1 * factorm1

mab = ema(src2, len2)
MA2 = allowRepainting2 ? security(syminfo.tickerid, res2, mab) : security(syminfo.tickerid, res2, mab[1], barmerge.lookahead_on)
ma2 = MA2 * factorm2

mac = ema(src3, len3)
MA3 = allowRepainting3 ? security(syminfo.tickerid, res3, mac) : security(syminfo.tickerid, res3, mac[1], barmerge.lookahead_on)
ma3 = MA3 * factorm3
//------------------------------------------------------------------------------------------------------------------------//

plot(turnon1 ? ma1 : na, color=color.fuchsia, offset=offset1)
plot(turnon2 ? ma2 : na, color=color.blue,  offset=offset2)
plot(turnon3 ? ma3 : na, color=color.orange,  offset=offset3)
//------------------------------------------------------------------------------------------------------------------------//

f_y_given_x(_x1, _y1, _x2, _y2, _new_x) =>
    _m = (_y2 - _y1) / (_x2 - _x1)
    _b = _y1 - _m * _x1
    _new_y = _m * _new_x + _b
    _new_y

var ma_line1 = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.fuchsia, extend = extend.none, style=line.style_dashed)
var ma_line2 = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.blue, extend = extend.none, style=line.style_dashed)
var ma_line3 = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.orange, extend = extend.none, style=line.style_dashed)

x1 = bar_index - 1
y1 = ma1[1]
x2 = bar_index
y2 = ma1
x3 = bar_index + extend
y3 = f_y_given_x(x1, y1, x2, y2, x3)
line.set_xy1(extendonoff1 ? ma_line1 : na, x = x1, y = y1)
line.set_xy2(extendonoff1 ? ma_line1 : na, x = x3, y = y3)

y22 = ma2[1]
y33 = ma2
y44 = f_y_given_x(x1, y22, x2, y33, x3)
line.set_xy1(extendonoff2 ? ma_line2 : na, x = x1, y = y22)
line.set_xy2(extendonoff2 ? ma_line2 : na, x = x3, y = y44)

y55 = ma3[1]
y66 = ma3
y77 = f_y_given_x(x1, y55, x2, y66, x3)
line.set_xy1(extendonoff3 ? ma_line3 :na, x = x1, y = y55)
line.set_xy2(extendonoff3 ? ma_line3 : na, x = x3, y = y77)

