Robinhood trailing stop loss

Hello fellow investors,

If you are a robinhood user, you may have noticed that the platform does not support trailing stop loss orders. Using the power of google and the quantopian forums, I was able to write a simple code that would sell when a stock falls 3.5% from the highest price since I bought it. Can anyone review my code and give me suggestions on how to make it better? My ultimate goal is to make it so that the algorithm makes a trailing stop loss to whatever stock I have bought. Thank you in advance



def initialize(context):
context.stock = symbol("NAK")
set_benchmark(context.stock)
context.stop_price = 1.34
context.stop_pct = 0.965

def handle_data(context, data):
set_trailing_stop(context, data)
if data.current(context.stock, 'price') < context.stop_price:
order_target(context.stock, 0)
context.stop_price = 0
record(price=data.current(context.stock, 'price'), stop=context.stop_price)

def set_trailing_stop(context, data):
if context.portfolio.positions[context.stock].amount:
price = data.current(context.stock, 'price')
context.stop_price = max(context.stop_price, context.stop_pct * price)


12 responses

I forgot to mention that I will be buying manually. What I am hoping to achieve with this is just the trailing stop loss feature

i think you can use the position's average cost to determine the price you paid. You can loop through your positions instead of hard coding the stock.

One issue is that there isn't a way to determine when you acquired something. I'm not sure of a good way to do this. You could walk backwards on the price and take a time that matches your average cost but it wouldn't necessarily be correct.

I'll have to look into that. That would be a very helpful feature to have because right now I have to change the algorithm each time I buy a new stock

Not a native Robinhood solution, but this has worked well for me: https://www.trailingstoploss.com

While that website is interesting as a service, I think perfecting some TSL logic outside of placing the order is interesting, algorithmically, so someone with lvl 2 data can't see an algorithmic TSL order and trigger a large sell just to tap it out and rebuy again.

You bring up a good point, Andrew. Algorithms written here on Quantopian or the one used by that website have that advantage due to the "masking effect" from Level 2 since orders aren't placed until the algorithm decides to place one.

Secondarily, it does make me wonder if you have a manually opened position on robinhood can be seen by an algorithm deployed here and linked to that account. In theory, you should be able to do that. Like an "update_positions" method as opposed to just holding a dict of things started in the algo. Conversely, that would be bad for people running multiple algos against one account.

Wow! You guys are definitely advanced coders in quantopian lol I have to research what level 2 and masking effect is. I'm a total newbie at this

@Sirus wow that website is pretty interesting. I wonder how accurate it is. I might have to test it with a small position.

@Andrew I have to look more into "update-position" and see how I can code it into the code I have. I've tested the code I posted above and the code recognized when I purchased the stock and immediately started tracking the price

It's not there, but it should be. afaik. Open to being corrected.

Looks like Q is shutting down their service, any alternative suggestion?

For a trailing stop loss? Just use the Robinhood API endpoints -- somebody has documented them on github. You can run your script locally on your own computer and use any programming language you wish (so long as it supports cURL). Just query the price from the Robinhood API, keep track of the highest price since you bought the security, and place a sell order when it crosses the trailing stop loss threshold. The only thing you need to replicate from Quantopian is a loop that starts at market open and stops and market close. Not too complicated.

Robinhood's apis are not for general public right? They only allow for other brokerages i suppose.