# robinhood-python

## prepare robinhood 2factor auth

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

   ![](/files/B1O2jkQJ3Atyvbxu3nH5)
3. click update

   ![](/files/1FTjBHYeJ5gBTp9yvWd8)
4. click "i can't scan the code

   ![](/files/GWMX3QvcyyvR26XhS5MB)
5. copy code and save it (APP-KEY)

   ![](/files/TQ4IY9QIXB2rGQseUvzM)
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
   ```

   ![](/files/ofG2iOENehSaHlXoZ1od)

   ![](/files/MaqDeRXvRnUksOJq8DDt)
8. copy and save backup code for future

   ![](/files/pHq9aZ7Mfzij3DM6OuRB)
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

![](/files/wgcDVb0F2HxFN0MlLyiK)

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

![](/files/UWxFBuO35QzBmqNRCDpZ)

## Caution

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://teamsmiley.gitbook.io/devops/stock/robinhood-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
