Plot 52 week high and low with chart labels


Category:
0
0

Hey Pete – I’d like an indicator that shows the 52 week high regardless of time frame chart I’m viewing. The premarket high-low study that you created (linked below) is in the same spirit of what I’m looking for, only with the 52-week highs/lows instead of premarket highs/lows.

I tried using the “Daily High/Low” study on ToS, and changing the aggregation to Year, with a value of 1. That looked close, but the line didn’t stay on my charts when the time period was less than 1 year, I could only see it when I used a chart with a time period of 1yr or greater.

Premarket high/low: https://www.hahn-tech.com/ans/plot-premarket-high-and-low-during-each-trading-session/

Thanks!

Marked as spam
Posted by (Questions: 3, Answers: 1)
Asked on November 4, 2020 9:15 pm
1050 views
0
Private answer

Sorry but this cannot be achieved. You must have data in order to compute a value. On Thinkorswim, if you do not have at least 52 weeks (1 year) of data viewable on the chart you cannot plot a 52 week high. The lowest time frame in which you can include 1 year of data on the chart is the 1 hour time frame. To be clear, that means you would set the chart to 1 hour time frame. Then adjust the "Time Interval" to 360 days. The "Time Interval" setting is found when you access the custom time frame settings on the chart. There are no ways to work around these limitations through any custom code on Thinkorswim.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on November 5, 2020 8:27 am
0
Hey Pete - I tried to post an updated solution to this but it said something along the lines of "your comment is under review by the moderator". Let me know if I need to re-post. Long-story short, I did find a solution.
( at November 17, 2020 10:02 pm)
0
I have approved the new solution you have provided for this post. I will update the title of this question so the rest of our viewers searching for a similar solution can find this using the search function and clearly understand the context before clicking to view the details.
( at November 18, 2020 9:08 am)
0
Private answer

I found that this thread ( https://www.hahn-tech.com/ans/lowest-low-and-highest-high-between-2-dates/ ) combined with your answer above came close to what I'm looking for. I got what I want using the ToS's "Daily_High_Low", then setting it to Daily aggregation and a length of 253 (number of trading days in a year). I seem to have gotten a study I'm quite pleased with (code below). Interestingly enough, this actually does work on any time frame less than 1 year, the lines and labels stop showing up on any time frames greater than 1 year. That being said, I have a couple things that would make it better:

  1. Can you help me combine my four labels into two labels? I couldn't figure out how to make one label say "52wk High: [value of DailyHigh]", and then the same for 52wk Low.
  2. I tried to apply the same logic to get lines and label for an all-time high/low, but I couldn't get it to work. Either it wouldn't go back far enough or it would have to go back too far, and so any chart with a smaller timeframe the study won't show (in plain speak, if I pull 10 years of data, any charts with less than 10 years of data won't show). If you have any suggestions I'm all ears.

Anyway, here's my code:

input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = no;

plot DailyHigh;
plot DailyLow;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
DailyHigh = Double.NaN;
DailyLow = Double.NaN;
} else {
DailyHigh = Highest(high(period = aggregationPeriod)[-displace], length);
DailyLow = Lowest(low(period = aggregationPeriod)[-displace], length);
}

DailyHigh.SetDefaultColor(GetColor(4));
DailyHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailyLow.SetDefaultColor(GetColor(4));
DailyLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

AddLabel(yes, "52wk High:", color.CYAN);
AddLabel(yes,DailyHigh , color.CYAN);
AddLabel(yes, "52wk Low:", color.MAGENTA);
AddLabel(yes,DailyLow , color.MAGENTA);

Attachments:
Marked as spam
Posted by (Questions: 3, Answers: 1)
Answered on November 15, 2020 2:32 pm
0
Hi man, amazing code, how cool could it be if we include % from both values to the mark (if its possible or close price)
( at June 27, 2024 2:23 pm)
0

Here are two lines of code you can use in place of the existing AddLabel() statements. The first will display the description and value of both levels in the same level:

AddLabel(yes, Concat("52wk High: ", Concat(DailyHigh, Concat("; 52wk Low: ", DailyLow))), color.CYAN);

The second will display the percent value that the current close is from each of the two levels, also in one label:

AddLabel(yes, Concat("Prct From High: ", Concat(Round(100 * (close / DailyHigh - 1), 1), Concat(" Prct From Low: ", Round(100 * (close / DailyLow - 1), 1)))), color.CYAN);
( at June 27, 2024 4:19 pm)