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?
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: