Back to Community
How to close a position after a certain number of days?

I want to hold a security for a specified number of days, does anybody know how to code that? I didn't find anything useful in the APIs.
Thanks!

3 responses

More specifically, how to close each long or short position in a stock X days after it was opened.

@Ernesto, i think its a useful question. Please can you advise?

Hi Da, Tony

The answer really depends on the structure of your strategy and the frequency at which you want to trade. For example, the simplest way of closing a position after X days would be to schedule a rebalance function to execute every X days and close out all open positions before opening new ones. This approach assumes you rebalance your portfolio at a set frequency.

If your strategy trades securities at different times/rates, one way of doing it could be to maintain a python dictionary mapping dates to a lists of securities ordered on those dates. You could use the result of get_datetime().date() for the keys. Then, you would need to iterate through the dictionary every day and check the different between the current value of get_datetime().date() and each key, and liquidate positions under the keys that are older than 5 days.

Again depending on how your strategy works, the second approach could get more complex. For example, if you open a position on a given day and then trade the same security on the next day you would have the same security under two dates in your dictionary. It would be up to you to decide how to handle these special cases.

I hope that gives you an idea of how to approach the problem. Let me know if you have any questions.

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.