Price Perfo

Nick

New member
Years ago, I received this script
https://usethinkscript.com/threads/ytd-percent-issue-working-on-futures.1116/
by attending an AMTD webinar or from the Thinkscript lounge. Recently, I applied it to one of my charts and the labels do not appear. I have spent a fair amount of time trying to find the problem, unsuccessfully. There are no error messages.
It is a nice addition to a chart showing a label for the YTD, monthly, and weekly price changes.
Can someone fix the code?
Code:
# PercYMWTDpercRet2_Label_TOS_sha

declare hide_on_intraday ;

#def allows you to teach ThinkScript new "words" that you can reference later in your code
#GetYYYYMMDD() returns today's date
#there are 252 trading days each year

def yearstart = getyear() * 10000 + 101;
def tradedays = countTradingDays(yearstart ,  GetYYYYMMDD()) ;
def closeEOY = getvalue(close ,  tradedays ,  252) ;
def YTDnetchange = ((close - closeEOY)/closeEOY) * 100 ;

AddLabel(yes,  "Year: " + yearstart);
#YTDnetchange could have been the plot line ,  however to view results with a limited number of decimal places that line was another def or definition for ThinkScript.  And a new "word" value was used for the plot line in this code.  2 means round to 2 decimal places

def value = round(YTDnetchange ,  2) ;


#AddLabel tells ThinkScript how you want the results of your code diplayed.  Yes means do show the label.  Words or sybols in " " will appear with your label.
#This label could have been created complicated using concat function ... or the simplified method of using a + sign between values and text you want to appear in the label

AddLabel(yes ,  "YTD change: " + value + "%" ,  if value == 0
     then Color.LIGHT_GRAY
     else if value > 0
     then CreateColor(51 , 204 , 0)
     else color.RED) ;

#any watchlist column label can be quickly converted into a chart label via copy/paste/tweak into a new chart Study or Strategy.  HOWEVER AssignBackgroundColor on a chart label will change entire background of the chart when condition is met

#AssignBackgroundColor(if value < 0
#     then Color.RED
#     else if value > 0
#     then Color.GREEN
#     else color.LIGHT_GRAY) ;


#def monthstart = getMonth() * 10000 + 101 ;
#def tradedaysMTD = countTradingDays(monthstart ,  GetYYYYMMDD()) ;
#def MTDcloseEOY = getvalue(close ,  tradedaysMTD ,  21) ;
#def MTDnetchange = ((close - MTDcloseEOY)/MTDcloseEOY) * 100 ;

def MonthClose = if GetLastMonth() == GetMonth() then close(period = aggregationPeriod.Month)[1] else Double.NaN ;
def MTDnetchange = ((close - MonthClose) / MonthClose) * 100 ;

def valueMTD = round(MTDnetchange ,  2) ;

AddLabel(yes ,  "Month MTD change: " + valueMTD + "%" ,  if valueMTD == 0
     then Color.LIGHT_GRAY
     else if valueMTD > 0
     then CreateColor(51 , 204 , 0)
     else color.RED) ;



def WeekClose = if GetLastWeek() == GetWeek() then close(period = aggregationPeriod.Week)[1] else Double.NaN ;
def WTDnetchange = ((close - WeekClose) / WeekClose) * 100 ;

def valueWTD = round(WTDnetchange ,  2) ;

AddLabel(yes ,  "Week WTD change: " + valueWTD + "%" ,  if valueWTD == 0
     then Color.LIGHT_GRAY
     else if valueWTD > 0
     then CreateColor(51 , 204 , 0)
     else color.RED) ;
 
Last edited by a moderator:
Years ago, I received this script by attending an AMTD webinar or from the Thinkscript lounge. Recently, I applied it to one of my charts and the labels do not appear. I have spent a fair amount of time trying to find the problem, unsuccessfully. There are no error messages.
It is a nice addition to a chart showing a label for the YTD, monthly, and weekly price changes.
Can someone fix the code?
Code:
# PercYMWTDpercRet2_Label_TOS_sha

declare hide_on_intraday ;

#def allows you to teach ThinkScript new "words" that you can reference later in your code
#GetYYYYMMDD() returns today's date
#there are 252 trading days each year

def yearstart = getyear() * 10000 + 101;
def tradedays = countTradingDays(yearstart ,  GetYYYYMMDD()) ;
def closeEOY = getvalue(close ,  tradedays ,  252) ;
def YTDnetchange = ((close - closeEOY)/closeEOY) * 100 ;

AddLabel(yes,  "Year: " + yearstart);
#YTDnetchange could have been the plot line ,  however to view results with a limited number of decimal places that line was another def or definition for ThinkScript.  And a new "word" value was used for the plot line in this code.  2 means round to 2 decimal places

def value = round(YTDnetchange ,  2) ;


#AddLabel tells ThinkScript how you want the results of your code diplayed.  Yes means do show the label.  Words or sybols in " " will appear with your label.
#This label could have been created complicated using concat function ... or the simplified method of using a + sign between values and text you want to appear in the label

AddLabel(yes ,  "YTD change: " + value + "%" ,  if value == 0
     then Color.LIGHT_GRAY
     else if value > 0
     then CreateColor(51 , 204 , 0)
     else color.RED) ;

#any watchlist column label can be quickly converted into a chart label via copy/paste/tweak into a new chart Study or Strategy.  HOWEVER AssignBackgroundColor on a chart label will change entire background of the chart when condition is met

#AssignBackgroundColor(if value < 0
#     then Color.RED
#     else if value > 0
#     then Color.GREEN
#     else color.LIGHT_GRAY) ;


#def monthstart = getMonth() * 10000 + 101 ;
#def tradedaysMTD = countTradingDays(monthstart ,  GetYYYYMMDD()) ;
#def MTDcloseEOY = getvalue(close ,  tradedaysMTD ,  21) ;
#def MTDnetchange = ((close - MTDcloseEOY)/MTDcloseEOY) * 100 ;

def MonthClose = if GetLastMonth() == GetMonth() then close(period = aggregationPeriod.Month)[1] else Double.NaN ;
def MTDnetchange = ((close - MonthClose) / MonthClose) * 100 ;

def valueMTD = round(MTDnetchange ,  2) ;

AddLabel(yes ,  "Month MTD change: " + valueMTD + "%" ,  if valueMTD == 0
     then Color.LIGHT_GRAY
     else if valueMTD > 0
     then CreateColor(51 , 204 , 0)
     else color.RED) ;



def WeekClose = if GetLastWeek() == GetWeek() then close(period = aggregationPeriod.Week)[1] else Double.NaN ;
def WTDnetchange = ((close - WeekClose) / WeekClose) * 100 ;

def valueWTD = round(WTDnetchange ,  2) ;

AddLabel(yes ,  "Week WTD change: " + valueWTD + "%" ,  if valueWTD == 0
     then Color.LIGHT_GRAY
     else if valueWTD > 0
     then CreateColor(51 , 204 , 0)
     else color.RED) ;

This should fix it to display. The problem was the dynamic offset in this line of 252 was too large.

def closeEOY = getvalue(close , tradedays , 252) ; replaced it with def closeEOY = getvalue(close , tradedays) ;

Full code with changes

Ruby:
# PercYMWTDpercRet2_Label_TOS_sha

declare hide_on_intraday ;

#def allows you to teach ThinkScript new "words" that you can reference later in your code
#GetYYYYMMDD() returns today's date
#there are 252 trading days each year

def yearstart = GetYear() * 10000 + 101;
def tradedays = CountTradingDays(yearstart ,  GetYYYYMMDD()) ;
def closeEOY =  getvalue(close ,  tradedays) ;
def YTDnetchange = ((close - closeEOY) / closeEOY) * 100 ;

AddLabel(yes,  "Year: " + AsPrice(yearstart));
#YTDnetchange could have been the plot line ,  however to view results with a limited number of decimal places that line was another def or definition for ThinkScript.  And a new "word" value was used for the plot line in this code.  2 means round to 2 decimal places

def value = Round(YTDnetchange ,  2) ;


#AddLabel tells ThinkScript how you want the results of your code diplayed.  Yes means do show the label.  Words or sybols in " " will appear with your label.
#This label could have been created complicated using concat function ... or the simplified method of using a + sign between values and text you want to appear in the label

AddLabel(yes ,  "YTD change: " + value + "%" ,  if value == 0
     then Color.LIGHT_GRAY
     else if value > 0
     then CreateColor(51 , 204 , 0)
     else Color.RED) ;

#any watchlist column label can be quickly converted into a chart label via copy/paste/tweak into a new chart Study or Strategy.  HOWEVER AssignBackgroundColor on a chart label will change entire background of the chart when condition is met

#AssignBackgroundColor(if value < 0
#     then Color.RED
#     else if value > 0
#     then Color.GREEN
#     else color.LIGHT_GRAY) ;


#def monthstart = getMonth() * 10000 + 101 ;
#def tradedaysMTD = countTradingDays(monthstart ,  GetYYYYMMDD()) ;
#def MTDcloseEOY = getvalue(close ,  tradedaysMTD ,  21) ;
#def MTDnetchange = ((close - MTDcloseEOY)/MTDcloseEOY) * 100 ;

def MonthClose = if GetLastMonth() == GetMonth() then close(period = AggregationPeriod.MONTH)[1] else Double.NaN ;
def MTDnetchange = ((close - MonthClose) / MonthClose) * 100 ;

def valueMTD = Round(MTDnetchange ,  2) ;

AddLabel(yes ,  "Month MTD change: " + valueMTD + "%" ,  if valueMTD == 0
     then Color.LIGHT_GRAY
     else if valueMTD > 0
     then CreateColor(51 , 204 , 0)
     else Color.RED) ;



def WeekClose = if GetLastWeek() == GetWeek() then close(period = AggregationPeriod.WEEK)[1] else Double.NaN ;
def WTDnetchange = ((close - WeekClose) / WeekClose) * 100 ;

def valueWTD = Round(WTDnetchange ,  2) ;

AddLabel(yes ,  "Week WTD change: " + valueWTD + "%" ,  if valueWTD == 0
     then Color.LIGHT_GRAY
     else if valueWTD > 0
     then CreateColor(51 , 204 , 0)
     else Color.RED) ;
 

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