Weekly & ON/PM Range Label

FelixTheCatheter

New member
Hello,

I normally excel everything but this one is a little more time consuming. Ideally, I am looking for 2 types of labels.

1. Weekly Range Label
Calculate last week's RTH high minus low and put that value on a label.
Calculate 2, 3, 4, and 5 prior week's RTH high minus low values, divide by 4, and put that on a label.

2. ON/PM Range Label
Calculate the last 14 day's overnight or PM high minus low value, divide by 14, and put that on a label.
 
Last edited:
Solution
Hello,

I normally excel everything but this one is a little more time consuming. Ideally, I am looking for 2 types of labels.

1. Weekly Range Label
Calculate last week's RTH high minus low and put that value on a label.
Calculate 2, 3, 4, and 5 prior week's RTH high minus low values, divide by 4, and put that on a label.

2. ON/PM Range Label
Calculate the last 14 day's overnight or PM high minus low value, divide by 14, and put that on a label.
Enjoy!

1. Be sure to turn on the Chart Option (Equities tab) "Show Extended-Hours Trading Session" - check on.
2. Be sure you have over 20 days worth of data. Time Frame - Time Interval > 20 days.

Code:
# ########################################################
# Weekly and Overnight...
Hello,

I normally excel everything but this one is a little more time consuming. Ideally, I am looking for 2 types of labels.

1. Weekly Range Label
Calculate last week's RTH high minus low and put that value on a label.
Calculate 2, 3, 4, and 5 prior week's RTH high minus low values, divide by 4, and put that on a label.

2. ON/PM Range Label
Calculate the last 14 day's overnight or PM high minus low value, divide by 14, and put that on a label.
Enjoy!

1. Be sure to turn on the Chart Option (Equities tab) "Show Extended-Hours Trading Session" - check on.
2. Be sure you have over 20 days worth of data. Time Frame - Time Interval > 20 days.

Code:
# ########################################################
# Weekly and Overnight Range Labels
# ########################################################

# 1. WEEKLY RANGE CALCULATIONS
def wH = high(period = AggregationPeriod.WEEK);
def wL = low(period = AggregationPeriod.WEEK);

# Last week's Range (Index 1)
def lastWeekRange = wH[1] - wL[1];

# Average of 2, 3, 4, and 5 weeks ago
def prev4Sum = (wH[2] - wL[2]) + (wH[3] - wL[3]) + (wH[4] - wL[4]) + (wH[5] - wL[5]);
def prev4Avg = prev4Sum / 4;

# 2. OVERNIGHT / PM RANGE CALCULATIONS (Last 14 Days)
# Define the RTH session boundaries
def isRTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and GetTime() < RegularTradingEnd(GetYYYYMMDD());

# Track the High and Low during the Overnight Session
def onHigh = if !isRTH and isRTH[1] then high else if !isRTH then Max(high, onHigh[1]) else onHigh[1];
def onLow = if !isRTH and isRTH[1] then low else if !isRTH then Min(low, onLow[1]) else onLow[1];

# Capture the range exactly when the RTH session opens
def dailyONRange = if isRTH and !isRTH[1] then (onHigh[1] - onLow[1]) else Double.NaN;

# Create a rolling 14-day average of the captured overnight ranges
# We use a recursive sum and counter to handle the "once-per-day" data point
def sumON = if IsNaN(dailyONRange) then sumON[1] else sumON[1] + dailyONRange - if !IsNaN(dailyONRange[14]) then dailyONRange[14] else 0;
def countON = if IsNaN(dailyONRange) then countON[1] else Min(countON[1] + 1, 14);
def avgONRange14 = sumON / countON;

# ########################################################
# LABELS
# ########################################################

AddLabel(yes, "Last Week Range: " + Round(lastWeekRange, 2), Color.CYAN);

AddLabel(yes, "Prev 2-5 Wk Avg: " + Round(prev4Avg, 2), Color.YELLOW);

AddLabel(yes, "14-Day Avg ON Range: " + Round(avgONRange14, 2), Color.MAGENTA);
 
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
958 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