52-week Highs/Lows For ThinkOrSwim

We can find how many bars ago the high or low was but we can't use a dynamic index with GetYYYYMMDD()[offset]. It requires a constant. GetValue() is a workaround for this but GetValue() assumes we're getting a number, such as high or low, and it loses the date formatting. The result for the current date, when displayed in a label, ends up being 20,210,817. The code below will give you the correct date with wrong formatting. I can't think of a solution for getting the correct date with the correct formatting. Another option, if you don't like this formatting, would be to display the offset itself, i.e. the number 12 would mean 12 weeks ago.

Ruby:
AddLabel(
 yes,
 GetValue(GetYYYYMMDD(), GetMaxValueOffset(high(period=AggregationPeriod.WEEK), 52), 52),
 Color.LIGHT_GRAY
);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

We can find how many bars ago the high or low was but we can't use a dynamic index with GetYYYYMMDD()[offset]. It requires a constant. GetValue() is a workaround for this but GetValue() assumes we're getting a number, such as high or low, and it loses the date formatting. The result for the current date, when displayed in a label, ends up being 20,210,817. The code below will give you the correct date with wrong formatting. I can't think of a solution for getting the correct date with the correct formatting. Another option, if you don't like this formatting, would be to display the offset itself, i.e. the number 12 would mean 12 weeks ago.

Ruby:
AddLabel(
 yes,
 GetValue(GetYYYYMMDD(), GetMaxValueOffset(high(period=AggregationPeriod.WEEK), 52), 52),
 Color.LIGHT_GRAY
);
i
The asprice() will remove the misplaced commas. I have tried and have not been able to display a date associated with a bar that is not on the intraday chart though.
 
We can find how many bars ago the high or low was but we can't use a dynamic index with GetYYYYMMDD()[offset]. It requires a constant. GetValue() is a workaround for this but GetValue() assumes we're getting a number, such as high or low, and it loses the date formatting. The result for the current date, when displayed in a label, ends up being 20,210,817. The code below will give you the correct date with wrong formatting. I can't think of a solution for getting the correct date with the correct formatting. Another option, if you don't like this formatting, would be to display the offset itself, i.e. the number 12 would mean 12 weeks ago.

Ruby:
AddLabel(
 yes,
 GetValue(GetYYYYMMDD(), GetMaxValueOffset(high(period=AggregationPeriod.WEEK), 52), 52),
 Color.LIGHT_GRAY
);
I'm beginning to see the challenge associated with this.

So how about this: You mentioned that we get the week number, so how about:
  1. if we change the aggregation to days and use 252 days to get the high
  2. This way we get the number of days (instead of weeks)
  3. we subtract the days from todays date and get a date for the high which we would show...
haha. is this feasible?

I did not see any ToS function related to date/Time that would subtract x number of days from today and give you the resulting date
(https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time)
 
Last edited:
I'm beginning to see the challenge associated with this.

So how about this: You mentioned that we get the week number, so how about:
  1. if we change the aggregation to days and use 252 days to get the high
  2. This way we get the number of days (instead of weeks)
  3. we subtract the days from todays date and get a date for the high which we would show...
haha. is this feasible?

I did not see any ToS function related to date/Time that would subtract x number of days from today and give you the resulting date
(https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time)

So far, I'm not seeing a way to make this work. I tried getting the offset, subtracting it from GetYYYYMMDD() and then using AsPrice() to remove the commas. This gives the wrong result for a couple of reasons. First, the offset is trading days and we're subtracting calendar days. Second, TOS isn't treating this as date math. It's just subtracting. For example:

Today minus 20 days:
20210817 - 20 = 20210797 = 2021/07/97 = the 97th day of the 7th month in 2021.
 
Last edited:
@stormy77 I noticed you show a 3 minute chart with 10 days worth of data, how important is it this to you? The lowest intra-day aggregation I could use would be hourly, but it would require the full 360 days worth of data in the time interval setting for it to work. If you would be willing to run the script on a weekly chart, it would be quite easy - but it would only show you the date of the Monday of the week in which the high or low happened, not the exact date. There are multiple ways to do it, each has slight drawbacks.
 
@stormy77 I noticed you show a 3 minute chart with 10 days worth of data, how important is it this to you? The lowest intra-day aggregation I could use would be hourly, but it would require the full 360 days worth of data in the time interval setting for it to work. If you would be willing to run the script on a weekly chart, it would be quite easy - but it would only show you the date of the Monday of the week in which the high or low happened, not the exact date. There are multiple ways to do it, each has slight drawbacks.
Most common timeframes I use are 1D3M, 5D3M, 10D3M.
This may be a silly question, but is it not possible to make the code look at a different timeframe and aggregation than what my chart shows?
I thought that by specifying AggregationPeriod.Week in the script would take care of this.

In any case, say we can make the script shopw high/low as it does now, but also shows date (as you indicated) on the D chart, I can live with it. I think it would be far easier to switch to D chart and get a close enough date than try to look p date manually...
 
So far, I'm not seeing a way to make this work. I tried getting the offset, subtracting it from GetYYYYMMDD() and then using AsPrice() to remove the commas. This gives the wrong result for a couple of reasons. First, the offset is trading days and we're subtracting calendar days. Second, TOS isn't treating this as date math. It's just subtracting. For example:

Today minus 20 days:
20210817 - 20 = 20210797 = 2021/07/97 = the 97th day of the 7th month in 2021.
Thanks for looking into this. Appreciate it
 
This appears to be working so far on the daily, its just rudimentary. @ me if you encounter any issues, ill take another look at it.

Ruby:
def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def LastHigh = HighestAll(if BarNumber() == LastBar then Highest(high(period = AggregationPeriod.Week),52) else Double.NaN);
def Day; def Month; def Year;
    if High == LastHigh{
        Day = GetDayOfMonth(getyYYYMMDD());
        Month = getmonth();
        Year = getYear();
    } else {
        Day = Day[1];
        Month = Month[1];
        Year = Year[1];
    }
Addlabel(yes, "High is: " + LastHigh + " on " + month + " / " + day + " / " + asprice(year),color.white);
 
Close >= Highest(High(period = "DAY"), 252)*0.9


Did you try the code? It is supposed to put a LABEL up just as you described.
so I want to scan for stocks that are between 50% and 80% of their 52w high. Is this the right code to use in custom study:
Code:
Close <= Highest(High(period = "DAY"), 252)*0.8 and Close >= Highest(High(period = "DAY"), 252)*0.50
 
I only get 52 week highs even when I replicate part 1 and change all high and highest words to low and lowest.

Please provide advice to correct. TY in advance!
 
I only get 52 week highs even when I replicate part 1 and change all high and highest words to low and lowest.

Please provide advice to correct. TY in advance!
You only need to change line 2, 4, and 13 really. The rest of the are just variable names, you can change them if it bothers you the way it looks, but functionally the names of the variables are irrelevant.

Ruby:
def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def LastHigh = HighestAll(if BarNumber() == LastBar then Lowest(Low(period = AggregationPeriod.Week),52) else Double.NaN); #2
def Day; def Month; def Year;
    if low == LastHigh{ #4
        Day = GetDayOfMonth(getyYYYMMDD());
        Month = getmonth();
        Year = getYear();
    } else {
        Day = Day[1];
        Month = Month[1];
        Year = Year[1];
    }
Addlabel(yes, "Low is: " + LastHigh + " on " + month + " / " + day + " / " + asprice(year),color.white);#13
 
Pardon my totally noob question since I know this topic has been partially discussed. I need help creating a script that will help identify names that are trading 70% to 85% below the 52 week high. Of course, I am equally interested in the opposite script, one that helps identify names trading 15% to 30% above the 52 week low.

The Near_High-Lows study is not useful in my case since the results include too many situations that fall outside of the desired range.

Your help will be greatly appreciated as I embark in my journey to learn how to write scripts by myself. Thanks. Harvey
 
Pardon my totally noob question since I know this topic has been partially discussed. I need help creating a script that will help identify names that are trading 70% to 85% below the 52 week high. Of course, I am equally interested in the opposite script, one that helps identify names trading 15% to 30% above the 52 week low.

The Near_High-Lows study is not useful in my case since the results include too many situations that fall outside of the desired range.

Your help will be greatly appreciated as I embark in my journey to learn how to write scripts by myself. Thanks. Harvey
Try adding the script below. Just change the pctThreshold with your requirement.

# Scan filter
# Finds ticker within pctThreshold
# @cabe1332

# code start
def pctThreshold = 0.30; # just change 0.30 to what pct you want
def ath = Highest(high, 365);

plot signal = (ath-close)/close <= pctThreshold;

# code ends

Good luck! @cabe1332
 
@cabe1332 Thank you so much for your quick response.
I had actually tried something very similar to what you propose. Would your script return names that are 30% below the 52 wk high and lower? What I was trying to accomplish is to find names that are in the range of 15% to 30% below the high.
Thanks again. I feel I am learning.
 
@cabe1332 Thank you so much for your quick response.
I had actually tried something very similar to what you propose. Would your script return names that are 30% below the 52 wk high and lower? What I was trying to accomplish is to find names that are in the range of 15% to 30% below the high.
Thanks again. I feel I am learning.
@Berkhar that's exactly what it does "return names that are 30% below the 52 wk high". Good luck! @cabe1332
 
I created this to help alert when price action is within a specified user defined percentage of the 52 wk high or 52 wk low. Please let me know what you think. The video link will show the complete functionality:

Code:
def aggregationPeriod = AggregationPeriod.WEEK;
def length = 52;
input displace = -1;
input Percent = 10.000;
input showOnlyLastPeriod = yes;

plot High;
plot Low;

High.hide();
Low.hide();

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

plot percenthiup = High + (High * (Percent / 100));
plot percenthidn = High - (High * (Percent / 100));
plot percentloup = Low + (Low * (Percent / 100));
plot percentlodn = Low - (Low * (Percent / 100));

percenthiup.hide();
percenthidn.hide();
percentloup.hide();
percentlodn.hide();



def cond1hi = (close <= percenthiup and close >= percenthidn);
def cond2lo = (close <= percentloup and close >= percentlodn);

AddLabel(yes, if cond1hi then (percent + "% Top: " + Round(High, 2) + "," + " LO: " + Round(Low, 2))
        else if cond2lo then ("HI: " + Round(High, 2) + "," + " " + percent + "% Btm: " + Round(Low, 2))
        else ("HI: " + Round(High, 2) + " LO: " + Round(Low, 2)),
        if cond1hi then GlobalColor("Top")
        else if cond2lo then GlobalColor("Btm")
        else GlobalColor("neutral"));

DefineGlobalColor("Top", Color.GREEN);
DefineGlobalColor("Btm", Color.RED);
DefineGlobalColor("neutral", Color.LIGHT_GRAY);
 
Does anyone know of a way to use the SCAN tab to search for stocks that are at least 10% OFF their 52 week high? I see the NEAR_HIGH_LOW study, however, it allows me to search for stocks WITHIN X% of their 52 week high. I am looking for the opposite... stocks NOT within X% of their 52 week high.

Any help is appreciated. TY
 
Thread starter Similar threads Forum Replies Date
inthefutures New Intraday Highs and Lows For ThinkOrSwim Indicators 0

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
303 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