Back to Community
Towards a simple Robinhood Buy Low Sell High Algorithm

Hi everyone. I have been working on developing a simple trading algorithm for Robinhood. Unlike many here I am neither a coding or trading genius but have been working to write some basic code:

  1. To trade some stocks by buying low and selling high.
  2. To deal with day trading rules, I have put a buffer of $25,000 that just sits there and allows me to make day trades without penalty.
  3. I have put in commissions, stock slippage code found here from others.
  4. To diversify the risk and manage leverage I want to spread the remaining money so that I only put 3% into any one stock. Currently I have 5 stocks listed instead of 33 but have been struggling to get the script to work with just one stock right now. I realize that at this price level 3% is a very small number of stocks but it is realistic to the trading funds I have right now.

I was expecting the algorithm to check the opening price of the first stock and then each minute check the current price of the first stock and if it did not have the stock in the portfolio and it met the buy logic, it would buy stock up to the 3% funds limit with the logic provided and then sell it with the logic provided, which right now is just buy it when it is $0.05 lower than the opening price and sell it when it goes above the opening price by $0.01 and then repeat - buy / sell / buy / sell as the criteria was met.

For May 15, I was expecting it to buy 1 share of BAC at the price of $24.01 (opening price - $0.05) and sell 1 share when it went above $24.07 (opening price + $0.01) and keep looping through the day (though it would be nice to know how to have it do this only once and then stop for the day too). Instead the logs show that it keeps selling the stock but never buying it while the transaction tab show that there were none.

In a perfect world, while it was doing this for the first stock, it would be doing the same thing for other stocks selected as well.

If there is anyone who would be willing to take a look at the code to see what I am doing wrong, I would appreciate it! And would be happy to make a final version, that incorporated any changes provided, available here to help any others who like me are new to this.

Clone Algorithm
26
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 59359a706b508c698cdb29f8
There was a runtime error.
4 responses

So you can make them like this, and for loop.

context.stocks = [sid(700), sid(2673), sid(40852), sid(7792), sid(5061)]
amount=1.0/len(context.stocks)
for stock in context stocks:
order_target_percent(stock, amount)

Although this is a pain. You should use pipeline, it's much easier and will give you a universe of stocks. It reduces bias if you trade more stocks in your backtest. This looks at fundamentals, and selects 10 companies every month based on a set of rules.

Clone Algorithm
35
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 59307db73f6918696f903313
There was a runtime error.

Thank you Eric for your response!

Eric,

Why don't you use the "def make_pipeline()" method? I've seen tutorials on this and it's still confusing because some people like you put the pipeline code in the "def initialize()". What are the rules regarding this topic?

Hi Jared,

This is older code. I usually call the function in the def make_pipeline, and return pipe to attach pipeline. Then have function attached it the initialize method. here is another algorithm where I do that. This is much easier It's def make_pipeline(context) because of the Static Assets function.

Clone Algorithm
184
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 58d574609c3d951bf1155704
There was a runtime error.