Skip to content

Commit

Permalink
degiroConverterV3: formatFloat()
Browse files Browse the repository at this point in the history
  • Loading branch information
ikruglov committed Jan 5, 2025
1 parent 505c517 commit 5fdaf7c
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/converters/degiroConverterV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class DeGiroConverterV3 extends AbstractConverter {

// For buy/sale records, only the total amount is recorded. So the unit price needs to be calculated.
const totalAmount = record.getAmount();
unitPrice = parseFloat((Math.abs(totalAmount) / numberShares).toFixed(3));
unitPrice = Math.abs(totalAmount) / numberShares;

// If amount is negative (so money has been removed) or it's stock dividend (so free shares), thus it's a buy record.
if (totalAmount < 0 || record.description.toLocaleLowerCase().indexOf("stock dividend") > -1) {
Expand All @@ -298,9 +298,8 @@ export class DeGiroConverterV3 extends AbstractConverter {
} else {

// Otherwise, get the transaction fee info.
const amount = record.getAmount();
feeAmount = parseFloat(Math.abs(amount).toFixed(3));
orderType = amount < 0 ? GhostfolioOrderType.sell : GhostfolioOrderType.buy;
feeAmount = record.getAbsoluteAmount();
orderType = record.getAmount() < 0 ? GhostfolioOrderType.sell : GhostfolioOrderType.buy;
numberShares = 1;
unitPrice = 0;
}
Expand All @@ -311,10 +310,10 @@ export class DeGiroConverterV3 extends AbstractConverter {
return {
accountId: process.env.GHOSTFOLIO_ACCOUNT_ID,
comment: record.orderId ?? `${orderType === GhostfolioOrderType.buy ? "Buy" : "Sell"} ${record.isin} @ ${record.date}T${record.time}`,
fee: feeAmount,
fee: this.formatFloat(feeAmount),
quantity: numberShares,
type: orderType,
unitPrice: unitPrice,
unitPrice: this.formatFloat(unitPrice),
currency: security.currency ?? record.currency ?? "",
dataSource: "YAHOO",
date: date.format("YYYY-MM-DDTHH:mm:ssZ"),
Expand All @@ -328,8 +327,7 @@ export class DeGiroConverterV3 extends AbstractConverter {
const mappedTxFeeRecords = transactionFeeRecords.map(r => this.mapRecordToActivity(r, security, true));

// Extract the fee from the transaction fee record and put it in the action record.
const fee = mappedTxFeeRecords.reduce((sum, r) => sum + r.fee, 0);
mappedActionRecord.fee = parseFloat(fee.toFixed(3));
mappedActionRecord.fee = mappedTxFeeRecords.reduce((sum, r) => sum + r.fee, 0);

return mappedActionRecord;
}
Expand All @@ -343,10 +341,10 @@ export class DeGiroConverterV3 extends AbstractConverter {
return {
accountId: process.env.GHOSTFOLIO_ACCOUNT_ID,
comment: `Dividend ${dividendRecord.isin} @ ${dividendRecord.date}T${dividendRecord.time}`,
fee: feeAmount,
fee: this.formatFloat(feeAmount),
quantity: 1,
type: GhostfolioOrderType.dividend,
unitPrice: unitPrice,
unitPrice: this.formatFloat(unitPrice),
currency: security.currency ?? dividendRecord.currency,
dataSource: "YAHOO",
date: date.format("YYYY-MM-DDTHH:mm:ssZ"),
Expand All @@ -360,7 +358,7 @@ export class DeGiroConverterV3 extends AbstractConverter {
return {
accountId: process.env.GHOSTFOLIO_ACCOUNT_ID,
comment: "",
fee: feeAmount,
fee: this.formatFloat(feeAmount),
quantity: 1,
type: GhostfolioOrderType.fee,
unitPrice: 0,
Expand All @@ -380,7 +378,7 @@ export class DeGiroConverterV3 extends AbstractConverter {
fee: 0,
quantity: 1,
type: GhostfolioOrderType.interest,
unitPrice: interestAmount,
unitPrice: this.formatFloat(interestAmount),
currency: record.currency,
dataSource: "MANUAL",
date: date.format("YYYY-MM-DDTHH:mm:ssZ"),
Expand Down Expand Up @@ -455,4 +453,8 @@ export class DeGiroConverterV3 extends AbstractConverter {
md5.update(record.orderId);
return md5.digest('hex');
}

private formatFloat(val: number): number {
return parseFloat(val.toFixed(3));
}
}

0 comments on commit 5fdaf7c

Please sign in to comment.