Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how to use percent return in algorithm, sell at certain percent loss

tell to sell at percent loss of 3% for example

1 response

Hello Charles,

You could try something like this:
def initialize:
context.exitafterlossof = True
context.losspcent = 0.97

def handle_data(context, data):
if context.exitafterlossof == True:
for security in context.portfolio.positions:
purchase_price = context.portfolio.positions[security].cost_basis
exit_price = purchase_price * context.losspcent
price_history = data.history(security, 'price', 2, '1m')
current_price = price_history[-1]
if current_price < exit_price:
if get_open_orders(security):
continue
if data.can_trade(security):
order_target_percent(security, 0)

Hope that helps.