From 9c9272477564b84dfcc72a44628655cd6822b53f Mon Sep 17 00:00:00 2001 From: Gerben Jacobs Date: Sat, 24 Jun 2023 00:33:34 +0200 Subject: [PATCH] fix issue with new dividend naming convention --- trading212/aggregate.go | 4 ++-- trading212/aggregate_test.go | 4 ++-- trading212/collect_test.go | 20 ++++++++++---------- trading212/export.go | 21 ++++----------------- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/trading212/aggregate.go b/trading212/aggregate.go index 5a50882..d58cc7e 100644 --- a/trading212/aggregate.go +++ b/trading212/aggregate.go @@ -15,14 +15,14 @@ func Aggregate(splits []fin.Splits, events []TradeEvent) ([]fin.Aggregate, fin.T var totals fin.Totals for _, e := range events { // handle deposits - if e.Action == ActionDeposit { + if e.Action == "Deposit" { totals.Deposits += e.Deposit totals.DepositFees += e.DepositFee continue } // handle interest - if e.Action == ActionInterest { + if e.Action == "Interest on cash" { totals.Interest += e.Total continue } diff --git a/trading212/aggregate_test.go b/trading212/aggregate_test.go index 05b8320..018644e 100644 --- a/trading212/aggregate_test.go +++ b/trading212/aggregate_test.go @@ -42,8 +42,8 @@ func TestAggregate(t *testing.T) { { name: "Test with a split", events: []TradeEvent{ - {Action: ActionMarketBuy, Time: DateTime{Time: time.Date(2021, 9, 27, 13, 19, 13, 0, time.UTC)}, TickerSymbol: "ABEC", ShareCount: 0.005, SharePrice: 2000.00, Total: 10.00, ID: "EOF1"}, - {Action: ActionMarketBuy, Time: DateTime{Time: time.Date(2022, 9, 27, 13, 19, 13, 0, time.UTC)}, TickerSymbol: "ABEC", ShareCount: 0.125, SharePrice: 80.00, Total: 10.00, ID: "EOF2"}, + {Action: "Market buy", Time: DateTime{Time: time.Date(2021, 9, 27, 13, 19, 13, 0, time.UTC)}, TickerSymbol: "ABEC", ShareCount: 0.005, SharePrice: 2000.00, Total: 10.00, ID: "EOF1"}, + {Action: "Market buy", Time: DateTime{Time: time.Date(2022, 9, 27, 13, 19, 13, 0, time.UTC)}, TickerSymbol: "ABEC", ShareCount: 0.125, SharePrice: 80.00, Total: 10.00, ID: "EOF2"}, }, want: []fin.Aggregate{ {Symbol: "ABEC", ShareCount: 0.225, AvgPrice: 88.88, ShareCost: 20, ShareCostLocal: 20, ShareResult: 0, TotalDividend: 0, Fees: 0, Final: 0, LastUpdate: time.Date(2022, 9, 27, 13, 19, 13, 0, time.UTC)}, diff --git a/trading212/collect_test.go b/trading212/collect_test.go index 26a1ca9..2828f45 100644 --- a/trading212/collect_test.go +++ b/trading212/collect_test.go @@ -9,16 +9,16 @@ import ( // defaultTestDataEvents is a list of TradeEvents that matches testdata/tradign212.csv // this can be used by several tests var defaultTestDataEvents = []TradeEvent{ - {Action: ActionDeposit, Time: DateTime{Time: time.Date(2021, 8, 9, 15, 25, 29, 0, time.UTC)}, Deposit: 1000, DepositFee: 0, Total: 1000, Notes: "Transaction ID: xxx", ID: "d0ca160f-f407-4b9b-bb36-xxx"}, - {Action: ActionMarketBuy, Time: DateTime{Time: time.Date(2021, 8, 9, 18, 31, 41, 0, time.UTC)}, ISIN: "US30303M1027", TickerSymbol: "FB", TickerName: "Meta Platforms", ShareCount: 0.0863914000, SharePrice: 362, ShareCurrency: "USD", ExchangeRate: "1.17437", Total: 26.67, ID: "EOF1", FXFee: 0.04}, - {Action: ActionMarketBuy, Time: DateTime{Time: time.Date(2021, 8, 9, 18, 31, 41, 0, time.UTC)}, ISIN: "US88160R1014", TickerSymbol: "TSLA", TickerName: "Tesla", ShareCount: 0.0766547000, SharePrice: 713.93, ShareCurrency: "USD", ExchangeRate: "1.17437", Total: 46.67, ID: "EOF2", FXFee: 0.07}, - {Action: ActionMarketBuy, Time: DateTime{Time: time.Date(2021, 8, 9, 18, 31, 41, 0, time.UTC)}, ISIN: "US5949181045", TickerSymbol: "MSFT", TickerName: "Microsoft", ShareCount: 0.2709950000, SharePrice: 288.53, ShareCurrency: "USD", ExchangeRate: "1.17437", Total: 66.68, ID: "EOF3", FXFee: 0.10}, - {Action: ActionMarketSell, Time: DateTime{Time: time.Date(2021, 8, 30, 13, 30, 3, 0, time.UTC)}, ISIN: "US5949181045", TickerSymbol: "MSFT", TickerName: "Microsoft", ShareCount: 0.2709950000, SharePrice: 301.14, ShareCurrency: "USD", ExchangeRate: "1.17951", Result: 2.61, Total: 69.09, ID: "EOF4", FXFee: 0.10}, - {Action: ActionDeposit, Time: DateTime{Time: time.Date(2021, 9, 7, 13, 43, 10, 0, time.UTC)}, Deposit: 1001.4, DepositFee: 1.40, Total: 1000, Notes: "Transaction ID: xxx", ID: "3e8f5274-1c62-46d6-baf4-xxx"}, - {Action: ActionMarketBuy, Time: DateTime{Time: time.Date(2021, 9, 27, 13, 19, 13, 0, time.UTC)}, ISIN: "US02079K1079", TickerSymbol: "ABEC", TickerName: "Google", ShareCount: 0.0041253700, SharePrice: 2424.00, ShareCurrency: "EUR", ExchangeRate: "1.00000", Total: 10.00, ID: "EOF5"}, - {Action: ActionDividend, Time: DateTime{Time: time.Date(2021, 9, 30, 11, 15, 32, 0, time.UTC)}, ISIN: "US5949181045", TickerSymbol: "MSFT", TickerName: "Microsoft", ShareCount: 0.2709950000, SharePrice: 0.48, ShareCurrency: "USD", ExchangeRate: "Not available", Total: 0.11, Tax: 0.02, TaxCurrency: "USD"}, - {Action: ActionMarketBuy, Time: DateTime{Time: time.Date(2022, 3, 7, 16, 10, 26, 0, time.UTC)}, ISIN: "FR0000120578", TickerSymbol: "SAN", TickerName: "Sanofi", ShareCount: 0.1117960000, SharePrice: 89.18, ShareCurrency: "EUR", ExchangeRate: "1.00000", Total: 10.00, ID: "EOF6", FRFee: 0.03}, - {Action: ActionMarketBuy, Time: DateTime{Time: time.Date(2022, 7, 29, 14, 28, 17, 0, time.UTC)}, ISIN: "US02079K1079", TickerSymbol: "ABEC", TickerName: "Alphabet (Class C)", ShareCount: 2.2887315000, SharePrice: 113.60, ShareCurrency: "EUR", ExchangeRate: "1.00000", Total: 260.00, ID: "EOF7"}, + {Action: "Deposit", Time: DateTime{Time: time.Date(2021, 8, 9, 15, 25, 29, 0, time.UTC)}, Deposit: 1000, DepositFee: 0, Total: 1000, Notes: "Transaction ID: xxx", ID: "d0ca160f-f407-4b9b-bb36-xxx"}, + {Action: "Market buy", Time: DateTime{Time: time.Date(2021, 8, 9, 18, 31, 41, 0, time.UTC)}, ISIN: "US30303M1027", TickerSymbol: "FB", TickerName: "Meta Platforms", ShareCount: 0.0863914000, SharePrice: 362, ShareCurrency: "USD", ExchangeRate: "1.17437", Total: 26.67, ID: "EOF1", FXFee: 0.04}, + {Action: "Market buy", Time: DateTime{Time: time.Date(2021, 8, 9, 18, 31, 41, 0, time.UTC)}, ISIN: "US88160R1014", TickerSymbol: "TSLA", TickerName: "Tesla", ShareCount: 0.0766547000, SharePrice: 713.93, ShareCurrency: "USD", ExchangeRate: "1.17437", Total: 46.67, ID: "EOF2", FXFee: 0.07}, + {Action: "Market buy", Time: DateTime{Time: time.Date(2021, 8, 9, 18, 31, 41, 0, time.UTC)}, ISIN: "US5949181045", TickerSymbol: "MSFT", TickerName: "Microsoft", ShareCount: 0.2709950000, SharePrice: 288.53, ShareCurrency: "USD", ExchangeRate: "1.17437", Total: 66.68, ID: "EOF3", FXFee: 0.10}, + {Action: "Market sell", Time: DateTime{Time: time.Date(2021, 8, 30, 13, 30, 3, 0, time.UTC)}, ISIN: "US5949181045", TickerSymbol: "MSFT", TickerName: "Microsoft", ShareCount: 0.2709950000, SharePrice: 301.14, ShareCurrency: "USD", ExchangeRate: "1.17951", Result: 2.61, Total: 69.09, ID: "EOF4", FXFee: 0.10}, + {Action: "Deposit", Time: DateTime{Time: time.Date(2021, 9, 7, 13, 43, 10, 0, time.UTC)}, Deposit: 1001.4, DepositFee: 1.40, Total: 1000, Notes: "Transaction ID: xxx", ID: "3e8f5274-1c62-46d6-baf4-xxx"}, + {Action: "Market buy", Time: DateTime{Time: time.Date(2021, 9, 27, 13, 19, 13, 0, time.UTC)}, ISIN: "US02079K1079", TickerSymbol: "ABEC", TickerName: "Google", ShareCount: 0.0041253700, SharePrice: 2424.00, ShareCurrency: "EUR", ExchangeRate: "1.00000", Total: 10.00, ID: "EOF5"}, + {Action: "Dividend (Ordinary)", Time: DateTime{Time: time.Date(2021, 9, 30, 11, 15, 32, 0, time.UTC)}, ISIN: "US5949181045", TickerSymbol: "MSFT", TickerName: "Microsoft", ShareCount: 0.2709950000, SharePrice: 0.48, ShareCurrency: "USD", ExchangeRate: "Not available", Total: 0.11, Tax: 0.02, TaxCurrency: "USD"}, + {Action: "Market buy", Time: DateTime{Time: time.Date(2022, 3, 7, 16, 10, 26, 0, time.UTC)}, ISIN: "FR0000120578", TickerSymbol: "SAN", TickerName: "Sanofi", ShareCount: 0.1117960000, SharePrice: 89.18, ShareCurrency: "EUR", ExchangeRate: "1.00000", Total: 10.00, ID: "EOF6", FRFee: 0.03}, + {Action: "Market buy", Time: DateTime{Time: time.Date(2022, 7, 29, 14, 28, 17, 0, time.UTC)}, ISIN: "US02079K1079", TickerSymbol: "ABEC", TickerName: "Alphabet (Class C)", ShareCount: 2.2887315000, SharePrice: 113.60, ShareCurrency: "EUR", ExchangeRate: "1.00000", Total: 260.00, ID: "EOF7"}, } func TestCollect(t *testing.T) { diff --git a/trading212/export.go b/trading212/export.go index b6c229d..079cf20 100644 --- a/trading212/export.go +++ b/trading212/export.go @@ -1,35 +1,22 @@ package trading212 import ( + "strings" "time" ) const format = "2006-01-02 15:04:05" -const ( - ActionDeposit = "Deposit" - ActionMarketBuy = "Market buy" - ActionMarketSell = "Market sell" - ActionDividend = "Dividend (Ordinary)" - ActionDividendBonus = "Dividend (Bonus)" - ActionDividendROC = "Dividend (Return of capital)" - ActionLimitBuy = "Limit buy" - ActionLimitSell = "Limit sell" - ActionStopBuy = "Stop buy" - ActionStopSell = "Stop sell" - ActionInterest = "Interest on cash" -) - func (e *TradeEvent) IsBuying() bool { - return e.Action == ActionMarketBuy || e.Action == ActionLimitBuy || e.Action == ActionStopBuy + return strings.Contains(e.Action, "buy") } func (e *TradeEvent) IsSelling() bool { - return e.Action == ActionMarketSell || e.Action == ActionLimitSell || e.Action == ActionStopSell + return strings.Contains(e.Action, "sell") } func (e *TradeEvent) IsDividend() bool { - return e.Action == ActionDividend || e.Action == ActionDividendBonus || e.Action == ActionDividendROC + return strings.Contains(e.Action, "Dividend") } func (e *TradeEvent) Fees() float64 {