Intra Year High Low Range Indicator

dugafish

New member
Plus
I have a custom indicator (below) that I call the Intra year High Low Opening Range ...its calculating the Higest High and Lowest Low that prints during the first 3 weeks of any new calendar year,,,and then plots these values across the charts until these values change the following calendar year. I am trying to have the results of this indicator(a high and low plot value that persists for each year until the next year time period completes) plot in any time frame that I view a chart....,especially daily and weekly time frames which is what I use most. As it works now, it displays the correct values in daily charts but it does not display/persist in weekly charts-it disappears.

input numberdays = 12;
input secondAggregation = AggregationPeriod.DAY;
def agg = AggregationPeriod.DAY;
def hl1bn = if GetYear() != GetYear()[1]
then BarNumber()
else hl1bn[1];
def h1 = if BarNumber() == hl1bn
then high(period = agg)
else if Between(BarNumber(), hl1bn, ((hl1bn) + numberdays))
and high(period = agg) > h1[1]
then high(period = agg)
else h1[1];
def l1 = if BarNumber() == hl1bn
then low(period = agg)
else if Between(BarNumber(), hl1bn, (hl1bn + numberdays))
and low(period = agg) < l1[1]
then low(period = agg)
else l1[1];
plot InsiideintrayearH = if BarNumber() >= (hl1bn + numberdays) then h1 else Double.NaN;
plot InsiideintrayearL = if BarNumber() >= (hl1bn + numberdays) then l1 else Double.NaN;


AddLabel(1, AsPrice(GetYear()) + ":" + " Intra_Year High: " + Round( h1,2) + " Intra_Year Low: " + Round( l1,2), if close > h1 then Color.GREEN else if close < l1 then Color.RED else Color.YELLOW);


Alert(close <= InsiideintrayearL, " Intramonh Breakout Down", Alert.BAR, Sound.Bell);
Alert(close >= InsiideintrayearH, " Intramonh Breakout Up", Alert.BAR, Sound.Bell);
 
Solution
I have a custom indicator (below) that I call the Intra year High Low Opening Range ...its calculating the Higest High and Lowest Low that prints during the first 3 weeks of any new calendar year,,,and then plots these values across the charts until these values change the following calendar year. I am trying to have the results of this indicator(a high and low plot value that persists for each year until the next year time period completes) plot in any time frame that I view a chart....,especially daily and weekly time frames which is what I use most. As it works now, it displays the correct values in daily charts but it does not display/persist in weekly charts-it disappears.

input numberdays = 12;
input secondAggregation =...
I have a custom indicator (below) that I call the Intra year High Low Opening Range ...its calculating the Higest High and Lowest Low that prints during the first 3 weeks of any new calendar year,,,and then plots these values across the charts until these values change the following calendar year. I am trying to have the results of this indicator(a high and low plot value that persists for each year until the next year time period completes) plot in any time frame that I view a chart....,especially daily and weekly time frames which is what I use most. As it works now, it displays the correct values in daily charts but it does not display/persist in weekly charts-it disappears.

input numberdays = 12;
input secondAggregation = AggregationPeriod.DAY;
def agg = AggregationPeriod.DAY;
def hl1bn = if GetYear() != GetYear()[1]
then BarNumber()
else hl1bn[1];
def h1 = if BarNumber() == hl1bn
then high(period = agg)
else if Between(BarNumber(), hl1bn, ((hl1bn) + numberdays))
and high(period = agg) > h1[1]
then high(period = agg)
else h1[1];
def l1 = if BarNumber() == hl1bn
then low(period = agg)
else if Between(BarNumber(), hl1bn, (hl1bn + numberdays))
and low(period = agg) < l1[1]
then low(period = agg)
else l1[1];
plot InsiideintrayearH = if BarNumber() >= (hl1bn + numberdays) then h1 else Double.NaN;
plot InsiideintrayearL = if BarNumber() >= (hl1bn + numberdays) then l1 else Double.NaN;


AddLabel(1, AsPrice(GetYear()) + ":" + " Intra_Year High: " + Round( h1,2) + " Intra_Year Low: " + Round( l1,2), if close > h1 then Color.GREEN else if close < l1 then Color.RED else Color.YELLOW);


Alert(close <= InsiideintrayearL, " Intramonh Breakout Down", Alert.BAR, Sound.Bell);
Alert(close >= InsiideintrayearH, " Intramonh Breakout Up", Alert.BAR, Sound.Bell);

The problem with the weekly was that you were using a lower agg "DAY" on a weekly chart. TOS does not allow that. Also to extend the plot of one year onto the next requires separate plots for each year, which are then extended, rather than using the 2 plots you have in your script.

This fixes both of those. It will switch between "DAY" and "WEEK" aggregations and numberofdays between 12 days and 3 weeks depending on whether you are respectfully viewing a "DAY' or "WEEK" chart.

The script function was used to extend the plots onto the next year. There are 3 yearly plots. You can create more if you want by using the logic of those 3.

Screenshot-2022-12-04-095000.png
Ruby:
#Intra Year High Low Range Indicator
script hhll {
    input years = 0;
    def numberdays = if GetAggregationPeriod() == AggregationPeriod.DAY
then 12
else 3;
    def agg        = if GetAggregationPeriod() == AggregationPeriod.DAY
then AggregationPeriod.DAY
else AggregationPeriod.WEEK;
    def ymd      = GetYear();
    def candles  = !IsNaN(close);
    def capture  = candles and ymd != ymd[1];
    def Count = CompoundValue(1, if capture then Count[1] + 1 else Count[1], 0);
    def thisyear  = (HighestAll(Count) - Count) ;
    def hl1bn = if GetYear() != GetYear()[1]
then BarNumber()
else hl1bn[1];
    def h1 = if thisyear < years
then h1[1]
else if thisyear == years
and BarNumber() == hl1bn
then high(period = agg)
else if Between(BarNumber(), hl1bn, ((hl1bn) + numberdays - 1))
and high(period = agg) > h1[1]
then high(period = agg)
else h1[1];
    def l1 = if thisyear < years
then l1[1]
else if thisyear == years
and BarNumber() == hl1bn
then low(period = agg)
else if Between(BarNumber(), hl1bn, (hl1bn + numberdays - 1))
and low(period = agg) < l1[1]
then low(period = agg)
else l1[1];
    plot InsiideintrayearH = if thisyear == years - 1 or thisyear == years
and BarNumber() >= (hl1bn + numberdays)
then h1[1]
else Double.NaN;
    plot InsiideintrayearL = if thisyear == years - 1 or thisyear == years
and BarNumber() >= (hl1bn + numberdays)
then l1[1]
else Double.NaN;
}

plot hi0 = hhll();
plot lo0 = hhll().InsiideintrayearL;
hi0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lo0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot hi1 = hhll(1);
plot lo1 = hhll(1).InsiideintrayearL;
hi1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lo1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot hi2 = hhll(2);
plot lo2 = hhll(2).InsiideintrayearL;
hi2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lo2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

AddLabel(1, AsPrice(GetYear()) + ":" + " Intra_Year High: " + Round( hi0, 2) + " Intra_Year Low: " + Round( lo0, 2), if close > hi0 then Color.GREEN else if close < lo0 then Color.RED else Color.YELLOW);

Alert(close <= lo0, " Intramonh Breakout Down", Alert.BAR, Sound.Bell);
Alert(close >= hi0, " Intramonh Breakout Up", Alert.BAR, Sound.Bell);
 
Solution
The problem with the weekly was that you were using a lower agg "DAY" on a weekly chart. TOS does not allow that. Also to extend the plot of one year onto the next requires separate plots for each year, which are then extended, rather than using the 2 plots you have in your script.

This fixes both of those. It will switch between "DAY" and "WEEK" aggregations and numberofdays between 12 days and 3 weeks depending on whether you are respectfully viewing a "DAY' or "WEEK" chart.

The script function was used to extend the plots onto the next year. There are 3 yearly plots. You can create more if you want by using the logic of those 3.
Much appreciated...I think I may have not explained the concept accurately.....Each New Calendar Year these levels will change as the highest high and the lowest low for the First Three Weeks of the new calendar year will change...I only want the current year's levels to perisist throughout the chart for this year, leaving intact the prior years for what they actually were.
 
Much appreciated...I think I may have not explained the concept accurately.....Each New Calendar Year these levels will change as the highest high and the lowest low for the First Three Weeks of the new calendar year will change...I only want the current year's levels to perisist throughout the chart for this year, leaving intact the prior years for what they actually were.

This allows you to display the highs/lows as defined for each year, except for the last year you can either display it across the whole_chart or just the last year, extended to the right edge of the chart.

Screenshot-2022-12-06-131555.png
Ruby:
input display_last_year = {default whole_chart, last_year};

def numberdays = if GetAggregationPeriod() == AggregationPeriod.DAY
then 12
else 3;

def agg        = if GetAggregationPeriod() == AggregationPeriod.DAY
then AggregationPeriod.DAY
else AggregationPeriod.WEEK;

def ymd      = GetYear();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def Count = CompoundValue(1, if capture then Count[1] + 1 else Count[1], 0);
def thisyear  = (HighestAll(Count) - Count) ;

def hl1bn = if IsNaN(close)
then hl1bn[1]
else if GetYear() != GetYear()[1]
then BarNumber()
else hl1bn[1];

#Highs

def h1 = if ymd[1] != ymd
and BarNumber() == hl1bn
then high(period = agg)
else if Between(BarNumber(), hl1bn, ((hl1bn) + numberdays - 1))
and high(period = agg) > h1[1]
then high(period = agg)
else h1[1];

def high0 = if thisyear == 0 and BarNumber() >= (hl1bn + numberdays)
then h1[1] else Double.NaN;

plot LastYearH = if display_last_year == display_last_year.last_year and
BarNumber() >= (hl1bn + numberdays) and thisyear == 0
then HighestAll(high0)
else if display_last_year == display_last_year.whole_chart
then HighestAll(high0)
else Double.NaN;

plot InsiideintrayearH = if thisyear >= 1 and BarNumber() >= (hl1bn + numberdays)
then h1[1]
else Double.NaN;

#Lows

def l1 = if ymd[1] != ymd
and BarNumber() == hl1bn
then low(period = agg)
else if Between(BarNumber(), hl1bn, (hl1bn + numberdays - 1))
and low(period = agg) < l1[1]
then low(period = agg)
else l1[1];

def low0  = if thisyear == 0 and BarNumber() >= (hl1bn + numberdays)
then l1[1]
else Double.NaN;

plot LastYearL = if display_last_year == display_last_year.last_year and
BarNumber() >= (hl1bn + numberdays) and thisyear == 0
then HighestAll(low0)
else if display_last_year == display_last_year.whole_chart
then HighestAll(low0)
else Double.NaN;

plot InsiideintrayearL = if thisyear >= 1 and BarNumber() >= (hl1bn + numberdays)
then l1[1]
else Double.NaN;
#
 

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