Getting the OI of the options contract that's closest to the volume profile POC - way to make the value retained/stable, instead of toggling as "N/A"?

lmk99

Active member
VIP
Hi everyone,

This shared chart has debug labels showing the problem: https://tos.mx/DZl33Ve

Alternatively, this is the code:

Code:
declare lower;


############################## Volume Profile for creating points of control as strike price anchors

input pricePerRowHeightMode = {default TICKSIZE, AUTOMATIC}; #{default TICKSIZE, AUTOMATIC};
input timePerProfile = {default Bar, Chart}; #{default Bar, CHART};
def onExpansion = no;
def profile_period;
switch (timePerProfile) {
case CHART:
profile_period = 0;
case Bar:
profile_period = BarNumber() - 1;}
rec count1 = if profile_period != profile_period[1] then (count1[1] + profile_period - profile_period[1]) % 2 else count1[1];
def cond1 = count1 < count1[1] + profile_period - profile_period[1];


def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
height = PricePerRow.AUTOMATIC;
case TICKSIZE:
height = PricePerRow.TICKSIZE;}
profile vol1 = VolumeProfile("startNewProfile" = cond1, "onExpansion" = onExpansion, "numberOfProfiles" = 6, "pricePerRow" = height, "value area percent" = 68);


def pc1 = vol1.GetPointOfControl();
def x= if isNan(close) then double.nan else if !IsNaN(pc1) then pc1 else x[1];
def pc1_offset = x[3]; #this makes a previous VPOC get used to avoid repainting issues

def pc1_recursive;
if ( cond1 ) {
  pc1_recursive = pc1_offset;
}
else {
  pc1_recursive = pc1_recursive[1];
}

############################## Debug labels:

Addlabel(yes,"pc1_recursive: "+pc1_recursive,color.cyan);

##############################

# Strike Definitions



def Strike_POC_C_switch =  pc1_recursive;


Addlabel(yes,"Strike_POC_C_switch: "+Strike_POC_C_switch,color.cyan);

def Strike_POC_C = Round(Strike_POC_C_switch, 0) ;

Addlabel(yes,"Strike_POC_C: "+Strike_POC_C,color.cyan);


# At The Money


def ATM_OI_C_A = open_interest(".SPY220930C"+ Strike_POC_C, aggregationPeriod.DAY); ######### If "Strike_POC_C" is replaced with a strike price like "361" then the OI displays correctly on the label. But if the "Strike_POC_C" variable is used to provide the strike price, the OI displays alternatingly as the correct value and as "N/A" which shows that the recursion to make Strike_POC_C retain its value isn't working. *The reason this confuses me is that the preceding label for "Strike_POC_C" shows the correct value and is stable, not vacillating between showing N/A.*

#Strike_POC_C

Addlabel(yes,"ATM_OI_C_A: "+ATM_OI_C_A,color.cyan);
#AddVerticalline(close>0,"OPRA: "+("." + "SPY" + Concat((2), "") + Concat((2), "") + Concat((0), "") + Concat((9), "") + Concat((3), "") + Concat((0), "") + "C" + Strike_POC_C));

def ATMOIC_A;
if ( cond1 ) {
  ATMOIC_A = ATM_OI_C_A;
}
else {
  ATMOIC_A = ATMOIC_A[1];
}

Addlabel(yes,"ATMOIC_A: "+ATMOIC_A,color.cyan);

It's really confusing to me why the labels that should display the OI for the contract closest to the VPOC keep togging between showing the OI correctly and showing "N/A." The label that displays the value for "Strike_POC_C," which is providing the strike price for which the OI is being pulled, has a stable looking (not flickering/toggling to N/A) and correct value itself. So why does the OI display correctly when the strike price integer (e.g. "370") is manually plugged in, but flicker between N/A when the variable "Strike_POC_C" is used instead?

I think it could be very useful and interesting to use the VPOC as an anchor variable for referencing contract OI in studies, among other things. I tried asking about this in the TOS Thinkscript Lounge chat but after some of them looked at this, and didn't see what was causing the problem, they suggested that I should contact support. But support will probably just send me back to them and I think someone here is probably knowledgeable enough to see what's going wrong. Maybe it's a problem with using "cond1" as the condition for retaining the OI value in the variable definition "ATMOIC_A" but I don't see why that wouldn't work because that is the condition for generating a new volume profile after every two bars, and the volume profile is plotting correctly.
 

Similar threads

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.
Top