# Calculate top and bottom half of the hour

#### gavin

##### New member
Hi all, I'm trying to scan for inside hourly bars on equities with the following settings:

1. Show Extended-Hours Trading session is disabled
2. Start aggregations at market open is enabled

The problem I'm running into is that I'm unaware of a way to account for #2 above in my basic test condition: `high[1] < high[2] and low[1] > low[2]`

The problem is, that's only valid at the top half of any hour, from XX:00 - XX:29. At XX:30 - XX:59 I need to switch over to another scan, but it would be great if there's a way to account for the current time and include all conditions in one scan. Obviously this is because I'm hacking this together by aggregating two 30m bars, and the lookback period depends on how many 30m bars exist.

If anyone has an idea of how to do this or a workaround then I'd really appreciate it!

Last edited:
Solution
You can calculate the half hour of the day using code like this:
Code:
``````declare lower;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def Half_Hour_Index = Floor((epoch % D) / HalfHour);

def lower_half = Half_Hour_Index % 2;``````

lower_half will return 1 when true (when you are in the second half of the hour) and 0 when false (first half of the hour). You can then use this as a driver for which equation to use in the calculation of your study.

Hope that helps

Mashume
You can calculate the half hour of the day using code like this:
Code:
``````declare lower;

def Y = 31556926;
def M = 2629743;
def D = 86400;
def H = 3600;
def HalfHour = 1800;
def epoch = (getTime() / 1000);
def YMD = (epoch % Y) - (epoch / Y);
def month = (Floor(YMD / M) + 1);
def Half_Hour_Index = Floor((epoch % D) / HalfHour);

def lower_half = Half_Hour_Index % 2;``````

lower_half will return 1 when true (when you are in the second half of the hour) and 0 when false (first half of the hour). You can then use this as a driver for which equation to use in the calculation of your study.

Hope that helps

Mashume

Thank you Mashume!

I put it all together and it seems to be working well!

Last edited:

87k+ Posts
420 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?