Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Random selection with ratcheting price

So, here's my monkey-with-a-dartboard approach. It just randomly chooses stocks that are on an upwards-ish trend and buys up to $1000 worth at a time, and then sells it as soon as the price drops to either 10% below the highest price that's been seen or 5% below the purchase price (after holding on for at least a day so as to not be subject to day-trading requirements).

Each run generates different results, of course, but in general it seems to perform pretty well.

I'll probably want to tune the algorithm a bit more to sell a bit more aggressively.

8 responses

Thanks for sharing the algo! This type of algorithm can be fun to play with and explore in backtesting. Especially trying different dollar volume universes.

However, the random style wouldn't work in live trading because the algorithms need to be deterministic. Meaning that if I ran it five times, I would get identical results each time. It is however, an interesting twist on a stock screening strategy.

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

What is the purpose to the deterministic requirement? And, how does that work with, say, external CSV files which may or may not be generated on-the-fly? Is the requirement documented anywhere in the API? The only reference I saw to random was it being unsupported due to how the object is seeded at start-of-day.

Also, it is possible to make deterministic random number generators. The easy approach is to just always seed based on the time stamp (or some other fixed factor that's known ahead of time), and "seeding" can just be "take hash of the time stamp." Or you can get fancier and do something like Perlin noise (which is well-known in the computer graphics world).

This is a current limitation in our beta for live trading, based on the initial technical structure. For Fetcher, the file should remain unchanged and if you want to add new data, you can append new rows to the bottom of the file. For more information on the live trading details and limitations, take a look at the help doc: https://www.quantopian.com/help#overview-livetrading

In particular,

  • If your algorithm uses fetch_csv(), it is important that all historical data in your fetched data be accessible and unchanged. We do not keep a copy of fetched data; it is reloaded at the start of every trading day. If historical data in the fetched file is altered or removed, the algorithm will not run properly. Providing additional, new data in the file for dates in the future is fine.
  • The use of random() isn't supported because of technical limitations with our start-of-day initialization.

The use of random() may be plausible as we move to remove the beta label, but we do have some limitations in the current architecture.

Okay, I've made it deterministic by using the current time stamp (multiplied by a large-ish prime number) as the random value. It's pretty trivial but whatever.

I had an excruciatingly dumb bug in which it was taking the min() of the cost-basis vs. max-price threshold, instead of the max(). So the algorithm was just buying and then holding on to things forever.

So I changed it to be the max(), and it ended up losing a lot of money, since it was too aggressive in its selling.

I changed the sell threshold to be the maximum of 95% of cost basis vs. 85% of max price, and that gave it reasonable performance again (17.6%, compared to 9.8% on SPY), but not as good as the previous buy-and-hold strategy (21.1%). I guess I need to perform a longer-term backtest so that it actually looks at performance through multiple depressions and so on.

And some more tweaking, and a full 1-year backtest... now it properly tracks the total amount of stock that's worth more than its sale threshold, and the total amount that's below the tracked maximum value. If the latter exceeds the former, then it more aggressively sells off stocks which have slipped in value from their maximum. This seems to behave much better during sudden market depressions.

So here's my current version of the code and my most recent backtests. Because of the purchase strategy, the amount of money in the portfolio can make a huge difference in overall returns even on the same time period, since the random selection can cause one stock to be overallocated. In the future I might change the purchasing instead to be a rebalancing based on the upward trends, or something, but that has its own share of problems.

So, for example, with a $10K portfolio, it makes 5% over a particular time period, while with a $35K portfolio, it loses 1.5%. I wonder if I need to change the way in which it determines the 'price below max' ratio.

Here's the $10K portfolio backtest...

and here's the $35K portfolio backtest: