How can I add chart warnings or arrows to plots based on conditions?

mufasis

New member
So I just created this simple script for z-score and I want to create some arrows or warnings when z-score is above 3 or below -3, how can I do that? Thanks!

# Declare
declare lower;

# Inputs
input price = close;
input length = 21;

# Calculate rolling mean and standard deviation
def mean = movingAverage(averageType.SIMPLE, price, length);
def deviation = standardDeviation(price, length);

# Calculate Z-Score
def z_score = (price - mean) / deviation;

# Plots
plot zscore = z_score;
plot zscore_avg = movingAverage(averageType.SIMPLE, z_score, length);
plot zero = 0;
plot three = 3;
plot negativeThree = -3;

# Set Z-Score to histogram and set colors
zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
zscore.assignValueColor(if zscore > 0 then color.GREEN else color.red);

# Define signals

# Plot signals
 
You can not use a lower study to plot arrows (or anything) on an upper. You can use AssignPriceColor() to change the candle color, but that's one of the few things you can do.

To make your z-score study into an upper, you need to convert the plot statements to def statements, and then add two plots...
Code:
declare upper;

# Inputs
input price = close;
input length = 21;

# Calculate rolling mean and standard deviation
def mean = movingAverage(averageType.SIMPLE, price, length);
def deviation = standardDeviation(price, length);

# Calculate Z-Score
def z_score = (price - mean) / deviation;

# Plots
def zscore = z_score;
def zscore_avg = movingAverage(averageType.SIMPLE, z_score, length);
def zero = 0;
def three = 3;
def negativeThree = -3;

# Set Z-Score to histogram and set colors
# zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
# zscore.assignValueColor(if zscore > 0 then color.GREEN else color.red);

# Define signals
def too_low = if z_score < negativeThree then 1 else 0;
def too_high = if z_score > three then 1 else 0;

# Plot signals
plot low_signal = if too_low == 1 then low else double.nan;
plot high_signal = if too_high == 1 then high else double.nan;

# Style Signals
low_signal.setPaintingStrategy(paintingStrategy.POINTS);
high_signal.setPaintingStrategy(paintingStrategy.POINTS);
low_signal.SetLineWeight(3);
high_signal.SetLineWeight(3);
low_signal.SetDefaultColor(color.red);
high_signal.SetDefaultColor(color.green);

-mashume
 
High and Low are price points and tell the script where (on the vertical / price axis) to plot the line.

This should help explain some of the rest:

GR3R1Jt.png


-mashume
 

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
282 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