Browse Source

Extend EodHistoricalDataService

* asset and asset sub class
* symbol mapping (ISIN)
pull/1791/head
Thomas 3 years ago
parent
commit
650d51f4a7
  1. 12
      apps/api/src/services/data-gathering.service.ts
  2. 93
      apps/api/src/services/data-provider/eod-historical-data/eod-historical-data.service.ts

12
apps/api/src/services/data-gathering.service.ts

@ -127,9 +127,11 @@ export class DataGatheringService {
); );
for (const [symbol, assetProfile] of Object.entries(assetProfiles)) { for (const [symbol, assetProfile] of Object.entries(assetProfiles)) {
const symbolMapping = symbolProfiles.find((symbolProfile) => { const symbolMapping =
return symbolProfile.symbol === symbol; assetProfile.symbolMapping ??
})?.symbolMapping; symbolProfiles.find((symbolProfile) => {
return symbolProfile.symbol === symbol;
})?.symbolMapping;
for (const dataEnhancer of this.dataEnhancers) { for (const dataEnhancer of this.dataEnhancers) {
try { try {
@ -155,7 +157,7 @@ export class DataGatheringService {
name, name,
sectors, sectors,
url url
} = assetProfiles[symbol]; } = assetProfile;
try { try {
await this.prismaService.symbolProfile.upsert({ await this.prismaService.symbolProfile.upsert({
@ -168,6 +170,7 @@ export class DataGatheringService {
name, name,
sectors, sectors,
symbol, symbol,
symbolMapping,
url url
}, },
update: { update: {
@ -177,6 +180,7 @@ export class DataGatheringService {
currency, currency,
name, name,
sectors, sectors,
symbolMapping,
url url
}, },
where: { where: {

93
apps/api/src/services/data-provider/eod-historical-data/eod-historical-data.service.ts

@ -8,7 +8,12 @@ import {
import { DATE_FORMAT } from '@ghostfolio/common/helper'; import { DATE_FORMAT } from '@ghostfolio/common/helper';
import { Granularity } from '@ghostfolio/common/types'; import { Granularity } from '@ghostfolio/common/types';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { DataSource, SymbolProfile } from '@prisma/client'; import {
AssetClass,
AssetSubClass,
DataSource,
SymbolProfile
} from '@prisma/client';
import bent from 'bent'; import bent from 'bent';
import { format, isToday } from 'date-fns'; import { format, isToday } from 'date-fns';
@ -30,12 +35,21 @@ export class EodHistoricalDataService implements DataProviderInterface {
public async getAssetProfile( public async getAssetProfile(
aSymbol: string aSymbol: string
): Promise<Partial<SymbolProfile>> { ): Promise<Partial<SymbolProfile>> {
const { items } = await this.search(aSymbol); const [searchResult] = await this.getSearchResult(aSymbol);
const symbolMapping = searchResult?.ISIN
? {
ISIN: searchResult.ISIN
}
: undefined;
return { return {
currency: items[0]?.currency, symbolMapping,
assetClass: searchResult?.assetClass,
assetSubClass: searchResult?.assetSubClass,
currency: searchResult?.currency,
dataSource: this.getName(), dataSource: this.getName(),
name: items[0]?.name name: searchResult?.name
}; };
} }
@ -156,7 +170,27 @@ export class EodHistoricalDataService implements DataProviderInterface {
} }
public async search(aQuery: string): Promise<{ items: LookupItem[] }> { public async search(aQuery: string): Promise<{ items: LookupItem[] }> {
let items: LookupItem[] = []; const searchResult = await this.getSearchResult(aQuery);
return {
items: searchResult
.filter(({ symbol }) => {
return !symbol.toLowerCase().endsWith('forex');
})
.map(({ currency, dataSource, name, symbol }) => {
return { currency, dataSource, name, symbol };
})
};
}
private async getSearchResult(aQuery: string): Promise<
(LookupItem & {
assetClass: AssetClass;
assetSubClass: AssetSubClass;
ISIN: string;
})[]
> {
let searchResult = [];
try { try {
const get = bent( const get = bent(
@ -167,10 +201,18 @@ export class EodHistoricalDataService implements DataProviderInterface {
); );
const response = await get(); const response = await get();
items = response.map( searchResult = response.map(
({ Code, Currency: currency, Exchange, Name: name }) => { ({ Code, Currency: currency, Exchange, ISIN, Name: name, Type }) => {
const { assetClass, assetSubClass } = this.parseAssetClass({
Exchange,
Type
});
return { return {
assetClass,
assetSubClass,
currency, currency,
ISIN,
name, name,
dataSource: this.getName(), dataSource: this.getName(),
symbol: `${Code}.${Exchange}` symbol: `${Code}.${Exchange}`
@ -181,6 +223,41 @@ export class EodHistoricalDataService implements DataProviderInterface {
Logger.error(error, 'EodHistoricalDataService'); Logger.error(error, 'EodHistoricalDataService');
} }
return { items }; return searchResult;
}
private parseAssetClass({
Exchange,
Type
}: {
Exchange: string;
Type: string;
}): {
assetClass: AssetClass;
assetSubClass: AssetSubClass;
} {
let assetClass: AssetClass;
let assetSubClass: AssetSubClass;
switch (Type?.toLowerCase()) {
case 'common stock':
assetClass = AssetClass.EQUITY;
assetSubClass = AssetSubClass.STOCK;
break;
case 'currency':
assetClass = AssetClass.CASH;
if (Exchange?.toLowerCase() === 'cc') {
assetSubClass = AssetSubClass.CRYPTOCURRENCY;
}
break;
case 'etf':
assetClass = AssetClass.EQUITY;
assetSubClass = AssetSubClass.ETF;
break;
}
return { assetClass, assetSubClass };
} }
} }

Loading…
Cancel
Save