aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/parsers/ohlcv_external/crypto_compare.ts
blob: 3efb90384bf276bce08dd7733b8c38c6904b36fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { CryptoCompareOHLCVRecord } from '../../data_sources/ohlcv_external/crypto_compare';
import { OHLCVExternal } from '../../entities';

const ONE_SECOND = 1000; // Crypto Compare uses timestamps in seconds instead of milliseconds

export interface OHLCVMetadata {
    exchange: string;
    fromSymbol: string;
    toSymbol: string;
    source: string;
    observedTimestamp: number;
    interval: number;
}
/**
 * Parses OHLCV records from Crypto Compare into an array of OHLCVExternal entities
 * @param rawRecords an array of OHLCV records from Crypto Compare (not the full response)
 */
export function parseRecords(rawRecords: CryptoCompareOHLCVRecord[], metadata: OHLCVMetadata): OHLCVExternal[] {
    return rawRecords.map(rec => {
        const ohlcvRecord = new OHLCVExternal();
        ohlcvRecord.exchange = metadata.exchange;
        ohlcvRecord.fromSymbol = metadata.fromSymbol;
        ohlcvRecord.toSymbol = metadata.toSymbol;
        ohlcvRecord.startTime = rec.time * ONE_SECOND - metadata.interval;
        ohlcvRecord.endTime = rec.time * ONE_SECOND;

        ohlcvRecord.open = rec.open;
        ohlcvRecord.close = rec.close;
        ohlcvRecord.low = rec.low;
        ohlcvRecord.high = rec.high;
        ohlcvRecord.volumeFrom = rec.volumefrom;
        ohlcvRecord.volumeTo = rec.volumeto;

        ohlcvRecord.source = metadata.source;
        ohlcvRecord.observedTimestamp = metadata.observedTimestamp;
        return ohlcvRecord;
    });
}