Skip to content

Commit

Permalink
Dividends details bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rylorin committed Apr 21, 2024
1 parent 629fb5e commit d0d43ff
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/app/components/Portfolio/Report/DividendsDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DividendsDetails = ({ theReports, ..._rest }: Props): React.ReactNode => {
id: statement.id,
date: new Date(statement.date),
country: statement.country,
amount: statement.amount > 0 ? statement.amount * statement.fxRateToBase : 0,
amount: statement.amount * statement.fxRateToBase,
tax: 0,
description: statement.description,
};
Expand Down
5 changes: 3 additions & 2 deletions src/models/bond_statement.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export class BondStatement extends Model<
@Column({ type: DataType.STRING(2), field: "country" })
declare country: string;

@Column({ type: DataType.FLOAT, field: "accruedInt" })
declare accruedInterests: number;
// accruedInterests are part of PnL
// @Column({ type: DataType.FLOAT, field: "accruedInt" })
// declare accruedInterests: number;

/** proceeds */
@Column({ type: DataType.FLOAT(8, 2), defaultValue: 0 })
Expand Down
5 changes: 3 additions & 2 deletions src/routers/statements.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ const makeSynthesys = (statements: Statement[]): Promise<StatementsSynthesysEntr
return Promise.resolve(value);
case StatementTypes.BondStatement:
return BondStatement.findByPk(item.id).then((statement) => {
value[idx].interests += statement ? statement.accruedInterests * item.fxRateToBase : 0;
value[idx].total += statement ? statement.accruedInterests * item.fxRateToBase : 0;
// value[idx].interests += statement ? statement.accruedInterests * item.fxRateToBase : 0;
// value[idx].total += statement ? statement.accruedInterests * item.fxRateToBase : 0;
value[idx].total += statement ? statement.realizedPnL * item.fxRateToBase : 0;
return value;
});
case StatementTypes.TaxStatement:
Expand Down
2 changes: 1 addition & 1 deletion src/routers/statements.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type BondStatementEntry = BaseStatement & {
statementType: "Bond";
country: string;
underlying: StatementUnderlyingEntry | undefined;
accruedInterests: number;
// accruedInterests: number;
quantity: number;
pnl: number;
fees: number;
Expand Down
64 changes: 34 additions & 30 deletions src/routers/statements.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const statementModelToStatementEntry = (item: Statement): Promise<Stateme
...baseStatement,
country,
underlying: item.stock,
accruedInterests: thisStatement.accruedInterests,
// accruedInterests: thisStatement.accruedInterests,
quantity: thisStatement.quantity,
pnl: thisStatement.realizedPnL,
fees: thisStatement.fees,
Expand Down Expand Up @@ -193,27 +193,31 @@ export const prepareReport = (portfolio: Portfolio): Promise<ReportEntry[]> => {
}
switch (statement.statementType) {
case StatementTypes.DividendStatement:
dividendEntry = report.dividendsSummary.find((item) => item.country == statement.country);
if (!dividendEntry) {
dividendEntry = { country: statement.country, grossAmountInBase: 0, taxes: 0, netAmountInBase: 0 };
report.dividendsSummary.push(dividendEntry);
}
dividendEntry.grossAmountInBase += statement.amount * statement.fxRateToBase;
dividendEntry.netAmountInBase += statement.amount * statement.fxRateToBase;
report.dividendsDetails.push(statement);
break;

case StatementTypes.TaxStatement:
dividendEntry = report.dividendsSummary.find((item) => item.country == statement.country);
if (!dividendEntry) {
dividendEntry = { country: statement.country, grossAmountInBase: 0, taxes: 0, netAmountInBase: 0 };
report.dividendsSummary.push(dividendEntry);
}
dividendEntry.taxes += statement.amount * statement.fxRateToBase;
if (statement.statementType == StatementTypes.DividendStatement)
dividendEntry.grossAmountInBase += statement.amount * statement.fxRateToBase;
else if (statement.statementType == StatementTypes.TaxStatement)
dividendEntry.taxes += statement.amount * statement.fxRateToBase;
dividendEntry.netAmountInBase += statement.amount * statement.fxRateToBase;
report.dividendsDetails.push(statement);
break;

// case StatementTypes.TaxStatement:
// dividendEntry = report.dividendsSummary.find((item) => item.country == statement.country);
// if (!dividendEntry) {
// dividendEntry = { country: statement.country, grossAmountInBase: 0, taxes: 0, netAmountInBase: 0 };
// report.dividendsSummary.push(dividendEntry);
// }
// dividendEntry.taxes += statement.amount * statement.fxRateToBase;
// dividendEntry.netAmountInBase += statement.amount * statement.fxRateToBase;
// report.dividendsDetails.push(statement);
// break;

case StatementTypes.InterestStatement:
interestEntry = report.interestsSummary.find(
(item) => item.country == (statement.country || portfolio.country),
Expand Down Expand Up @@ -270,24 +274,24 @@ export const prepareReport = (portfolio: Portfolio): Promise<ReportEntry[]> => {

case StatementTypes.BondStatement:
// interest part
interestEntry = report.interestsSummary.find(
(item) => item.country == (statement.country || portfolio.country),
);
if (!interestEntry) {
interestEntry = {
country: statement.country || portfolio.country,
grossCredit: 0,
netDebit: 0,
withHolding: 0,
netTotal: 0,
};
report.interestsSummary.push(interestEntry);
}
if (statement.accruedInterests > 0)
interestEntry.grossCredit += statement.accruedInterests * statement.fxRateToBase;
else interestEntry.netDebit += statement.accruedInterests * statement.fxRateToBase;
interestEntry.netTotal += statement.accruedInterests * statement.fxRateToBase;
report.interestsDetails.push(statement);
// interestEntry = report.interestsSummary.find(
// (item) => item.country == (statement.country || portfolio.country),
// );
// if (!interestEntry) {
// interestEntry = {
// country: statement.country || portfolio.country,
// grossCredit: 0,
// netDebit: 0,
// withHolding: 0,
// netTotal: 0,
// };
// report.interestsSummary.push(interestEntry);
// }
// if (statement.accruedInterests > 0)
// interestEntry.grossCredit += statement.accruedInterests * statement.fxRateToBase;
// else interestEntry.netDebit += statement.accruedInterests * statement.fxRateToBase;
// interestEntry.netTotal += statement.accruedInterests * statement.fxRateToBase;
// report.interestsDetails.push(statement);
// PnL part
report.tradesSummary.bondPnLInBase += statement.pnl * statement.fxRateToBase;
report.tradesSummary.totalPnL += statement.pnl * statement.fxRateToBase;
Expand Down
4 changes: 2 additions & 2 deletions src/routers/trades.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { OptionType, SecType } from "@stoqey/ib";
import express from "express";
import { Op } from "sequelize";
import logger, { LogLevel } from "../logger";
import { Contract, Currency, Portfolio, Position, Statement, StatementTypes, Trade } from "../models";
import { Contract, Currency, Portfolio, Position, Statement, Trade } from "../models";
import { expirationToDate } from "../models/date_utils";
import { ContractType, TradeStatus, TradeStrategy } from "../models/types";
import { ContractType, StatementTypes, TradeStatus, TradeStrategy } from "../models/types";
import { preparePositions } from "./positions.router";
import { prepareStatements } from "./statements.utils";
import {
Expand Down

0 comments on commit d0d43ff

Please sign in to comment.