Skip to content

Commit

Permalink
Handle option lots by using 100X the quantity when importing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zburatorul committed Dec 31, 2020
1 parent 85f3402 commit 25575d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
7 changes: 7 additions & 0 deletions beancount_import/source/schwab_csv.py
Expand Up @@ -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<account>.+) as of (?P<when>.+)"')
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]')


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions testdata/source/schwab_csv/test_basic/import_results.beancount
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -381,7 +381,7 @@
;; date: 2020-11-15
;; info: {"filename": "<testdata>/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": "<testdata>/test_basic/positions/All-Accounts-Positions-2020-11-15.CSV", "line": 14, "type": "text/csv"}
Expand Down

0 comments on commit 25575d2

Please sign in to comment.