Time Based Ranges Question

mattmar10

New member
So I have coded up a simple levels study based on the Daily Range. It is working well , but I am wondering, is it possible to adapt this code to use a specific time range (e.g., first hour of trading) instead of the entire day range?

Rich (BB code):
def StDevDay = StDev(high(period = AggregationPeriod.DAY)[1] - low(period = AggregationPeriod.DAY)[1], length);


plot PTL1 = open(period = AggregationPeriod.DAY) + .5 * StDevDay;
plot PTS1 = open(period = AggregationPeriod.DAY) - .5 * StDevDay;

plot PTL2 = open(period = AggregationPeriod.DAY) + StDevDay;
plot PTS2 = open(period = AggregationPeriod.DAY) - StDevDay;

plot PTL3 = open(period = AggregationPeriod.DAY) + (2 * StDevDay);
plot PTS3 = open(period = AggregationPeriod.DAY) - (2 * StDevDay);

plot PTL4 = open(period = AggregationPeriod.DAY) + (3 * StDevDay);
plot PTS4 = open(period = AggregationPeriod.DAY) - (3 * StDevDay);

plot PTL5 = open(period = AggregationPeriod.DAY) + (4 * StDevDay);
plot PTS5 = open(period = AggregationPeriod.DAY) - (4 * StDevDay);


plot Open = open(period = AggregationPeriod.DAY);


Open.SetDefaultColor(Color.WHITE);


PTS1.SetDefaultColor(Color.WHITE);
PTS2.SetDefaultColor(Color.GREEN);
PTS3.SetDefaultColor(Color.GREEN);
PTS4.SetDefaultColor(Color.GREEN);
PTS5.SetDefaultColor(Color.GREEN);
PTL1.SetDefaultColor(Color.WHITE);
PTL2.SetDefaultColor(Color.RED);
PTL3.SetDefaultColor(Color.RED);
PTL4.SetDefaultColor(Color.RED);
PTL5.SetDefaultColor(Color.RED);
PTS1.SetStyle(Curve.SHORT_DASH);
PTS2.SetStyle(Curve.SHORT_DASH);
PTS3.SetStyle(Curve.SHORT_DASH);
PTS4.SetStyle(Curve.SHORT_DASH);
PTL1.SetStyle(Curve.SHORT_DASH);
PTL2.SetStyle(Curve.SHORT_DASH);
PTL3.SetStyle(Curve.SHORT_DASH);
PTL4.SetStyle(Curve.SHORT_DASH);
PTL5.SetLineWeight(2);
PTS5.SetLineWeight(2);

Screenshot 2023-05-16 at 1.04.06 PM.png
 
Solution
This code will give you ohlc values for the opening hour, or whatever window you choose. It plots them as a sanity check, you may remove that as needed.

-mashume

Code:
declare upper;

input window_begin = 0930;
input window_end = 1030;

def in_window = if secondsFromTime(Window_Begin) >= 0 and secondsTillTime(Window_End) >= 0 then 1 else 0;

def o = if in_window == 1 and in_window[1] == 0 then open else o[1];

def h = if in_window == 1 and in_window[1] == 0 then high else if in_window == 1 and high > h[1] then high else h[1];

def l = if in_window == 1 and in_window[1] == 0 then low else if in_window == 1 and low < l[1] then low else l[1];

def c = if in_window == 0 and in_window[1] == 1 then close else c[1];

plot op = o;
plot hi = h...
This code will give you ohlc values for the opening hour, or whatever window you choose. It plots them as a sanity check, you may remove that as needed.

-mashume

Code:
declare upper;

input window_begin = 0930;
input window_end = 1030;

def in_window = if secondsFromTime(Window_Begin) >= 0 and secondsTillTime(Window_End) >= 0 then 1 else 0;

def o = if in_window == 1 and in_window[1] == 0 then open else o[1];

def h = if in_window == 1 and in_window[1] == 0 then high else if in_window == 1 and high > h[1] then high else h[1];

def l = if in_window == 1 and in_window[1] == 0 then low else if in_window == 1 and low < l[1] then low else l[1];

def c = if in_window == 0 and in_window[1] == 1 then close else c[1];

plot op = o;
plot hi = h;
plot lo = l;
plot cl = c

So to use it in your code you would do something like this:
Code:
def StDevDay = StDev(h - l)[1], length);
plot PTL1 = o + .5 * StDevDay;
 
Solution

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