Is there a way to create a label that shows the number of days that a futures contract expires? Like in this example, to display "13 Days" to expiration?
Apparently, The CountTradingDays() function doesn't really work. It still counts market closures and holidays, only ignoring weekends. It seems to simply count Mondays through Fridays within the date range.
Holidays push the expiration a day early when they both coincide. Without CountTradingDays() functioning as assumed, this might take a bit longer than I thought.
Juneteenth is throwing it off, but that's a fixed date and easy to adjust for. Good Friday is also potentially problematic, but that requires some really weird moon-math. I'll update it when I have some free time.
Also note that thinkscript sees the date at the most recent bar. Some of the really forward dated contracts might not have traded...
Not directly, not dynamically on an all-cases basis. Certain aspects would have to be hard coded to specific contracts. You can't parse out the market name space, (SYMBOL:XCME), and the day of the month varies from contract to contract.
You can, however, assemble possible symbols in a fold on the fly, checking each against GetSymbol() until it matches. You can get the year and the month this way, then work out the rest based on the contract's more specific rules.
Not directly, not dynamically on an all-cases basis. Certain aspects would have to be hard coded to specific contracts. You can't parse out the market name space, (SYMBOL:XCME), and the day of the month varies from contract to contract.
You can, however, assemble possible symbols in a fold on the fly, checking each against GetSymbol() until it matches. You can get the year and the month this way, then work out the rest based on the contract's more specific rules.
Apparently, The CountTradingDays() function doesn't really work. It still counts market closures and holidays, only ignoring weekends. It seems to simply count Mondays through Fridays within the date range.
Holidays push the expiration a day early when they both coincide. Without CountTradingDays() functioning as assumed, this might take a bit longer than I thought.
Juneteenth is throwing it off, but that's a fixed date and easy to adjust for. Good Friday is also potentially problematic, but that requires some really weird moon-math. I'll update it when I have some free time.
Also note that thinkscript sees the date at the most recent bar. Some of the really forward dated contracts might not have traded today, and that throws off the perceived date. It also can't detect when it's Saturday. Plus, with futures, it is already tomorrow's pre-market after maintenance, so the date rolls to tomorrow's calendar date at 6:00 PM today, differing by contract. Sunday's trading is Monday.
I am also not sure, as of yet, if the counter in the trade tab is accurate either. At least in terms of how it counts holidays, and whether or not the final day is one day remaining, or if it works more like 0-DTE.
I've somehow managed to match my results to the the trade tab for the current front month -- but I know that's wrong, because expiration date for that contract is the wrong date due to Juneteenth.
Unfinished, but this is what I've got so far. Only mostly works with MNQ*** for now.
Code:
declare Once_Per_Bar;
def Date =
GetYYYYMMDD();
def FindExp =
fold index = 1 to 25
with data = -1 while data < 0 do
if GetSymbol() ==
"/MNQ"
+ (
if index % 12 == 01 then "F"
else if index % 12 == 02 then "G"
else if index % 12 == 03 then "H"
else if index % 12 == 04 then "J"
else if index % 12 == 05 then "K"
else if index % 12 == 06 then "M"
else if index % 12 == 07 then "N"
else if index % 12 == 08 then "Q"
else if index % 12 == 09 then "U"
else if index % 12 == 10 then "V"
else if index % 12 == 11 then "X"
else "Z"
)
+ (
if index <= 12
then (GetYear() % 100)
else (GetYear() % 100 + 1)
)
+ ":XCME"
then index else -1;
def ExpYear =
if FindExp > 12
then (GetYear() % 100) + 1
else (GetYear() % 100);
def ExpMonth =
if FindExp > 12
then FindExp - 12
else FindExp;
def FoM =
((2000 + ExpYear) * 10000)
+ (ExpMonth * 100) + 1;
def WkD; def FRI; def DTE;
if FindExp > 0 then {
WkD = GetDayOfWeek(FoM);
FRI = 5 - WkD + 14 + FoM;
DTE = DaysTillDate(FRI);
} else {
WkD = 0;
FRI = 0;
DTE = 0;
}
Def AggCheck =
GetaggregationPeriod() <=
AggregationPeriod.DAY;
AddLabel(FindExp > 0 and AggCheck, " " +
"MNQ | " + "DTE: " + DTE
+ " | " + asPrice(Fri)
+ " " , Color.LIGHT_GREEN, 0, 2);
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.
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.