• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

52-Week High / Low Labels for ThinkorSwim

drkellog

New member
VIP
The following code will create two labels that display the 52 week high and low on any time frame. Please note: you must have at least 2 Days of data. In other words, this works on 2D:1M not 1D:1M. It works on 5D:5M, 5D:15M, 10D:30M, 20D:1H, 180D:4H, 1Y:1D and 3Y:1W. Again, to use this on the 1 min chart you need to create a custom time frame of at least 2D:1M for it to work.

RFlpggy.png


Code:
#CODE BELOW
AddLabel(1, "52Week HIGH = " + Highest(high(period = AggregationPeriod.Week),52), Color.green);
AddLabel(1, "52Week LOW = " + Lowest(low(period = AggregationPeriod.Week),52), Color.red);
#END CODE
 

eugchen

Member
LOVE THIS CODE. i use a white background and the font shows up white. anyway to keep the red and green background, but how would i change the font to black?

also on this code, how do i make sure digits are rounded to nearest hundredth digits? on some stocks, it goes to 4 digits. not sure why.

Capture4r.jpg
 

eugchen

Member
Can you help me in figuring out where the round function goes? Cant figure it out. when I added it before the color, I get errors :(

Code:
#CODE BELOW
AddLabel(1, "52Week HIGH = " + Highest(high(period = AggregationPeriod.Week),52), Round(close, 2), Color.green);
AddLabel(1, "52Week LOW = " + Lowest(low(period = AggregationPeriod.Week),52), Round(close, 2), Color.red);
#END CODE
 

rad14733

Well-known member
VIP
@eugchen Some of the TOS functions are more touchy than others... I have found that it is sometimes easier to do most or all of the calculating first and then use the results in the function by passing said previously defined variables to the function... And some functions won't throw an error if something is wrong, they just don't return results, which can make debugging difficult...
 

eugchen

Member
Ok after extensive research, I have figured it out if anyone is interested...

Code:
def hi52 = Highest(high(period = AggregationPeriod.Week),52);
addlabel(1, "52Wk HI: $" + Round(hi52, 2), color.green);

def low52 = Lowest(low(period = AggregationPeriod.Week),52);
addlabel(1, "52Wk LOW: $" + Round(low52, 2), color.red);

This code rounds to nearest 2 digits after decimal and adds a "$".

Thanks guys!
 

cabe1332

Active member
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:

binhvesting

Member
VIP
I am not sure if this any additional help you all. But, since this site has been helpful to me and I will share a few features I've added. Please see the code below and attached screenshots. I started plotting a line where 52 Wk Hi/Lo and the were displayed improper or not acceptable due to appearance setting "Fit Studies". So, I use bubbles instead to display last close/price. Since I am bullish near 52Wk Hi is one of my fav strategies, I only coded to add "percent near 52 Wk Hi". I hope this helps you. Good luck... cabe1332 :)

#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;

# 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" else "Price " + round(close,4),if price > price[1] then color.light_green else color.light_red);

# end of code

Gm0NFml.png


RgbF91r.png


aOsEYU9.png
Hi @cabe1332 , thanks for sharing. Would you mind sharing the buy/sell and Dist/Accumulation scripts as well? or are they on this site?
 

cabe1332

Active member
Hi @cabe1332 , thanks for sharing. Would you mind sharing the buy/sell and Dist/Accumulation scripts as well? or are they on this site?
@binhvesting Yes, I have posted my code on Market Pulse. For the buy/sell signals look for TrendReversal script on this site. I have modified mine to lesser signals and text to meet my requirements. Good luck... cabe1332 :)
 

stormy77

New member
Ok after extensive research, I have figured it out if anyone is interested...

Code:
def hi52 = Highest(high(period = AggregationPeriod.Week),52);
addlabel(1, "52Wk HI: $" + Round(hi52, 2), color.green);

def low52 = Lowest(low(period = AggregationPeriod.Week),52);
addlabel(1, "52Wk LOW: $" + Round(low52, 2), color.red);

This code rounds to nearest 2 digits after decimal and adds a "$".

Thanks guys!
Is there a way to show dotted lines representation the 52w H/L marks? I tried using the 52W high and low provided in the studies section, but they only show accurate when viewing a daily chart (6mon/1D; 3 mo/1D etc), but when I try to see hourly timeframe like 6mo/4hr etc, the numbers are not correct
 

stormy77

New member
Ok after extensive research, I have figured it out if anyone is interested...

Code:
def hi52 = Highest(high(period = AggregationPeriod.Week),52);
addlabel(1, "52Wk HI: $" + Round(hi52, 2), color.green);

def low52 = Lowest(low(period = AggregationPeriod.Week),52);
addlabel(1, "52Wk LOW: $" + Round(low52, 2), color.red);

This code rounds to nearest 2 digits after decimal and adds a "$".

Thanks guys!
Is there a way to add dates to these H/L values?
 

george321

New member
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
Hi
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
@cabe1332 Your indicator with buy/sell signals looks great and the plot (colored line) as well. Where did you get that script? I have been trying to find something similar but unsuccessful at this time. Thanks in advance for your help.
 

cabe1332

Active member
Hi

@cabe1332 Your indicator with buy/sell signals looks great and the plot (colored line) as well. Where did you get that script? I have been trying to find something similar but unsuccessful at this time. Thanks in advance for your help.
Hi @george321, my indicators are custom to my strategies and work well with my trading. Some may disagree that it is TOO much going on, but I trade on what I see. Someone might say that "it is a bit novice or noob, but it works for me. I've rather be a noob with the discipline trading process everyday.

Trend Reversal - the buy/sell signals are from Trend Reversal. Search and apply the script. You can unplot (uncheck plot) and show the signals. The script is available here on this site for you free. Yes, I modified the script for proper signal + chart bubble.

Trend Painter - also available here and searchable. Very useful if you're a trend trading, which I highly suggest. I've added "Buy", "Strong Buy", and "Strong Sell' chart bubble to help with scalping or pullbacks.

Hull Moving Average Turning Points and Concavity - multi-colored (9 EMA) chart line. Very useful and effective with pivots.

I went through iterations of indicators, found and learn the TOS code from this site beginning in November 2020, put a lot of time to develop what works for my trading. Now, if I can only have my long-term positions or company FDA approved to gain enough capital and let these indicators make a difference in a bigger way. As they say, it is the shooter that pulls the trigger (smartly), but it is nice to have all the tools and data in front of you. Good luck! @cabe1332

9OO4g4a.png
 

Similar threads

Top