Feature/render average prices in position detail chart (#144)

* Render average buy prices

* Update changelog
pull/145/head
Thomas 4 years ago committed by GitHub
parent 026a5011d4
commit ef272360fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Added a dedicated page for the account registration - Added a dedicated page for the account registration
- Rendered the average buy prices in the position detail chart (useful for recurring transactions)
### Changed ### Changed

@ -20,6 +20,7 @@ import {
getYear, getYear,
isAfter, isAfter,
isSameDay, isSameDay,
parse,
parseISO, parseISO,
setDate, setDate,
setMonth, setMonth,
@ -215,6 +216,8 @@ export class PortfolioService {
transactionCount transactionCount
} = portfolio.getPositions(new Date())[aSymbol]; } = portfolio.getPositions(new Date())[aSymbol];
const orders = portfolio.getOrders(aSymbol);
const historicalData = await this.dataProviderService.getHistorical( const historicalData = await this.dataProviderService.getHistorical(
[aSymbol], [aSymbol],
'day', 'day',
@ -227,6 +230,7 @@ export class PortfolioService {
} }
const historicalDataArray: HistoricalDataItem[] = []; const historicalDataArray: HistoricalDataItem[] = [];
let currentAveragePrice: number;
let maxPrice = marketPrice; let maxPrice = marketPrice;
let minPrice = marketPrice; let minPrice = marketPrice;
@ -234,9 +238,24 @@ export class PortfolioService {
for (const [date, { marketPrice }] of Object.entries( for (const [date, { marketPrice }] of Object.entries(
historicalData[aSymbol] historicalData[aSymbol]
)) { )) {
const currentDate = parse(date, 'yyyy-MM-dd', new Date());
if (
isSameDay(currentDate, parseISO(orders[0]?.getDate())) ||
isAfter(currentDate, parseISO(orders[0]?.getDate()))
) {
// Get snapshot of first day of month
const snapshot = portfolio.get(setDate(currentDate, 1))[0]
.positions[aSymbol];
orders.shift();
if (snapshot?.averagePrice) {
currentAveragePrice = snapshot?.averagePrice;
}
}
historicalDataArray.push({ historicalDataArray.push({
averagePrice,
date, date,
averagePrice: currentAveragePrice,
value: marketPrice value: marketPrice
}); });

@ -486,7 +486,13 @@ export class Portfolio implements PortfolioInterface {
.reduce((previous, current) => previous + current, 0); .reduce((previous, current) => previous + current, 0);
} }
public getOrders() { public getOrders(aSymbol?: string) {
if (aSymbol) {
return this.orders.filter((order) => {
return order.getSymbol() === aSymbol;
});
}
return this.orders; return this.orders;
} }

Loading…
Cancel
Save