OHLC Moving Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FX365_Thailand

//Revision history
    //v31.0 First release
    //v32.0 Added two options;
            // 1. Gradation
            // 2. MA Color Change

//@version=5
//Define indicator name
indicator('OHLC Moving Average', 'OHLC MA', timeframe='', timeframe_gaps=true, overlay=true)

//User inputs
ma_len = input(title='Length', defval=20)
ma_type = input.string(title='MA Type', options=['SMA', 'EMA'], defval='SMA')
ma_offset = input(title='Offset', defval=0)
i_fill = input(true, title='Fill MA')
i_grad = input(true, title='Gradation')
i_macolor = input(true, title='MA Color Change')


//Function to calculate MA
ma_f(ma_type, src, ma_len) =>
    ma_type == 'EMA' ? ta.ema(src, ma_len) : ta.sma(src, ma_len)

//Calculate MA
ma1 = ma_f(ma_type, open, ma_len)
ma2 = ma_f(ma_type, high, ma_len)
ma3 = ma_f(ma_type, low, ma_len)
ma4 = ma_f(ma_type, close, ma_len)

//Plot MA
bull = ma1 > ma1[1] and ma2 > ma2[1] and ma3 > ma3[1] and ma4 > ma4[1]
bear = ma1 < ma1[1] and ma2 < ma2[1] and ma3 < ma3[1] and ma4 < ma4[1]

a = plot(ma1, color=(i_macolor and bull) ? color.new(color.green, 0) : (i_macolor and bear) ? color.new(color.red,0) : color.new(color.gray,0), offset=ma_offset, title='Open MA')
b = plot(ma2, color=(i_macolor and bull) ? color.new(color.green, 0) : (i_macolor and bear) ? color.new(color.red,0) : color.new(color.gray,0), offset=ma_offset, title='High MA')
c = plot(ma3, color=(i_macolor and bull) ? color.new(color.green, 0) : (i_macolor and bear) ? color.new(color.red,0) : color.new(color.gray,0), offset=ma_offset, title='Low MA')
d = plot(ma4, color=(i_macolor and bull) ? color.new(color.green, 0) : (i_macolor and bear) ? color.new(color.red,0) : color.new(color.gray,0), offset=ma_offset, title='Close MA')

//Fill
fill(b, c, i_fill ? color.red : na, title='Fill MA', transp=90)

//Gradation
fill(c, b, top_value = ma2, bottom_value= ma3, top_color = (i_grad and bull) ? color.new(color.green,80) : (i_grad and bear) ? color.new(color.red,20) : (i_grad) ? color.new(color.gray,20) : na, bottom_color = (i_grad and bull) ? color.new(color.green,20) : (i_grad and bear) ? color.new(color.red,80) : (i_grad) ? color.new(color.gray,80) : na, title="Gradation color")


