Is it possible to show lunch hours on my chart?

murkr

Member
VIP
I was reading this article that goes over the importance of trading during different times of the day.
During 11:45 a.m.–1:30 p.m EST lunchtime the market trades sideways in /MES charts.

Is it possible to highlight the chart at certain times? I would like to visually see when 11:45 a.m.–1:30 p.m EST is on my chart.

It would also be great to highlight other important times as stated in that article. So that I can visually see when this time occurs.
 

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
Sure, try this code:

AddVerticalLine(secondsTillTime(0930) == 0, "RTH", color.cyan, Curve.Short_Dash);

Make sure to adjust the time.
 

murkr

Member
VIP
Sure, try this code:

AddVerticalLine(secondsTillTime(0930) == 0, "RTH", color.cyan, Curve.Short_Dash);

Make sure to adjust the time.
Thank you @BenTen ! I'm not a coder though. Can you just give me a quick explanation of setting the two times?

For example, 11:45 a.m.–1:15 p.m EST.

I think after I see how you do it I will be able to figure it out for all the rest of those times in that article.
AddVerticalLine(secondsTillTime(1145) == 0, "RTH", color.cyan, Curve.Short_Dash);
AddVerticalLine(secondsTillTime(1315) == 0, "RTH", color.cyan, Curve.Short_Dash);

I assumed this is what I had to do above, but it doesnt work. I also followed the TOS tutorial here but I couldn't figure it out.

By the way, I see this creates a verticle line, I was hoping to highlight the background of the chart. So instead of black its cyan for that particular time period. Is that possible to do? Or do I have to stick with these verticle lines?
 
Last edited:

murkr

Member
VIP
I was looking at this post to help me create verticle lines + colored clouds but I can't figure out how to edit it to measure different times.

Goal:
I want to highlight 11:45 am to 1:15 EST time frame and mark it as "lunch" that way, I can visually see on my chart when its lunchtime so I can avoid trading at that time.

Here is the code:
Code:
# +--------------------------------------------------+


# |          Highlight days around earnings          |
# |                   Robert Payne                   |
# |           http://rrpayne.blogspot.com            |
# +--------------------------------------------------+
input daysBefore = 10;
input daysAfter = 10;

input showLines = yes;
input paintBackground = yes;

DefineGlobalColor("Before Earnings", Color.YELLOW);
DefineGlobalColor("Earnings Release Date", Color.YELLOW);
DefineGlobalColor("After Earnings", Color.CYAN);
DefineGlobalColor("Fill Before", CreateColor(178, 216, 166));
DefineGlobalColor("Fill After", CreateColor(131, 191, 213));

AddVerticalLine(showLines and HasEarnings()[-daysBefore], Concat(daysBefore, " Days Before" ), GlobalColor("Before Earnings" ), Curve.FIRM);
AddVerticalLine(showLines and HasEarnings(), "Earnings!", GlobalColor("Earnings Release Date" ), Curve.FIRM);
AddVerticalLine(showLines and HasEarnings()[daysAfter], Concat(daysAfter, " Days After" ), GlobalColor("After Earnings" ), Curve.FIRM);

def before = Sum(HasEarnings(), daysBefore)[-daysBefore];
def after = Sum(HasEarnings(), daysAfter)[1];

def value1 = Double.Positive_Infinity;
def value2 = if paintBackground and before then Double.Negative_Infinity else Double.NaN;
def value3 = if paintBackground and after  then Double.Negative_Infinity else Double.NaN;

AddCloud(value1, value2, GlobalColor("Fill Before" ));
AddCloud(value1, value3, GlobalColor("Fill After" ));
 

mashume

Well-known member
VIP
Lifetime
Lunch Hour Vertical Lines:

Code:
declare upper;

def eat = 1145;
def back = 1315;

def lunch = if secondsFromTime(eat) >= 0 and secondsTillTime(back) > 0 then 1 else 0;

addVerticalLine(lunch[1] == 0 and lunch == 1, text = "LUNCH", color = Color.RED );
addVerticalLine(lunch[1] == 1 and lunch == 0, text = "OVER" , color = Color.BLUE );

and some fun with lunchtime trading ranges:
Code:
declare upper;

def eat = 1145;
def back = 1315;

def lunch = if secondsFromTime(eat) >= 0 and secondsTillTime(back) > 0 then 1 else 0;
def before_lunch = if secondsTillTime(eat) > 0 then 1 else 0;
def after_lunch = if secondsTillTime(back) >= 0 then 1 else 0;

addVerticalLine(lunch[1] == 0 and lunch == 1, text = "LUNCH", color = Color.RED );
addVerticalLine(lunch[1] == 1 and lunch == 0, text = "OVER" , color = Color.BLUE );

def lunch_h = if before_lunch == 1 then double.nan else
    if lunch == 1 and lunch[1] == 0 then high
    else if high > lunch_h[1] and lunch == 1 then high else lunch_h[1];

def lunch_l = if before_lunch == 1 then double.nan else
if lunch == 1 and lunch[1] == 0 then low
    else if low < lunch_l[1] and lunch == 1 then low else lunch_l[1];

plot lunch_max = lunch_h;
plot lunch_min = lunch_l;

addcloud(lunch_max, lunch_min, color.cyan);

def pm_breakout = if close > lunch_max then 1 else double.nan;
def pm_breakdown = if close < lunch_min then 1 else double.nan;

plot breakout = pm_breakout;
breakout.setPaintingStrategy(paintingStrategy.BOOLEAN_WEDGE_UP);

plot breakdown = pm_breakdown;
breakdown.SetPaintingStrategy(paintingStrategy.BOOLEAN_WEDGE_DOWN);

-mashume
 
Last edited:

murkr

Member
VIP
@mashume Thank you!
#1 looks very close to what I envision. Would it be possible to make a verticle cloud during that time? Similar to how they do it in this picture. Because I plan to put multiple different important time clouds on my chart. IE: this cloud will be for lunch hours. but ill have other clouds of different colors for other times. Source: https://www.thebalance.com/common-intra-day-stock-market-patterns-1031456

#2 This one is pretty cool how it measures the top and bottom during the time specified. I think I may edit that to see if I find any patterns.
 

lmk99

Active member
VIP
Hi all,

So this is perhaps kind of a weird request, but I've analyzed my trading performance and see that the vast majority of my losses occur between 9:45et and 10:15et. Perhaps because of the style of trading I'm doing - whatever the reason, I would be much more profitable if I simply didn't trade during this time span (at least until I learn how to perform well in market conditions that tend to occur at that time). It's a challenge to have the mindfulness to just stop trading for that 30 minute period, especially if I'm on a roll and up in the opening minutes. So I thought it would be a good idea if I made a script that displayed a label and/or used the AssignBackgroundColor function to basically throw up a big stop sign that I can't ignore, but have it only appear during that window of time every day and then disappear. I think it is probably possible but does anyone know how this would be coded?

EDIT: My thread was moved here and this is an awesome solution, thanks.

I had been thinking along these lines and was about to post this in my thread before it was moved:

I see this thread discusses making a timer: https://usethinkscript.com/threads/countdown-to-bar-close-in-thinkorswim.1056/

I tried this but obviously it doesn't work:

Code:
input OpenTime = 0930;
input DurationMinutes = 10 ;

def durationSec = DurationMinutes * 60;
def secondsPassed = SecondsFromTime(OpenTime);
def NoTradeZoneTime = OpenTime + durationSec ;

AddLabel(yes, (NoTradeZoneTime - OpenTime), Color.Black);

It seems that this general idea of this kind of timer displaying on a label hasn't been solved, I hadn't expected it would be so difficult to do with Thinkscript. Therefore not expecting a solution in this thread but will watch the other one.

Although actually I thought Welkin's volume tick projection study timer which counts the elapsed seconds of an aggregation period could give some insight into a workaround for my desired effect, but maybe it only works because of being on a tick chart? This is the code:

Code:
input aggregationInSeconds = 120;
def start = 0000;
def end = 1600;
def min = Floor(SecondsFromTime(start) / aggregationInSeconds);
def till = SecondsTillTime(end) / aggregationInSeconds;
def secondselapsed = (AbsValue(till - Ceil(till)) * aggregationInSeconds);
def secondsleft = aggregationInSeconds - secondselapsed;
def percntelapsed = Round(((secondselapsed / aggregationInSeconds) * 100), 0);
AddLabel(1, "Secs " + secondselapsed + "/" + aggregationInSeconds + "   " + percntelapsed + "%", CreateColor(137, 140, 55));

My idea was to change the aggregation in seconds to 10 minutes (600 seconds) so that at market open, it just counts to 600 and then at the end of that time there could be an alert sound or some other trigger. And a condition for setting that the trigger doesn't happen again/after a certain time threshold such as 9:41et (if I wanted the alert effect, whether it's audio or visual, to happen only once in the day, at 9:40et).

I can't tell if it will in fact work on a candle time period or only a tick time period right now because even on the tick period the counter is frozen, I guess because it will only increment during market hours. At least that is my assumption for why adding the above label shows it on the chart now frozen at 0/120.

The solution in this thread is a much easier and better solution to deal with.
 
Last edited:

SleepyZ

Well-known member
VIP
Lifetime
Is there a way to only add the vertical lines for the current trading session. I love this idea but I'd like to clean up the chart a bit.

You can input to show just today's lines or all. In the code, Getday() == Getlastday() defines the last day's trading.

Ruby:
declare upper;
input showtodayonly = yes;

def eat = 1145;
def back = 1315;

def lunch = if showtodayonly and getday()!=getlastday() then double.nan else if secondsFromTime(eat) >= 0 and secondsTillTime(back) > 0 then 1 else 0;

addVerticalLine(lunch[1] == 0 and lunch == 1, text = "LUNCH", color = Color.RED );
addVerticalLine(lunch[1] == 1 and lunch == 0, text = "OVER" , color = Color.BLUE );
 

naveed505

New member
@mashume Thank you!
#1 looks very close to what I envision. Would it be possible to make a verticle cloud during that time? Similar to how they do it in this picture. Because I plan to put multiple different important time clouds on my chart. IE: this cloud will be for lunch hours. but ill have other clouds of different colors for other times. Source: https://www.thebalance.com/common-intra-day-stock-market-patterns-1031456

#2 This one is pretty cool how it measures the top and bottom during the time specified. I think I may edit that to see if I find any patterns.
Did you end up getting the clouds between the lunch hours? I have also been searching for this.
Thanks!
 

mashume

Well-known member
VIP
Lifetime
I hadn't previously, but have now:
set your times appropriately... east coast time, 24h format.

Code:
declare upper;

input eat = 1145;
input back = 1330;

def lunch = if secondsFromTime(eat) >= 0 and secondsTillTime(back) > 0 then 1 else 0;

plot lunch_top = if lunch == 1 then double.POSITIVE_INFINITY else double.nan;
plot lunch_bottom = if lunch == 1 then double.NEGATIVE_INFINITY else double.nan;
AddCloud(data1 = lunch_top, data2 = lunch_bottom, color2 = Color.YELLOW);

-mashume
 

SleepyZ

Well-known member
VIP
Lifetime
Did you end up getting the clouds between the lunch hours? I have also been searching for this.
Thanks!

This will add clouds. I made them red so you can see them initally. Change them to what you want either within the code or the input screen.


Ruby:
declare upper;
input showtodayonly = yes;

input eat  = 1145;
input back = 1315;

def lunch = if showtodayonly and GetDay() != GetLastDay() then Double.NaN else if SecondsFromTime(eat) >= 0 and SecondsTillTime(back) > 0 then 1 else 0;

AddVerticalLine(lunch[1] == 0 and lunch == 1, text = "LUNCH", color = Color.RED );
AddVerticalLine(lunch[1] == 1 and lunch == 0, text = "OVER" , color = Color.BLUE );

defineGlobalColor("Lunch", color.red);
AddCloud(if SecondsFromTime(eat) >= 0 and SecondsFromTime(back) <= 0
         then Double.POSITIVE_INFINITY else Double.NaN,
         Double.NEGATIVE_INFINITY, globalColor("Lunch"), globalColor("Lunch"));
 

naveed505

New member
This will add clouds. I made them red so you can see them initally. Change them to what you want either within the code or the input screen.


Ruby:
declare upper;
input showtodayonly = yes;

input eat  = 1145;
input back = 1315;

def lunch = if showtodayonly and GetDay() != GetLastDay() then Double.NaN else if SecondsFromTime(eat) >= 0 and SecondsTillTime(back) > 0 then 1 else 0;

AddVerticalLine(lunch[1] == 0 and lunch == 1, text = "LUNCH", color = Color.RED );
AddVerticalLine(lunch[1] == 1 and lunch == 0, text = "OVER" , color = Color.BLUE );

defineGlobalColor("Lunch", color.red);
AddCloud(if SecondsFromTime(eat) >= 0 and SecondsFromTime(back) <= 0
         then Double.POSITIVE_INFINITY else Double.NaN,
         Double.NEGATIVE_INFINITY, globalColor("Lunch"), globalColor("Lunch"));
This is perfect! Thank you! How do I do it so it's highlighted only for the current day?
 
Last edited:

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