I've read this post: https://www.quantopian.com/posts/weird-behaviour-of-stoploss
It doesn't seem to help.
So, in my handle_data I have my code executing a limit order if a certain condition is met. Further down I am placing a stop order if that limit order is filled. I get that I can't place a stoporder on a position not yet filled. So I have it executing on the portfolio object:
if context.portfolio.positions[s].amount > 0:
order(s, context.portfolio.positions[s].amount)
message = '{stock}: Stop Order {orderNumber} {time} {price}'
message = message.format(stock=s, orderNumber=context.order_id, time=exchange_time, price=l[s][-2])
log.info(message)
The frustrating part is the build error I'm getting is this:
Runtime exception: TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'
The line for this error is in here:
if exchange_time >= oo.dt + timedelta(days=2):
cancel_order(oo.id)
message = '{stock} Order {order_id} cancelled due to two-day rule'
message = message.format(stock=oo.sid.symbol, order_id=oo.id)
log.info(message)
So, to me, it would seem the exchange_time piece is were my code is breaking. But after spending a day trying to figure out what in the hell I was doing wrong I isolated the problem to the first piece of code posted above.
In other words, if I remove the stopOrder code, the program runs just fine. So I know it's an issue with the stopOrder.
What I don't know is why in the world it's doing this... To me it seems very simple - I'm checking for open positions on the s (security) object. I have some. I want to place a stop order. But the program does not want to run with that one line to place the stop order. If I comment out that order line the code runs without issues and every time I get into that if block I have open positions.
What am I missing???