Back

90 Day Annualized Regression Slope and R-squared

Hi all -very new here with limited to no coding experience but keen to learn. I have gone through some of the C# tutorials on the Microsoft site but may be trying to move too fast here. I have just read Andreas Clenow's book "Stocks on the Move" (great book) and I want to be able to code a backtest where by I buy members of the S&P500 based on their adjusted slope values. Basically, I want to:

1. Determine the 90 day annualized exponential regression slope for each stock
2. Determine the 90 day R-squared value for each of the stock
3. Multiply #1 * #2 and then sort from highest to lowest
4. Buy the top stocks based on the formula ((AccountValue * 0.001)/ATR(20))

There is some other logic that helps to determine when to buy and when to sell (like buy only if the S&P500 is above its 200 day SMA) I am not even sure where to start; but am not asking for someone to do it for me. Is there any suggestions on where to start with a coding challenge like this? Is there a better way to go about building this algorithm?

Thanks in advance!
Update Backtest








Hey @Jeremy, welcome to QuantConnect!

It looks like you have done the hardest part already, defining your strategy. Once I have a strategy defined, my next steps are typically to break the problem down into small pieces. It looks like you've done this already as well. After that, I try and write some reusable code that solves each piece of the puzzle. Once all pieces are solved, then I write the main algorithm which 'glues' them all together.

So for the steps given, I would probably tackle it like this:
1. Define an indicator that produces the exponential regression slope
2. Define an indicator that produces the R-squared value
3. This can be done easily within the algorithm 'glue' code
4. Can make use of the AverageTrueRange indicator and Portfolio.TotalPortfolioValue to perform this calculation

Creating an indicator can be easy or complicated depending on the math and data dependencies required. There are tons of examples of indicators available in github. If you have trouble writing the indicator class please feel free to post in a new thread, but be sure to include a link to some reference material and preferably some example calculations.

Once the indicators are defined, the next step I would take is writing the 'glue' code in the algorithm which combines all the pieces together. The main entry point for data into your algorithm is the OnData methods, this is where the bulk of 'glue' code will go, inspecting the indicators and making buy/sell decisions.

After all that is said and done, the next steps are intense testing and one of the most important features of any algorithm is risk control... but let's not get the cart ahead of the horse here!

Have a peak at this other thread where I describe my pattern for storing indicators and other information specific to a symbol in the algorithm.

I've written quite a bit here, let me know if you need clarification or help with anything!

Some helpful tips for new coders:
- Try to keep the algorithm code as 'clean' as possible, hiding away complexity in other functions and/or classes
- Write comments not about what you're doing but why you're doing it! You'll thank yourself in the future :)
- Write only a little code at a time and test frequently, try to think of 'edge cases' that may break your assumptions of the data and/or behavior.
- Don't be afraid to ask for help and/or clarification with how C# and/or the QC API behaves
3

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Michael thank you so much for the guidance! This is fantastic and exactly what I needed as I think I was getting way ahead of myself and every time I tried to start I got overwhelmed! I am going to do exactly as you suggested...and more than likely post to this forum a lot for suggestions and advice. Hopefully I don't become too big a pain in the a$$ ; my first step is going to be the indicators one by one.

P.S. I am not sure if you meant to link another thread describing your pattern for storing indicators? If so are you able to add that - but if not I am sure I can track it down.
0

Jeremy, thanks for letting me know about the missing link.

As always, don't hesitate to reach out for help. That's what I'm here for :)
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hey Jeremy,

I am at the same place where you were three years back. Good to see someone started like me..

I haven't read the book and was wondering if you could provide mathematical construct of 90 day annualized exponential regression slope for each stock.. 

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed