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

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?
 

Similar threads

Top