167 lines
4.7 KiB
TypeScript
Raw Normal View History

import { LookupItem } from '@ghostfolio/api/app/symbol/interfaces/lookup-item.interface';
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { ghostfolioFearAndGreedIndexSymbol } from '@ghostfolio/common/config';
import { DATE_FORMAT, getToday, getYesterday } from '@ghostfolio/common/helper';
import { Granularity } from '@ghostfolio/common/types';
import { Injectable, Logger } from '@nestjs/common';
import { DataSource } from '@prisma/client';
2021-04-13 21:53:58 +02:00
import * as bent from 'bent';
import { format, subMonths, subWeeks, subYears } from 'date-fns';
import {
IDataProviderHistoricalResponse,
IDataProviderResponse,
MarketState
2021-04-13 21:53:58 +02:00
} from '../../interfaces/interfaces';
import { DataProviderInterface } from '../interfaces/data-provider.interface';
2021-04-13 21:53:58 +02:00
@Injectable()
export class RakutenRapidApiService implements DataProviderInterface {
public static FEAR_AND_GREED_INDEX_NAME = 'Fear & Greed Index';
public constructor(
private readonly configurationService: ConfigurationService,
private readonly prismaService: PrismaService
) {}
2021-04-13 21:53:58 +02:00
public canHandle(symbol: string) {
return !!this.configurationService.get('RAKUTEN_RAPID_API_KEY');
}
2021-04-13 21:53:58 +02:00
public async get(
aSymbols: string[]
): Promise<{ [symbol: string]: IDataProviderResponse }> {
if (aSymbols.length <= 0) {
return {};
}
try {
const symbol = aSymbols[0];
if (symbol === ghostfolioFearAndGreedIndexSymbol) {
2021-04-13 21:53:58 +02:00
const fgi = await this.getFearAndGreedIndex();
return {
[ghostfolioFearAndGreedIndexSymbol]: {
2021-04-13 21:53:58 +02:00
currency: undefined,
dataSource: DataSource.RAKUTEN,
2021-04-13 21:53:58 +02:00
marketPrice: fgi.now.value,
marketState: MarketState.open,
2021-04-13 21:53:58 +02:00
name: RakutenRapidApiService.FEAR_AND_GREED_INDEX_NAME
}
};
}
} catch (error) {
Logger.error(error);
2021-04-13 21:53:58 +02:00
}
return {};
}
public async getHistorical(
aSymbols: string[],
aGranularity: Granularity = 'day',
from: Date,
to: Date
): Promise<{
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse };
}> {
if (aSymbols.length <= 0) {
return {};
}
try {
const symbol = aSymbols[0];
if (symbol === ghostfolioFearAndGreedIndexSymbol) {
2021-04-13 21:53:58 +02:00
const fgi = await this.getFearAndGreedIndex();
try {
// Rebuild historical data
// TODO: can be removed after all data from the last year has been gathered
// (introduced on 27.03.2021)
2021-08-07 22:38:07 +02:00
await this.prismaService.marketData.create({
2021-04-13 21:53:58 +02:00
data: {
symbol,
dataSource: DataSource.RAKUTEN,
2021-04-13 21:53:58 +02:00
date: subWeeks(getToday(), 1),
marketPrice: fgi.oneWeekAgo.value
}
});
2021-08-07 22:38:07 +02:00
await this.prismaService.marketData.create({
2021-04-13 21:53:58 +02:00
data: {
symbol,
dataSource: DataSource.RAKUTEN,
2021-04-13 21:53:58 +02:00
date: subMonths(getToday(), 1),
marketPrice: fgi.oneMonthAgo.value
}
});
2021-08-07 22:38:07 +02:00
await this.prismaService.marketData.create({
2021-04-13 21:53:58 +02:00
data: {
symbol,
dataSource: DataSource.RAKUTEN,
2021-04-13 21:53:58 +02:00
date: subYears(getToday(), 1),
marketPrice: fgi.oneYearAgo.value
}
});
///////////////////////////////////////////////////////////////////////////
} catch {}
return {
[ghostfolioFearAndGreedIndexSymbol]: {
2021-07-28 16:11:19 +02:00
[format(getYesterday(), DATE_FORMAT)]: {
2021-04-13 21:53:58 +02:00
marketPrice: fgi.previousClose.value
}
}
};
}
} catch (error) {}
return {};
}
public getName(): DataSource {
return DataSource.RAKUTEN;
}
public async search(aSymbol: string): Promise<{ items: LookupItem[] }> {
return { items: [] };
}
2021-04-13 21:53:58 +02:00
private async getFearAndGreedIndex(): Promise<{
now: { value: number; valueText: string };
previousClose: { value: number; valueText: string };
oneWeekAgo: { value: number; valueText: string };
oneMonthAgo: { value: number; valueText: string };
oneYearAgo: { value: number; valueText: string };
}> {
try {
const get = bent(
`https://fear-and-greed-index.p.rapidapi.com/v1/fgi`,
'GET',
'json',
200,
{
useQueryString: true,
'x-rapidapi-host': 'fear-and-greed-index.p.rapidapi.com',
'x-rapidapi-key': this.configurationService.get(
'RAKUTEN_RAPID_API_KEY'
)
2021-04-13 21:53:58 +02:00
}
);
const { fgi } = await get();
return fgi;
} catch (error) {
Logger.error(error);
2021-04-13 21:53:58 +02:00
return undefined;
}
}
}