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?
 
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);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@eugchen You sure?

NlEFrOu.png
 
i'm not sure how far back you want to look back.
in your 1st sentence, you mention YTD , year to date. which implies a change since the beginning of the year.
later you mention looking back to jan 19 , 2021 ?? which implies 1 year ago.

the study is looking at data from the 1st trading day of the year, so YTD.


then this
... 58.44/30.5= 91% drop. which is wrong. 58.44 to 30.50 is a 48% drop.
... at 33.47 on jan 3 2022 = 33.47/30.5 = 9.7%. 33.47 to 30.5 is a 8.9% drop

------------------------------------

to test your study, i copied it into a column. i set the time to day.

i set it to the most basic version it could be.
i changed it to plot just counter2, so i could see what index it was using.
the numbers in the column came out to 10. 10 days before today was the first day of the year.
that part is working.
Code:
def date = getYear();
def JanFirstClose = fold counter2 = 0 to 255
  while date == Date[counter2]
  do counter2;
plot z = JanFirstClose;


then i changed it so it just displayed the close price of x bars ago. (now i know it to be 10 bars ago, jan 3)
i compared the column numbers of several stocks, to chart bars and verified the prices are from jan 3.
Code:
def date = getYear();
def JanFirstClose = fold counter2 = 0 to 255
  while date == Date[counter2]
  do close[counter2];
plot z = JanFirstClose;

it is getting the correct daily close price, of the first trading day of the year.
so either that isn't the date you think it is or your % math is wrong.
How can I create a percent return calculation for a two, three, ...five year period?
 
How can I create a percent return calculation for a two, three, ...five year period?
Can someone add 1Y, 3Y and 5Y % returns to BenTen Script? Thanks
This is a YTD script. The logic can't be applied to prior years.

You could post a request in the Question Forums.
Sounds like something you may have to post a picture of to explain what you are trying to accomplish with a more detailed explanation.
IE: percentage of what? Year to year? Each year to current? etc…
Unsure of how to upload screenshots to the forum, Here are directions.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
310 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