Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
I don't know how to exit a position after my target has been hit?

Hello, once again I am stuck and I am hoping you can help me out. I am building algo's on drops at the moment. When a particular symbol drops 2.1% I want to get long. Then when the symbol mean reverts and reaches a percent change of 0, I want to exit the trade. I am able to get in the position fine, the trouble is I can't get out of the position. I do not know how to identify when I am in the trade, to tell the IDE when to get out of the trade. So then the algo just keeps buying and never selling. I have played around with get_open_orders and get_order, but I do not know how to use them apparently. Any help is greatly appreciated,

3 responses

Hello Josh,

Attached.

Thank you so much!!! I knew I was over thinking it. I tried for 3 hours reading documentation yesterday and trying different things. Thank you very much!

Part of that is ok but the way to exit a position is order_target(security_object, 0) because order(security_object, 0) would make no change.
Often people will use order_target_percent(security_object, 0). For the purist (that would be me), that's an extra unnecessary step for the code to decide how many shares 0% of the portfolio represent.

https://www.quantopian.com/help#api-order-methods

order_optimal_portfolio  
order  
order_percent  
order_target  
order_target_percent  
order_target_value  
order_value