# How to code past x time Aggregation period ?

##### New member
Hello ,

Need help with Aggregation period to search for current time minus desired time

For example need to code this

input Agg= AggregationPeriod.Four_hours;

As

input Agg = "last 4 hours from now "

Any help is appreciated

Hello ,

Need help with Aggregation period to search for current time minus desired time

For example need to code this

input Agg= AggregationPeriod.Four_hours;

As

input Agg = "last 4 hours from now "

Any help is appreciated

Hopefully this provides some useful information for you.

The first part with data and data1 @4hr agg defines the value of the bar prior to the current 4hr agg

The second part with x and xx finds the value of the candle 4hrs prior to that based upon the agg of the chart being used.

The last part of Time bubbles shows the current time of the last candle and that of 4hrs prior to the last candle.

Ruby:
``````#This displays the bar's value at the 4hr agg prior to the current 4hr bar
input Agg  = AggregationPeriod.FOUR_HOURS;
def data  = high(period = Agg);
def data1 = high(period = Agg)[1];
AddLabel(1, "High @prior 4hr agg candle: " + data1, Color.YELLOW);

#x=last bar being displayed
#xx= high @bar 4hrs before x
def x   = if !IsNaN(close) and IsNaN(close[-1]) then BarNumber() else 0;
plot xx = if BarNumber() == (HighestAll(x) - 4 * 60 / (GetAggregationPeriod() / 60000)) then high else Double.NaN;
xx.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
AddLabel(1, "High 4hrs ago based upon chart agg: " + HighestAll(xx), Color.WHITE);

#######
#Example Time_Gettime
#Sleepz 20210315
#Usethinkscript request

#Time Stamp based upon Gettime and RegularTradingStart. Choose timezone
input timezone = {default "ET", "CT", "MT", "PT"};

def starthour  = (if timezone == timezone."ET"
then 9
else if timezone == timezone."CT"
then 8
else if timezone == timezone."MT"
then 7
else 6) ;
def hour = Floor(((starthour * 60 + 30) + (GetTime() - RegularTradingStart(GetYYYYMMDD())) / 60000) / 60);
def minutes = (GetTime() - RegularTradingStart(GetYYYYMMDD())) / 60000 - ((hour - starthour) * 60 + 30) + 60;

AddChartBubble(IsNaN(close[-1]), low, "Time:" + hour + ":" + (if minutes < 10 then "0" else "") + minutes, Color.WHITE, no);
AddChartBubble(IsNaN(close[-1]), low, "Time 4hrs ago :" + (hour - 4) + ":" + (if minutes < 10 then "0" else "") + minutes, Color.WHITE, no);``````

I will try to use this in my code, Thanks for helping me out ! really appreciate it !!

87k+ Posts
134 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?