candle crossing upper / lower ATR bands

I am Investing

New member
VIP
Can someone help me? I want to show the candle closing beyond the defined upper and lower bounds. Why does the script show an arrow on every bar in the chart? And how can I show the arrow on the chart instead of below it? I have attached the script and screen. Thank you.

input length = 14;
input averageType = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.WEEK;
input ATRMultiplier = 2.0;

def pATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def tATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def ATR = max(pATR,tATR);

def thisPeriodHigh = high(period = BasePeriod)[0];
def thisPeriodLow = low(period = BasePeriod)[0];
def thisPeriodRange = thisPeriodHigh - thisPeriodLow;
def lastPeriodClose = close(period = BasePeriod)[1];

def hatr = lastPeriodClose + ATR * ATRMultiplier;
def hdtr = thisPeriodLow + ATR * ATRMultiplier;
plot uBound = if hatr < hdtr then hatr else hdtr;
uBound.SetDefaultColor(color = Color.RED);

def latr = lastPeriodClose - ATR * ATRMultiplier;
def ldtr = thisPeriodHigh - ATR * ATRMultiplier;
plot lBound = if latr > ldtr then latr else ldtr;
lBound.SetDefaultColor(color = Color.GREEN);

plot isOver = close(period = BasePeriod) > uBound;
isOver.setpaintingStrategy(paintingStrategy.ARROW_UP);
isOver.setdefaultColor(color.CYAN);
isOver.setlineWeight(1);
plot isUnder = close(period = BasePeriod) < lBound;
isUnder.setpaintingStrategy(paintingStrategy.ARROW_DOWN);
isUnder.setdefaultColor(color.CYAN);
isUnder.setlineWeight(1);
 

Attachments

  • 2025-01-16-TOS_CHARTS.png
    2025-01-16-TOS_CHARTS.png
    78.9 KB · Views: 38
Solution
I didn't test this corrected code but it should work... I found two issues... First, I replaced < and > with crosses below and crosses above so you only get one boolean trigger instead of multiple... Second, we use BOOLEAN_ARROW_UP/DOWN for boolean decisions like crosses above/below... No other logic was checked...

Let us know if this works...

Ruby:
input length = 14;
input averageType = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.WEEK;
input ATRMultiplier = 2.0;

def pATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def tATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1]...
I didn't test this corrected code but it should work... I found two issues... First, I replaced < and > with crosses below and crosses above so you only get one boolean trigger instead of multiple... Second, we use BOOLEAN_ARROW_UP/DOWN for boolean decisions like crosses above/below... No other logic was checked...

Let us know if this works...

Ruby:
input length = 14;
input averageType = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.WEEK;
input ATRMultiplier = 2.0;

def pATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def tATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def ATR = max(pATR,tATR);

def thisPeriodHigh = high(period = BasePeriod)[0];
def thisPeriodLow = low(period = BasePeriod)[0];
def thisPeriodRange = thisPeriodHigh - thisPeriodLow;
def lastPeriodClose = close(period = BasePeriod)[1];

def hatr = lastPeriodClose + ATR * ATRMultiplier;
def hdtr = thisPeriodLow + ATR * ATRMultiplier;
plot uBound = if hatr < hdtr then hatr else hdtr;
uBound.SetDefaultColor(color = Color.RED);

def latr = lastPeriodClose - ATR * ATRMultiplier;
def ldtr = thisPeriodHigh - ATR * ATRMultiplier;
plot lBound = if latr > ldtr then latr else ldtr;
lBound.SetDefaultColor(color = Color.GREEN);

plot isOver = close(period = BasePeriod) crosses above uBound;
isOver.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
isOver.setdefaultColor(color.CYAN);
isOver.setlineWeight(1);

plot isUnder = close(period = BasePeriod) crosses below lBound;
isUnder.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
isUnder.setdefaultColor(color.CYAN);
isUnder.setlineWeight(1);
 
Solution
I believe I can count the number of crossovers above and below using a counter.

I use 'def' to declare the counter, but I wonder what would happen if I used 'rec' instead. The outcome appears to be the same.

My code is provided below:

input length = 14;
input averageType = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.WEEK;
input ATRMultiplier = 1.7;

def pATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def tATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def ATR = max(pATR,tATR);

def thisPeriodHigh = high(period = BasePeriod)[0];
def thisPeriodLow = low(period = BasePeriod)[0];
def thisPeriodRange = thisPeriodHigh - thisPeriodLow;
def lastPeriodClose = close(period = BasePeriod)[1];

def hatr = lastPeriodClose + ATR * ATRMultiplier;
def hdtr = thisPeriodLow + ATR * ATRMultiplier;
plot uBound = if hatr < hdtr then hatr else hdtr;
uBound.SetDefaultColor(color = Color.RED);

def latr = lastPeriodClose - ATR * ATRMultiplier;
def ldtr = thisPeriodHigh - ATR * ATRMultiplier;
plot lBound = if latr > ldtr then latr else ldtr;
lBound.SetDefaultColor(color = Color.GREEN);

plot isOver = close(period = BasePeriod) crosses above uBound;
isOver.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
isOver.setdefaultColor(color.CYAN);
isOver.setlineWeight(1);
def aboveCount = if IsNaN(aboveCount[1]) then 0
else if isOver then aboveCount[1] + 1 else aboveCount[1];

plot isUnder = close(period = BasePeriod) crosses below lBound;
isUnder.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
isUnder.setdefaultColor(color.CYAN);
isUnder.setlineWeight(1);
def belowCount = if IsNaN(belowCount[1]) then 0
else if isUnder then belowCount[1] + 1 else belowCount[1];

def totalCount = aboveCount + belowCount;

AddLabel(visible = yes, text = "No Of Bars: " + BarNumber() + ", Close Above = " + aboveCount + ", Close Below = " + belowCount + " , Total = " + totalCount, color = Color.LIGHT_GRAY);

Addressed a bug.

I checked if it was Over and Under before the NaN.

I have included the updated code below.

input length = 14;
input averageType = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.WEEK;
input ATRMultiplier = 1.7;

def pATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def tATR = MovingAverage(averageType, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), length);
def ATR = max(pATR,tATR);

def thisPeriodHigh = high(period = BasePeriod)[0];
def thisPeriodLow = low(period = BasePeriod)[0];
def thisPeriodRange = thisPeriodHigh - thisPeriodLow;
def lastPeriodClose = close(period = BasePeriod)[1];

def hatr = lastPeriodClose + ATR * ATRMultiplier;
def hdtr = thisPeriodLow + ATR * ATRMultiplier;
plot uBound = if hatr < hdtr then hatr else hdtr;
uBound.SetDefaultColor(color = Color.RED);

def latr = lastPeriodClose - ATR * ATRMultiplier;
def ldtr = thisPeriodHigh - ATR * ATRMultiplier;
plot lBound = if latr > ldtr then latr else ldtr;
lBound.SetDefaultColor(color = Color.GREEN);

plot isOver = close(period = BasePeriod) crosses above uBound;
isOver.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
isOver.setdefaultColor(color.GREEN);
isOver.setlineWeight(5);
#def aboveCount = if IsNaN(aboveCount[1]) then 0
#else if isOver then aboveCount[1] + 1 else aboveCount[1];
def aboveCount;
if isOver then {
aboveCount = if IsNaN(aboveCount[1]) then 1 else aboveCount[1] + 1;
} else {
aboveCount = aboveCount[1];
};

plot isUnder = close(period = BasePeriod) crosses below lBound;
isUnder.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
isUnder.setdefaultColor(color.RED);
isUnder.setlineWeight(5);
def belowCount;
if isUnder then {
belowCount = if IsNaN(belowCount[1]) then 1 else belowCount[1] + 1;
} else {
belowCount = belowCount[1];
};

def totalCount = aboveCount + belowCount;

AddLabel(visible = yes, text = "No Of Bars: " + BarNumber() + ", Close Above = " + aboveCount + ", Close Below = " + belowCount + " , Total = " + totalCount, color = Color.LIGHT_GRAY);
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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