Watchlist column Simple 200 day Moving Average

the Watchlist Column for SMA doesn't allow the user to select the number of lookback bars on the daily time frame

suppose we want to have a column return a 0 (N/A), 1 (bearish), or 2 (bullish) based on the close of the current candle in relation to crossing the 200 day simple moving average. a stock is bullish if above the 200 day and bearish if below

I need to correct the following code. this isn't working and only returning a 2. it should return all three numbers. I get an error on half of the stocks in a watchlist NAN

can someone please help me fix this script?

declare lower;
def lookback = 200;
def sma = SimpleMovingAvg();
def bull = close() crosses above sma;
def bear = close() crosses below sma;
def bull_lookback = highest(bull, lookback);
def bear_lookback = highest(bear, lookback);
plot signal = if bull_lookback then 2 else if bear_lookback then 1 else 0;
 
Solution
@DanielManahan Try this (coded in chat, might have errors) -
Code:
# watchlist column
def lookback = 200;
def sma = average(close, lookback);
plot signal = if close > sma then 2 else if close < sma then 1 else 0;
As always, you must choose the watchlist aggregation from the TOS-provided selection. Also, if the stock has traded for less than 200 days, you will still get a NaN or a 0 result since there will not be enough bars to calculate the moving average.

Here are some customization choices, not necessary of course.
To change the color of the 2, 1, or 0, paste this below the plot -
Code:
signal.AssignValueColor(if close > sma then color.green else if close < sma then color.red else color.white);
To add background color...
@DanielManahan Try this (coded in chat, might have errors) -
Code:
# watchlist column
def lookback = 200;
def sma = average(close, lookback);
plot signal = if close > sma then 2 else if close < sma then 1 else 0;
As always, you must choose the watchlist aggregation from the TOS-provided selection. Also, if the stock has traded for less than 200 days, you will still get a NaN or a 0 result since there will not be enough bars to calculate the moving average.

Here are some customization choices, not necessary of course.
To change the color of the 2, 1, or 0, paste this below the plot -
Code:
signal.AssignValueColor(if close > sma then color.green else if close < sma then color.red else color.white);
To add background color, paste this below the above code -
Code:
AssignBackgroundColor(if close > sma then color.dark_green else if close < sma then color.dark_red else color.dark_gray);
 
Solution

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