Accumulated Put/Call Ratio V2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © skiviz

//@version=5
//Works in daily, weekly and monthly charts
indicator("Accumulated P/C Ratio")

aggregation = input (5, title = "N bars of accumulation")

call_vol_lowerTF = request.security("CVOL", '', close)
put_vol_lowerTF = request.security("PVOL", '', close)

//call_vol_higherTF & put_vol_higherTF only used for weekly and monthly timeframes
call_vol_higherTF = request.security_lower_tf("CVOL", 'D', close)
put_vol_higherTF = request.security_lower_tf("PVOL",'D',close)

higher_timeframe = timeframe.isweekly or timeframe.ismonthly

call_vol = higher_timeframe ? array.sum(call_vol_higherTF) : call_vol_lowerTF
put_vol = higher_timeframe ? array.sum(put_vol_higherTF) : put_vol_lowerTF


float call_agg = 0.0
float put_agg = 0.0

for i = 0 to aggregation
    call_agg := call_agg + call_vol[i]
    put_agg := put_agg + put_vol[i]

PCR = put_agg/call_agg  //Put Call Ratio


PCR_array = array.new_float(0)
lookback = input(20, title = "Lookback Period")

for i = 0 to lookback
	array.push(PCR_array, PCR[i])
	
max = array.max(PCR_array)
min = array.min(PCR_array)

diff_max = max - PCR  //when this approaches 0, P/C ratio is near max, which is bearish;
diff_min = min - PCR  //when this approaches 0, P/C ratio is near min, which is bullish;

STDEV_period = input (200, title = "STDEV Period")

diffmax_array = array.new_float(0)
diffmin_array = array.new_float(0)

for i = 0 to STDEV_period
	array.push(diffmax_array, diff_max[i])
	array.push(diffmin_array, diff_min[i])
	
diffmax_STDEV = array.stdev(diffmax_array)
diffmin_STDEV = array.stdev(diffmin_array)

diffmax_mean = array.avg(diffmax_array)
diffmin_mean =array.avg(diffmin_array)

//Color for difference from max
diffmax_in_1stdev = diff_max > diffmax_mean and diff_max < diffmax_mean + diffmax_STDEV
diffmax_in_2stdev = diff_max > diffmax_mean + diffmax_STDEV and diff_max < diffmax_mean + 2*diffmax_STDEV
diffmax_in_3stdev = diff_max > diffmax_mean + 2*diffmax_STDEV and diff_max < diffmax_mean + 3*diffmax_STDEV
diffmax_in_mean = diff_max < diffmax_mean 

color_max = if diffmax_in_mean 
    color.new(color.green, 80)
else if diffmax_in_1stdev
    color.new(color.green, 60)
else if diffmax_in_2stdev
    color.new(color.green, 40)
else if diffmax_in_3stdev
    color.new(color.green, 20)
else
    color.new(color.green, 0)  //Beyond 3 STDEV

//Color for difference from min
diffmin_in_1stdev = diff_min < diffmin_mean and diff_min > diffmin_mean - diffmin_STDEV
diffmin_in_2stdev = diff_min < diffmin_mean - diffmin_STDEV and diff_min > diffmin_mean - 2*diffmin_STDEV
diffmin_in_3stdev = diff_min < diffmin_mean - 2*diffmin_STDEV and diff_min > diffmin_mean - 3*diffmin_STDEV
diffmin_in_mean = diff_min > diffmin_mean 

color_min = if diffmin_in_mean
    color.new(color.red, 80)
else if diffmin_in_1stdev
    color.new(color.red, 60)
else if diffmin_in_2stdev
    color.new(color.red, 40)
else if diffmin_in_3stdev
    color.new(color.red, 20)
else
    color.new(color.red, 0)  //Beyond 3 STDEV

plot (diff_max , color=color_max, style = plot.style_columns, title="Diff. From Max")
plot (diff_min , color=color_min, style = plot.style_columns, title="Diff. From Min")

total = diff_max + diff_min
plot (total,"Total", color=color.black)
