I coded a Robinhood Trading Bot

I coded a Robinhood Trading Bot
Click a star to review this guide
Average Rating 5.00 / 5
2 Reviews

10067

Share :


Hey everyone, this is Jacob here and recently I coded my own Robinhood trading bot using the pyrh library. I made a Youtube video of me actually coding it and the link to download is locating in the video as well, today I wanted to talk about my automated Robinhood stock trading bot so sit back and enjoy.


I've been building automated algorithmic trading bots for a while now and I know Robinhood is the most common brokerage for us millennials but from my previous research I could never find an API to use for Robinhood. In fact Robinhood does not have an official API, but some sneaky people have scraped and found an API to use and made easy to use for public software developers like myself. With a google search a couple of weeks ago I found at least 3 different unofficial Robinhood API's, heck yeah I'm totally coding a bot now!


So I ended up coding it in Python as most of viewers prefer that language probably because it's platform agnostic for the most part working on Windows, Apple and MacOS as well as Linux really well.


I imported the pyrh library which allows use to login in a Robinhood account and place trades, tulipy for indicators (this bot uses RSI to buy and sell) and schedulers libraries.


from pyrh import Robinhood
import numpy as np
import tulipy as ti
import sched
import time
#A Simple Robinhood Python Trading Bot using RSI (buy <=30 and sell >=70 RSI)
#Youtube : Jacob Amaral
# Log in to Robinhood app (will prompt for two-factor)
rh = Robinhood()
rh.login(username="Robinhood Username Here", password="Robinhood Password")
#Setup our variables, we haven't entered a trade yet and our RSI period
enteredTrade = False
rsiPeriod = 5
#Initiate our scheduler so we can keep checking every minute for new price changes
s = sched.scheduler(time.time, time.sleep)





I then define a run() method and schedule it to run every 5 minutes to keep getting new price changes, then with tuliply I calculate the RSI (Relative Strength Index) for every 5 periods, if it's below 30 I place a buy order and when it's above 70 I place a sell order. In this example I used Ford Stock $F as it's a cheap stock.



def run(sc):
   global enteredTrade
   global rsiPeriod
   print("Getting historical quotes")
   # Get 5 minute bar data for Ford stock
   historical_quotes = rh.get_historical_quotes("F", "5minute", "day")
   closePrices = []
   #format close prices for RSI
   currentIndex = 0
   for key in historical_quotes["results"][0]["historicals"]:
       if (currentIndex >= len(historical_quotes["results"][0]["historicals"]) - (rsiPeriod + 1)):
           closePrices.append(float(key['close_price']))
       currentIndex += 1
   DATA = np.array(closePrices)
   if (len(closePrices) > (rsiPeriod)):
       #Calculate RSI
       rsi = ti.rsi(DATA, period=rsiPeriod)
       instrument = rh.instruments("F")[0]
       #If rsi is less than or equal to 30 buy
       if rsi[len(rsi) - 1] <= 30 and not enteredTrade:
           print("Buying RSI is below 30!")
           rh.place_buy_order(instrument, 1)
           enteredTrade = True
       #Sell when RSI reaches 70
       if rsi[len(rsi) - 1] >= 70 and enteredTrade:
           print("Selling RSI is above 70!")
           rh.place_sell_order(instrument, 1)
           enteredTrade = False
       print(rsi)
   #call this method again every 5 minutes for new price changes
   s.enter(300, 1, run, (sc,))

s.enter(1, 1, run, (s,))
s.run()





I made some money on it too! If you want to see the results on how much I made and the full source code check out my Youtube video below, hope you found value in this WeTradeHQ guide!