Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
creating a trailing stop in Quantopian for Robinhood?

Hi all, I am an extreme beginner, and I want to start by making my first algorithm only take care of selling stocks I've bought on Robinhood. To that end, I want to make an algorithm that is able to pick up what stock I have, and based on a couple very simple rules, enter stop limit orders, or limit orders in Robinhood.

I basically want to create a trailing stop limit, as well as a OCO type of function, where it can cancel the stop limit order, and enter a limit order if my price target is reached. This way I still control the buy side, and since Robin wont let you sell short, there isn't a danger of it going crazy that way either. It's really a way to dip my toes into Quantopian, beyond just using the research notebooks.

A couple reach goals is to later on make a version of the trailing stop limit that would reduce the trail based on a counter or milestones, ie- starts off with a 0.5 point trail, then if it gets half way to my price goal or it's been hovering over an hour, convert to a 0.4 point trail, etc.

I'm a bit stumped on where to even start on this though, so I was hoping someone has previously coded a trailing stop before?
What are your experiences trying to do so? Is there any fundamental issue with this approach? How do I make sure the algo picks up any buy I make during the day, to know its available to sell, or will it only pick up my shares from the day before. Also, are there any examples or references you would recommend I read on the technical side of trailing stops?

Thanks for any feedback and help.

4 responses

Hi there,

Let me know if you are still looking for a answer!

You can answer him for me instead ha


        p=data.current(stock,"price")  
        if p>context.trailingp:  
            context.trailingp = p  
            orders = get_open_orders(stock)  
            if len(orders)==0:  
                return  
            cancel_order(orders[0])  
            order_target_percent(stock,0,style=StopLimitOrder(context.trailingp*.98,context.trailingp*.979))  

Put something like the above code in handle_data for your trailing stop loss. When buying, set trailingp to your cost basis. You can adapt this for multiple stocks buy making trailingp a dictionary and taking out the return statement obviously. Q cancels open orders at the end of the day, so if you have a multiday strategy the above code also only works if you have a separate function at market open that submits an initial stoploss order.

The above code is a stop limit order. A stop market order instead will work also and it will also work fine in backtests, but in practice I use Robinhood too and don't like their order execution. However, when dealing with a volatile stock that's tanking you run the risk that your limit order does not execute, so you'll want to add code to see check that your order is filled, and if not, then cancel your last order a re-submit with a new limit price. I only use a trailing stop loss for a single pair of stocks in a high latency strategy so I never bothered with this. If you're trading a large basket, that is definitely necessary.

Let me know if you need any help. I was once an extreme beginner as well and still am regarding a lot of python packages and math skills, but messing around on Q is a fun way to learn.

A word of caution. Generally don't save absolute prices (or in this case a stop price) across days. Those saved prices won't reflect any stock splits or dividends. If a split did occur it may look like you hit your stop price. Instead you may want to save the 'stop percent' and then multiply it by the current day price.