Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
First Algo! Using Regression, Volume, and Standard Deviation

Hey Everyone!

For this Algo I wanted to construct a strategy that would:

  • be as passive as possible
  • have low draw down

In addition I took to heart the idea, perhaps dangerously, that "everyone is a genius in a bull market"

As a quick overview...

My code takes a user defined time period to establish a trend, fits a linear regression to the points in the time period, and then predicts a future price based on the regression. It does this multiple times and takes the majority expectation if the price will be higher. The order decisions are sorted by standard deviations, whether the volume confirms the trend directions, and what the long term trend is in the security ("bullish vs bearish sentiment").

I'm aware of a few issues in it, and I'd love some feedback. I've been a lurker for a bit and I'm excited to join in. Cheers!

11 responses

Hello James,

Nice code. A couple quick notes:

  • Conjuring up an algo to avoid the Great Recession event is a great way to learn the system, but there is a tendency to over fit, since one can get whopper returns over that time period. Generally, I'm skeptical how relevant the time period is for backtesting. Eventually, there will be another slump/crash, but will have the same characteristics?
  • To move up the learning curve, you could try a couple things. Convert your algo to run on minute data (but trade once per day, at most), and add a bond ETF. Then, adjust the stock/bond ratio based on your indicators.

Grant

Grant,

Thank you!

I totally hear you there, about whether new slumps will have those same characteristics, definitely had the same worry. What's funny actually is that I didn't engineer that crash resistance in there on purpose, not that I'm complaining. I do worry that I game the back tester with my knowledge of the time period. You have over 5k tests, how do you get around that, and test your code more rigorously? Your suggestion of running on minute data, is that purely so the regression can fit to more data? Definitely going to roll in that Bond ETF idea, cause I don't like seeing those flat spots in the chart where cash is just sitting there unused.

James,

You're welcome. Regarding minute data, you can't live trade or enter the contest without an algo that runs on minute bars, so the sooner you take the plunge, the better.

There's a big learning curve just to use Quantopian (let alone develop a decent algo), so just keep plugging away. If you have time, I suggest reading help and FAQs straight through once to get a sense for what you can do.

Gotta run. If you have updates, just post here, and I'll get an e-mail.

Grant

Here is an updated version, new additions are:

  • added a bond ETF to take care of the standing cash issue
  • removed the if/else and buy all/sell all strategy (only a Sith deals in absolutes)
  • replaced it with a strategy that allocates the portfolio according to the number of positive indicators
  • reworked the Boolean indicator logic to more accurately reflect the intent of the strategy
  • records the portfolio allocation ratios
  • syntactical, naming, structure changes...

Hi

I tried porting this code to run on new Quantopian APIs as a learning exercise, but my backtest results are completely different than what is posted in the thread.
I am unable to figure out whats wrong. Most probably my understanding of how to convert older mvg() function is wrong.
Can anyone please help with whats wrong with my conversion attempt.

Thanks

New commission/slippage model

Thanks for the pointer. Will read about it :)

Yo! I it may be the t-costs but if I remember correctly I designed this to make as few transaction as possible so I wouldn't expect it to be that much of a problem. However, I haven't looked at this incredibly overfit algo, based on a naive understanding (or lack of understanding) of statistics since I wrote it 3 years ago!

Quantopian has some extremely good examples that would be much better learning exercises than this.

Thanks a lot for your feedback

hi just wondering has this algo been tested OOS?

No, because it’s a bad algo.