Download a CSV of your Robinhood portfolio

Robinhood has always bugged me with not being able to output my portfolio as a csv. Since I’ve started managing my stocks through automation and self-hosted portfolio visualizations, I need to occassionally make sure things are sync’d up correctly. All the major brokers allow you to output as CSV, except for Robinhood.

The below python script fixes that. It uses the robin_hood library to interact with their service. One could even do trading on there - something I’m currently looking into.

First, you’ll want to install the pip package robin_stocks and set your environmental variables accordingly. We don’t want to hard code our passwords after all.

$ pip install robin_stocks
$ export ROBINHOOD_USERNAME='your_robinhood_username'
$ export ROBINHOOD_PASSWORD='your_robinhood_password'

fetch_rh_stocks.py

    import robin_stocks.robinhood as r
    import pandas as pd
    import os

    # Login to Robinhood using environment variables
    username = os.getenv('ROBINHOOD_USERNAME')
    password = os.getenv('ROBINHOOD_PASSWORD')
    r.login(username, password)

    # Get portfolio data
    portfolio_data = r.build_holdings()

    # Create a list of dictionaries with the desired information
    data = []
    for stock, info in portfolio_data.items():
        data.append({
            'Ticker': stock,
            'Average Purchase Price': info['average_buy_price'],
            'Quantity': info['quantity']
        })

    # Create a DataFrame and save to CSV
    df = pd.DataFrame(data)
    df.to_csv('robinhood_portfolio.csv', index=False)

    print("CSV file created successfully!")

    # Logout from Robinhood
    r.logout()
Previous: Smoked Turkey Roast Next: Tofu Stir Fry