# Max pain code

#### piyushkp

##### New member
can someone refer how to calculate option 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.

-mashume

@piyushkp I am not sure if this can be done on the ToS platform.
Here is a video for doing the manual calculations in excel:

Last edited:
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;

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;

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

#Thinkscript not detecting Option

#input Date1 = 20230530;

#input Date2 = 20230531;

#input Date3 = 20230601;

#input Date4 = 20230602;

input Date6 = 20230605;
addlabel(1, "date6 " + asprice(date6), color.magenta);

input Date7 = 20230606;
addlabel(1, "date7 " + asprice(date7), color.magenta);

input Date8 = 20230607;
addlabel(1, "date8 " + asprice(date8), color.magenta);

input Date9 = 20230608;
addlabel(1, "date9 " + asprice(date9), color.magenta);

input Date10 = 20230609;
addlabel(1, "date10 " + asprice(date10), color.magenta);
#``````

purple date is the data on input code line
yellow is the option symbol. notice the 1 day difference.

Last edited:
Th
i'm going to repeat myself again,

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

#Thinkscript not detecting Option

#input Date1 = 20230530;

#input Date2 = 20230531;

#input Date3 = 20230601;

#input Date4 = 20230602;

input Date6 = 20230605;
addlabel(1, "date6 " + asprice(date6), color.magenta);

input Date7 = 20230606;
addlabel(1, "date7 " + asprice(date7), color.magenta);

input Date8 = 20230607;
addlabel(1, "date8 " + asprice(date8), color.magenta);

input Date9 = 20230608;
addlabel(1, "date9 " + asprice(date9), color.magenta);

input Date10 = 20230609;
addlabel(1, "date10 " + asprice(date10), color.magenta);
#``````

purple date is the data on input code line
yellow is the option symbol. notice the 1 day difference.
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:

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

Max Pain for Expiration of 06.06.2023:

Screenshot of SPY Chart

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

Last edited:
can someone refer how to calculate option max pain code ?

did you create thinkscript ?

i tried the code its showign as N/A for SPY, spx, AAPL for 20240503 input date - Appreciate the response - Raj

Last edited by a moderator:
did you create thinkscript ?

i tried the code its showign as N/A for SPY, spx, AAPL for 20240503 input date - Appreciate the response - Raj

You can click on a member's name to determine if they are still active on the forum.
@piyushkp has not.

87k+ Posts
257 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?