Skip to content

Commit

Permalink
[schwab_csv] add support for reverse split.
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Nov 14, 2021
1 parent fd08d20 commit 253ef80
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
21 changes: 20 additions & 1 deletion beancount_import/source/schwab_csv.py
Expand Up @@ -177,6 +177,7 @@ class BrokerageAction(enum.Enum):
NON_QUALIFIED_DIVIDEND = "Non-Qualified Div"
REINVEST_DIVIDEND = "Reinvest Dividend"
REINVEST_SHARES = "Reinvest Shares"
REVERSE_SPLIT = "Reverse Split"
SECURITY_TRANSFER = "Security Transfer"
SELL = "Sell"
SELL_TO_CLOSE = "Sell to Close"
Expand Down Expand Up @@ -336,7 +337,7 @@ def get_processed_entry(
interest_account=interest_account,
**shared_attrs,
)
if self.action == BrokerageAction.STOCK_SPLIT:
if self.action in (BrokerageAction.REVERSE_SPLIT, BrokerageAction.STOCK_SPLIT):
assert self.quantity is not None
lot_splits = lots.split(schwab_account, self.symbol, self.date, self.quantity)
return StockSplit(
Expand Down Expand Up @@ -1593,11 +1594,18 @@ def _load_banking_transactions(reader: csv.DictReader, account: str, filename):
return entries


@dataclass(frozen=True)
class ReverseSplitFirstLine:
symbol: str
quantity: Decimal


def _load_brokerage_transactions(reader: csv.DictReader, account: str,
filename):
entries = []
found_total_line = False
merger_spec = None
reverse_split = None
for lno, row in enumerate(reader):
# Final row in CSV is not a real transaction
if row["Date"] == "Transactions Total":
Expand All @@ -1621,6 +1629,17 @@ def _load_brokerage_transactions(reader: csv.DictReader, account: str,
# special logic: next CSV line is the second posting related to the merger
merger_spec = MergerSpecification(symbol, quantity, description)
continue
elif action == BrokerageAction.REVERSE_SPLIT and reverse_split is None:
# reverse splits occupy two lines in CSV
assert quantity is not None, quantity
reverse_split = ReverseSplitFirstLine(symbol=symbol, quantity=quantity)
continue
if reverse_split is not None:
assert action == BrokerageAction.REVERSE_SPLIT
assert quantity is not None, quantity
quantity += reverse_split.quantity
symbol = reverse_split.symbol
reverse_split = None
entries.append(
RawBrokerageEntry(
account=account,
Expand Down
1 change: 1 addition & 0 deletions testdata/source/schwab_csv/test_lots/accounts.txt
Expand Up @@ -2,4 +2,5 @@ Assets:Schwab:Intelligent-4321
Assets:Schwab:Intelligent-4321:Cash
Assets:Schwab:Intelligent-4321:FNDA
Assets:Schwab:Intelligent-4321:HYLB
Assets:Schwab:Intelligent-4321:IAU
Assets:Schwab:Intelligent-4321:SCHA
44 changes: 36 additions & 8 deletions testdata/source/schwab_csv/test_lots/import_results.beancount
@@ -1,5 +1,5 @@
;; date: 2020-08-24
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 9, "type": "text/csv"}
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 12, "type": "text/csv"}

; features: []
2020-08-24 * "BUYSTOCK - SCHWAB FUNDAMENTAL US SMALL COM ETF"
Expand All @@ -13,7 +13,7 @@
source_desc: "SCHWAB FUNDAMENTAL US SMALL COM ETF"

;; date: 2020-08-24
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 8, "type": "text/csv"}
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 11, "type": "text/csv"}

; features: []
2020-08-24 * "BUYSTOCK - SCHWAB US SMALL CAP ETF"
Expand Down Expand Up @@ -52,7 +52,7 @@
2020-08-25 price SCHA 71.39 USD

;; date: 2020-08-30
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 7, "type": "text/csv"}
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 10, "type": "text/csv"}

; features: []
2020-08-30 * "BUYSTOCK - XTRACKERS USD HIGH YIELDCOR BND ETF"
Expand All @@ -66,7 +66,7 @@
source_desc: "XTRACKERS USD HIGH YIELDCOR BND ETF"

;; date: 2020-08-31
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 6, "type": "text/csv"}
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 9, "type": "text/csv"}

; features: []
2020-08-31 * "BUYSTOCK - XTRACKERS USD HIGH YIELDCOR BND ETF"
Expand All @@ -80,7 +80,21 @@
source_desc: "XTRACKERS USD HIGH YIELDCOR BND ETF"

;; date: 2020-09-24
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 5, "type": "text/csv"}
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 8, "type": "text/csv"}

; features: []
2020-09-24 * "BUYSTOCK - ISHARES GOLD ETF"
Assets:Schwab:Intelligent-4321:IAU 10 IAU {10.00 USD}
date: 2020-09-24
schwab_action: "Buy"
source_desc: "ISHARES GOLD ETF"
Assets:Schwab:Intelligent-4321:Cash -100.00 USD
date: 2020-09-24
schwab_action: "Buy"
source_desc: "ISHARES GOLD ETF"

;; date: 2020-09-24
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 7, "type": "text/csv"}

; features: []
2020-09-24 * "BUYSTOCK - SCHWAB US SMALL CAP ETF"
Expand Down Expand Up @@ -119,7 +133,7 @@
2020-09-25 price SCHA 72.56 USD

;; date: 2020-10-13
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 4, "type": "text/csv"}
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 6, "type": "text/csv"}

; features: []
2020-10-13 * "STOCKSPLIT - XTRACKERS USD HIGH YIELDCOR BND ETF"
Expand All @@ -141,7 +155,7 @@
source_desc: "XTRACKERS USD HIGH YIELDCOR BND ETF"

;; date: 2020-10-19
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 3, "type": "text/csv"}
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 5, "type": "text/csv"}

; features: []
2020-10-19 * "SELLSTOCK - SCHWAB FUNDAMENTAL US SMALL CAP ETF"
Expand All @@ -156,7 +170,7 @@
Income:Capital-Gains:Schwab:FNDA

;; date: 2020-10-19
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 2, "type": "text/csv"}
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 4, "type": "text/csv"}

; features: []
2020-10-19 * "SELLSTOCK - SCHWAB US SMALL CAP ETF"
Expand All @@ -174,6 +188,20 @@
source_desc: "SCHWAB US SMALL CAP ETF"
Income:Capital-Gains:Schwab:SCHA

;; date: 2020-10-22
;; info: {"filename": "<testdata>/test_lots/transactions/Intelligent_Transactions_20201115-180120.CSV", "line": 3, "type": "text/csv"}

; features: []
2020-10-22 * "STOCKSPLIT - ISHARES GOLD ETF XXXREVERSE SPLIT EFF: 10/21/20"
Assets:Schwab:Intelligent-4321:IAU -10 IAU {10.00 USD, 2020-09-24}
date: 2020-10-22
schwab_action: "Reverse Split"
source_desc: "ISHARES GOLD ETF XXXREVERSE SPLIT EFF: 10/21/20"
Assets:Schwab:Intelligent-4321:IAU 5.0 IAU {20.0 USD, 2020-09-24}
date: 2020-10-22
schwab_action: "Reverse Split"
source_desc: "ISHARES GOLD ETF XXXREVERSE SPLIT EFF: 10/21/20"

;; date: 2020-10-25
;; info: {"filename": "<testdata>/test_lots/positions/Intelligent-Positions-2020-10-25.CSV", "line": 4, "type": "text/csv"}

Expand Down
@@ -0,0 +1,5 @@
"IAU Lot Details for XXXX-4321 as of 02:45 AM ET, 09/25/2020"

"Open Date","Quantity","Price","Cost/Share","Market Value","Cost Basis","Gain/Loss $","Gain/Loss %","Holding Period",
"09/24/2020 13:42:07","10","$10.00","$10.00","$100.00","$100.00","$0.00","+0.00%","Short Term",
"Total","10","--","--","$100.00","$100.00","$0.00","+0.00%","--",
@@ -0,0 +1,5 @@
"IAU Lot Details for XXXX-4321 as of 02:45 AM ET, 10/25/2020"

"Open Date","Quantity","Price","Cost/Share","Market Value","Cost Basis","Gain/Loss $","Gain/Loss %","Holding Period",
"09/24/2020 13:42:07","5","$20.00","$20.00","$100.00","$100.00","$0.00","+0.00%","Short Term",
"Total","5","--","--","$100.00","$100.00","$0.00","+0.00%","--",
@@ -1,9 +1,12 @@
"Transactions for account Intelligent XXXX-4321 as of 11/15/2020 18:01:20 ET"
"Date","Action","Symbol","Description","Quantity","Price","Fees & Comm","Amount",
"10/22/2020","Reverse Split","IAU","ISHARES GOLD ETF","5","","","",
"10/22/2020","Reverse Split","464285105","ISHARES GOLD ETF XXXREVERSE SPLIT EFF: 10/21/20","-10","","","",
"10/19/2020","Sell","SCHA","SCHWAB US SMALL CAP ETF","19.5","$74.23","","$1447.49",
"10/19/2020","Sell","FNDA","SCHWAB FUNDAMENTAL US SMALL CAP ETF","1","$35.00","","$35.00",
"10/14/2020 as of 10/13/2020","Stock Split","HYLB","XTRACKERS USD HIGH YIELDCOR BND ETF","20","$5.00","","",
"09/24/2020","Buy","SCHA","SCHWAB US SMALL CAP ETF","20","$72.56","","-$1451.20",
"09/24/2020","Buy","IAU","ISHARES GOLD ETF","10","$10.00","","-$100.00",
"08/31/2020","Buy","HYLB","XTRACKERS USD HIGH YIELDCOR BND ETF","12","$10.00","","-$120.00",
"08/30/2020","Buy","HYLB","XTRACKERS USD HIGH YIELDCOR BND ETF","8","$20.00","","-$160.00",
"08/24/2020","Buy","SCHA","SCHWAB US SMALL CAP ETF","10","$71.35","","-$713.50",
Expand Down

0 comments on commit 253ef80

Please sign in to comment.