YTD (Year to Date) Return Label for ThinkorSwim

eugchen

Member
Anyway to get a week to date chart label? So specifically, id like a label on the chart that shows the performance assuming monday start to current price. so lets say Monday open on a stock is $100, and now its wed and stock is $110. id like a label saying "WTD: 10%"

someone suggested a code like this for me, but doesnt seem to work right.

Code:
#use on daily aggregation only   
def newweek = getweek() != getweek()[1];
def openweek= if newweek then close else openweek[1];
def wtdchange = ((close-openweek)/openweek)*100;
addlabel(yes, "WTD %Change " + AsPercent(wtdchange));

On a similar question, what about a year to date label?
 

BenTen

Administrative
Staff
VIP
This will give you the YTD and MTD return as labels on your chart. It should be easy to change one of them to display Week to Date return.

Code:
# YTD Return
# Mobius
# 06.15.2015

declare hide_on_intraday;

input price = close;
input length = 1;
input displace = 0;
input Show_Labels = yes;

def newyear = if getYear() != getYear()[1] then 1 else 0;
def newmonth = if getMonth() != getMonth()[1] then 1 else 0;
def yo = compoundValue(1, if newyear then open else yo[1], open);
def mo = compoundValue(1, if newmonth then open else mo[1], open);
def yearopen = yo;
def monthopen = mo;
def SMA = Average(price[-displace], length);
def yochange = (sma - yearopen) / yearopen ;
def mochange = (close - monthopen) / monthopen;

AddLabel(Show_Labels, concat("Year % change = ", AsPercent(yochange)), color.white);
AddLabel(Show_Labels, "Month % change = " + (AsPercent(mochange)), color.white);

Here is another one by Paris in the thinkScript Lounge. Percent Change YTD for watchlists.

Code:
# percent change YTD as custom column on watch list

# Script created by TOS that show year to date percentages.
# This is setup for watchlists. zzz modified them to make the
# background color optional, in this case for charting, it is "no".

#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;
def YTDnetchange = ((close - closeEOY)/closeEOY);

#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);
plot value = YTDnetchange;

value.hide();

#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);
AddLabel(yes, AsPercent(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
input showbackgroundcolor = yes;
AssignBackgroundColor(if showbackgroundcolor then if value < 0
     then Color.RED
     else if value > 0
     then Color.GREEN
     else color.LIGHT_GRAY else color.current);
 

BenTen

Administrative
Staff
VIP
@eugchen You sure?

NlEFrOu.png
 

rad14733

Well-known member
VIP
Please use the Search feature prior to asking questions... What you are looking for quite possibly already exists, or you may find something that would work as a good starting point... 💡
 

Similar threads

Top