Percent Change Watchlist Column Issue

BullBear_

New member
Hello!

I was wondering if the more savvy coders here can help me with the below code. I'd like to get percentage change of underlying for the last month, last 2 weeks and YTD without having to manually update the code everytime. Is this possible?
Thanks!

Code:
#follow @KRose_TDA on twitter for updates to this and other custom scripts
#Script below finds the percentage return for the dates inputed
#Enter BegDate and EndDate using YYYYMMDD format (note the dates must
#be market open days Weekends will not work
#You can go back 250 bar days, 105 bar weekly, 110+ bar Monthly
#If you get NAN or other error message, check your dates to make sure they are #trading dates. If that does not correct the problem try using a longer aggregation #like Mo for Month.


input BegDate = 20220103;
input EndDate = 20220902;

def condBegDate = if condBegDate[1] > 0  then condBegDate[1] else if GetYYYYMMDD() == begDate then close else 0;

def condEndDate = if condEndDate[1] > 0  then condEndDate[1] else if GetYYYYMMDD() == endDate then close else 0;

def Pcond = ((condEndDate - condBegDate)/ condBegDate) * 100;
def Days = endDate - begDate;

plot Percnt = (Round(Pcond,2));

Percnt.AssignValueColor(if Percnt > 1.0 then Color.white else if Percnt < -1.0 then Color.white else Color.CURRENT); AssignBackgroundColor(if Percnt > 1.0 then Color.dark_GREEN else if Percnt < -1.0 then Color.dark_RED else Color.CURRENT);

#AddLabel(yes, Concat(Round(Pcond,2), "%"));
 
Hello!

I was wondering if the more savvy coders here can help me with the below code. I'd like to get percentage change of underlying for the last month, last 2 weeks and YTD without having to manually update the code everytime. Is this possible?
Thanks!

Code:
#follow @KRose_TDA on twitter for updates to this and other custom scripts
#Script below finds the percentage return for the dates inputed
#Enter BegDate and EndDate using YYYYMMDD format (note the dates must
#be market open days Weekends will not work
#You can go back 250 bar days, 105 bar weekly, 110+ bar Monthly
#If you get NAN or other error message, check your dates to make sure they are #trading dates. If that does not correct the problem try using a longer aggregation #like Mo for Month.


input BegDate = 20220103;
input EndDate = 20220902;

def condBegDate = if condBegDate[1] > 0  then condBegDate[1] else if GetYYYYMMDD() == begDate then close else 0;

def condEndDate = if condEndDate[1] > 0  then condEndDate[1] else if GetYYYYMMDD() == endDate then close else 0;

def Pcond = ((condEndDate - condBegDate)/ condBegDate) * 100;
def Days = endDate - begDate;

plot Percnt = (Round(Pcond,2));

Percnt.AssignValueColor(if Percnt > 1.0 then Color.white else if Percnt < -1.0 then Color.white else Color.CURRENT); AssignBackgroundColor(if Percnt > 1.0 then Color.dark_GREEN else if Percnt < -1.0 then Color.dark_RED else Color.CURRENT);

#AddLabel(yes, Concat(Round(Pcond,2), "%"));
See if this helps

Capture.jpg
Code:
AddLabel(1,  "%Chg: ", Color.WHITE);

def weekchg  = (close - close(period = "WEEK")[2]) / close(period = "WEEK")[2] * 100;
AddLabel(1, " 2wks: " + Round(weekchg) + "%" , if weekchg > 1.0 then Color.GREEN else if weekchg < -1.0 then Color.RED else Color.WHITE);

def monthchg = (close - close(period = "MONTH")[1]) / close(period = "MONTH")[1] * 100;
AddLabel(1, "Month: " + Round(monthchg) + "%", if monthchg > 1.0 then Color.GREEN else if monthchg < -1.0 then Color.RED else Color.WHITE);

def ytdchg   = (close - close(period = "YEAR")[1]) / close(period = "YEAR")[1] * 100;
AddLabel(1, "YTD: " + Round(ytdchg) + "%", if ytdchg > 1.0 then Color.GREEN else if ytdchg < -1.0 then Color.RED else Color.WHITE);
 
@SleepyZ : Much appreciated for helping out! I inserted the code you quoted above however there's a discrepancy with TOS calculations, for example AAPL graph (and the manual code I provided initially) give -14.39% and your suggested code gives -12.25% YTD. Do you know what's causing the difference? Thanks again.

gWYUuQw.png
 
@SleepyZ : Much appreciated for helping out! I inserted the code you quoted above however there's a discrepancy with TOS calculations, for example AAPL graph (and the manual code I provided initially) give -14.39% and your suggested code gives -12.25% YTD. Do you know what's causing the difference? Thanks again.

gWYUuQw.png
Yes, I used the close of the prior year of 177.57 to compare to the current close. Your chart timeframe starts at 1/3/2022, so you used the close of of 182.01 from that bar. I think the code I provided is correct. TOS will generally only use bar data from bars displayed on the chart you are viewing as in this case.
 
@SleepyZ : Much appreciated for helping out! I inserted the code you quoted above however there's a discrepancy with TOS calculations, for example AAPL graph (and the manual code I provided initially) give -14.39% and your suggested code gives -12.25% YTD. Do you know what's causing the difference? Thanks again.

gWYUuQw.png
that's awesome. can you share your scripts?
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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