# Watchlist column Simple 200 day Moving Average

#### DanielManahan

##### Member
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

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);``
@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);``

great thanks. I will check out your code and see what I did wrong and learn.

yes that worked and I learned a bit too.

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