Professional Documents
Culture Documents
Back to Community
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
11 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
https://www.quantopian.com/posts/robinhood-trailing-stop-loss-1 1/3
9/28/2017 Robinhood trailing stop loss
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.
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
About Quantopian
Careers
Community
Events
QuantCon
Help
Academia
Lectures
Workshops
Investor Relations
Status
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