YTD Percent - Issue working on Futures

mc01439

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

oozGdkC.png


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

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
 

CaptG907

New member
@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
How do you add this to your watchlist?
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Why is it that when you sort YTD Percent it is not in order? For example
5.87% then 59.7% then 6.01%
Thanks for your time.
This watchlist column is not formatted as a number so it can't be sorted as numbers.
It sorts alphanumeric so all the 'fives' (ie: 5.87% and 59.7%) are sorted together.)
Dark Mode people need the extra formatting that comes with the alphanumeric sort, ie: black text.
Here is the script formatted as a number but you have to live with white text and no % signs as numeric formats don't have those.
Code:
# YTD Percent Watchlist
# Tomsk
# 11.20.2019

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

CaptG907

New member
Can someone help me with this code? I'm looking for just numbers and not percentages?
Thank you

#HINT: This chart label shows the percent change down from the high.above \n\n IMPORTANT: If a new IPO you need to change the length to max of number of days since IPO release.

input length = 52; #HINT length: There are 252 trading days in a year
input show_label = yes;
input Label_Color_Choice = {"magenta", "green", "pink", "cyan", "orange", default "red", "blue", "gray", "violet"};

def top = highest(high(period = AggregationPeriod.WEEK), length);
def prctDrop = ((close(period = AggregationPeriod.week)/top)-1);

def NewHigh = high == top;

AddLabel(show_label and !NewHigh, AsPercent(prctDrop), GetColor(Label_Color_Choice));
 

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.
Top