Skip to content

Commit

Permalink
Fix parser of DeGiro with the changed format
Browse files Browse the repository at this point in the history
  • Loading branch information
VibeNL committed Sep 19, 2023
1 parent 365d34d commit 0958eab
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task ConvertToOrders_TestFileSingleOrder_Converted()
}

[Fact]
public async Task ConvertToOrders_TestFileMuitpleOrders_Converted()
public async Task ConvertToOrders_TestFileMultipleOrders_Converted()
{
// Arrange
var parser = new DeGiroParser(api.Object);
Expand Down Expand Up @@ -120,5 +120,38 @@ public async Task ConvertToOrders_TestFileMuitpleOrders_Converted()
ReferenceCode = "67e39ca1-2f10-4f82-8365-1baad98c398f"
} });
}

[Fact]
public async Task ConvertToOrders_TestFileDividend_Converted()
{
// Arrange
var parser = new DeGiroParser(api.Object);
var fixture = new Fixture();

var asset = fixture.Build<Asset>().With(x => x.Currency, "EUR").Create();
var account = fixture.Create<Account>();

api.Setup(x => x.GetAccountByName(account.Name)).ReturnsAsync(account);
api.Setup(x => x.FindSymbolByISIN("NL0009690239", null)).ReturnsAsync(asset);

// Act
var orders = await parser.ConvertToOrders(account.Name, new[] { "./FileImporter/TestFiles/DeGiro/Example4/TestFileDividend.csv" });

// Assert
orders.Should().BeEquivalentTo(new[] { new Order {
AccountId = account.Id,
Asset = asset,
Comment = "Transaction Reference: [DIVIDEND_14-9-2023_06:32_NL0009690239]",
Currency = asset.Currency,
FeeCurrency = asset.Currency,
Date = new DateTime(2023,09,14,6, 32,0, DateTimeKind.Utc),
Fee = 1.44M,
Quantity = 1,
Type = OrderType.DIVIDEND,
UnitPrice = 9.57M,
ReferenceCode = "DIVIDEND_14-9-2023_06:32_NL0009690239"
} });
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task LoadAccounts()

// Assert
testImporter.Verify(x => x.ConvertToOrders("Coinbase", It.Is<IEnumerable<string>>(y => y.Count() == 2)), Times.Once);
testImporter.Verify(x => x.ConvertToOrders("DeGiro", It.Is<IEnumerable<string>>(y => y.Count() == 3)), Times.Once);
testImporter.Verify(x => x.ConvertToOrders("DeGiro", It.Is<IEnumerable<string>>(y => y.Count() == 4)), Times.Once);
testImporter.Verify(x => x.ConvertToOrders("ScalableCapital", It.Is<IEnumerable<string>>(y => y.Count() == 5)), Times.Once);
testImporter.Verify(x => x.ConvertToOrders("Trading212", It.Is<IEnumerable<string>>(y => y.Count() == 8)), Times.Once);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Datum,Tijd,Valutadatum,Product,ISIN,Omschrijving,FX,Mutatie,,Saldo,,Order Id
10-07-2023,17:34,10-07-2023,FLATEX EURO BANKACCOUNT,NLFLATEXACNT,Degiro Cash Sweep Transfer,,EUR,"78,30",EUR,"100,00",
07-07-2023,05:01,06-07-2023,,,iDEAL Deposit,,EUR,"100,00",EUR,"21,70",
07-07-2023,05:01,06-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,"-100,00",EUR,"-78,30",
06-07-2023,09:39,06-07-2023,VANGUARD S&P500,IE00B3XXRP09,DEGIRO Transactiekosten en/of kosten van derden,,EUR,"-1,00",EUR,"21,70",b7ab0494-1b46-4e2f-9bd2-f79e6c87cb5b
06-07-2023,09:39,06-07-2023,VANGUARD S&P500,IE00B3XXRP09,"Koop 1 @ 77,3 EUR",,EUR,"-77,30",EUR,"22,70",b7ab0494-1b46-4e2f-9bd2-f79e6c87cb5b
06-07-2023,09:39,06-07-2023,VANGUARD S&P500,IE00B3XXRP09,DEGIRO Transactiekosten en/of kosten van derden,,EUR,-1.00,EUR,21.70,b7ab0494-1b46-4e2f-9bd2-f79e6c87cb5b
06-07-2023,09:39,06-07-2023,VANGUARD S&P500,IE00B3XXRP09,"Koop 1 @ 77,3 EUR",,EUR,-77.30,EUR,22.70,b7ab0494-1b46-4e2f-9bd2-f79e6c87cb5b
06-07-2023,09:37,06-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,"100,00",EUR,"100,00",
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
Datum,Tijd,Valutadatum,Product,ISIN,Omschrijving,FX,Mutatie,,Saldo,,Order Id
13-07-2023,11:49,12-07-2023,,,"Overboeking van uw geldrekening bij flatexDEGIRO Bank 976,17 EUR",,,,EUR,"23,83",
13-07-2023,11:49,12-07-2023,FLATEX EURO BANKACCOUNT,NLFLATEXACNT,Degiro Cash Sweep Transfer,,EUR,"976,17",EUR,"1000,00",
12-07-2023,04:51,11-07-2023,,,iDEAL Deposit,,EUR,"1000,00",EUR,"23,83",
12-07-2023,04:51,11-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,"-1000,00",EUR,"-976,17",
11-07-2023,16:34,11-07-2023,,,"Overboeking van uw geldrekening bij flatexDEGIRO Bank 21,7 EUR",,,,EUR,"23,83",
11-07-2023,16:34,11-07-2023,FLATEX EURO BANKACCOUNT,NLFLATEXACNT,Degiro Cash Sweep Transfer,,EUR,"21,70",EUR,"45,53",
11-07-2023,09:33,11-07-2023,VANECK GLOBAL REAL ESTATE UCITS ETF,NL0009690239,DEGIRO Transactiekosten en/of kosten van derden,,EUR,"-1,00",EUR,"23,83",67e39ca1-2f10-4f82-8365-1baad98c398f
11-07-2023,09:33,11-07-2023,VANECK GLOBAL REAL ESTATE UCITS ETF,NL0009690239,"Koop 29 @ 34,375 EUR",,EUR,"-996,88",EUR,"24,83",67e39ca1-2f10-4f82-8365-1baad98c398f
11-07-2023,08:51,11-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,"1000,00",EUR,"1021,70",
10-07-2023,17:34,10-07-2023,,,"Overboeking van uw geldrekening bij flatexDEGIRO Bank 78,3 EUR",,,,EUR,"21,70",
10-07-2023,17:34,10-07-2023,FLATEX EURO BANKACCOUNT,NLFLATEXACNT,Degiro Cash Sweep Transfer,,EUR,"78,30",EUR,"100,00",
07-07-2023,05:01,06-07-2023,,,iDEAL Deposit,,EUR,"100,00",EUR,"21,70",
07-07-2023,05:01,06-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,"-100,00",EUR,"-78,30",
06-07-2023,09:39,06-07-2023,VANGUARD S&P500,IE00B3XXRP09,DEGIRO Transactiekosten en/of kosten van derden,,EUR,"-1,00",EUR,"21,70",b7ab0494-1b46-4e2f-9bd2-f79e6c87cb5b
06-07-2023,09:39,06-07-2023,VANGUARD S&P500,IE00B3XXRP09,"Koop 1 @ 77,3 EUR",,EUR,"-77,30",EUR,"22,70",b7ab0494-1b46-4e2f-9bd2-f79e6c87cb5b
06-07-2023,09:37,06-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,"100,00",EUR,"100,00",
13-07-2023,11:49,12-07-2023,,,"Overboeking van uw geldrekening bij flatexDEGIRO Bank 976,17 EUR",,,,EUR,23.83,
13-07-2023,11:49,12-07-2023,FLATEX EURO BANKACCOUNT,NLFLATEXACNT,Degiro Cash Sweep Transfer,,EUR,976.17,EUR,1000.00,
12-07-2023,04:51,11-07-2023,,,iDEAL Deposit,,EUR,1000.00,EUR,23.83,
12-07-2023,04:51,11-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,-1000.00,EUR,-976.17,
11-07-2023,16:34,11-07-2023,,,"Overboeking van uw geldrekening bij flatexDEGIRO Bank 21,7 EUR",,,,EUR,23.83,
11-07-2023,16:34,11-07-2023,FLATEX EURO BANKACCOUNT,NLFLATEXACNT,Degiro Cash Sweep Transfer,,EUR,21.70,EUR,45.53,
11-07-2023,09:33,11-07-2023,VANECK GLOBAL REAL ESTATE UCITS ETF,NL0009690239,DEGIRO Transactiekosten en/of kosten van derden,,EUR,-1.00,EUR,23.83,67e39ca1-2f10-4f82-8365-1baad98c398f
11-07-2023,09:33,11-07-2023,VANECK GLOBAL REAL ESTATE UCITS ETF,NL0009690239,"Koop 29 @ 34,375 EUR",,EUR,-996.88,EUR,24.83,67e39ca1-2f10-4f82-8365-1baad98c398f
11-07-2023,08:51,11-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,1000.00,EUR,1021.70,
10-07-2023,17:34,10-07-2023,,,"Overboeking van uw geldrekening bij flatexDEGIRO Bank 78,3 EUR",,,,EUR,21.70,
10-07-2023,17:34,10-07-2023,FLATEX EURO BANKACCOUNT,NLFLATEXACNT,Degiro Cash Sweep Transfer,,EUR,78.30,EUR,100.00,
07-07-2023,05:01,06-07-2023,,,iDEAL Deposit,,EUR,100.00,EUR,21.70,
07-07-2023,05:01,06-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,-100.00,EUR,-78.30,
06-07-2023,09:39,06-07-2023,VANGUARD S&P500,IE00B3XXRP09,DEGIRO Transactiekosten en/of kosten van derden,,EUR,-1.00,EUR,21.70,b7ab0494-1b46-4e2f-9bd2-f79e6c87cb5b
06-07-2023,09:39,06-07-2023,VANGUARD S&P500,IE00B3XXRP09,"Koop 1 @ 77,3 EUR",,EUR,-77.30,EUR,22.70,b7ab0494-1b46-4e2f-9bd2-f79e6c87cb5b
06-07-2023,09:37,06-07-2023,,,Reservation iDEAL / Sofort Deposit,,EUR,100.00,EUR,100.00,
02-07-2023,16:41,30-06-2023,,,Flatex Interest Income,,EUR,0.00,EUR,0.00,
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Datum,Tijd,Valutadatum,Product,ISIN,Omschrijving,FX,Mutatie,,Saldo,,Order Id
14-09-2023,06:32,13-09-2023,VANECK GLOBAL REAL ESTATE UCITS ETF,NL0009690239,Dividend,,EUR,9.57,EUR,33.96,
14-09-2023,06:32,13-09-2023,VANECK GLOBAL REAL ESTATE UCITS ETF,NL0009690239,Dividendbelasting,,EUR,-1.44,EUR,24.39,
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<None Update="FileImporter\TestFiles\DeGiro\Example1\TestFileSingleOrder.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FileImporter\TestFiles\DeGiro\Example4\TestFileDividend.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FileImporter\TestFiles\Generic\Example1\Example1.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
45 changes: 37 additions & 8 deletions GhostfolioSidekick/FileImporter/DeGiro/DeGiroParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ protected override async Task<IEnumerable<Order>> ConvertOrders(DeGiroRecord rec
var asset = await api.FindSymbolByISIN(record.ISIN);
var fee = GetFee(record, allRecords);

if (string.IsNullOrWhiteSpace(record.OrderId))
{
record.OrderId = $"{orderType}_{record.Datum}_{record.Tijd}_{record.ISIN}";
}

var order = new Order
{
AccountId = account.Id,
Expand All @@ -31,9 +36,9 @@ protected override async Task<IEnumerable<Order>> ConvertOrders(DeGiroRecord rec
Comment = $"Transaction Reference: [{record.OrderId}]",
Fee = Math.Abs(fee?.Item1 ?? 0),
FeeCurrency = fee?.Item2 ?? record.Mutatie,
Quantity = GetQuantity(record),
Quantity = GetQuantity(orderType, record),
Type = orderType.Value,
UnitPrice = GetUnitPrice(record),
UnitPrice = GetUnitPrice(orderType, record),
ReferenceCode = record.OrderId,
};

Expand All @@ -47,18 +52,27 @@ protected override CsvConfiguration GetConfig()
HasHeaderRecord = true,
CacheFields = true,
Delimiter = ",",

};
}

private Tuple<decimal?, string>? GetFee(DeGiroRecord record, IEnumerable<DeGiroRecord> allRecords)
{
var feeRecord = allRecords.SingleOrDefault(x => x.OrderId == record.OrderId && x != record);
if (feeRecord == null)
// Costs of stocks
var feeRecord = allRecords.SingleOrDefault(x => !string.IsNullOrWhiteSpace(x.OrderId) && x.OrderId == record.OrderId && x != record);
if (feeRecord != null)
{
return null;
return Tuple.Create(feeRecord.Total, feeRecord.Mutatie);
}

return Tuple.Create(feeRecord.Total, feeRecord.Mutatie);
// Taxes of dividends
feeRecord = allRecords.SingleOrDefault(x => x.Datum == record.Datum && x.ISIN == record.ISIN && x.Omschrijving == "Dividendbelasting");
if (feeRecord != null)
{
return Tuple.Create(feeRecord.Total, feeRecord.Mutatie);
}

return null;
}

private OrderType? GetOrderType(DeGiroRecord record)
Expand All @@ -68,18 +82,33 @@ protected override CsvConfiguration GetConfig()
return OrderType.BUY;
}

if (record.Omschrijving.Equals("Dividend"))
{
return OrderType.DIVIDEND;
}

// TODO, implement other options
return null;
}

private decimal GetQuantity(DeGiroRecord record)
private decimal GetQuantity(OrderType? orderType, DeGiroRecord record)
{
if (orderType == OrderType.DIVIDEND)
{
return 1;
}

var quantity = Regex.Match(record.Omschrijving, $"Koop (?<amount>\\d+) @ (?<price>.*) EUR").Groups[1].Value;
return decimal.Parse(quantity, GetCultureForParsingNumbers());
}

private decimal GetUnitPrice(DeGiroRecord record)
private decimal GetUnitPrice(OrderType? orderType, DeGiroRecord record)
{
if (orderType == OrderType.DIVIDEND)
{
return record.Total.GetValueOrDefault();
}

var quantity = Regex.Match(record.Omschrijving, $"Koop (?<amount>\\d+) @ (?<price>.*) EUR").Groups[2].Value;
return decimal.Parse(quantity, GetCultureForParsingNumbers());
}
Expand Down
3 changes: 1 addition & 2 deletions GhostfolioSidekick/FileImporter/DeGiro/DeGiroRecord.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CsvHelper.Configuration.Attributes;
using System.Globalization;

namespace GhostfolioSidekick.FileImporter.DeGiro
{
Expand All @@ -25,7 +24,7 @@ public class DeGiroRecord
public string Mutatie { get; set; }

[Index(8)]
[CultureInfo("nl-NL")]
//[CultureInfo("en-EN")]
public decimal? Total { get; set; }

public string Saldo { get; set; }
Expand Down

0 comments on commit 0958eab

Please sign in to comment.