Back to Community
exit after n-days?

My algo buys stocks based on pipeline filters. How to implement an exit for the security after an n-days?

4 responses

Maxim -

You could create a dictionary in context, keyed by security. The dictionary would hold the number of days elapsed since stocks were bought (I'd probably use order submission as the trigger). Then, it would be a matter of running something like this every day:

for stock in context.days_elapsed:  
    if context.days_elapsed[stock] > context.n:  
        order_target(stock,0)  

When a stock is order is submitted, you would set context.days_elapsed[stock] = 0 and every day, for every stock, context.days_elapsed[stock] += 1.

This doesn't manage lots of a given stock. For example, if you buy 100 shares of stock A one day, and another 100 shares of stock A 2 days later, then you'll end up holding the first 100 shares for 2 days longer than you'd like.

It's a simple solution that works . Thank you!

You're welcome. Enjoy! --Grant

Thanks Grant, I was trying to find a simple solution for this the other day as well.