Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
bracket orders?

When trading with robinhood, how do we program bracket orders?

I want it to sell if it drops x% below the purchase price, or y% above the purchase price.

1 response

https://www.quantopian.com/help
Search page for cost_basis. This is copied from there.

def check_positions_for_loss_or_profit(context, data):  
    # Sell our positions on longs/shorts for profit or loss  
    for security in context.portfolio.positions:  
        is_stock_held = context.stocks_held.get(security) >= 0  
        if data.can_trade(security) and is_stock_held and not get_open_orders(security):  
            current_position = context.portfolio.positions[security].amount  
            cost_basis = context.portfolio.positions[security].cost_basis  
            price = data.current(security, 'price')  
            # On Long & Profit  
            if price >= cost_basis * 1.10 and current_position > 0:  
                order_target_percent(security, 0)  
                log.info( str(security) + ' Sold Long for Profit')  
                del context.stocks_held[security]  
            # On Short & Profit  
            if price <= cost_basis* 0.90 and current_position < 0:  
                order_target_percent(security, 0)  
                log.info( str(security) + ' Sold Short for Profit')  
                del context.stocks_held[security]  
            # On Long & Loss  
            if price <= cost_basis * 0.90 and current_position > 0:  
                order_target_percent(security, 0)  
                log.info( str(security) + ' Sold Long for Loss')  
                del context.stocks_held[security]  
            # On Short & Loss  
            if price >= cost_basis * 1.10 and current_position < 0:  
                order_target_percent(security, 0)  
                log.info( str(security) + ' Sold Short for Loss')  
                del context.stocks_held[security]