Example for taking profit at a given threshold over cost basis (for both long or short).
You could drop this into an algorithm at the end of initialize with a single paste quickly to see how it does with the settings as-is.
# Take profit several times a day
context.profit_threshold = .05
context.profit_logging = 1
for i in range(20, 390, 60): # (low, high, every i minutes)
#break # uncomment to deactivate
schedule_function(take_profit, date_rules.every_day(), time_rules.market_open(minutes=i))
def take_profit(context, data): # Close some positions to take profit
pos = context.portfolio.positions
for s in pos:
if not data.can_trade(s): continue
prc = data.current(s, 'price')
amt = pos[s].amount
if (amt / abs(amt)) * ((prc / pos[s].cost_basis) - 1) > context.profit_threshold:
order_target(s, 0)
if not context.profit_logging: continue
log.info('take profit {} {} cb {} now {}'.format(
amt, s.symbol, '%.2f' % pos[s].cost_basis, prc))
First, this image is the backtest without the code active, 30.9%
Then with the function active, the log shows long and short positions being closed when beyond the threshold at 5% profit.
cb is cost basis, price per share on open with commission per share added
2016-01-05 06:31 longs:33 INFO order SPY
2016-01-06 06:31 shrts:39 INFO order SH
2016-03-30 06:50 take_profit:28 INFO close -137 SH cb 21.42 now 20.3
2016-04-06 06:31 shrts:39 INFO order SH
2016-06-07 06:50 take_profit:28 INFO close 14 SPY cb 201.72 now 211.81
2016-06-14 06:31 longs:33 INFO order SPY
2016-07-11 06:50 take_profit:28 INFO close -78.0 SH cb 41.04 now 38.885
2016-07-13 06:31 shrts:39 INFO order SH
2016-08-11 08:50 take_profit:28 INFO close 16 SPY cb 208.24 now 218.72
2016-08-16 06:31 longs:33 INFO order SPY
2016-12-08 06:50 take_profit:28 INFO close -92 SH cb 38.53 now 36.583
This is the backtest with code active, 36.1% (an increase of 5.2 percentage points in this instance).
Edit: Sometimes when editing a message, an attached backtest disappears. Regrettably, that happened with this one, however everything needed is in the code above.