Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Set target and stop at specific price levels that are then saved?

I can't seem to figure out how to set my target and stop loss at a specific price that can be saved. Right now I can set a target and stop, but because my algorithm is basing the target and stop on moving data, they change. I tried setting 'target' and 'stop' properties on context.portfolio.positions right after I open the position, but it says the positions object is not assignable. Is there an easy way to do this?

3 responses

I just realized I could probably just use stop loss and stop-limit orders to accomplish this. I'll give that a shot.

So there doesn't appear to be any way to set stop loss or limit orders on open positions, is this correct? The following looks like it should work, but it never sells. Or would it be because all open orders are canceled at the end of the day?

order = order_target_percent(stock, -0.2)  
order_target_percent(stock, 0, style=LimitOrder(100))  
order_target_percent(stock, 0, style=StopOrder(0)  

EDIT: Nevermind, I think I understand what I need to do.

For anyone interested, what I ended up doing instead of trying to setup stop / limit orders was add a "days_held" value to every security in my portfolio, increment it every day in before_trading_starts, then I go back that many bars in my sell conditions, for example:

held_security['predicted'][-1 - held_security['days_held']]:  

It appears to be working as intended.