MA + VolumeArea
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Crypto_Elk
// I am Now a Millionaire. Thank you! Jan.
//@version=5
indicator(title='JL | MArea', shorttitle='MArea', overlay=true, precision=0)

ml = input(50, title='Array N') 
qn = input.int(defval=1, title='Number of searches in Array', options=[1,2,3,4,5,6,7,8,9,10])

shb = input(title='Up Area', defval=true)
shr = input(title='Down Area', defval=true)

source = input(defval=close, title='Source MA')
xl1 = input.int(21, title='Length Line1', minval=1)
ll1 = input(title='MA Line1', defval=true)
xl2 = input.int(50, title='Length Line2', minval=1)
ll2 = input(title='MA Line2', defval=true)
xl3 = input.int(100, title='Length Line3', minval=1)
ll3 = input(title='MA Line3', defval=true)
xl4 = input.int(200, title='Length Line4', minval=1)
ll4 = input(title='MA Line4', defval=true)

///////////////////////////////////////////

ln1 = ta.ema(source, xl1)
ln2 = ta.ema(source, xl2)
ln3 = ta.ema(source, xl3)
ln4 = ta.ema(source, xl4)

plot(ll1 ? ln1 : na, color=color.new(#ffffff, 50), linewidth=1, title='Line1')
plot(ll2 ? ln2 : na, color=color.new(#ffe500, 50), linewidth=1, title='Line2')
plot(ll3 ? ln3 : na, color=color.new(#ff9800, 50), linewidth=1, title='Line3')
plot(ll4 ? ln4 : na, color=color.new(#ff001a, 50), linewidth=1, title='Line4')


///////////////////////////////////////////

BM = close > open ? volume : 0
RM = close < open ? volume : 0

rm = array.new_float(0)
for i = 0 to ml by qn
    array.push(rm, RM[i])

bm = array.new_float(0)
for i = 0 to ml by qn
    array.push(bm, BM[i])

bmax = volume == array.max(bm) ? 1 : na
rmax = volume == array.max(rm) ? 1 : na

///////////////////////////////////////////

con = bmax
val = open <= close ? close : close

var line l1Line = na
var line l2Line = na

if con and bar_index and shb
    line.set_x2(l1Line, bar_index )
    line.set_extend(l1Line, extend.none)
    line.set_x2(l2Line, bar_index )
    line.set_extend(l2Line, extend.none)
    l1Line := line.new(bar_index, val, bar_index, val, style=line.style_dashed, color=color.new(close > open ? #1680d4 : #ff001a,100), width=1, extend=extend.none)
    l2Line := line.new(bar_index, open <= close ? high : low, bar_index, open <= close ? high : low, style=line.style_dashed, color=color.new(close > open ? #1680d4 : #ff001a,100), width=1, extend=extend.none)

if not na(l1Line) and not na(l2Line) and line.get_x2(l1Line) != bar_index
    line.set_x2(l1Line, bar_index )
    line.set_x2(l2Line, bar_index )

UpB = plot(line.get_price(l1Line, bar_index), color=color.new(#1680d4, 100), linewidth=1, title='1', editable=false)
DownB = plot(line.get_price(l2Line, bar_index), color=color.new(#1680d4, 100), linewidth=1, title='2', editable=false)
fill(UpB, DownB, color=color.new(#1680d4, 90), title='Up Area', editable=true, fillgaps=false)

///////////////////////////////////////////

con1 = rmax
val1 = open <= close ? close : close

var line l3Line = na
var line l4Line = na

if con1 and bar_index and shr
    line.set_x2(l3Line, bar_index )
    line.set_extend(l3Line, extend.none)
    line.set_x2(l4Line, bar_index )
    line.set_extend(l4Line, extend.none)
    l3Line := line.new(bar_index, val1, bar_index, val1, style=line.style_dashed, color=color.new(close > open ? #1680d4 : #ff001a,100), width=1, extend=extend.none)
    l4Line := line.new(bar_index, open <= close ? high : low, bar_index, open <= close ? high : low, style=line.style_dashed, color=color.new(close > open ? #1680d4 : #ff001a,100), width=1, extend=extend.none)

if not na(l3Line) and not na(l4Line) and line.get_x2(l3Line) != bar_index
    line.set_x2(l3Line, bar_index )
    line.set_x2(l4Line, bar_index )

UpR = plot(line.get_price(l3Line, bar_index), color=color.new(#ff001a, 100), linewidth=1, title='1', editable=false)
DownR = plot(line.get_price(l4Line, bar_index), color=color.new(#ff001a, 100), linewidth=1, title='2', editable=false)
fill(UpR, DownR, color=color.new(#ff001a, 90), title='Down Area', editable=true, fillgaps=false)

///////////////////////////////////////////