This program creates a brief, two-page document for an investor to summarize the historical outcome of the investor's portfolio transactions over the selected time period.
- Allow investors to input their tranaction data via a .csv file
- Handle time-series transactions based on First-In-First-Out (FIFO) Logic
- Utilize the QuantStats library to display portfolio statistics against a benchmark
- Print the statistics output to a .pdf for simpler sharing
- Uses the pandas_market_calendars library to find all relevant trading days within a specified timeframe
- Automatically filters out non-trading days based on the market
- Sets NYSE as the calendar, and then standardizes the timestamps to make them easy to join on later
- Takes a folder name and filename for where the .csv if located and creates a dataframe out of it
- Takes an array of stock tickers with a start and end date and grabs the data using the YFinance library
- Create an empty dataframe called stocks_with_sales to which we'll add adjusted positions, and another dataframe holding all of the transactions labeled as buys
- Here we calculate the realized gain of each sell order
- Add to the adjusted positions dataframe, the positions that never had sales, sales that occur in the future, and any zeroed out rows to create a record of your active holdings as of the start date
- Flters sales to find any that have occurred on the current date and creates a dataframe of positions not affected by sales
- Then use
position_adjust
to zero-out any positions with active sales and append the positions with no changes, leaving you with an accurate daily snapshot of your porfolio positions - Any rows with zero 'Qty' are left for realized gain performance calculations
- Provide our dataframe of active positions, find the sales, and zero-out sales against buy positions.
- Loop through using our market_cal list with valid trading days
- Filter to positions that have occurred before or at the current date and make sure there are only buys.
- Add a Date Snapshot column with the current date in the market_cal loop, then append it to our per_day_balance list
- Before adjusting a position, we make sure to save all future transactions to another dataframe and append them later
- Merges provided dataframes and calculates daily adjusted cost
- Finds end date closing prices for each ticker
- Finds start date closing prices for each ticker
- Applies a bunch of calculations against the data we’ve been modifying, and returns a final dataframe
- Runs
modified_cost_per_share
,portfolio_end_of_year_stats
,portfolio_start_of_year_stats
,calc_returns
and returns a daily snapshot of the portfolio
- Formats the per day portfolio calculations for use with the QuantStats library by adding up the daily realized, unrealized gains, dividends and cash
- Utilizes the QuantStats library to create a .html and .pdf of the tearsheet returns as well as display the output to the consol with quantitative statistics
- Connect to a Runtime
- Press
Ctrl + F9
# Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')
# Gather Data and Generate Report
generate_report('Tearsheet Generator', 'TransactionHistory2020-2022withCash', '2020-04-30')
Symbol | Qty | Type | Open date | Adj cost per share | Adj cost |
---|---|---|---|---|---|
AAPL | 10 | Buy | 4/29/2020 | 71 | 710 |
MSFT | 10 | Buy | 5/1/2020 | 175 | 1750 |
AAPL | 5 | Sell.FIFO | 5/15/2020 | 77 | 385 |
... | ... | ... | ... | ... | ... |