SMA/EMA Labels Help

DarthVaider

New member
I found this script and was super excited when I loaded it up and edited the colors and what not. Went to test it out today and realized that the values are incorrect. When I actually check the 13EMA, 50SMA & 200SMA values on my chart they are correct, when I check the figures in the actual label they are not corresponding to the lines on the chart which are the correct values.

Not sure if I can upload a photo from my computer.

So Labels Read:

13EMA - 572.38 Current Value: 621.85
50SMA - 573.59 Current Value: 673.60
200SMA - 573.43 Current Value: 586.84

I'm not sure why the Labels are not picking up the correct values.

Here is the script im using:

Code:
input price = close;
input length = 13;
input displace = 0;
input showBreakoutSignals = no;

plot AvgExp = ExpAverage(price[-displace], length);
plot UpSignal = price crosses above AvgExp;
plot DownSignal = price crosses below AvgExp;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

AvgExp.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

addlabel(Yes,"MovAvgExponential = " + price, color.greeN);


Please let me know what I am doing incorrect.

Thank You
 

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

@DarthVaider Use the Round() function. Something like Round(data,2) will round it to two decimal places.
I'll give that a try.

Last thing and I think i'll be good to go. I've got an indicator for the RSI which will display green if overbought, red of oversold and gray for just neutral. How can I display the RSI number in the label as well? Here is what I have:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.Color("OverBought") else if RSI < over_Sold then RSI.Color("OverSold") else RSI.Color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

AddLabel(yes, "RSI", if rsi > rsi[70] then Color.RED else if rsi < rsi[30] then Color.GREEN else Color.GRAY);
 
I'll give that a try.

Last thing and I think i'll be good to go. I've got an indicator for the RSI which will display green if overbought, red of oversold and gray for just neutral. How can I display the RSI number in the label as well? Here is what I have:

#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.Color("OverBought") else if RSI < over_Sold then RSI.Color("OverSold") else RSI.Color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

AddLabel(yes, "RSI", if rsi > rsi[70] then Color.RED else if rsi < rsi[30] then Color.GREEN else Color.GRAY);

@DarthVaider Well first, cut out all the unnecessary code such as the signals and overbought/oversold levels, and change the RSI plot into a variable. Then, put the variable into your label to get the finished result -
Code:
input length = 14;
input price = close;
input averageType = AverageType.WILDERS;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);

AddLabel(yes, "RSI = " + round(RSI), if rsi > rsi[70] then Color.RED else if rsi < rsi[30] then Color.GREEN else Color.GRAY);

I should have mentioned the the Round() function rounds to two decimal places by default. It's possible to adjust the number in the previous example for as few or as many decimal places as you like.
 
@DarthVaider Well first, cut out all the unnecessary code such as the signals and overbought/oversold levels, and change the RSI plot into a variable. Then, put the variable into your label to get the finished result -
Code:
input length = 14;
input price = close;
input averageType = AverageType.WILDERS;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);

AddLabel(yes, "RSI = " + round(RSI), if rsi > rsi[70] then Color.RED else if rsi < rsi[30] then Color.GREEN else Color.GRAY);

I should have mentioned the the Round() function rounds to two decimal places by default. It's possible to adjust the number in the previous example for as few or as many decimal places as you like.

That worked! How in the world did you get it to work so easy? Thank you so much.
 
@DarthVaider It wasn't hard at all - just follow my small explanation above while comparing your code to mine. You should be understand what I did. The main changes were converting the RSI plot into a variable (using "def") and also adding " + round(RSI) " into your label.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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