#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © skiviz
#//Works in daily, weekly and monthly charts
#indicator("Accumulated P/C Ratio")
# Converted and mod by Sam4Cok@Samer800 - 02/2023
declare lower;
#// Inputs
input SowLabel = yes;
input colorBars = yes; # "Color bars"
input ShowBand = yes;
input AccumPeriod = {Default "Chart Time", Weekly, Monthly};
input barsOfAccumulation = 5;
input lookback = 20;
input STDEV_period = 200; # "STDEV Period"
input CalcMethod = {Default "Based on Price & Volume", "Based on Price Only", "Based on Volume Only"};
input index = {default "Put/Call All", "Put/Call S&P","Put/Call NASAQ", "Put/Call Dow Jones","Put/Call Russell"};
input percent_factor_volume = 0;# "Percent to reduce from volume factor [0 - normal, 100 - disable volume]"
input emaSmooth = yes; # "EMA smoothing"
input emaLength = 5; # "EMA Length"
AddLabel(SowLabel, CalcMethod + " On : " + AccumPeriod, Color.WHITE);
def na = Double.NaN;
def last = isNaN(close);
def CurrentAgg = GetAggregationPeriod();
def Style = if CalcMethod==CalcMethod."Based on Price Only" then 1 else
if CalcMethod==CalcMethod."Based on Volume Only" then -1 else 0;
def TF = if CurrentAgg< AggregationPeriod.DAY then CurrentAgg else
if AccumPeriod==AccumPeriod.Monthly then AggregationPeriod.MONTH else
if AccumPeriod==AccumPeriod.Weekly then AggregationPeriod.WEEK else CurrentAgg;
#--- Color
#--- RED
DefineGlobalColor("Red1" , CreateColor(255,0,0));
DefineGlobalColor("Red2" , CreateColor(216,0,0));
DefineGlobalColor("Red3" , CreateColor(177,0,0));
DefineGlobalColor("Red4" , CreateColor(137,0,0));
DefineGlobalColor("Red5" , CreateColor(98,0,0));
#---- Green
DefineGlobalColor("Green1" , CreateColor(0,255,0));
DefineGlobalColor("Green2" , CreateColor(0,216,0));
DefineGlobalColor("Green3" , CreateColor(0,177,0));
DefineGlobalColor("Green4" , CreateColor(0,137,0));
DefineGlobalColor("Green5" , CreateColor(0,98,0));
#---
def _tickc;
def _tickv;
switch (index) {
case "Put/Call All":
_tickc = Fundamental(FundamentalType.CLOSE, "$PCALL", TF);
_tickv = Fundamental(FundamentalType.VOLUME, "$PCALL", TF);
case "Put/Call Dow Jones":
_tickc = Fundamental(FundamentalType.CLOSE, "$PCI", TF);
_tickv = Fundamental(FundamentalType.VOLUME, "$PCI", TF);
case "Put/Call Russell":
_tickc = Fundamental(FundamentalType.CLOSE, "$PCRL", TF);
_tickv = Fundamental(FundamentalType.VOLUME, "$PCRL", TF);
case "Put/Call S&P":
_tickc = Fundamental(FundamentalType.CLOSE, "$PCSP", TF);
_tickv = Fundamental(FundamentalType.VOLUME, "$PCSP", TF);
case "Put/Call NASAQ":
_tickc = Fundamental(FundamentalType.CLOSE, "$PCND", TF);
_tickv = Fundamental(FundamentalType.VOLUME, "$PCND", TF);
}
def tickc = if isNaN(_tickc) or _tickc==0 then tickc[1] else _tickc;
def tickv = if isNaN(_tickv) or _tickv==0 then tickv[1] else _tickv;
#-----
def reduced_percent;
if BarNumber()<2 {
reduced_percent = if (percent_factor_volume==0) then 1 else 0;
} else {
reduced_percent = AbsValue(percent_factor_volume/100-1);
}
def CondPos = (tickc/tickc[1])*(max(min(tickv/tickv[1],2)*reduced_percent,1));
def CondNeg = (tickc[1]/tickc)*(max(min(tickv/tickv[1],2)*reduced_percent,1));
def points_array;
def points;
def point = if points[1]==0 then 1 else points[1];
def points_totals;
#//Positive
if if(Style<0,1,tickc>tickc[1]) and if(Style>0, 1 , (tickv>tickv[1])) {
points_array = if Style>0 then (tickc/tickc[1]) else if Style<0 then (tickv/tickv[1]) else CondPos;
points = points[1] + 1;
points_totals = points_array / point;
#//Negative
} else
if if(Style<0,1,tickc<tickc[1]) and if (Style>0, 1, tickv>tickv[1]) {
points_array = if Style>0 then -(tickc[1]/tickc) else if Style<0 then -(tickv[1]/tickv) else -CondNeg;
points = points[1] + 1;
points_totals = points_array / point;
} else {
points_array = points_array[1];
points = if points[1]==0 then 1 else point;
points_totals = points_totals[1];
}
def sum_total = Sum(points_totals, barsOfAccumulation);
def PCR_TEMP = if emaSmooth then ExpAverage(sum_total,emaLength) else sum_total;
def PCR = PCR_TEMP;
#-----
def max = highest(PCR, Lookback);
def min = lowest(PCR, Lookback);
def diff_max1 = max - PCR;# //when this approaches 0, P/C ratio is near max, which is bearish;
def diff_min1 = min - PCR;# //when this approaches 0, P/C ratio is near min, which is bullish;
def diff_max = if isNaN(diff_max1) then diff_max[1] else diff_max1;
def diff_min = if isNaN(diff_min1) then diff_min[1] else diff_min1;
def diffmax_STDEV = stdev(diff_max, STDEV_period);
def diffmin_STDEV = stdev(diff_min, STDEV_period);
def diffmax_mean = average(diff_max, STDEV_period);
def diffmin_mean = average(diff_min, STDEV_period);
#//Color for difference from max
def diffmax_in_1stdev = diff_max > diffmax_mean and diff_max < diffmax_mean + diffmax_STDEV;
def diffmax_in_2stdev = diff_max > diffmax_mean + diffmax_STDEV and diff_max < diffmax_mean + 2*diffmax_STDEV;
def diffmax_in_3stdev = diff_max > diffmax_mean + 2*diffmax_STDEV and diff_max < diffmax_mean + 3*diffmax_STDEV;
def diffmax_in_mean = diff_max < diffmax_mean;
#//Color for difference from min
def diffmin_in_1stdev = diff_min < diffmin_mean and diff_min > diffmin_mean - diffmin_STDEV;
def diffmin_in_2stdev = diff_min < diffmin_mean - diffmin_STDEV and diff_min > diffmin_mean - 2*diffmin_STDEV;
def diffmin_in_3stdev = diff_min < diffmin_mean - 2*diffmin_STDEV and diff_min > diffmin_mean - 3*diffmin_STDEV;
def diffmin_in_mean = diff_min > diffmin_mean;
def total = if last then na else diff_max + diff_min;
def Color_max = if diffmax_in_mean then 5 else
if diffmax_in_1stdev then 4 else
if diffmax_in_2stdev then 3 else
if diffmax_in_3stdev then 2 else 1;
def color_min = if diffmin_in_mean then 5 else
if diffmin_in_1stdev then 4 else
if diffmin_in_2stdev then 3 else
if diffmin_in_3stdev then 2 else 1;
def ExtUp = Total > diffmax_STDEV and Total > Total[1];
def Up = Total > diffmax_STDEV and Total < Total[1];
def ExtDn = Total < -diffmin_STDEV and Total < Total[1];
def Dn = Total < -diffmin_STDEV and Total > Total[1];
plot ZeroLine = if last then na else 0;
ZeroLine.SetDefaultColor(Color.YELLOW);
plot BandUp = if last then na else diffmax_STDEV;
BandUp.SetHiding(!ShowBand);
BandUp.SetStyle(Curve.SHORT_DASH);
BandUp.SetDefaultColor(Color.GRAY);
plot BandDn = if last then na else -diffmin_STDEV;
BandDn.SetHiding(!ShowBand);
BandDn.SetStyle(Curve.SHORT_DASH);
BandDn.SetDefaultColor(Color.GRAY);
plot Totals = if last then na else total;
Totals.SetDefaultColor(Color.WHITE);
plot FromMax = if last then na else diff_max;
FromMax.SetLineWeight(3);
FromMax.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
FromMax.AssignValueColor(if Color_max==5 then GlobalColor("Green5") else
if Color_max==4 then GlobalColor("Green4") else
if Color_max==3 then GlobalColor("Green3") else
if Color_max==2 then GlobalColor("Green2") else GlobalColor("Green1"));
plot FromMin = if Last then na else diff_min;
FromMin.SetLineWeight(3);
FromMin.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
FromMin.AssignValueColor(if color_min==5 then GlobalColor("RED5") else
if color_min==4 then GlobalColor("RED4") else
if color_min==3 then GlobalColor("RED3") else
if color_min==2 then GlobalColor("RED2") else GlobalColor("RED1"));
#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
if ExtUp then Color.GREEN else
if Up then Color.DARK_GREEN else
if ExtDn then Color.RED else
if Dn then Color.DARK_RED else Color.GRAY);
# --- END CODE