Market Hours Thinkscript

Does anyone have a thinkscript to identify when the current symbol is about to close? Some sort of label indicating mkt closes in x mins or at x time. Specific for futures because market has differing hours.

i.e. the trading hours for Wheat Futures (/ZW) are as follows: Sunday to Friday: 7:00 PM to 7:45 AM Central Time (CT) with a 45-minute break each day from 7:45 AM to 8:30 AM CT Monday to Friday: 8:30 AM to 1:20 PM CT

the trading hours for Gold Futures (/GC) are as follows: Sunday to Friday: 5:00 PM to 4:00 PM Central Time (CT) with a 60-minute break each day from 4:00 PM to 5:00 PM CT I couldn't find anything on the usethinkscript website

I am looking for a script to take the current symbol and let me know how long until the market closes.

Thanks in advance!
 
Does anyone have a thinkscript to identify when the current symbol is about to close? Some sort of label indicating mkt closes in x mins or at x time. Specific for futures because market has differing hours.

i.e. the trading hours for Wheat Futures (/ZW) are as follows: Sunday to Friday: 7:00 PM to 7:45 AM Central Time (CT) with a 45-minute break each day from 7:45 AM to 8:30 AM CT Monday to Friday: 8:30 AM to 1:20 PM CT

the trading hours for Gold Futures (/GC) are as follows: Sunday to Friday: 5:00 PM to 4:00 PM Central Time (CT) with a 60-minute break each day from 4:00 PM to 5:00 PM CT I couldn't find anything on the usethinkscript website

I am looking for a script to take the current symbol and let me know how long until the market closes.

Thanks in advance!


i think this will do what you asked for,
i am not familiar with futures or their trading hours.

display labels that show how many minutes from a start time and minutes to a stop time.
does the opposite if current bar is not in the desired time period.

it uses RegularTradingStart(GetYYYYMMDD() to determine the normal trading hours of the symbol.

the smallest time increment is whatever the chart is set to.

times are in 24 hour mode
230 is am , not pm. 1430 would be 2:30pm.
can pick the local timezone.

can pick start and stop times from,
...chart symbol
...enter 2 numbers
input time_period = { default chart , start_stop_times }

time period can span over midnight

draws dots above the bars that are within the time period

test symbols
GE, SPY, /CL, /ZW, /GC, /ES

turn on extended hours

can turn on bubbles for testing

Code:
# min_left_in_day

# https://usethinkscript.com/threads/market-hours-thinkscript.15454/
#Market Hours Thinkscript
#Does anyone have a thinkscript to identify when the current symbol is about to close? 

#----------------------

script hrmin {
 input m = 1;
 input zone = 0;
 # localhouradj
 # def hr1 = m/60;
 def hr1 = (m+(zone*60))/60;
 def hr2 = floor(hr1);
 def min2 = (hr1-hr2)*60;
 plot hr = hr2;
 plot min = min2;
}

#-----------------------

def na = double.nan;
def bn = barnumber();

# -------------------
# misc
def lastbar = (!isnan(close) and isnan(close[-1]));
def lastcls = if isnan(close) then lastcls[1] else close;

def newday = getday() <> getday()[1];
#def newweek = getweek() <> getweek()[1];
def currentday = (getday() == getlastday());
#def currentweek = (Getweek() == Getlastweek());

# minutes from midnight
def t0 = 0;
def t = secondsfromTime(t0);
def m = t/60;

# where to look for period start and stop times
# 2 day before current day
def days_back_date = 2;
def dayx = (getday() + days_back_date) == getlastday();

# restrict calculations to just recent days
def recent = (getlastday() - getday()) <= days_back_date;

#-------------------------

input timezone = { "EST", default "CST", "MST", "PST"};

#def starthour  = (if timezone == timezone."EST" then 9
#  else if timezone == timezone."CST" then 8
#  else if timezone == timezone."MST" then 7
#  else 6) ;

def localhouradj  = (if timezone == timezone."EST" then 0
  else if timezone == timezone."CST" then -1
  else if timezone == timezone."MST" then -2
  else -3);

#-----------------------

def hr_est = hrmin(m).hr;
def min_est = hrmin(m).min;

def hr_local = hrmin(m, localhouradj).hr;
def min_local = hrmin(m, localhouradj).min;

#-----------------------

# chart symbol trading hours , or some manual entered times
input time_period = { default chart , start_stop_times };

# open/close times  (EST)
input start = 0930;
input end = 1600;

def manualtime;
if end > start then {
 manualtime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;
} else {
 manualtime = if (secondsfromTime(start) >= 0 and secondstillTime(2359) >= 0) or (secondsfromTime(0) >= 0 and secondstillTime(end) > 0) then 1 else 0;
}

# daytime_regstartstop
def charttime = (GetTime() >= RegularTradingStart(GetYYYYMMDD()) and GetTime() < RegularTradingEnd(GetYYYYMMDD()));
def daytime = if time_period == time_period.chart then charttime else manualtime;

#------------------------
# m = minutes after midnight

def startm = if bn == 1 then 0 else if dayx and !daytime[1] and daytime then m else startm[1];
def endm = if bn == 1 then 0 else if dayx and daytime[1] and !daytime[0] then m else endm[1];

# EST times
def hr_st = hrmin(startm).hr;
def min_st = hrmin(startm).min;
def hr_end = hrmin(endm).hr;
def min_end = hrmin(endm).min;

# local times
def local_hr_st = hrmin(startm, localhouradj).hr;
def local_min_st = hrmin(startm, localhouradj).min;
def local_hr_end = hrmin(endm, localhouradj).hr;
def local_min_end = hrmin(endm, localhouradj).min;

#------------------------

def minutes_period = if endm > startm then (endm - startm) 
 else if endm < startm then ((endm + 1440) - startm) 
 else 0;

def periodhr = hrmin(minutes_period).hr;
def periodmin = hrmin(minutes_period).min;

#-----------------------

# minutes from a time
def minafterstart = if daytime and m > startm then (m - startm) 
 else if daytime and m < startm then ((m + 1440) - startm) 
 else 0;

def minbeforeend = if daytime and endm > startm then (endm - m) 
 else if daytime and endm < startm then ((endm + 1440) - m) 
 else 0;

def minafterend = if !daytime and m > endm then (m - endm) 
 else if !daytime and m < endm then ((m + 1440) - endm) 
 else 0;

def minbeforestart = if !daytime and startm > m then (startm - m)
 else if !daytime and startm < m then ((startm + 1440) - m)
 else 0;


#------------------------

addlabel(1, " ", color.black);
addlabel(1, (hr_st + ":" + min_st + " to " + hr_end + ":" + min_end) + "  EST", color.magenta);

addlabel(1, " ", color.black);
addlabel(1, "period minutes " + minutes_period, color.cyan);
addlabel(1, periodhr + ":" + periodmin, color.cyan);

addlabel(1, " ", color.black);
addlabel(1, (local_hr_st + ":" + local_min_st + " to " + local_hr_end + ":" + local_min_end) + "  " + timezone, color.yellow);

addlabel(1, " ", color.black);
addlabel(daytime, minafterstart + " minutes after period start", color.yellow);
addlabel(daytime, minbeforeend + " minutes until period end", color.yellow);

addlabel(!daytime, minafterend + " minutes after period end", color.gray);
addlabel(!daytime, minbeforestart + " minutes before period start", color.gray);

#--------------------------

input test_daytime_dots = yes;
plot zd = if test_daytime_dots and daytime then high * 1.004 else Double.NaN;
zd.SetPaintingStrategy(PaintingStrategy.POINTS);
zd.SetDefaultColor(Color.WHITE);
# x.setlineweight(1);
# x.hidebubble();


input test1 = no;
addchartbubble(test1, low*0.999,
dayx + " D\n" +
currentday + " c\n" +
recent + " r"
, (if dayx then color.magenta else if recent then color.yellow else color.gray), no);


input test2_stats = no;
addchartbubble(test2_stats and recent, low*0.999,
#dayx + "\n" +
m + " m\n" +
(hr_est + ":" + min_est) + " EST\n" +
(hr_local + ":" + min_local) + " " + timezone + "\n" +
startm + " st\n" +
endm + " end\n" +
minafterstart + " aftst\n" + 
minbeforeend + " toend\n" + 
minafterend + " aftend\n" + 
minbeforestart + " tost\n" 
, (if daytime then color.yellow else color.gray), no);
#

bar is within the time period
SPY 5 min
yellow labels
zxskSir.jpg


bar is not within the time period
/CL 5 min
gray labels
zXRJJGD.jpg
 

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
382 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