Advanced Choppiness Indicator with CPMA
// 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("Advanced Choppiness Indicator with CPMA", format=format.price, precision=0, timeframe="", timeframe_gaps=true)

// Import necessary scripts 
import chhagansinghmeena/BankNifty_CSM/16 as CSM

// This is an Advanced Choppiness Indicator with CPMA (Conceptive Price Moving Average).
// It calculates the choppiness index and detects choppy zones in the market based on ATR (Average True Range) and CPMA.
// The indicator also provides a custom trend calculation using CPMA and plots the results on the chart.
// It includes a range box that highlights the choppy zones and provides visual cues for trend determination.

// Summary:
// This script calculates the choppiness index and detects choppy zones based on the ATR and CPMA indicators.
// It also provides a custom trend calculation using CPMA and plots the results on the chart.
// Choppy zones are highlighted with a range box, and trend determination is based on the CPMA angle and choppiness thresholds.
// To use this indicator, apply it to your chart and observe the choppy zones and trend indications.
// Adjust the input parameters (ATR length, CPMA length, thresholds) according to your preferences and trading strategy.
// Choppy zones indicate periods of low momentum and erratic price movement, while trends indicate sustained price direction.
// Use this information to make informed trading decisions, such as avoiding choppy zones and taking trades in the direction of the trend.


// Choppiness Calculation
length = input.int(14, minval=1, title="ATR Length")
candleBodySize = input.int(10, minval=1, title="Candle Body Size")
cpma_length = input.int(14, minval=1, title="CPMA Length", tooltip="This is a custom trend indicator. It provides better support and resistance levels than SMA or EMA.")
// Choppy Zone Thresholds
choppyThresholdHigh = input(60, title="Choppy Zone High Threshold")
choppyThresholdLow = input(40, title="Choppy Zone Low Threshold")

//Chopiness calculation
trueRange = ta.tr(true)
atrValue = ta.sma(trueRange, length)
highestHigh = ta.highest(high, length)
lowestLow = ta.lowest(low, length)
span = 25 / (highestHigh - lowestLow) * lowestLow

choppiness = atrValue / (highestHigh - lowestLow) * 100

// Conceptive Price Moving Average (CPMA) Calculation
CPMA = CSM.CSM_CPMA(length = cpma_length)

// Calculate the angle of CPMA
x1_CPMA = 0
x2_CPMA = 1
y1_CPMA = 0
y2_CPMA = (CPMA[1] - CPMA) / hlc3 * span
c_CPMA = math.sqrt((x2_CPMA - x1_CPMA)*(x2_CPMA - x1_CPMA) + (y2_CPMA - y1_CPMA)*(y2_CPMA - y1_CPMA))
emaAngle_1 = math.round(180 * math.acos((x2_CPMA - x1_CPMA)/c_CPMA) / math.pi)
emaAngle = y2_CPMA > 0 ? -emaAngle_1 : emaAngle_1

// Determine Choppy Zones
isChoppy = choppiness >= choppyThresholdLow and choppiness <= choppyThresholdHigh
candleBody = math.abs(close - open)
isGapTrending = math.abs(CPMA - close) < (candleBodySize + 4) or (candleBody <= candleBodySize and candleBody[1] <= candleBodySize)

// Determine Trend
// Thanks For LUX ALGO for gradient display idea.
[cpmaPlotVal, colorUp] = CSM.getGradientColor(isFirstbar = barstate.isfirst, src = ta.rsi(CPMA,cpma_length)/100 , length = cpma_length, isSmoothed = true) //getSmoothedValue(src = ta.rsi(CPMA,cpma_length)/100, length = cpma_length)

Is_choppiness = isGapTrending or isChoppy or emaAngle < 0 and emaAngle > -3 
// Plotting
plot(Is_choppiness ? 1 : na, color=color.yellow, style=plot.style_areabr, title = "Chopiness Area")
plot(not Is_choppiness ? cpmaPlotVal : na , color= colorUp , style=plot.style_histogram , title = "Histogram Trend")
plot(cpmaPlotVal, color= close > CPMA ? color.green : color.red , style=plot.style_line, title = "Trend Line", linewidth = 2)