How to close out trades given a condition is met. (i.e., N days have past, OR, the position has reached a % return threshold)

I have a fairly simple strategy with a signal. i.e. If 'signal' > 0.05, then short two specified stocks with specified weights. This will run once a day.

For example:

def handle_data(context, data):

....
if signal > 0.05:
if SPY not in open_orders and AAPLnot in open_orders:
order_percent(SPY, -0.01)
order_percent(AAPL, -0.01)



So if the 'signal' is > 0.05 for 4 days in a row, this means that the trades will have occurred 4 times.

I'm trying to figure out how to close the trades given a condition is met. e.g.

1. If a trade has been open for 10 days, close that trade.
OR
2. If a trade has gained more than x%

Thanks!