trevorwelch / rh-profit-and-loss

Get profit and loss for your trading on Robinhood, export trades/dividends/options history, generate buy-and-hold comparison
39 stars 13 forks source link

RH Profit and Loss

A Python script to get a look at your trading history from trading options and individual equities on Robinhood: calculate profit/loss, sum dividend payouts and generate buy-and-hold comparison.

UPDATE 2020-01-21: This code is so bad I almost want to delete it from my GH, but it works - and I've seen it get some attention so I'm pushing this small update to make it usable (at least until RH changes their API again). Thanks to @nikhilsaraf who forked it and fixed the auth'ing problem.

Features

Download the repo

From the command line:

git clone git@github.com:trevorwelch/rh-profit-and-loss.git
cd rh-profit-and-loss

Run it, run it

First, grab an oauth token from your browser

Run with defaults (this will process your full account history, starting from your first trade):

python3 get_profit_and_loss.py --username <username> --password <password> --access_token <longtextblob>

For example:

python3 get_profit_and_loss.py --username 'timmyturtlehands@gmail.com' --password 'LovePizza!11one' --access_token <longtextblob>

You'll see output like:

From November 4, 2018 to today, your total PnL is $486.45
You've made $390.1 buying and selling individual equities, received $16.35 in dividends, and made $80.0 on options trades

Run it and utilize other features

See how your portfolio performed over a specified date range

Specify --start_date and --end_date args

For example:

python3 get_profit_and_loss.py --username 'timmyturtlehands@gmail.com' --password 'LovePizza!11one' --access_token <longtextblob> --start_date 'July 1, 2018' --end_date 'August 1, 2018'

Export CSV files for further exploration

Use the --csv flag

The script can output a number of CSV files:

For example:

python3 get_profit_and_loss.py --username 'timmyturtlehands@gmail.com' --password 'LovePizza!11one' --access_token <longtextblob> --csv

Export dataframes as pickles for further exploration:

Use the --pickle flag

Similar exports to the CSV section, but as pickled dataframes which can be loaded directly into pandas for further exploration like so:

import pandas as pd
df_pnl = pd.read_pickle('df_pnl')

# Find worst ticker and best ticker, dataframe is already sorted by net_pnl
best_ticker = df_pnl.iloc[0].name
best_ticker_pnl = df_pnl.iloc[0].net_pnl
worst_ticker = df_pnl.iloc[-1].name
worst_ticker_pnl = df_pnl.iloc[-1].net_pnl

print("Your best individual equities trade over this time period was with {}, with ${} in gains".format(best_ticker, best_ticker_pnl))
print("Your worst individual equities trade over this time period was with {}, with ${} in gains".format(worst_ticker, worst_ticker_pnl))

For example:

python3 get_profit_and_loss.py --username 'timmyturtlehands@gmail.com' --password 'LovePizza!11one' --pickle

Example command with custom options chained together

python3 get_profit_and_loss.py --username 'timmyturtlehands@gmail.com' --password 'LovePizzaFhdjeiw!22222' --start_date 'July 1, 2018' --end_date 'November 10, 2018' --starting_allocation '5000' --csv

Requirements

numpy
pandas
requests
six

other notes and 'bibliography' ;)