You are on page 1of 3

9/28/2017 Robinhood trailing stop loss

Back to Community

Robinhood trailing stop loss


Jose Rubio posted Mar 22, 2017

collaboration request getting started live trading momentum seeking help

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)

11 responses

Jose Rubio Mar 22, 2017

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

James Hutchison Mar 22, 2017

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.

Jose Rubio Mar 23, 2017

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

https://www.quantopian.com/posts/robinhood-trailing-stop-loss-1 1/3
9/28/2017 Robinhood trailing stop loss

Sirus Saeedipour Mar 23, 2017

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

Andrew King Mar 23, 2017

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.

Sirus Saeedipour Mar 23, 2017

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.

Andrew King Mar 23, 2017

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.

Jose Rubio Mar 23, 2017

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

Andrew King Mar 23, 2017

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

Tony Wang Aug 23, 2017

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

Viridian Hawk Aug 23, 2017

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.

https://www.quantopian.com/posts/robinhood-trailing-stop-loss-1 2/3
9/28/2017 Robinhood trailing stop loss

Please sign in or join Quantopian to post a reply.

About Quantopian

Careers

Community

Events

QuantCon

Help

Academia

Lectures

Workshops

Investor Relations

Status

Facebook

Twitter

LinkedIn

YouTube

Blog

Terms of Use

Privacy Policy

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any
security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian.

In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to
engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any
plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a
fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to
Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk,
including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may
have become unreliable for various reasons, including changes in market conditions or economic circumstances.

https://www.quantopian.com/posts/robinhood-trailing-stop-loss-1 3/3

You might also like