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:
- 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.
- 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);