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

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:

Similar threads

Top