ATR long or short Label in Watchlist

Hi All!

Been trying to code a Watchlist label where:
  • Daily aggregation
  • Label turns Green when current Price is >= plus 1 ATR from Yesterday's Closing
or Label turns Red when current Price is <= minus 1 ATR from Yesterday's Closing

Below is my effort in coding it. The Long signal works but cannot make the label work both on long and short. Thanks in advanced. (Mod, please delete in case there's already something like this posted here; just can't find it).

Code:
### Initial Computations

## ATR

# ATR
input PeriodATR = aggregationPeriod.DAY;
input length_atr = 14;
input averageType_atr = AverageType.WILDERS;

def pre_ATR = MovingAverage(averageType_atr, TrueRange(high(period = PeriodATR), close(period = PeriodATR), low(period = PeriodATR)), length_atr);
def ATR = pre_ATR;


# ATR percentage
def pre_ATR_perc = (pre_ATR/close()) * 100;
def ATR_perc = pre_ATR_perc;


## All Closings: for computation ##

# Closings
input aggregationPeriod_Closing = AggregationPeriod.DAY;

def Closing = close(period = aggregationPeriod_Closing);


## Closings +/- ATR

# Upper
def Plus_ATR = Closing + ATR;

# Lower
def Minus_ATR = Closing - ATR;


## Conditions
def pre_Short = if (Closing <= Minus_ATR[1]) then 1 else double.NaN;

#plot Short = pre_Short;

def pre_Long = if (Closing >= Plus_ATR[1]) then 1 else double.NaN;

#plot Long = pre_Long;

def pre_Signal = (if pre_Long == 1 then 1 else if pre_Short == 1 then -1 else double.NaN);

plot Signal = pre_Signal;

AddLabel (yes, Signal, color.Black);# maybe not needed


Signal.AssignValueColor(if Signal == 1 then Color.GREEN else Color.Black);

AssignBackgroundColor(if Signal == 1 then Color.GREEN else Color.Black);


Signal.AssignValueColor(if Signal < 0 then Color.Red else Color.Black);

AssignBackgroundColor(if Signal < 0 then Color.Red else Color.Black);
 
Hi All!

Been trying to code a Watchlist label where:
  • Daily aggregation
  • Label turns Green when current Price is >= plus 1 ATR from Yesterday's Closing
or Label turns Red when current Price is <= minus 1 ATR from Yesterday's Closing

Below is my effort in coding it. The Long signal works but cannot make the label work both on long and short. Thanks in advanced. (Mod, please delete in case there's already something like this posted here; just can't find it).

Code:
### Initial Computations

## ATR

# ATR
input PeriodATR = aggregationPeriod.DAY;
input length_atr = 14;
input averageType_atr = AverageType.WILDERS;

def pre_ATR = MovingAverage(averageType_atr, TrueRange(high(period = PeriodATR), close(period = PeriodATR), low(period = PeriodATR)), length_atr);
def ATR = pre_ATR;


# ATR percentage
def pre_ATR_perc = (pre_ATR/close()) * 100;
def ATR_perc = pre_ATR_perc;


## All Closings: for computation ##

# Closings
input aggregationPeriod_Closing = AggregationPeriod.DAY;

def Closing = close(period = aggregationPeriod_Closing);


## Closings +/- ATR

# Upper
def Plus_ATR = Closing + ATR;

# Lower
def Minus_ATR = Closing - ATR;


## Conditions
def pre_Short = if (Closing <= Minus_ATR[1]) then 1 else double.NaN;

#plot Short = pre_Short;

def pre_Long = if (Closing >= Plus_ATR[1]) then 1 else double.NaN;

#plot Long = pre_Long;

def pre_Signal = (if pre_Long == 1 then 1 else if pre_Short == 1 then -1 else double.NaN);

plot Signal = pre_Signal;

AddLabel (yes, Signal, color.Black);# maybe not needed


Signal.AssignValueColor(if Signal == 1 then Color.GREEN else Color.Black);

AssignBackgroundColor(if Signal == 1 then Color.GREEN else Color.Black);


Signal.AssignValueColor(if Signal < 0 then Color.Red else Color.Black);

AssignBackgroundColor(if Signal < 0 then Color.Red else Color.Black);

why are you using 2nd aggregation?
why not just set the watchlist time to day?

can use only 1 AssignBackgroundColor() in a study. combine them
should load only 1 study having AssignBackgroundColor().

you have plots and labels. should only use 1, 1 plot OR 1 addlabel
 

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

why are you using 2nd aggregation?
why not just set the watchlist time to day?

can use only 1 AssignBackgroundColor() in a study. combine them
should load only 1 study having AssignBackgroundColor().

you have plots and labels. should only use 1, 1 plot OR 1 addlabel


What I posted above was actually a hodgepodge of codes I got from somewhere (it just proves my lack of proper coding skills).

Thanks a lot for the pointers though coz I was able to figure it out (i think). It now works.

Working code below. Again, thanks a lot.

Code:
### Plus/ Minus ATR watchlist


# ATR
input length_atr = 40;
input averageType_atr = AverageType.WILDERS;
def ATR = MovingAverage(averageType_atr, TrueRange(high, close, low), length_atr);

# ATR percentage
def ATR_perc = (ATR/close()) * 100;

# Closings
def Closing = close;

# Upper ATR
def Plus_ATR = Closing + ATR;

# Lower ATR
def Minus_ATR = Closing - ATR;

# Conditions
plot Signal = if (Closing >= Plus_ATR[1]) then 1 else if (Closing <= Minus_ATR[1]) then -1 else 0;

# Value and Background colors
Signal.AssignValueColor(if Signal== 1 then Color.GREEN else if Signal == -1 then color.red else Color.Black);
AssignBackgroundColor(if Signal == 1 then Color.GREEN else if Signal == -1 then Color.Red else Color.Black);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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