Recursive(?) Code for Atypical Look-back Period

Hi All -

Having some trouble coding up something to use as part of a scanning tool.
I'm wanting to multiply gross monthly returns and end up with a cumulative net return for the lookback period (e.g.: +1.5% return from stock ABC last month with a -2.0% return from ABC the month prior, we have (1 + 0.015)(1 - 0.02) - 1= -0.53% for that 2 month period) as a part of a momentum model I'm putting together.
The lookback period I intend to use is going back 12 months, but I want to exclude the most recent month. My hunch is that some clean script can be written recursively, but I'm having trouble getting it together (after days of research and attempts).
Once built out, the intention is to use it as part of a scan so that the stocks with the highest cumulative net return for that lookback period can be identified.
I greatly appreciate any input, and thank you kindly for taking the time/effort.
 
Solution
p3vZcpS.png

Code:
def f = 0.015;
def s = -0.02;
def a = ((1 + F) * (1 + s)) - 1;
addlabel(yes,aspercent(a),color.lime);

def Ch =
    ((Close - Close[1]) / Close[1]);
def CNR =
    ( Fold Index = 2 to 12
      with Data = (1 + Ch[1]) do
      Data * (1 + GetValue(Ch,index))
    ) - 1;
plot Test = CNR;
plot z = 0;

It excludes the current month by initialing with the prior month...
Data = (1 + Ch[1])

...and then iterates from the next prior month backwards.
Index = 2 to 12

12 is the point of termination, that iteration is not evaluated.
For example, 0 to 5 would be 0, 1, 2, 3, 4.

So, if it's December, it should calculate November through January.
However, January's own % change is...

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

@Autodidact's Paradox Unfortunately, any form of recursion is difficult at best within Thinkscript as the scripting language was not designed to support true recursion... On top of that, many of the standard functionalities that we can use in Studies, Strategies, and Custom Watchlist Columns will not work within StockHacker Scans or OptionHacker Scans... Fortunately for us, StockHacker supports more than OptionHacker... Once we see what you're working on we can better make a determination as to whether or not your concept is feasible...
 
p3vZcpS.png

Code:
def f = 0.015;
def s = -0.02;
def a = ((1 + F) * (1 + s)) - 1;
addlabel(yes,aspercent(a),color.lime);

def Ch =
    ((Close - Close[1]) / Close[1]);
def CNR =
    ( Fold Index = 2 to 12
      with Data = (1 + Ch[1]) do
      Data * (1 + GetValue(Ch,index))
    ) - 1;
plot Test = CNR;
plot z = 0;

It excludes the current month by initialing with the prior month...
Data = (1 + Ch[1])

...and then iterates from the next prior month backwards.
Index = 2 to 12

12 is the point of termination, that iteration is not evaluated.
For example, 0 to 5 would be 0, 1, 2, 3, 4.

So, if it's December, it should calculate November through January.
However, January's own % change is from December of the prior year.
((Close - Close[1]) / Close[1]);
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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