-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fidelity_brokerage_csv importer (ofx is limited to 90 days)
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
beancount_reds_importers/importers/fidelity/fidelity_brokerage_csv.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Fidelity Brokerage csv importer for beancount.""" | ||
|
||
import re | ||
|
||
from beancount_reds_importers.libreader import csvreader | ||
from beancount_reds_importers.libtransactionbuilder import investments | ||
|
||
|
||
class Importer(investments.Importer, csvreader.Importer): | ||
IMPORTER_NAME = "Fidelity Cash Management Account" | ||
|
||
def custom_init(self): | ||
self.max_rounding_error = 0.04 | ||
self.filename_pattern_def = ".*History" | ||
self.date_format = "%m/%d/%Y" | ||
self.header_identifier = "" | ||
self.column_labels_line = ( | ||
"Run Date,Action,Symbol,Description,Type,Quantity,Price ($),Commission ($),Fees ($),Accrued Interest ($),Amount ($),Cash Balance ($),Settlement Date" | ||
) | ||
self.header_map = { | ||
"Run Date": "date", | ||
"Action": "memo", | ||
"Symbol": "security", | ||
"Amount ($)": "amount", | ||
"Settlement Date": "settleDate", | ||
"Quantity": "units", | ||
"Accrued Interest ($)": "accrued_interest", | ||
"Fees ($)": "fees", | ||
"Commission ($)": "commission", | ||
"Cash Balance ($)": "balance", | ||
"Price ($)": "unit_price", | ||
} | ||
self.transaction_type_map = { | ||
"DIVIDEND RECEIVED": "dividends", | ||
"TRANSFERRED FROM": "cash", | ||
"YOU BOUGHT": "buystock", | ||
"YOU SOLD": "sellstock", | ||
} | ||
self.skip_transaction_types = [] | ||
# fmt: on | ||
|
||
def deep_identify(self, file): | ||
last_four = self.config.get("account_number", "")[-4:] | ||
return re.match(self.header_identifier, file.head(), flags=re.DOTALL) and f"{last_four}" in file.name | ||
|
||
def prepare_table(self, rdr): | ||
for field in ["Action", "Symbol", "Description"]: | ||
rdr = rdr.convert(field, lambda x: x.lstrip()) | ||
|
||
rdr = rdr.addfield("total", lambda x: x["Amount ($)"]) | ||
rdr = rdr.addfield("tradeDate", lambda x: x["Run Date"]) | ||
rdr = rdr.cutout('Type') | ||
rdr = rdr.capture("Action", "(\\S+(?:\\s+\\S+)?)", ["type"], include_original=True) | ||
|
||
# for field in ["memo"]: | ||
# rdr = rdr.convert(field, lambda x: x.lstrip()) | ||
|
||
return rdr |
0cef611
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, I'm not sure if my unfinished PR way from years ago helps ( #17 -> https://github.com/savingsandloan/beancount_reds_importers/commits/fidelity_csv_old ) but it might have some inspiration for expanding this.
I still technically use that personal branch and for similar motivating reasons (I do updates at much slower intervals so the 90 day ofx never worked for me). Slowly catching up on beancount again and all the improvements you've done to your importers over the years.