Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Close Open Positions X Days After Opening Them

Hey Guys,

I am trying to get my algorithm to cover positions that it has entered into 10 trading days after it opened the position. I tried this using context.holding_time to create an empty dict. Then I added securities that I have entered into as a key with a value of zero and added to that value in the rebalance function until it gets to 10 then close the position. This doesn't seem to work as the backtest results aren't indicative of it following this pattern. I'd appreciate any help with this that you guys can offer, this has had me scratching my head. Thanks!

2 responses

Take a look at the attached algo. The logic to update the 'days held' is put into a separate function and, more importantly, is based on if a security is in the portfolio and not simply that it was ordered. That should be more positive.

There is also an added logging line to list the currently held positions and how long they have been held. The logs show that securities are indeed held for 10 days and then closed.

Note that an interesting thing happens when a security is held for 10 days AND it is still in the open list. Take a look at FCX on 2011-01-28 in the logs as an example. Not sure how you want your logic to handle that situation.

Hope that helps.

Thank you, this is exactly what I was trying to accomplish.