Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Algorithm sells some stocks every few days (but supposed to rebalance monthly)

I have an algorithm that is meant to rebalance quaterly (and it does), but it'll make small transactions between the major rebalance days.

I have constraints for position size, and position size changes each day. Could this be a reason it makes small transactions?

9 responses

Here's a screenshot of what's going on. (long only strategy) https://imgur.com/a/mmsCUco

I've changed it so that position size ONLY changes when it is supposed to rebalance, and it's still selling off random stocks. I can't seem to figure out why.

If you could share the code, that would help

Attached a backtest that re-creates the problem. If you go to custom data, you can see that the number of long positions slowly declines over the course of the quarter (when it should only be trading once every 3 months)

Positions are closed automatically when stocks are delisted. You see some of your long positions being sold because the company is delisted. I didn't check every one but it's true for the ones I could find (ie CTI Molecular Imaging CTMI which was sold on 2005-05-10).

Perfect, that would explain it. Did you figure out that quantopian closes delisted positions on your own, or did you read it somewhere? I can't seem to find anything on their API about it.

@ Ma Ak There was a post awhile ago on how Quantopian handles delisted stocks. You are correct that it's not obvious from reading just the API docs lone. Check it out here https://www.quantopian.com/posts/delisted-securities-now-removed-from-portfolio.

Thanks for that link. Something doesn't add up though, it says:

In backtesting, when a security becomes delisted, the system will exit the position at the last known sale price and return cash to your portfolio. If you are short the stock, we will take the equivalent cash position from your cash balance.

If someone shorted a stock that delisted due to bankruptcy, why should cash be removed? Shorting a stock that ends up going bankrupt is a god thing, isn't it?

Short positions are closed when a stock is delisted. It looks just like a normal 'buy' at the last known price which then zeros out the held shares. The wording in the documentation may be a bit misleading. It's correct that one will typically profit on a short position when the security is delisted. However, there are a number of reasons why a stock can be delisted (eg a merger, going private, failed to file required statements, etc). Not all of them result in a lower price. Mergers and going private, for instance, often result in a higher price which one would loose if the security was held short.

See attached algo. Look at the 'Daily Positions and Gains' report. Note that on 8-1-2018 the short position of IMNP was closed at a profit.

Ah right. What would happen in this situation: I enter a short position at $15 per share, the stock drops to $2 per share and then files for bankruptcy and becomes delisted. Quantopian would then cover the short position at $2 per share, resulting in a $13 per share profit. Correct?

Is this what happens in reality though? In our real world example, would you profit the entire $15 since the stock went bankrupt, or only $13?