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