Chilllax Moving Averages with Qullamaggie colors
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chilllaxtrader

//@version=4

study(title="Chilllax Moving Averages with Qullamaggie colors", shorttitle="ChillLax Moving Averages", overlay=true)

// Display 2 MAs, default is 10d sma and 20d sma. You can choose any length. Choose sma, or ema. Choose ma of Open, High, Low, or Close
// Color code is based on Qullamaggie's idea:
// Dark green = 10d ma >  20d ma, and both trending up
// Light green = 10d ma > 20d ma, but only 10d ma trending up
// Yellow = 10d ma > 20d ma, but neither trending up

// Trend is comparing the ma from X trendlen days ago. Default to 5 days ago.

yourmama = input(title='', options=['SMA', 'EMA'], defval='SMA', group='Moving Averages Type')

ma1 = input(10, minval=1, title="First MA length")
src1 = input(close, title="OHLC")
out1 = yourmama == 'SMA' ? sma(src1, ma1) : yourmama == 'EMA' ? ema(src1, ma1) : na

ma2 = input(20, minval=0, title="Second MA length")
src2 = input(close, title="OHLC")
out2 = yourmama == 'SMA' ? sma(src2, ma2) : yourmama == 'EMA' ? ema(src2, ma2) : na

plot(out1, color=color.red)
plot(out2, color=color.black)

// the number of bars to determine uptrend, if trendlen is 5, and if today's ma is higher than 5 days ago's ma, then it is an uptrend
trendlen = input(5, minval=1, title="number of bars to determine uptrend")

over = out1 > out2
out1up = out1 > out1[trendlen]
out2up = out2 > out2[trendlen]

green = out1 > out2 and out1up and out2up
lightgreen = out1 > out2 and out1up and not out2up
yellow = out1 > out2 and not out1up and not out2up

bgColor = (green ? color.new(color.rgb(0, 150, 0), 50) : lightgreen ? color.new(color.rgb(0, 255, 0), 80) : yellow ? color.new(color.yellow, 80) : na)
bgcolor(color=bgColor)


