Browse Source

Fix empty historical market data for asset profiles without activities

pull/7623/head
Thomas Kaul 2 weeks ago
parent
commit
0720bdd69b
  1. 114
      libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts

114
libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts

@ -123,75 +123,79 @@ export class GfHistoricalMarketDataEditorComponent
}
public ngOnChanges() {
if (this.dateOfFirstActivity) {
let date = this.dateOfFirstActivity;
const missingMarketData: { date: Date; marketPrice?: number }[] = [];
if (this.historicalDataItems()?.[0]?.date) {
while (
isBefore(
date,
parse(this.historicalDataItems()[0].date, DATE_FORMAT, new Date())
)
) {
missingMarketData.push({
date,
marketPrice: undefined
});
date = addDays(date, 1);
}
}
const startDate =
this.dateOfFirstActivity ?? first(this.marketData())?.date;
if (!startDate) {
return;
}
let date = startDate;
const missingMarketData: { date: Date; marketPrice?: number }[] = [];
const marketDataItems = [...missingMarketData, ...this.marketData()];
if (this.historicalDataItems()?.[0]?.date) {
while (
isBefore(
date,
parse(this.historicalDataItems()[0].date, DATE_FORMAT, new Date())
)
) {
missingMarketData.push({
date,
marketPrice: undefined
});
const lastDate = last(marketDataItems)?.date;
if (!lastDate || !isToday(lastDate)) {
marketDataItems.push({ date: new Date() });
date = addDays(date, 1);
}
}
this.marketDataByMonth = {};
const marketDataItems = [...missingMarketData, ...this.marketData()];
for (const marketDataItem of marketDataItems) {
const currentDay = parseInt(format(marketDataItem.date, 'd'), 10);
const key = format(marketDataItem.date, 'yyyy-MM');
const lastDate = last(marketDataItems)?.date;
if (!lastDate || !isToday(lastDate)) {
marketDataItems.push({ date: new Date() });
}
if (!this.marketDataByMonth[key]) {
this.marketDataByMonth[key] = {};
}
this.marketDataByMonth = {};
for (const marketDataItem of marketDataItems) {
const currentDay = parseInt(format(marketDataItem.date, 'd'), 10);
const key = format(marketDataItem.date, 'yyyy-MM');
this.marketDataByMonth[key][
currentDay < 10 ? `0${currentDay}` : currentDay
] = {
date: marketDataItem.date,
day: currentDay,
marketPrice: marketDataItem.marketPrice
};
if (!this.marketDataByMonth[key]) {
this.marketDataByMonth[key] = {};
}
// Fill up missing months
const dates = Object.keys(this.marketDataByMonth).sort();
const startDateString = first(dates);
const startDate = min([
this.dateOfFirstActivity,
...(startDateString ? [parseISO(startDateString)] : [])
]);
const endDateString = last(dates);
this.marketDataByMonth[key][
currentDay < 10 ? `0${currentDay}` : currentDay
] = {
date: marketDataItem.date,
day: currentDay,
marketPrice: marketDataItem.marketPrice
};
}
if (endDateString) {
const endDate = parseISO(endDateString);
// Fill up missing months
const dates = Object.keys(this.marketDataByMonth).sort();
const startDateString = first(dates);
const endDateString = last(dates);
let currentDate = startDate;
if (endDateString) {
const endDate = parseISO(endDateString);
while (isBefore(currentDate, endDate)) {
const key = format(currentDate, 'yyyy-MM');
if (!this.marketDataByMonth[key]) {
this.marketDataByMonth[key] = {};
}
let currentDate = min([
startDate,
...(startDateString ? [parseISO(startDateString)] : [])
]);
currentDate = addMonths(currentDate, 1);
while (isBefore(currentDate, endDate)) {
const key = format(currentDate, 'yyyy-MM');
if (!this.marketDataByMonth[key]) {
this.marketDataByMonth[key] = {};
}
currentDate = addMonths(currentDate, 1);
}
}
}

Loading…
Cancel
Save