Browse Source

Improve handling of indices

pull/7738/head
Thomas Kaul 2 days ago
parent
commit
2e2b354937
  1. 61
      apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts

61
apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts

@ -142,9 +142,18 @@ export class FinancialModelingPrepService
.then((res) => res.json()); .then((res) => res.json());
if (!assetProfile) { if (!assetProfile) {
throw new AssetProfileDelistedError( const indexProfile = await this.getIndexProfile({
`No data found, ${symbol} (${this.getName()}) may be delisted` requestTimeout,
); symbol
});
if (!indexProfile) {
throw new AssetProfileDelistedError(
`No data found, ${symbol} (${this.getName()}) may be delisted`
);
}
return { ...response, ...indexProfile };
} }
const { assetClass, assetSubClass } = const { assetClass, assetSubClass } =
@ -615,10 +624,9 @@ export class FinancialModelingPrepService
items = result items = result
.filter(({ exchange, symbol }) => { .filter(({ exchange, symbol }) => {
if ( const isIndex = exchange === 'INDEX' || symbol.startsWith('^');
exchange === 'FOREX' ||
(includeIndices === false && symbol.startsWith('^')) if (exchange === 'FOREX' || (includeIndices === false && isIndex)) {
) {
return false; return false;
} }
@ -663,6 +671,45 @@ export class FinancialModelingPrepService
return name; return name;
} }
/**
* Financial Modeling Prep has no profile for an index, thus the currency and
* the name are taken from the search endpoint.
*/
private async getIndexProfile({
requestTimeout,
symbol
}: {
requestTimeout: number;
symbol: string;
}): Promise<Pick<SymbolProfile, 'currency' | 'name'>> {
const queryParams = new URLSearchParams({
apikey: this.apiKey,
query: symbol
});
const results = await this.fetchService
.fetch(
`${this.getUrl({ version: 'stable' })}/search-symbol?${queryParams.toString()}`,
{
signal: AbortSignal.timeout(requestTimeout)
}
)
.then((res) => res.json());
const index = results?.find(({ exchange, symbol: symbolOfResult }) => {
return exchange === 'INDEX' && symbolOfResult === symbol;
});
if (!index) {
return undefined;
}
return {
currency: index.currency,
name: this.formatName({ name: index.name })
};
}
private getUrl({ version }: { version: number | 'stable' }) { private getUrl({ version }: { version: number | 'stable' }) {
const baseUrl = 'https://financialmodelingprep.com'; const baseUrl = 'https://financialmodelingprep.com';

Loading…
Cancel
Save