From 25575d294ade23120afaf5c085be7ca40bf4ef4b Mon Sep 17 00:00:00 2001 From: Eugeniu Plamadeala Date: Sat, 19 Dec 2020 17:20:52 -0800 Subject: [PATCH] Handle option lots by using 100X the quantity when importing. --- beancount_import/source/schwab_csv.py | 7 +++++++ .../schwab_csv/test_basic/import_results.beancount | 12 ++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/beancount_import/source/schwab_csv.py b/beancount_import/source/schwab_csv.py index 9f01b64c..ea307c6d 100644 --- a/beancount_import/source/schwab_csv.py +++ b/beancount_import/source/schwab_csv.py @@ -725,6 +725,7 @@ def process_positions( TAXES_ACCOUNT_KEY = "taxes_account" DATE_FORMAT = "%m/%d/%Y" TITLE_RE = re.compile(r'"Transactions for account (?P.+) as of (?P.+)"') +OPTION_RE = re.compile(r'\w{1,4} \d\d\/\d\d\/\d\d\d\d \d*\.\d* [PC]') STRIP_FROM_SYMBOL_RE = re.compile(r'[^\d\w]') @@ -941,6 +942,9 @@ def _load_transactions(filename: str) -> List[RawEntry]: price = _convert_decimal(row["Price"]) fees = _convert_decimal(row["Fees & Comm"]) amount = _convert_decimal(row["Amount"]) + if OPTION_RE.match(row["Symbol"]) and quantity: + # this is an option, sold in lots of 100 + quantity *= 100 entries.append( RawEntry( account=account, @@ -1077,6 +1081,9 @@ def _load_positions_csv( value_d = _convert_decimal(row["Market Value"]) assert value_d is not None, row["Market Value"] value = Amount(value_d, currency="USD") + if OPTION_RE.match(row["Symbol"]) and quantity: + # this is an option, sold in lots of 100 + quantity *= 100 entries.append( RawPosition( date=date, diff --git a/testdata/source/schwab_csv/test_basic/import_results.beancount b/testdata/source/schwab_csv/test_basic/import_results.beancount index 3e85cd63..d9955274 100644 --- a/testdata/source/schwab_csv/test_basic/import_results.beancount +++ b/testdata/source/schwab_csv/test_basic/import_results.beancount @@ -89,7 +89,7 @@ ; features: [] 2020-03-17 * "BUYOPT - PUT SMALL CAP INDEX $32 EXP 04/01/20" - Assets:Schwab:Intelligent-4321:SMAL040120203200P 1 SMAL040120203200P {12.53 USD} + Assets:Schwab:Intelligent-4321:SMAL040120203200P 100 SMAL040120203200P {12.53 USD} date: 2020-03-17 schwab_action: "Buy to Open" source_desc: "PUT SMALL CAP INDEX $32 EXP 04/01/20" @@ -104,7 +104,7 @@ ; features: [] 2020-03-18 * "SELLOPT - PUT SMALL CAP INDEX $32 EXP 04/01/20" - Assets:Schwab:Intelligent-4321:SMAL040120203200P -1 SMAL040120203200P {} @ 15.66 USD + Assets:Schwab:Intelligent-4321:SMAL040120203200P -100 SMAL040120203200P {} @ 15.66 USD date: 2020-03-18 schwab_action: "Sell to Close" source_desc: "PUT SMALL CAP INDEX $32 EXP 04/01/20" @@ -120,7 +120,7 @@ ; features: [] 2020-03-27 * "SELLOPT - PUT SMALL CAP INDEX $10 EXP 06/19/20" - Assets:Schwab:Intelligent-4321:SMAL061920201000P -1 SMAL061920201000P {} @ 8.48 USD + Assets:Schwab:Intelligent-4321:SMAL061920201000P -100 SMAL061920201000P {} @ 8.48 USD date: 2020-03-27 schwab_action: "Sell to Open" source_desc: "PUT SMALL CAP INDEX $10 EXP 06/19/20" @@ -135,7 +135,7 @@ ; features: [] 2020-03-30 * "BUYOPT - CALL SMALL CAP INDEX $83 EXP 04/03/20" - Assets:Schwab:Intelligent-4321:SMAL040320208300C 2 SMAL040320208300C {0.49 USD} + Assets:Schwab:Intelligent-4321:SMAL040320208300C 200 SMAL040320208300C {0.49 USD} date: 2020-03-30 schwab_action: "Buy to Open" source_desc: "CALL SMALL CAP INDEX $83 EXP 04/03/20" @@ -150,7 +150,7 @@ ; features: [] 2020-06-12 * "BUYOPT - PUT SMALL CAP INDEX $10 EXP 06/19/20" - Assets:Schwab:Intelligent-4321:SMAL061920201000P 1 SMAL061920201000P {0.06 USD} + Assets:Schwab:Intelligent-4321:SMAL061920201000P 100 SMAL061920201000P {0.06 USD} date: 2020-06-12 schwab_action: "Buy to Close" source_desc: "PUT SMALL CAP INDEX $10 EXP 06/19/20" @@ -381,7 +381,7 @@ ;; date: 2020-11-15 ;; info: {"filename": "/test_basic/positions/All-Accounts-Positions-2020-11-15.CSV", "line": 13, "type": "text/csv"} -2020-11-15 balance Assets:Schwab:Intelligent-4321:SMAL040120203200P 1 SMAL040120203200P +2020-11-15 balance Assets:Schwab:Intelligent-4321:SMAL040120203200P 100 SMAL040120203200P ;; date: 2020-11-15 ;; info: {"filename": "/test_basic/positions/All-Accounts-Positions-2020-11-15.CSV", "line": 14, "type": "text/csv"}