working on a script and trying to see how to have it find the increase in price for last 3 months, 6 months, 1 year and 2 years.

Solution
I see how to have it track trading days, but I want it to track by the timeframe.
This should help. If you want to use different closes for prior month end figures, select input debug = yes and find the close per how they were done in the code and make any changes similarly.

Capture.jpg
Ruby:
def month    = GetMonth();
def candles  = !IsNaN(close);
def capture  = candles and month != month[1];
def moCount  = CompoundValue(1, if capture then moCount[1] + 1 else moCount[1], 0);
def thismo   = (HighestAll(moCount) - moCount) ;

def close3   = if thismo[1] == 4 and thismo == 3 then close[1] else close3[1];
def close6   = if thismo[1] == 7 and thismo == 6 then close[1] else close6[1];
def close12  = if thismo[1] == 13 and...
I see how to have it track trading days, but I want it to track by the timeframe.
This should help. If you want to use different closes for prior month end figures, select input debug = yes and find the close per how they were done in the code and make any changes similarly.

Capture.jpg
Ruby:
def month    = GetMonth();
def candles  = !IsNaN(close);
def capture  = candles and month != month[1];
def moCount  = CompoundValue(1, if capture then moCount[1] + 1 else moCount[1], 0);
def thismo   = (HighestAll(moCount) - moCount) ;

def close3   = if thismo[1] == 4 and thismo == 3 then close[1] else close3[1];
def close6   = if thismo[1] == 7 and thismo == 6 then close[1] else close6[1];
def close12  = if thismo[1] == 13 and thismo == 12 then close[1] else close12[1];
def close24  = if thismo[1] == 25 and thismo == 24 then close[1] else close24[1];


AddLabel(1, "3mo " + ": " + close + "/" + close3 + " = " + (AsPercent(close / close3)), if ((close / close3)) < 1 then Color.PINK else Color.LIGHT_GREEN );
AddLabel(1, "6mo " + ": " + close + "/" + close6 + " = " + (AsPercent(close / close6)), if ((close / close6)) < 1 then Color.PINK else Color.LIGHT_GREEN );
AddLabel(1, "12mo " + ": " + close + "/" + close12 + " = " + (AsPercent(close / close12)), if ((close / close12)) < 1 then Color.PINK else Color.LIGHT_GREEN );
AddLabel(1, "24mo " + ": " + close + "/" + close24 + " = " + (AsPercent(close / close24)), if ((close / close24)) < 1 then Color.PINK else Color.LIGHT_GREEN );

input debug = no;
plot x = if !debug then double.nan else thismo;
x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
 
Solution

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

I see how to have it track trading days, but I want it to track by the timeframe.
I repurposed one of the codes I use for daily price movement to fetch movement by time frame.

plot x = Round(100 * ((close / close[255) - 1), 1);

That will return % gain or loss as a custom watchlist column for a year +/- a few days.

If you're wondering why I used 255 days, it's to account for weekends. I was trying to figure out
why the results didn't match the chart and then it hit me. So if you're adjusting for a period of months,
make sure to remova ~8 days per month (plus any holidays) to get accurate results.

Edit: I just re-read your above post and saw that you're trying to track specific timeframes.
You can do this manually by adjusting the above code to the following:

plot x = Round(100 * ((close / open) - 1), 1);

x.AssignValueColor( if x < 0 then Color.ORANGE else Color.CYAN);

Run the code as a custom wathlist column but set aggregation to yearly/monthly/etc in the editor
dropdown menu. Try it out!
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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