> For the complete documentation index, see [llms.txt](https://teamsmiley.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://teamsmiley.gitbook.io/devops/stock/robinhood-python.md).

# robinhood-python

## prepare robinhood 2factor auth

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

   ![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-43e73121b17e064a677ebb49fbd5b640f3acc2a0%2F2023-07-16-08-58-08.png?alt=media\&token=38c0c500-1efd-44cb-9775-93859cb2c82d)
3. click update

   ![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-56216c784b63ae033b1c3880e0ab8e1f4c864c7c%2F2023-07-16-08-58-55.png?alt=media\&token=efdd9d90-787a-49fb-8f20-8bf37f1b2bc7)
4. click "i can't scan the code

   ![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-1e51e3e3a1ddef4b6008e1864fb733650ac841d9%2F2023-07-16-08-59-52.png?alt=media\&token=532c4c3f-f1b3-4072-98a3-676fb19df1d9)
5. copy code and save it (APP-KEY)

   ![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-0649f4946a680081c30720477d97de17e31bd584%2F2023-07-16-09-01-00.png?alt=media\&token=2cda48ba-e7fe-4bc4-863c-b7ad8ba10996)
6. write code below

   ```sh
   pip install robin-stocks
   pip install pyotp
   vi robin.py
   ```

   ```python
   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

   ```sh
   python3 robin.py
   ```

   ![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-cea76721975211af0761b8e5ac1f4871fdf83699%2F2023-07-16-09-05-48.png?alt=media)

   ![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-d4a66053885deb7a0da9627839b6f6aa79a7be43%2F2023-07-16-09-07-13.png?alt=media\&token=dcd4c9ff-7413-4e70-a55c-93efa626168e)
8. copy and save backup code for future

   ![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-d5ddcaa226144a01d977ce1748d3dd081ed29042%2F2023-07-16-09-08-22.png?alt=media\&token=af7d195c-967c-47fc-aae0-bd42cdc85557)
9. let's create secret file

   ```sh
   vi  ./totp.txt
   ```

   ```
   APP-KEY
   USER-EMAIL
   USER-PASSWORD
   ```

## let's create python script

```sh
vi robinhood.py
```

```sh
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

![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-c3dfdf66abd51f8f2e9d3eba90a77fdd96198234%2F2023-07-16-09-21-12.png?alt=media)

## Get Qoute / Buy / Sell

```python
# 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)
```

![](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-4b596f5afe1b279655474b5aa25b1eea35031143%2F2023-07-16-09-25-23.png?alt=media)

## Caution

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