# ATR Color Bar

#### hovno

##### New member
Hello, I am trying to create a candle color bar when the ATR is greater than 2 times the avg. ATR, I have to code already but candles are all colored yellow, Am i Doing something wrong here? It must be just something stupid small because the logic is okay.

Code:
``````#

def atr = Average(TrueRange(high, close, low), 14);
def twoatr = (atr*2);
def atrBar = if twoatr then 1 else Double.NaN;

AssignPriceColor(if close > atrbar then Color.YELLOW else Color.CURRENT );``````

#### SleepyZ

##### Well-known member
VIP
Hello, I am trying to create a candle color bar when the ATR is greater than 2 times the avg. ATR, I have to code already but candles are all colored yellow, Am i Doing something wrong here? It must be just something stupid small because the logic is okay.

Code:
``````#

def atr = Average(TrueRange(high, close, low), 14);
def twoatr = (atr*2);
def atrBar = if twoatr then 1 else Double.NaN;

AssignPriceColor(if close > atrbar then Color.YELLOW else Color.CURRENT );``````

See if this helps

Code:
``````#

def atr    = Average(TrueRange(high, close, low), 14);
def twoatr = (atr*2);
def range  = high - low;
def atrBar = if range > twoatr then 1 else 0;

AssignPriceColor(if atrbar then Color.YELLOW else Color.CURRENT );``````

#### hovno

##### New member
That is it! Thanks.. So I should have define the range instead of comparing it directly to the "close"

#### SleepyZ

##### Well-known member
VIP
That is it! Thanks.. So I should have define the range instead of comparing it directly to the "close"
Yes, that is part of the change in the code.

Your definition of atrbar originally resulted in a value of either 1 or 0. This is generally used as a true or false result to identify candles with arrows, colors, etc.

When you used close as your constraint, you were comparing the close price to either of the values of 1 or 0. As such, for any close values greater than those, you would get a true statement and assignpricevalue the color of yellow to those candles. For any symbol trading above 1, you would likely get all of the candles painted yellow,

What the above code did was change the comparison from close to range and then compared it to twoatr's value rather than your original values of 1 or 0.

I hope this explanation helps.

#### hovno

##### New member
Thank you very much! I also have similar issue but with different study, would you mind helping me here?
I basically want to plot a horizontal line on the high of the major swings (basically something like highestAll function within last 10 bars. However the line keeps ploting only on that highest candle. I would like an extended horizontal like a resistance line basically on all the major high swings that are above the price. So lets say once the daily close is above that horizontal line --> that line can disappear. So only the lines above the price are valid & visible. I googled on tos learning center perhaps I should somehow use IsNaN function there? I am hopeless.

Code:
``````def pivothigh = if  high > Highest(high[1], 10) and high > Highest(high[-10], 10) then 1 else 0;
def pricebelowhighs = if close(period=aggregationPeriod.DAY) < pivothigh then 1 else 0;;

plot Swing = if pivothigh == 1 then high else Double.NaN;

Swing.setpaintingStrategy(paintingStrategy.HORIZONTAL);``````

#### SleepyZ

##### Well-known member
VIP
Thank you very much! I also have similar issue but with different study, would you mind helping me here?
I basically want to plot a horizontal line on the high of the major swings (basically something like highestAll function within last 10 bars. However the line keeps ploting only on that highest candle. I would like an extended horizontal like a resistance line basically on all the major high swings that are above the price. So lets say once the daily close is above that horizontal line --> that line can disappear. So only the lines above the price are valid & visible. I googled on tos learning center perhaps I should somehow use IsNaN function there? I am hopeless.

Code:
``````def pivothigh = if  high > Highest(high[1], 10) and high > Highest(high[-10], 10) then 1 else 0;
def pricebelowhighs = if close(period=aggregationPeriod.DAY) < pivothigh then 1 else 0;;

plot Swing = if pivothigh == 1 then high else Double.NaN;

Swing.setpaintingStrategy(paintingStrategy.HORIZONTAL);``````

Here are 2 scripts in the following link and displayed in the image below https://tos.mx/BShk47c

The lower pane is a simpler solution to your request. It is plot of a line that stops at each pivot or earlier if it is crossed before the next pivot. It does not extend lines to the right beyond the next pivot as the line is based upon one plot statement.

To extend lines to the right, it requires separate plot statements for each line and a more complex study. To do this, the study uses the TOS script function, which is reusable for many plot statements. You can create more plot statments and look and feel by using the logic used for the 7 in the study.

#### hovno

##### New member
Okay I see, This seems to be rather too complicated, I dont want to take your time as much. Is there a way to make it simple like lets say in a form of a simple label that will just say the price level of that last swing high that is currently above the price? So the very last swing high that is above the price would not be a vertical line but rather a simple label "last swing high= 402.50" and if that is possible perhaps at least one more with previous high. So two labels 1.previous (penultimate)swing high 2. last swing high (last swing high from the current candle).

If it is too hard to code & need a separate code structure then do not worry about it. I am already thankful for your effort! Big THANK YOU.

#### SleepyZ

##### Well-known member
VIP
Okay I see, This seems to be rather too complicated, I dont want to take your time as much. Is there a way to make it simple like lets say in a form of a simple label that will just say the price level of that last swing high that is currently above the price? So the very last swing high that is above the price would not be a vertical line but rather a simple label "last swing high= 402.50" and if that is possible perhaps at least one more with previous high. So two labels 1.previous (penultimate)swing high 2. last swing high (last swing high from the current candle).

If it is too hard to code & need a separate code structure then do not worry about it. I am already thankful for your effort! Big THANK YOU.

1. All of the swing high plot are not constrained and extend to the next swing high. These lines can be turned on/off at the input screen.
2. The test label finds the value of the last 5 swing highs. It can be turned on/off at the input screen
3. The individual labels will only plot if true. Only 2 of the 5 swing highs should appear for those above the current close. The distance from these 2 swing highs and close is shown in parenthesis.

Code:
``````input showlines = yes;

def pivothigh  = if  high > Highest(high[1], 10) and high > Highest(high[-10], 10) then 1 else 0;
def hi = if IsNaN(pivothigh) then hi[1] else if pivothigh == 1 then high else hi[1];

plot Swing = hi;
Swing.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Swing.SetHiding(!showlines);

def swing1 = if hi != hi[1] then hi else swing1[1];
def swing2 = if swing1 != swing1[1] then swing1[1] else swing2[1];
def swing3 = if swing2 != swing2[1] then swing2[1] else swing3[1];
def swing4 = if swing3 != swing3[1] then swing3[1] else swing4[1];
def swing5 = if swing4 != swing4[1] then swing4[1] else swing5[1];

input test = yes;
AddLabel(test, "LSH = " + AsDollars(Swing1) + " PSH = " + AsDollars(swing2[1]) + " NPSH = " + AsDollars(swing3[1]) + " NPSH = " + AsDollars(swing4[1]) + " NPSH = " + AsDollars(swing5[1]), Color.MAGENTA);

AddLabel(close < Swing1, "PSH below close : "  + asdollars(Swing1) + " (" + asdollars(Swing1 - close) +")", Color.yellow);
AddLabel(close < Swing2, "NSH below close : "  + asdollars(Swing2) + " (" + asdollars(Swing2 - close) +")", Color.yellow);
AddLabel(swing2 < close and close < Swing3, "NSH below close : "  + asdollars(Swing3) + " (" + asdollars(Swing3 - close) +")", Color.yellow);
AddLabel(swing2 < close and swing3 < close and close < Swing4, "NSH below close : "  + asdollars(Swing4) + " (" + asdollars(Swing4 - close) +")", Color.yellow);
AddLabel(swing2 < close and swing3 < close and Swing4 < close and close < swing5, "NSH below close : "  + asdollars(Swing5) + " (" + asdollars(Swing5 - close) +")", Color.yellow);``````

#### hovno

##### New member
Wow! thank you so much, this is how i wanted it! thanks.

87k+ Posts
250 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?