dickwolff / Export-To-Ghostfolio

Convert transaction history export from your favorite broker to a format that can be imported in Ghostfolio.
https://hub.docker.com/r/dickwolff/export-to-ghostfolio
Apache License 2.0
63 stars 10 forks source link

[Degiro exporter v2] Fees are not properly matched to their transaction #41

Closed Sonlis closed 7 months ago

Sonlis commented 8 months ago

The fees paid during a transaction are not linked to said transaction.

Error

In my example, using this CSV:

Date,Heure,Date de,Produit,Code ISIN,Description,FX,Mouvements,,Solde,,ID Ordre
11-03-2024,10:39,11-03-2024,QT GROUP OYJ,FI4000198031,Frais DEGIRO de courtage et/ou de parties tierces,,EUR,-4.90,EUR,33.53,cce1bd4c-9404-49b0-b69a-43a5c307d3c5
11-03-2024,10:39,11-03-2024,QT GROUP OYJ,FI4000198031,"Achat 6 QT GROUP OYJ@79,96 EUR (FI4000198031)",,EUR,-479.76,EUR,38.43,cce1bd4c-9404-49b0-b69a-43a5c307d3c5

The fees are not added to the transaction, even though they are from the same order.

Using the degiro sample export csv, the same error arises:

15-05-2019,09:05,15-05-2019,ISHARES MSCI WOR A,IE00B4L5Y983,"Compra 6 ISHARES MSCI WOR A@49,785 EUR (IE00B4L5Y983)",,EUR,-298.71,EUR,0.64,a47e2746-bfbd-4654-bd6c-5e58e470d32f
02-01-2024,14:42,02-01-2024,ISHARES MSCI WOR A,IE00B4L5Y983,Comissões de transação DEGIRO e/ou taxas de terceiros,,EUR,-1.00,EUR,2.54,7b377a93-5695-4131-8954-5c78996fbed4
02-01-2024,14:42,02-01-2024,ISHARES MSCI WOR A,IE00B4L5Y983,"Compra 1 ISHARES MSCI WOR A@82,055 EUR (IE00B4L5Y983)",,EUR,-82.06,EUR,3.54,7b377a93-5695-4131-8954-5c78996fbed4

The commision is added to the first transaction instead of the second, while it belongs to the second as the order number suggests.

    {
      "accountId": "ed7a85f8-1c5f-4d84-b700-c0081c6c6ff2",
      "comment": "",
      "fee": 1, <- 1 fee here even though it should be 0
      "quantity": 6,
      "type": "BUY",
      "unitPrice": 49.785,
      "currency": "EUR",
      "dataSource": "YAHOO",
      "date": "2019-05-15T09:05:00+03:00",
      "symbol": "IWDA.AS"
    },
    {
      "accountId": "ed7a85f8-1c5f-4d84-b700-c0081c6c6ff2",
      "comment": "",
      "fee": 0, <- 0 fee here even though there should be 1
      "quantity": 1,
      "type": "BUY",
      "unitPrice": 82.06,
      "currency": "EUR",
      "dataSource": "YAHOO",
      "date": "2024-01-02T14:42:00+02:00",
      "symbol": "IWDA.AS"
    },

Problem within the code

The code always tries to match a transaction and fee with transaction being n, and fee n+1. However, it is not always the case, as my example export shows, and even the sample one. https://github.com/dickwolff/Export-To-Ghostfolio/blob/e4d0ee324d4dfc46250a9cfab318da1f649f0ff5/src/converters/degiroConverterV2.ts#L114

if (this.lookaheadIsSameProduct(records, record, idx) || this.isBuyOrSellRecord(record) || this.isDividendRecord(record)) {
  const combinedRecord = this.combineRecords(record, records[idx + 1], security);

Possible solution

Add also a check to n-1 or/and add a check if the order number is the same between the order and the fee.

I could work on it if needed 👍

dickwolff commented 8 months ago

Thanks for reporting this! I thought I had this covered with isBuyOrSellRecordSet(), which checks for n+1 and n-1 (ie tx+buy and buy+tx). I will add a unit test for this case and look why my current method is not working.

Sonlis commented 8 months ago

I managed to find 2 problems:

record 0: transaction A record 1: transaction B fees record 2: transaction B

So the code sees that record 0 and record 0+1 are a transaction and fees, but it does not check if they belong to the same transaction, combine them, then goes to record 2 and finds no fees afterwards.

dickwolff commented 8 months ago

I removed ag from the ignored records. I have no recollection why it is in there to be honest. Can you try the 0.7.2-beta version I just pushed?

Sonlis commented 8 months ago

Seems to work properly now 👍

dickwolff commented 8 months ago

@Sonlis I merged your change with the order id match. This is now available in 0.7.4.

Are your issues resolved by these changes?

Sonlis commented 8 months ago

Yup they are! Thanks 👍