52-week Highs/Lows For ThinkOrSwim

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

52-week high/low is the highest and lowest price at which a stock has traded during the previous year. It is a technical indicator used by some traders and investors who view the 52-week high or low as an important factor in determining a stock's current value and predicting future price movement.

"Paris: Here's a watchlist you can use for percentage off 52 week high. For 52 week low, you can modify this."

Code:
# Percentage Off 52 Week High Watchlist
# Nick Name NAG®
# 11.02.2015

# This assumes a daily aggregation.  You will need to use
# daily or higher, adjust the 252 length as needed.

Round((close / Highest(high, 252) - 1) * 100)
I have a question on this one, I added this and I noticed for a few securities, it is showing NaN currently as seen below...

2021-02-15-20-45-15.jpg
 
@Almighty1 Not within TOS.
Would it be possible instead of using the indicator as we know the 52 week low and 52 week high numbers, to just have a % treating 0 as the 52 week low and 100% as the 52 week high? So basically if the 52 week low was $1.00 and 52 week high was $100.00, and the current price was $49.00, it would show 49%?
 
Would it be possible instead of using the indicator as we know the 52 week low and 52 week high numbers, to just have a % treating 0 as the 52 week low and 100% as the 52 week high? So basically if the 52 week low was $1.00 and 52 week high was $100.00, and the current price was $49.00, it would show 49%?
here's what you requested, its going to show as the numerical percentage from 0-100 value rather than a histogram.
Be sure to set the scan aggregation to MONTH
(instead of 52 week high, i used 12 month high to account better for leap years)
Code:
def priceHigh = high;
def pricelow = low;
def YearHigh = Highest(priceHigh, 12) ;
def YearLow = Lowest(priceLow, 12) ;
def range = Yearhigh-Yearlow;
def closing_value = close - Yearlow;
def percent = (closing_value/range)*100;
plot scan = percent;
 
Last edited:
@PATrader Here you go

Code:
def priceHigh = high;
def pricelow = low;
def YearHigh = Highest(priceHigh, 12) ;
def YearLow = Lowest(priceLow, 12) ;
def range = Yearhigh-Yearlow;
def closing_value = close - Yearlow;
def percent = (closing_value/range)*100;
AddLabel(yes, Concat("52wk% = ", percent), color.orange);
 
@PATrader Here you go

Code:
def priceHigh = high;
def pricelow = low;
def YearHigh = Highest(priceHigh, 12) ;
def YearLow = Lowest(priceLow, 12) ;
def range = Yearhigh-Yearlow;
def closing_value = close - Yearlow;
def percent = (closing_value/range)*100;
AddLabel(yes, Concat("52wk% = ", percent), color.orange);
Ben, thank you for the code. I checked and the % in the display box seems to be incorrect, compared to the 52w H/L.
Thanks
 
here's what you requested, its going to show as the numerical percentage from 0-100 value rather than a histogram.
Be sure to set the scan aggregation to MONTH
(instead of 52 week high, i used 12 month high to account better for leap years)
Code:
def priceHigh = high;
def pricelow = low;
def YearHigh = Highest(priceHigh, 12) ;
def YearLow = Lowest(priceLow, 12) ;
def range = Yearhigh-Yearlow;
def closing_value = close - Yearlow;
def percent = (closing_value/range)*100;
plot scan = percent;

Many thanks, just tried it with scan aggregation to Month and compared to the script in the OP of this thread using AAPL as a example for today which has the following data:
52 week low $53.1525
52 week high $145.09
Last $130.84

OP script shows -9.82% which would be $130.84
The script above shows 84.5% which would be $122.60105 as it should be 90.18%
 
Last edited:
Many thanks, just tried it with scan aggregation to Month and compared to the script in the OP of this thread using AAPL as a example for today which has the following data:
52 week low $53.1525
52 week high $145.09
Last $130.84

OP script shows -9.82% which would be $130.84
The script above shows 84.5% which would be $122.60105 as it should be 90.18%
you requested:
"to just have a % treating 0 as the 52 week low and 100% as the 52 week high"
thats what that script does in post #70 of this thread that i posted.

the OP (post in post #1) is completely different and is the % away from the 252 day high.

Also keep in mind there is a difference from 252 days and 12 months in your calculations.
 
you requested:
"to just have a % treating 0 as the 52 week low and 100% as the 52 week high"
thats what that script does in post #70 of this thread that i posted.

the OP (post in post #1) is completely different and is the % away from the 252 day high.

Also keep in mind there is a difference from 252 days and 12 months in your calculations.
Interesting. Is there a way to actually do it without the timeframe part since the lows and highs for 52 weeks are already defined so for example,
if the 52 week low is $50 and 52 week high is $150. So if the current price was $125, then it would be 75%, $110 would be 60%, $100 would be 50%, $90 would be 40%.
 
Interesting. Is there a way to actually do it without the timeframe part since the lows and highs for 52 weeks are already defined so for example,
if the 52 week low is $50 and 52 week high is $150. So if the current price was $125, then it would be 75%, $110 would be 60%, $100 would be 50%, $90 would be 40%.
youre requesting the same thing jus with a different example, the code for that is listed above.
 
youre requesting the same thing jus with a different example, the code for that is listed above.
Did it manually and you are right, the range is basically $91.9375 with YearHigh being 145.09 and YearLow being 53.125 so using that, if the closing was $130.84, which means it moved up $77.6875 from the YearLow so $77.6875/$91.9375=84.5%. Just need to understand how the different scan aggregation works even though one can probably tell that 252 means days in a year and 12 means months in a year.
 
Did it manually and you are right, the range is basically $91.9375 with YearHigh being 145.09 and YearLow being 53.125 so using that, if the closing was $130.84, which means it moved up $77.6875 from the YearLow so $77.6875/$91.9375=84.5%. Just need to understand how the different scan aggregation works even though one can probably tell that 252 means days in a year and 12 means months in a year.
you stated:
"Just need to understand how the different scan aggregation works"
its just the basics of distance, rate, and ratio over time
simpler example:
you can drive one kilometer in 2 mins or drive 0.621371 miles in 2 mins.. end result of making it to 1 kilometer in 2 mins is the same because one kilomter is 0.621371 miles. how you decide to mathematically break down the distance traveled per minute inbetween is up to you and the possible varying different statistics in the smaller increments of the drive (which can possibly be more complicated).
 
Last edited:
you stated:
"Just need to understand how the different scan aggregation works"
its just the basics of distance, rate, and ratio over time
simpler example:
you can drive one kilometer in 2 mins or drive 0.621371 miles in 2 mins.. end result of making it to 1 kilometer in 2 mins is the same because one kilomter is 0.621371 miles. how you decide to mathematically break down the distance traveled per minute inbetween is up to you and the possible varying different statistics in the smaller increments of the drive (which can possibly be more complicated).
What I actually meant was I am still new to ThinkorSwim, only used it briefly over 3 months and trying to understand thinkscript which is why I said 252 basically requires a day aggregation while 12 is months, I am sure the aggregation will be set to week if the 12 was changed to 52 since like you said, it's how it's broken down and ofcourse with what I wanted, the results are based on two other variables while for the OP, it's based just on one variable.
 
I want to build a scan and not sure how to get the below conditions to work. I checked the price performance study but couldn't find anything I can find to get the below one.

1. 52W high was hit in the last 60 days
2. The current price is 50% below 52W high.
 
@lowtrade

52 Week ( One Year) high has happened within past "Specified" Days (Bars)

Code:
#52 Week high has happened within past XYZ Days (bars);
#Default setting is 52 Week high has happened within past 60 Days
# By XeoNoX via usethinkscript.com
#Set Scan Aggregation to Day
declare lower;
input DaysWithin = 60;
#252 days represent one trading year
def YearHigh = Highest(high, 252);
def RecentHigh = Highest(high, DaysWithin);
plot scan = RecentHigh == YearHigh;

@lowtrade the 2nd part of your request can be found in default TOS or on this forum somewhere.
 
Last edited:
@lowtrade This should get you there, or close...

Ruby:
#52 weeks in days, or 1 trading year
def high252 = Highest(close, 252);
def result = close crosses above high252 within 60 bars and close < high252 / 2;
 
@lowtrade This should get you there, or close...

Ruby:
#52 weeks in days, or 1 trading year
def high252 = Highest(close, 252);
def result = close crosses above high252 within 60 bars and close < high252 / 2;
@rad14733, I tried adding this to ToS scanner as a custom study and it says "At least one plot should be defined" and wont let me save it. What am I missing?
 
@rad14733, I tried adding this to ToS scanner as a custom study and it says "At least one plot should be defined" and wont let me save it. What am I missing?
Ruby:
#52 weeks in days, or 1 trading year
def high252 = Highest(close, 252);
plot result = close crosses above high252 within 60 bars and close < high252 / 2;
 
I am not sure if the addition helps you all. But, since this site has been helpful. I would like to share a few features I've added. Please see the code below and attached screenshots. I started with plotting a line where 52 Wk Hi/Lo on the chart and it displayed improper or unreadable at time due to the appearance setting "Fit Studies" automatically fits all studies on the chart. So, I decided to use bubbles to display the last close/price with conditions. Since I am bullish and love 52Wk Hi strategies, I only coded to add "percent near 52 Wk Hi". I hope this helps anyone. Good luck... cabe1332 :)

You can scan for near 52 Wk Hi and alert you when one meets your criteria.

#52 Wk High and Low Label
# created by cabe1332 20210115_1730

# Start code

# determine if close greater than 52 wk hi and lo
def Highstate = if close >= Highest(high(period = AggregationPeriod.Week),52) then 1 else 0;
def Lowstate = if close <= Lowest(low(period = AggregationPeriod.Week),52) then 1 else 0;

# change background def if within 10% then green else light.gray
def HighstateRange = if (close >= Highest(high(period = AggregationPeriod.Week),52) * (.8995)) then 1 else 0;

# 52 Wk Hi
def Wk52Hi = Highest(high(period = AggregationPeriod.Week),52);
# calculate percentChg diff
def Pctawayhi = Round((close / Highest(high(period = AggregationPeriod.Week),52) - 1) * (-100));

# label for 52 Wk High
AddLabel(yes, " Price " + round(Pctawayhi,0) + "% away from " + "52Wk High: " + Highest(high(period = AggregationPeriod.Week),52) + " ", if Highstaterange then Color.GREEN else Color.light_GRAY);

# label for 52 Wk Low
AddLabel(yes, "52Wk Low: " + Lowest(low(period = AggregationPeriod.Week),52), if Lowstate then Color.Red else Color.light_GRAY);

# instead of the plotting a line, bubble is use to support "Fit Studies" appearance setting
# bubble for current bar
input barsback = 0;
input price = close;

def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(price) then bn else Double.NaN);

# bubble shows currentBar price (buy or sell), if price within 10% addChartBubble percent value
addChartBubble(bn == currentBar - barsBack, close, if Highstaterange then "Price " + round(close,4) + " | " + round(Pctawayhi,0) + "% near 52Wk High " + round(Wk52Hi) else "Price " + round(close,4),if price >= price[1] then color.green else color.light_red);

# end of code

bOMn4Sn.png



RgbF91r.png


aOsEYU9.png
 
Last edited:
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
479 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