Conceptive Price Moving Average [CSM]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chhagansinghmeena

//@version=5
indicator("Conceptive Price Moving Average [CSM]", shorttitle = "CPMA by CSM", overlay=true)

// Define the length of the moving average
source = input.source(title = "CMA Source", defval = close, group = "CSM CPMA")
length = input.int(21, "CMA Length", minval=1, group = "CSM CPMA")
length_09 = input.int(9, "EMA 9", minval=1, group = "EMA")
length_20 = input.int(20, "EMA 20", minval=20, group = "EMA")
length_50 = input.int(50, "EMA 50", minval=50, group = "EMA")
length_100 = input.int(100, "EMA 100", minval=100, group = "EMA")
length_200 = input.int(200, "EMA 200", minval=200, group = "EMA")

// Get various price types
[Open, price, High, Low , HL2, HLC3, HLCC4, OHLC4 ] = request.security(syminfo.tickerid, timeframe.period, [open, close, high, low, hl2, hlc3, hlcc4, ohlc4])

// Calculate the average of the last 3 candles for each price type
price_avg = ta.ema(price, length) 
HL2_avg =  ta.sma(HL2, length)
Open_avg = ta.ema(Open, length) 
High_avg =  ta.sma(High, length) 
Low_avg = ta.ema(Low, length)
OHLC4_avg =  ta.sma(OHLC4, length)
HLC3_avg = ta.ema(HLC3, length) 
HLCC4_avg = ta.sma(HLCC4, length)

// Calculate the average of the price types
price_average = (price_avg + HL2_avg + Open_avg + High_avg + Low_avg + OHLC4_avg + HLC3_avg + HLCC4_avg) / 8
price_average := na(price_average[1]) ? price_average : price_average[1] + (source - price_average[1]) / (length * math.pow(source/price_average[1], 4))

ema9 = ta.ema(close,length_09)
ema20 = ta.ema(close,length_20)
ema50 = ta.ema(close,length_50)
ema100 = ta.ema(close,length_100)
ema200 = ta.ema(close,length_200)

// Plot the average on the chart
plot(price_average, color=color.blue, linewidth=2, title="CPMA")
plot(ema9, color=color.red, linewidth=1, title="EMA 9")
plot(ema20, color=color.green, linewidth=2, title="EMA 20")
plot(ema50, color=color.orange, linewidth=3, title="EMA 50")
plot(ema100, color=color.yellow, linewidth=4, title="EMA 100")
plot(ema200, color=color.white, linewidth=4, title="EMA 200")


