Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Sell security after 1 year holding period, how?

My goal is to buy 3-5 Securities a month, selling them 12 months later.

My initial hope was that the Position object contained the date of purchase, in which case it'd be easy - close out any positions > 12 months old.

It seems I would have to write (to somewhere... a database or file?) the details of the opening trade, then when rebalancing check to see if 12 months has lapsed, but this seems cumbersome and I suspect there must be a more efficient approach.

Would a built-in function handle this case? Welcome any thoughts on the best approach. No need for detailed code... more of a conceptual question.

(Am new to Q, but familiar w/ securities markets and python, and have created a value/fundamental algo which will eventually be implemented in IB.)

Thank you

3 responses

You can store stuff in context.

For example, context.current_stocks could be a dictionary of purchase dates, keyed by security. Then, every month, you would review the list, and sell any stocks that were purchased 12 months ago or longer. The problem comes in if you stop your algo or it crashes and you are trading with a broker (not paper trading on Quantopian). Then I think you'd have to manually enter initial values for context.current_stocks and fire up the algo again.

Thanks Grant. You hit the nail on the head. That's the missing piece of info... stupidly didn't realise/appreciate that whatever I stored in context would persist. Thank you!

For live trading perhaps you could retrieve this information via API calls to your broker.