YTD Percent - Issue working on Futures

mc01439

mc01439

Well-known member
2019 Donor
VIP
Any thoughts on why this code does not work with futures?



Code:
#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;

#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

plot 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, value + "% YTD", color.BLACK);

#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);
 
T

tomsk

Well-known member
VIP
@mc01439 For some strange reason the moment I attempted to load your code into the watchlist, the system complained about the following statement. Hence I was unable to get this working even for equities. Odd!

def tradedays = countTradingDays(yearstart, GetYYYYMMDD());

Here's a workaround. I revamped the YTD Percent Watchlist. It works great for futures like /ES, /CL as well as for equities.
Here's the code, incorporating your color definitions. Have a go with this. Hopefully it will work better for you

Code:
# YTD Percent Watchlist
# Tomsk
# 11.20.2019

def YearOpen = if GetYear() != GetLastYear() then open else YearOpen[1];
def value = Round(100 * ((close - YearOpen) / YearOpen),2);
AddLabel(yes, value + "% YTD", color.BLACK);
AssignBackgroundColor(if value < 0
                      then Color.RED
                      else if value > 0
                      then Color.GREEN
                      else color.LIGHT_GRAY);
# End Code YTD Percent Watchlist
 
wilmanv

wilmanv

Member
VIP
Any script to show me YTD change on any symbol

Thanks
 

Similar threads

Top