Put/Call Ratio (PCR) Indicator for ThinkorSwim

Hi, I was wondering if there is any way to take the top 3 weekly calls and top 3 weekly puts based on open interest and automatically have those levels plotted on the chart.
There are various scripts in this thread. Plug&play and see if any of them meet your needs.
 
How about just the put call ratio for the nearest contract close date? A few weeks away is not helpful if looking for evidence of a near squeeze.
 
XFGi2GN.png


you may try the below where I used PUT/CALL ticker created by TOS. Major drawback of this change seems to be that sometimes don't get enough historical data. Still can be usable.

CODE:

CSS:
#// 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
 
Folks, given the recent interest in this thread, rather than hardcode symbols into the study, I have modified the study to take an input ticker, e.g. AAPL, or FB, or GS, etc and it will display the info requested. All you got to do is to change the symbol of your preference in the UI. Hence from symbol to symbol, not much work is required other than changing the value of the input symbol in the user interface. Hope this helps

Code:
# Put/Call
# 11.9.2019

declare lower;
    input symbol = "AMZN";
    def series = 1;
    def CurrentYear = GetYear();
    def CurrentMonth = GetMonth();
    def CurrentDOM = GetDayOfMonth(GetYYYYMMDD());
    def Day1DOW1 = GetDayOfWeek(CurrentYear * 10000 + CurrentMonth * 100 + 1);
    def FirstFridayDOM1 = if Day1DOW1 < 6
                          then 6 - Day1DOW1
                          else if Day1DOW1 == 6
                          then 7
                          else 6;
    def RollDOM = FirstFridayDOM1 + 14;
    def ExpMonth1 = if RollDOM > CurrentDOM
                    then CurrentMonth + series - 1
                    else CurrentMonth + series;
    def ExpMonth2 = if ExpMonth1 > 12
                    then ExpMonth1 - 12
                    else ExpMonth1;
    def ExpYear = if ExpMonth1 > 12
                  then CurrentYear + 1
                  else CurrentYear;
    def Day1DOW = GetDayOfWeek(ExpYear * 10000 + ExpMonth2 * 100 + 1);
    def FirstFridayDOM = if Day1DOW < 6
                         then 6 - Day1DOW
                         else if Day1DOW == 6
                         then 7
                         else 6;
    def ExpDOM = FirstFridayDOM + 14;
    def date = ExpYear * 10000 + ExpMonth2 * 100 + ExpDOM + 1;
    def PutVolume = if isNaN(volume(symbol = GetATMOption(symbol, date, OptionClass.PUT)))
                    then PutVolume[1]
                    else volume(symbol = GetATMOption(symbol, date, OptionClass.PUT));
    def CallVolume = if isNaN(volume(symbol = GetATMOption(symbol, date, OptionClass.CALL)))
                     then CallVolume[1]
                     else volume(symbol = GetATMOption(symbol, date, OptionClass.CALL));
    def PutTotal = PutVolume;
    def CallTotal = CallVolume;
AddLabel(yes,(concat("Ex date: ",
              concat(ExpMonth2,
              concat("/",
              concat(ExpDOM,
              concat("/",
              concat(AsPrice(ExpYear),""))))))), color.white);
   def Strike = Round(close(symbol = symbol) / .5, 0) * .5;
AddLabel(1, "Strikes " + symbol + ": $" + Strike, Color.White);
AddLabel(yes, Concat("ATM Put/Call Ratio ", Round(PutTotal / CallTotal, 2)) + " / 1", Color.White);
   def PV = if IsNaN(PutTotal)
            then PV[1]
            else PutTotal;
   def CV = if IsNaN(CallTotal)
            then CV[1]
            else CallTotal;
plot ChangeRatio = if isNaN(close) then Double.NaN else PV / CV;
     ChangeRatio.AssignValueColor(if ChangeRatio > 1
                 then color.green
                 else color.red);
plot AvgCR = if isNaN(close) then Double.NaN else Average(ChangeRatio, 5);
     AvgCR.SetDefaultColor(Color.Yellow);
plot Neutral = if isNaN(close) then Double.NaN else 1;
     Neutral.SetDefaultColor(Color.Gray);
# End Study
I'm not getting the indicator on my chart... Just my MACD is showing up... help?
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Thread starter Similar threads Forum Replies Date
chewie76 HOT ZONE - RSI with IV Percentile: Buy Stock or Sell Put Options Signal For ThinkOrSwim Indicators 88

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
454 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top