Back to Community
Anything wrong with this algo?

I'm very much a newb at quantitative finance.
I suspect something naive is happening in this algo but I'm curious what the community will make of it.

The algorithm does the following:

  • Every morning 35 min after open, it screens for stocks in Q1500 that just announced earnings, min price = $5
  • Long only, places equal weight on each stock that gapped > %4 overnight (positive reaction to earnings)
  • Hold all day and night and close all positions the next morning 25 min after open (10 min before opening more positions)

Despite the whipsawing and perpetual high specific risk, it seems to do well over time.
What do you think? Unrealistic?

Thanks!

Clone Algorithm
13
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 59f77a5fbc71f4410bddc517
There was a runtime error.
8 responses

I looked over your algo, and I don't see anything glaringly wrong about it. Some performance improvements:

  • You have some edge cases in your order handling - for instance, your orders might fill simultaneously with the order being cancelled, and you don't handle that. Rather than mess around fixing that you should just change over to the Optimize API and let it abstract away all that grossness.
  • your trade_stocks could be done in pipeline more efficiently
  • your log_num_positions I think can just be len(context.portfolio.positions)

As for the algo itself, it seems reasonable, but like you said, it's risky. Very high variance. And it doesn't place all that many bets, so you'd need a lot of out-of-sample in order to develop a more faith in it. How many times would you be willing to lose 40% of your value. . . . and still keep going?

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.

I forgot to attach the tearsheet

Loading notebook preview...
Notebook previews are currently unavailable.

Ditto and I'll try to add some basics.

The closing of positions were not all complete before opening positions, so then starting with 10k the amount invested was 72k, quite a bit of margin. It is an example of the importance of partial fills on our radar that affects us all.

This mainly sets a context variable to wait for all closing orders to be done, then trade_stocks() has a clean slate. Logging shows some detail on any incomplete orders. There's still a bit of margin you might want to look into, just to understand it. Except for minute in the chart, click legend items to toggle them off, there are times when incomplete closing orders linger over 100 minutes into the day, although, due to smoothing, you'd need to zoom in to see the extent of any spikes; they are more visible during a run, or look thru the time stamps in logging output. So, fewer partials-overlap-unknowns here, can hopefully help. Return on amount invested was 95% due to margin, this increases that to 756%, holding the amount invested to 11.6k. Interesting strategy.

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 59f79f75611ba340f5ab6078
There was a runtime error.

Thank you both for the replies.

Dan,
Good suggestions for efficiency, I will surely do that.
I agree, those drawdowns are stomach churning, and frequent. I like to look at them as being similar in severity to those one can experience in the market, but with steeper recoveries to follow : ) The notebook you provided was very insightful, esp. the stress event comparisons, and the holdings chart.

Blue Seahawk,
Wow thank you for shining a light on the partial fills/margin issue and for contributing your solution. I will try to understand the remaining margin.

Blue, have you updated your code to use the Optimize API? It's so much easier and cleaner - takes dozens of lines of code down to 5-10.

I use Optimize API in my algos.

Do the date-range restrictions for datasets in the backtest environment apply to paper trading? I would like to start generating out-of-sample but the strategy is based on the datasets.

Thanks

Yes, date-range restrictions apply to paper trading. Otherwise, that would be a really easy way to get the expensive data! Our business partners would not be amused. . . .

When you think about it, you start collecting out-of-sample data the day you run a full backtest. Every time you run a backtest a snapshot of the code is saved and timestamped (in the full backtest window, you can see it by clicking the button in the upper-right corner). That code can't possibly have been written with knowledge of the future. So if you re-run the backtest (identical code) in a month or 6 months or a year, that's a way to review your out-of-sample performance.