|
|
@ -12,11 +12,12 @@ import { |
|
|
import { |
|
|
import { |
|
|
DATE_FORMAT, |
|
|
DATE_FORMAT, |
|
|
getAssetProfileIdentifier, |
|
|
getAssetProfileIdentifier, |
|
|
getYesterday, |
|
|
getStartOfUtcDateOfYesterday, |
|
|
resetHours |
|
|
resetHours |
|
|
} from '@ghostfolio/common/helper'; |
|
|
} from '@ghostfolio/common/helper'; |
|
|
import { DataProviderHistoricalResponse } from '@ghostfolio/common/interfaces'; |
|
|
import { DataProviderHistoricalResponse } from '@ghostfolio/common/interfaces'; |
|
|
|
|
|
|
|
|
|
|
|
import { utc } from '@date-fns/utc'; |
|
|
import { Injectable, Logger } from '@nestjs/common'; |
|
|
import { Injectable, Logger } from '@nestjs/common'; |
|
|
import { |
|
|
import { |
|
|
eachDayOfInterval, |
|
|
eachDayOfInterval, |
|
|
@ -164,11 +165,19 @@ export class ExchangeRateDataService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async loadCurrencies() { |
|
|
public async loadCurrencies() { |
|
|
|
|
|
const startOfUtcDateOfYesterday = getStartOfUtcDateOfYesterday(); |
|
|
|
|
|
|
|
|
|
|
|
const dateStringOfYesterday = format( |
|
|
|
|
|
startOfUtcDateOfYesterday, |
|
|
|
|
|
DATE_FORMAT, |
|
|
|
|
|
{ in: utc } |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
const historicalData = await this.dataProviderService.getHistorical( |
|
|
const historicalData = await this.dataProviderService.getHistorical( |
|
|
this.currencyPairs, |
|
|
this.currencyPairs, |
|
|
'day', |
|
|
'day', |
|
|
getYesterday(), |
|
|
startOfUtcDateOfYesterday, |
|
|
getYesterday() |
|
|
startOfUtcDateOfYesterday |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
const quotes = await this.dataProviderService.getQuotes({ |
|
|
const quotes = await this.dataProviderService.getQuotes({ |
|
|
@ -196,7 +205,7 @@ export class ExchangeRateDataService { |
|
|
|
|
|
|
|
|
if (isNumber(quote?.marketPrice)) { |
|
|
if (isNumber(quote?.marketPrice)) { |
|
|
result[symbol] = { |
|
|
result[symbol] = { |
|
|
[format(getYesterday(), DATE_FORMAT)]: { |
|
|
[dateStringOfYesterday]: { |
|
|
marketPrice: quote.marketPrice |
|
|
marketPrice: quote.marketPrice |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
@ -219,17 +228,19 @@ export class ExchangeRateDataService { |
|
|
|
|
|
|
|
|
for (const symbol of Object.keys(resultExtended)) { |
|
|
for (const symbol of Object.keys(resultExtended)) { |
|
|
const [currency1, currency2] = symbol.match(/.{1,3}/g); |
|
|
const [currency1, currency2] = symbol.match(/.{1,3}/g); |
|
|
const date = format(getYesterday(), DATE_FORMAT); |
|
|
|
|
|
|
|
|
|
|
|
this.exchangeRates[symbol] = resultExtended[symbol]?.[date]?.marketPrice; |
|
|
this.exchangeRates[symbol] = |
|
|
|
|
|
resultExtended[symbol]?.[dateStringOfYesterday]?.marketPrice; |
|
|
|
|
|
|
|
|
if (!this.exchangeRates[symbol]) { |
|
|
if (!this.exchangeRates[symbol]) { |
|
|
// Not found, calculate indirectly via base currency
|
|
|
// Not found, calculate indirectly via base currency
|
|
|
this.exchangeRates[symbol] = |
|
|
this.exchangeRates[symbol] = |
|
|
resultExtended[`${currency1}${DEFAULT_CURRENCY}`]?.[date] |
|
|
resultExtended[`${currency1}${DEFAULT_CURRENCY}`]?.[ |
|
|
?.marketPrice * |
|
|
dateStringOfYesterday |
|
|
resultExtended[`${DEFAULT_CURRENCY}${currency2}`]?.[date] |
|
|
]?.marketPrice * |
|
|
?.marketPrice; |
|
|
resultExtended[`${DEFAULT_CURRENCY}${currency2}`]?.[ |
|
|
|
|
|
dateStringOfYesterday |
|
|
|
|
|
]?.marketPrice; |
|
|
|
|
|
|
|
|
// Calculate the opposite direction
|
|
|
// Calculate the opposite direction
|
|
|
this.exchangeRates[`${currency2}${currency1}`] = |
|
|
this.exchangeRates[`${currency2}${currency1}`] = |
|
|
|