this returns a strike price, you give it 3 parameters and a strike price will be returned

Russell_

New member
# input the nearest Strike you want to find, supply the StopStrike, and Expiration Date
# example: GetNearestStrikeAtOrAbove(221,230,"211126", returns 225
# give the return result up to 1 minute for results
script GetNearestStrikeAtOrAbove {
input Strike = 0; # enter the price you want to find for the nearest srike
input StopStrike = 0; # stop looking for the option once you hit this price
input ExpirationDate = "YYMMDD";


def OptionStrike = fold i = Strike to StopStrike with
OptStrike=0
while OptStrike==0 do
if IsNaN(close(GetNextOTMOption("."+GetUnderlyingSymbol()+ExpirationDate+"C"+Round(i,0)))) then 0 else i;
plot StrikePrice = OptionStrike;
}


# input the nearest Strike you want to find, supply the StopStrike, and Expiration Date
# example: GetNearestStrikeAtOrBelow(200,212,"211126", returns 200
# give the return result up to 1 minute for results
script GetNearestStrikeAtOrBelow {
input Strike = 0; # enter the price you want to find for the nearest srike
input StopStrike = 0; # stop looking for the option once you hit this price
input ExpirationDate = "YYMMDD";

def OptionStrike = fold i = Strike to Stopstrike with
OptStrike=0
while OptStrike==0 do
if IsNaN(close(GetNextOTMOption("."+GetUnderlyingSymbol()+ExpirationDate+"P"+Round(i,0)))) then 0 else i;
plot StrikePrice = OptionStrike;
}

AddLabel(yes,"GetNearestStrikeAtOrAbove="+GetNearestStrikeAtOrAbove(221,230,"211126"),color.white);

AddLabel(yes,"GetNearestStrikeAtOrBelow="+GetNearestStrikeAtOrBelow(200,212,"211126"),color.white);
 

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

my code above relies on GetNextOTMOption() not returning anything if the strike price provided isn't valid. The routines work horribly. The for loop in thinkscript doesn't provide descending values, only ascending making it a mess to find the strikes moving down the option ladder. The GetNextOTMOption() is slower than molasses in January when a bad strike is provided to generate failure, appears its having to time out before it eventually returns nothing. It is not returning NaN from what I can tell, it's just returning "blank" or Null (i suppose) no way to tell. I've not worked in thinkscript much working in C/C++ for over 35 years, thinkscript is almost good. What I'm trying to get thinkscript to do is provide the options inside the graph with lines representing the option pricing running through the graph in to the expiration date that way it's integrated and I can tell what option will have the most upside (or downside) against the price data of the symbol chosen. And I can "almost" get it to work.

This code provides date information that technically should be passed as parameters in to my script "subroutines" but, thinkscript can't seem to correctly pass , the parameters, part of the issue is I can't discern when a variable "def" is a single value or a series of values. Without a debugger it's difficult to nearly impossible to see what that def value is representing. Anyways, here is the code that provides the date information that should be passable in to the scripts: This variable "OptDateYYMMDD" should be passable into the script as the coming up expiration date/nearest expiration date, but, while it looks okay printed as a label (except) they are showing a comma, and i don't know if this comma is in the variable or merely displaced. Anyways, when I pass this by changing the input parameter to a double by removing the ""YYMMDD" and just doing a "input ExpirateDate = 0", it won't work, so not sure what's up with that. I'm kinda suspecting that it's because it's making the date look like "211,126" when passed to GetNextOTMOption(). I don't know, I need to contact TDAmeritrade and work there on there thinkscript code and fix it for them to make it good, I'd do it for free for a few months to add string handling and making the option primitives usable. I don't know who is in charge of there development now but, if someone knows mail it to me so I can get over there and fix this stuf.


input series = 1;
input showBands = no;
input show_itm_labels = yes;
input show_atm_labels = yes;
input show_otm_labels = yes;
input Days_In_Contract = 0;
Assert(series > 0, "'series' must be positive: " + series);

def RTHopen = open(period = AggregationPeriod.Day);
def CurrentYear = GetYear();
def CurrentMonth = GetMonth();
def CurrentDOM = GetDayOfMonth(GetYYYYMMDD());
def Day1DOW1 = GetDayOfWeek(CurrentYear * 10000 + CurrentMonth * 100 + 1); # First DOM is this DOW
def FirstFridayDOM1 = if Day1DOW1 < 6
then 6 - Day1DOW1
else if Day1DOW1 == 6
then 7
else 6;

def SecondFridayDOM = FirstFridayDOM1 + 7;
def ThirdFridayDOM = FirstFridayDOM1 + 14;
def FourthFridayDOM = FirstFridayDOM1 + 21;
def RollDOM = FirstFridayDOM1 + 21; #14; changed to 21 to pick up all Fridays of the current month for weekly options
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 = if currentDOM < FirstFridayDOM -1
then FirstFridayDOM1
else if between(currentDOM, FirstFridayDOM, SecondFridayDOM-1)
then SecondFridayDOM
else if between(currentDOM, SecondFridayDOM, ThirdFridayDOM-1)
then ThirdFridayDOM
else if between(currentDOM, ThirdFridayDOM, FourthFridayDOM-1)
then FourthFridayDOM
else FirstFridayDOM;

def NextFriday = DaysTillDate(ExpYear * 10000 + ExpMonth2 * 100 + ExpDOM);
def ExpirationDate = GetYYYYMMDD() + NextFriday;
def ExpData = (ExpirationDate / 1) + 1;
def yr = Round(GetYear() / 100, 0);
def yr2 = GetYear() - 2000;
def OptionDateString = ExpYear * 10000 + ExpMonth2 * 100 + ExpDOM + 1;
def OptDateYYMMDD = yr2 * 10000 + ExpMonth2 * 100 + ExpDOM;
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
514 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