robinhood-python

prepare robinhood 2factor auth

  1. login robinhood

  2. account >> setting >> security and privacy >> two-factor authentication click

  3. click update

  4. click "i can't scan the code

  5. copy code and save it (APP-KEY)

  6. write code below

    pip install robin-stocks
    pip install pyotp
    vi robin.py
    import robin_stocks.robinhood as robin
    import pyotp
    login = robin.login("YOUR-EMAIL","YOUR-PASSWORD")
    totp = pyotp.TOTP("APP-KEY").now()
    print("Current OTP",totp)
  7. run code then you can see OTP put into robinhood

    python3 robin.py
  8. copy and save backup code for future

  9. let's create secret file

    vi  ./totp.txt
    APP-KEY
    USER-EMAIL
    USER-PASSWORD

let's create python script

vi robinhood.py
import robin_stocks.robinhood as robin
import pyotp
import sys

SP = 20000.0

lines = open('./totp.txt').read().splitlines()
KEY = lines[0]
EMAIL = lines[1]
PASSWD = lines[2]

totp = pyotp.TOTP(KEY).now()
print("Current OTP",totp)

login = robin.login(EMAIL,PASSWD,mfa_code= totp)

my_stocks = robin.build_holdings()
for key,value in my_stocks.items():
    print(key,value)

run and check output

Get Qoute / Buy / Sell

# my_stocks = robin.build_holdings()
# for key,value in my_stocks.items():
#     print(key,value)

def Qoute(ticker):
    r = robin.get_latest_price(ticker)
    print(ticker.upper() + ": $" + str(r[0]))

def Buy(ticker,amount):
    r = robin.order_buy_market(ticker,amount)
    print(r)

def Sell(ticker,amount):
    r = robin.order_sell_market(ticker,amount)
    print(r)

TICKER = "tsla"
Qoute(TICKER)

AMOUNT = 1
Buy(TICKER,AMOUNT)
Sell(TICKER,AMOUNT)

Caution

You can only daytrade a limited number of times. Please note that

Last updated

Was this helpful?