Back to Community
Why does the number of positions (and leverage) creep up for this algorithm?

I'm attempting to implement something similar to Joel Greenblatt's Magic Formula with this algorithm. The strategy should not involve any leverage and every time I rebalance (which is once a year), I first sell off all my current positions and then buy a new set of 50 positions based on the formula for value.

The strange thing is that the number of positions creeps up beyond 50 over time. I'm not sure why, but I can only assume that I'm not fully selling off all my positions each time I attempt to. Is there something I'm doing wrong in my code? Are the orders being cancelled for some reason?

Clone Algorithm
12
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: 54e9460bdf19550f129277ca
There was a runtime error.
8 responses

Hi Rudiger,

I'm still looking into it, but at first glance, it looks like you may be accumulating delisted securities that you can't your positions from. I tried adding in this:

for stock in context.chosen_stocks:  
        if (stock.end_date - get_datetime()).days < 365:  
            continue  
        if stock in data:  

to your def rebalance() and that seemed to have helped!

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.

Hi Seong,

Thanks so much for looking into it. I'm guessing that I can't include your new if statement, because that would introduce a significant lookahead bias. Is there a way for me to remove delisted stocks from my portfolio after they have been delisted.

Also, does delisted imply that the shares held are worthless? In that case I guess I could just not worry about them. But, I should probably keep the counts of the listed and delisted stocks separate.

Hi Rudiger,

When the stock is delisted, it simply remains at the last trading price you've seen it at. So I would recommend trying to get rid of it by using some sort of heuristic as shown above but lower the number of days from 365 to 10ish or something similar. This might provide to be a better solution simply because you'd be holding unexitable positions in your portfolio that take up unnecessary cash.

Seong

Hi Seong,

I added a few lines to handle_data, which sell off stocks that will be delisted within two days (I figure in real life we'd get warnings about companies that are about to be delisted). It seemed to work at first, but later I run into the same problem that I had before. The number of positions goes up to 55. Am I missing something else?

Clone Algorithm
7
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: 54ec051e14cec10f20dc35d6
There was a runtime error.

Actually, nevermind. When I increase the time to 10 days, it works. The 2 day notice didn't work due to weekends.

Hi Rudiger,

Yeah, we're looking into ways on how we can better account for delisted securities. But I think for now, the solution that you're using might be the best available. Did you need help with anything else?

Seong

Thanks so much for all the help Seong!

There is one other thing I noticed while playing with this algorithm. I tried switching out roa for roa5_yr_avg, but it seems like the value was None for all securities over all time. Is this field never filled?

Hi Rudiger,

You are correct, that's one of the things we're working onto fix for now. Thanks for being patient :) In the meanwhile, we have a way to gain historical data through Fetcher if you're interested. Take a look at this tutorial for more information https://www.quantopian.com/posts/quantopian-tutorial-lesson-3-basic-fundamentals-with-piotroski-score-growth-stocks-and-uptrending-volatile-small-cap-algorithms

Thanks,
Seong