This is a set of scripts to download and analyze finance data.
The aim is to...
- download daily holdings automatically
- download historical data
- update YNAB accounts
Clone the repository.
- This repository uses the YNAB API by dmlerner.
- This repository uses the Personal Capital API by haochi.
Create a conda environment.
conda env create -f environment.yml
conda activate FinanceScripts
Set the following environment variables.
PC_PASSWORD=[Your PC Password]
PC_USERNAME=[Your PC Username]
YNAB_APIKEY=[Your YNAB apikey]
- These variables may be set in a filed called
.env
in the run directory. - Please note that the script will pause for 2-factor authentication on the 1st run.
import pandas as pd
import dataclasses
import datetime
import finance.apis
import finance.scrapers
if __name__ == '__main__':
# create API handler instance
handler = finance.apis.pcap.PCAPHandler()
# fetch all transaction objects
transactions = finance.scrapers.pcap.TransactionsScraper(handler, t0=datetime.datetime.now(), dt=1)
for transaction in transactions.objects:
print(transaction)
# create a dataframe from any dataclass based object
tframe: pd.DataFrame = pd.DataFrame(dataclasses.asdict(t) for t in transactions.objects)
A script to download the market value for an account. The script is given a starting date and a time period freqency. The rows of the resulting dataframe will display the market value at the timestamps.
cd ./workspace
conda activate FinanceScripts
python -m scraper.apps.pcap.marketvalue --start 2019-12-01 --freq W
For example, the following frame will be produced.
Vanguard : Roth IRA
Date Payee Memo Amount
0 2019-12-01 23:59:59.999999+00:00 Market 123.45
1 2019-12-08 23:59:59.999999+00:00 Market 678.90
2 2019-12-15 23:59:59.999999+00:00 Market 123.45
3 2019-12-22 23:59:59.999999+00:00 Market 678.90
4 2019-12-29 23:59:59.999999+00:00 Market 123.45
Total 1728.15
Fill in missing values by creating YAML based rule files in the run directory.
- The rules are a list of
where
andvalue
mappings.- If all items from the
where
mapping match an instance's attributes...- The items from the
value
mapping will be set on the instance.
- The items from the
- If all items from the
rules:
- where:
cusip: 'XXXXXXXXX'
userAccountId: 00000000
value:
accountName: 'RothIRA'
ticker: 'VTSAX'