AddLabel woes...

netarchitech

Well-known member
Given the following script, I am trying to simply color a label...Works fine if as long as the value of the RSI isn't 0...Problem is RSI can be 0 and the label unfortunately disappears...

Code:
declare lower;

input showRSI = yes;
input showRSIlabel = yes;

def na = double.nan;

def length2 = 14;
def averageType = AverageType.WILDERS;
def price2 = close;

def NetChgAvg2 = MovingAverage(averageType, price2 - price2[1], length2);
def TotChgAvg2 = MovingAverage(averageType, AbsValue(price2 - price2[1]), length2);
def ChgRatio2 = if TotChgAvg2 != 0 then NetChgAvg2 / TotChgAvg2 else 0;

plot RSI = if showRSI then 50 * (ChgRatio2 + 1) else na;
RSI.SetDefaultColor(CreateColor(0, 102, 204));
RSI.SetLineWeight(3);

def RSIlabel = 50 * (ChgRatio2 + 1);
AddLabel(if showRSILabel then RSILabel else na,"  " + "RSI: " + Round(RSIlabel,4) + "  ", if RSILabel < 1 then Color.GREEN else if RSILabel > 99 then Color.RED else CreateColor(0, 102, 204));

Any thoughts? Thanks in advance...
 
Last edited:
Solution
Fixed... The first parameter simply needs to be yes or no, not Double.NaN which doesn't need to be assigned to a variable... Scripting languages are meant to be as comment-free readable as possible and assigning Double.NaN to a variable is redundant and wastes valuable CPU clock cycles... Us old bit-fiddlers attempt to make code efficient and readable... At any rate, the code below works...

Ruby:
declare lower;

input showRSI = yes;
input showRSIlabel = yes;

def length2 = 14;
def averageType = AverageType.WILDERS;
def price2 = close;

def NetChgAvg2 = MovingAverage(averageType, price2 - price2[1], length2);
def TotChgAvg2 = MovingAverage(averageType, AbsValue(price2 - price2[1]), length2);
def ChgRatio2 = if TotChgAvg2 != 0 then...
See the documentation for AddLabel() as you are missing the first parameter, yes... Not tested but the first parameter is required...

Ruby:
AddLabel(yes, if showRSILabel then RSILabel else na,"  " + "RSI: " + Round(RSIlabel,4) + "  ", if RSILabel < 1 then Color.GREEN else if RSILabel > 99 then Color.RED else CreateColor(0, 102, 204));
it gives an error, 3 params expected but 4 found while calling addlabel.
 
@Kashkian Thanks for your recent reply...That particular error has been resolved...

The final script is below for reference, until the 0 issue is resolved:

Code:
declare lower;

input showRSI = yes;
input showRSIlabel = yes;

def length2 = 14;
def averageType = AverageType.WILDERS;
def price2 = close;

def NetChgAvg2 = MovingAverage(averageType, price2 - price2[1], length2);
def TotChgAvg2 = MovingAverage(averageType, AbsValue(price2 - price2[1]), length2);
def ChgRatio2 = if TotChgAvg2 != 0 then NetChgAvg2 / TotChgAvg2 else 0;

plot RSI = if showRSI then 50 * (ChgRatio2 + 1) else double.nan;
RSI.SetDefaultColor(CreateColor(0, 102, 204));
RSI.SetLineWeight(3);

def RSIlabel = 50 * (ChgRatio2 + 1);
AddLabel(if RSILabel < 1 or RSILabel > 99 then Color.RED else CreateColor(0, 102, 204));
 
Last edited:
Fixed... The first parameter simply needs to be yes or no, not Double.NaN which doesn't need to be assigned to a variable... Scripting languages are meant to be as comment-free readable as possible and assigning Double.NaN to a variable is redundant and wastes valuable CPU clock cycles... Us old bit-fiddlers attempt to make code efficient and readable... At any rate, the code below works...

Ruby:
declare lower;

input showRSI = yes;
input showRSIlabel = yes;

def length2 = 14;
def averageType = AverageType.WILDERS;
def price2 = close;

def NetChgAvg2 = MovingAverage(averageType, price2 - price2[1], length2);
def TotChgAvg2 = MovingAverage(averageType, AbsValue(price2 - price2[1]), length2);
def ChgRatio2 = if TotChgAvg2 != 0 then NetChgAvg2 / TotChgAvg2 else 0;

plot RSI = if showRSI then 50 * (ChgRatio2 + 1) else Double.NaN;
RSI.SetDefaultColor(CreateColor(0, 102, 204));
RSI.SetLineWeight(3);

def RSIlabel = 50 * (ChgRatio2 + 1);
AddLabel(showRSILabel,"  " + "RSI: " + Round(RSIlabel,4) + "  ", if RSILabel < 1 then Color.GREEN else if RSILabel > 99 then Color.RED else CreateColor(0, 102, 204));
 
Solution
@rad14733 Thanks for your time and effort in this endeavor...It is much appreciated! I'm happy to say the elusive "0" issue has been resolved with your code snippet as well :cool:

While I aspire to achieve "bit-fiddler" status one day, in the meantime, I will continue to value and hold in high estimation the code efficiency and readability of bit-fiddlers such as yourself...Thanks again...
 

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