Max pain code

Solution
I have updated the Max Pain script, and so far so good.

I have included a Shared Link to the code below if anyone would like to check it out. The Study calculates the Max Pain Price for the underlying asset, based on the Expiration Date provided (I have learned that one must enter the day after the actual expiration date at the study's input). I have only included 11 Options above ATM and 11 Options below ATM because I intend to use this the day of, or maybe a day or two before expiration, maximum. Anything beyond that may require coding in more Options on either side of the ATM strike (although I have chosen expirations out as far as 06.06.2023 and the Max Pain Price is showing up properly). The study also adds a label to the chart...
per https://www.investopedia.com/terms/m/maxpain.asp :

"""
For each in-the-money strike price for both puts and calls:
  1. Find the difference between stock price and strike price
  2. Multiply the result by open interest at that strike
  3. Add together the dollar value for the put and call at that strike
  4. Repeat for each strike price
  5. Find the highest value strike price. This price is equivalent to max pain price.
"""

I didn't write this... just quoted it. I've no idea how to code it. I don't trade options myself, though there are plenty of thinkscript coders who do and may be able to offer insight into how to go about doing this for all of the strike prices. Looping through things in ToS is not a piece of cake.

Happy Trading
-mashume
 
Has anyone had any issues with the GetATMOption method? I am writing a Max Pain script, and for the most part, it is working pretty well, but there are some random Expiration Dates that the GetATMOption method just will not detect, but they exist in the Options Chain.

This code is pretty simple, yet it does not work. Does anyone else have trouble with this snippet of code? It works if I set the expiration to 20230531, but not 20230530, however, according to the Options Chain in TOS, there is an entire series of Options with an expiration date of 20230530. This is really the only issue I have left to work out with the script I have written. Once I get it ironed out I will post it, in the event anyone else wants it.

input expirationDate = 20230530;
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), expirationDate, OptionClass.PUT));
 
Has anyone had any issues with the GetATMOption method? I am writing a Max Pain script, and for the most part, it is working pretty well, but there are some random Expiration Dates that the GetATMOption method just will not detect, but they exist in the Options Chain.

This code is pretty simple, yet it does not work. Does anyone else have trouble with this snippet of code? It works if I set the expiration to 20230531, but not 20230530, however, according to the Options Chain in TOS, there is an entire series of Options with an expiration date of 20230530. This is really the only issue I have left to work out with the script I have written. Once I get it ironed out I will post it, in the event anyone else wants it.

input expirationDate = 20230530;
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), expirationDate, OptionClass.PUT));

please post complete codes.
doing so, really does help others help you debug. and it may have helped you see what is happening.
don't be bashful. everyone started out not knowing anything.

you say it is simple, but yet it doesn't work for you.... so it isn't simple.


tip1 , test codes on a normal week. don't test codes on a holiday week. the holiday may mess up/confuse you.

tip2 , if something doesn't work, add more labels / bubbles to display some values and see what is going on.


answer, specify a date that is the day after the desired option date.



Code:
# options_atm4_daily

#https://usethinkscript.com/threads/thinkscript-not-detecting-option.15630/
#Thinkscript not detecting Option

#input Date1 = 20230530;
#AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date1, OptionClass.PUT), color.yellow);

#input Date2 = 20230531;
#AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date2, OptionClass.PUT), color.yellow);

#input Date3 = 20230601;
#AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date3, OptionClass.PUT), color.yellow);

#input Date4 = 20230602;
#AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date4, OptionClass.PUT), color.yellow);



input Date6 = 20230605;
addlabel(1, " ", color.black);
addlabel(1, "date6 " + asprice(date6), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date6, OptionClass.PUT), Color.YELLOW);

input Date7 = 20230606;
addlabel(1, " ", color.black);
addlabel(1, "date7 " + asprice(date7), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date7, OptionClass.PUT), Color.YELLOW);

input Date8 = 20230607;
addlabel(1, " ", color.black);
addlabel(1, "date8 " + asprice(date8), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date8, OptionClass.PUT), Color.YELLOW);

input Date9 = 20230608;
addlabel(1, " ", color.black);
addlabel(1, "date9 " + asprice(date9), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date9, OptionClass.PUT), Color.YELLOW);

input Date10 = 20230609;
addlabel(1, " ", color.black);
addlabel(1, "date10 " + asprice(date10), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date10, OptionClass.PUT), Color.YELLOW);
#

purple date is the data on input code line
yellow is the option symbol. notice the 1 day difference.
qca8e9G.jpg
 
Last edited:
Th
i'm going to repeat myself again,

please post complete codes.
doing so, really does help others help you debug. and it may have helped you see what is happening.
don't be bashful. everyone started out not knowing anything.

you say it is simple, but yet it doesn't work for you.... so it isn't simple.


tip1 , test codes on a normal week. don't test codes on a holiday week. the holiday may mess up/confuse you.

tip2 , if something doesn't work, add more labels / bubbles to display some values and see what is going on.


answer, specify a date that is the day after the desired option date.



Code:
# options_atm4_daily

#https://usethinkscript.com/threads/thinkscript-not-detecting-option.15630/
#Thinkscript not detecting Option

#input Date1 = 20230530;
#AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date1, OptionClass.PUT), color.yellow);

#input Date2 = 20230531;
#AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date2, OptionClass.PUT), color.yellow);

#input Date3 = 20230601;
#AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date3, OptionClass.PUT), color.yellow);

#input Date4 = 20230602;
#AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date4, OptionClass.PUT), color.yellow);



input Date6 = 20230605;
addlabel(1, " ", color.black);
addlabel(1, "date6 " + asprice(date6), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date6, OptionClass.PUT), Color.YELLOW);

input Date7 = 20230606;
addlabel(1, " ", color.black);
addlabel(1, "date7 " + asprice(date7), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date7, OptionClass.PUT), Color.YELLOW);

input Date8 = 20230607;
addlabel(1, " ", color.black);
addlabel(1, "date8 " + asprice(date8), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date8, OptionClass.PUT), Color.YELLOW);

input Date9 = 20230608;
addlabel(1, " ", color.black);
addlabel(1, "date9 " + asprice(date9), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date9, OptionClass.PUT), Color.YELLOW);

input Date10 = 20230609;
addlabel(1, " ", color.black);
addlabel(1, "date10 " + asprice(date10), color.magenta);
AddLabel(IsOptionable(), GetATMOption(GetUnderlyingSymbol(), Date10, OptionClass.PUT), Color.YELLOW);
#

purple date is the data on input code line
yellow is the option symbol. notice the 1 day difference.
qca8e9G.jpg
Thanks for the feedback! I def see that it would have been better to post the entire script, but it’s roughly 800 lines of code. I’ll post it in the morning. I did eventually work out the date issue you mentioned, and the way I got around it was by using the ‘GetNextExpirationOption’ method. I used that throughout my script, that way the correct date could be entered. Seeing that manually entering the date as a day prior seems to work based on your code, but without the use of the GetNextExpirationOption method, it may be something with that method that is causing the issue for me. Again, I’ll post it in the morning in its entirety, and if you have some more insight after reviewing it, that would be great.

Thanks!
 
Th

Thanks for the feedback! I def see that it would have been better to post the entire script, but it’s roughly 800 lines of code. I’ll post it in the morning. I did eventually work out the date issue you mentioned, and the way I got around it was by using the ‘GetNextExpirationOption’ method. I used that throughout my script, that way the correct date could be entered. Seeing that manually entering the date as a day prior seems to work based on your code, but without the use of the GetNextExpirationOption method, it may be something with that method that is causing the issue for me. Again, I’ll post it in the morning in its entirety, and if you have some more insight after reviewing it, that would be great.

Thanks!
o , well if it's that long, you don't need to post it. using the next expiration day isn't the same as specifying the day after, so it won't always work.
 
o , well if it's that long, you don't need to post it. using the next expiration day isn't the same as specifying the day after, so it won't always work.
Got it. Makes sense after I think about it in more detail. I will just change the code back to the original, and I will enter the day prior to expiration. Thanks!
 
I have updated the Max Pain script, and so far so good.

I have included a Shared Link to the code below if anyone would like to check it out. The Study calculates the Max Pain Price for the underlying asset, based on the Expiration Date provided (I have learned that one must enter the day after the actual expiration date at the study's input). I have only included 11 Options above ATM and 11 Options below ATM because I intend to use this the day of, or maybe a day or two before expiration, maximum. Anything beyond that may require coding in more Options on either side of the ATM strike (although I have chosen expirations out as far as 06.06.2023 and the Max Pain Price is showing up properly). The study also adds a label to the chart that displays the current deviation of the asset's price vs. the Max Pain Price, as well as a label displaying the current ATM option, just for verification of the date and to provide peace of mind that the script is tracking the current asset price properly.

Please check it out and let me know what you think.

Thanks!

PS. The Study takes a min to paint initially since the script is very long, but it does work. If you do not have plenty of memory in the PC, as well as RAM allocated to TOS, it may not perform well.

http://tos.mx/cQxakcd

Max Pain for Expiration of 05.30.2023:

Screenshot of SPY Chart:

1685448303525.png


This is a Max Pain Chart from:
https://optioncharts.io/options/SPY/chain/2023-05-30w/chart/max-pain

1685448348368.png




Max Pain for Expiration of 06.06.2023:

Screenshot of SPY Chart

1685448509948.png


This is a Max Pain Chart from:
https://optioncharts.io/options/SPY/chain/2023-05-30w/chart/max-pain

1685448561869.png
 
Last edited:
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
374 Online
Create Post

Similar threads

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