Hey there. I have written a module to manage stop losses. It is modular and can be plugged into any existing algorithm to perform a trailing stop-loss exit. It works for long as well as for short positions.
Functionalities are:
- identify any open positions and orders with no stop (short and long)
- create new stop levels
- manage existing stop levels
- create StopOrders with appropriate price and amount
To use it, please
- Copy my code lines 54-156 into your algorithm,
- Add these three lines of code into your algorithm:
# into initialize
context.SL_Manager = StopLoss_Manager()
schedule_function(context.SL_Manager.manage_orders, date_rules.every_day(), time_rules.market_open())
# at the end of rebalance and wherever you create new orders
context.SL_Manager.manage_orders(context, data)
To demonstrate the functionality, my algorithm opens a long-position for SPY. If this was closed through stop-loss, it will wait 10 days to re-open that position.
Right now, this will for swing-trading but not for pairs-trading. But I am planing to add functionality to handle spread-positions.
Cheers, Sebastian