Browse Source

Improve sector mapping

pull/8367/head
Thomas Kaul 4 weeks ago
parent
commit
c6db1a0ba4
  1. 78
      apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.spec.ts
  2. 30
      apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts

78
apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.spec.ts

@ -327,6 +327,84 @@ describe('TrackinsightDataEnhancerService', () => {
expect(response.holdings).toEqual([{ name: 'NVIDIA Corp.', weight: 0.08 }]);
});
it.each([
['Business Services', 'Industrials'],
['Consumer Cyclicals', 'Consumer Cyclical'],
['Consumer Discretionary', 'Consumer Cyclical'],
['Consumer Non-Cyclicals', 'Consumer Defensive'],
['Consumer Services', 'Consumer Cyclical'],
['Consumer Staples', 'Consumer Defensive'],
['Finance', 'Financial Services'],
['Financials', 'Financial Services'],
['Health Care', 'Healthcare'],
['Information Technology', 'Technology'],
['Materials', 'Basic Materials'],
['Non-Energy Materials', 'Basic Materials'],
['Telecommunications', 'Communication Services']
])('should map the sector %s to %s', async (name, expected) => {
const warn = jest.spyOn(Logger.prototype, 'warn').mockImplementation();
mockResponses({
holdings: Response.json({
...HOLDINGS,
sectors: { [name]: { weight: 0.5 } }
})
});
const response = await enhance();
expect(response.sectors).toEqual([{ name: expected, weight: 0.5 }]);
expect(warn).not.toHaveBeenCalled();
warn.mockRestore();
});
it('should merge the sectors with the same mapping', async () => {
mockResponses({
holdings: Response.json({
...HOLDINGS,
sectors: {
'Business Services': { weight: 0.25 },
'Consumer Cyclicals': { weight: 0.125 },
'Consumer Services': { weight: 0.125 },
Industrials: { weight: 0.5 }
}
})
});
const response = await enhance();
expect(response.sectors).toEqual([
{ name: 'Industrials', weight: 0.75 },
{ name: 'Consumer Cyclical', weight: 0.25 }
]);
});
it('should merge the sectors without a mapping', async () => {
const warn = jest.spyOn(Logger.prototype, 'warn').mockImplementation();
mockResponses({
holdings: Response.json({
...HOLDINGS,
sectors: {
'New Sector A': { weight: 0.25 },
'New Sector B': { weight: 0.25 },
Technology: { weight: 0.5 }
}
})
});
const response = await enhance();
expect(response.sectors).toEqual([
{ name: 'Other', weight: 0.5 },
{ name: 'Technology', weight: 0.5 }
]);
expect(warn).toHaveBeenCalledTimes(2);
warn.mockRestore();
});
it('should accept floating point noise in a weight', async () => {
mockResponses({
holdings: Response.json({

30
apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts

@ -29,12 +29,19 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface {
private static holdingsWeightTreshold = 0.85;
private static sectorsMapping: Record<string, SectorName> = {
'Business Services': 'Industrials',
'Consumer Cyclicals': 'Consumer Cyclical',
'Consumer Discretionary': 'Consumer Cyclical',
'Consumer Non-Cyclicals': 'Consumer Defensive',
'Consumer Services': 'Consumer Cyclical',
'Consumer Staples': 'Consumer Defensive',
Finance: 'Financial Services',
Financials: 'Financial Services',
'Health Care': 'Healthcare',
'Information Technology': 'Technology',
Materials: 'Basic Materials'
Materials: 'Basic Materials',
'Non-Energy Materials': 'Basic Materials',
Telecommunications: 'Communication Services'
};
// Allows for floating point noise in the sums of Trackinsight
@ -163,17 +170,24 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface {
!response.sectors ||
(response.sectors as unknown as Sector[]).length === 0
) {
response.sectors = [];
// Several names of Trackinsight can map to the same sector
const weightsBySector: Record<string, number> = {};
for (const [name, value] of Object.entries<any>(holdings.sectors ?? {})) {
response.sectors.push({
name: getSectorName({
name,
aliases: TrackinsightDataEnhancerService.sectorsMapping
}),
weight: value.weight
const sectorName = getSectorName({
name,
aliases: TrackinsightDataEnhancerService.sectorsMapping
});
weightsBySector[sectorName] =
(weightsBySector[sectorName] ?? 0) + value.weight;
}
response.sectors = Object.entries(weightsBySector).map(
([name, weight]) => {
return { name, weight };
}
);
}
return Promise.resolve(response);

Loading…
Cancel
Save