Edit: There is error in my logic here which doesn't account for order that did not get filled overnight. Working on a fix.
I see the issue, and I believe I have come up with a solution:
Before trading starts, I check to see if my position in a stock has been split overnight. If the stock was split, I determine the split ratio and apply this ratio to my stop price:
> def initialize(context):
> ...
> context.position_amount = 0
> ...
>
> def before_trading_start(context, data):
> if context.portfolio.positions[context.stock].amount != 0:
>
> # If the stock split overnight, adjust the context.stop_price
> if context.portfolio.positions[context.stock].amount != context.position_amount:
> shares_ratio = context.position_amount / context.portfolio.positions[context.stock].amount
> context.stop_price_old = context.stop_price
> context.stop_price = context.stop_price * shares_ratio
> log.info(context.stop_price_old, context.stop_price)
Thankyou @Simon Thornington for the insightful feedback.
Quantopian...if you are listening, It would be nice to incorporate a 'Trailing Stop' (that can account for stock splits) into the available 'Order types'.