-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_compare_latest_data.ts
224 lines (189 loc) · 7.92 KB
/
crypto_compare_latest_data.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
namespace Put {
//Holds access keys for APIs
let pH = require('./passwordsHolder');
//To connect to Amazon Web Services DynamoDB database
let AWS = require("aws-sdk");
//Used to writing to data json file
const fs = require("fs");
//Time library that we will use to increment dates.
const moment = require('moment');
//Axios will handle HTTP requests to web service
const axios = require('axios');
//Reads keys from .env file
const dotenv = require('dotenv');
//Copy variables in file into environment variables
dotenv.config();
interface Crypto {
time: number,
high: number,
low: number,
open: number,
volumefrom: number,
volumeto: number,
close: number,
conversionType: string,
conversionSymbol: string
}
//The data structure returned in the message body by cryptoCompare
interface CryptoCompareObject {
Response: string,
Message: string,
HasWarning: boolean,
Type: number,
RateLimit: [],
Data: {
Aggregated: boolean,
TimeFrom: number,
TimeTo: number,
Data: Array<Crypto>,
}
}
//The data structure of a cryptoCompare error
interface CryptoCompareError {
code: number,
type: string,
info: string,
}
class SageMakerData {
start: string;
target: Array<number>;
}
interface DynamoDBItem {
PriceTimeStamp: number,//Current time in milliseconds
Currency: string,
Price: number
}
const currencies = ["SOL", "LINK", "LUNA", "ATOM", "DOT"];
const numberOfPricesToGET = getHourSince00(); //gets the number of hours since 12:00 yesterday for the number of updates.
let dynamoDBBatch: Array<DynamoDBItem> = [];
//Class that wraps cryptoCompare web service
export class cryptoCompare {
//Base URL of CryptoCompare
baseURL: string = "https://min-api.cryptocompare.com/data/v2/histohour";
accessKey = pH.apiKeys.cryptoCompareAccessKey;
//Returns a Promise that will get the exchange rates for the specified date
getExchangeRates(currency): Promise<object> {
//Build URL for API call
let url: string = this.baseURL + "?";
url += "fsym=" + currency + "&tsym=USD&limit=" + numberOfPricesToGET;
url += "&api_key=" + this.accessKey;
//Output URL and return Promise
console.log("Building cryptoCompare Promise with URL: " + url);
return axios.get(url);
}
}
// assigns seconds and date
function convertSecondsToDateAndTime(secondsSinceEpoch) {
let date = new Date(secondsSinceEpoch*1000).toISOString().split('T');
return date[0] + " " + date[1].split('.')[0];
}
//Gets hours from 00:00 to next day 00:00
function getHourSince00() {
let timeNowInMS = new Date();
let hourNow = timeNowInMS.getHours();
return hourNow;
}
//Gets the historical data for a range of dates.
async function getHistoricalData() {
for(let index = 0; index < currencies.length; index++) {
let currency = currencies[index];
/* You should check that the start date plus the number of days is
less than the current date*/
//Create moment date, which will enable us to add days easily.
// let start = moment(startDate);
//Create instance of cryptoCompare class
let cryptoCompare1: cryptoCompare = new cryptoCompare();
//Array to hold promises
let promiseArray: Array<Promise<object>> = [];
promiseArray.push(cryptoCompare1.getExchangeRates(currency));
try {
let sageMakerTrain = new SageMakerData();
let sageMakerEndpoint = new SageMakerData();
let trainTarget: Array<number> = [];
let endpointTarget: Array<number> = [];
let trainTargetIndex = 0;
let trainLimit = Math.ceil(numberOfPricesToGET * 0.6);
let resultArray: Array<object> = await Promise.all(promiseArray);
// resultArray = promiseArray['data'];
console.log(resultArray[0]['data']);
//Output the data
//data contains the body of the web service response
let data: CryptoCompareObject = resultArray[0]['data'];
//Check that API call succeeded.
if (data.Response != "Success")
console.log("UNSUCCESSFUL REQUEST" + JSON.stringify(data.Response));
let cryptoData = data.Data.Data;
let endpointIndex = trainLimit;
let secondsSinceEpochTrain = cryptoData[trainTargetIndex].time;
let secondsSinceEpochEndpoint = cryptoData[endpointIndex].time;
let trainStart = convertSecondsToDateAndTime(secondsSinceEpochTrain);
let endpointStart = convertSecondsToDateAndTime(secondsSinceEpochEndpoint);
sageMakerTrain.start = trainStart;
sageMakerEndpoint.start = endpointStart;
cryptoData.forEach((crypto, index) => {
console.log(crypto);
if (data == undefined) {
console.log("Error: undefined" + JSON.stringify(data));
} else {
//Create date object to get date in UNIX priceTimeStamp
let date: Date = new Date();
let price: number = (crypto.open + crypto.low + crypto.high) / 3; //takes the average price for the coin
let priceTimeStamp = crypto.time;
let dynamoDBItem = {
PriceTimeStamp : priceTimeStamp,
Price : price,
Currency : currency
};
dynamoDBBatch.push(dynamoDBItem);
}
});
} catch (error) {
console.log("Error: " + JSON.stringify(error));
}
}
/* Write to DynamoDB table */
AWS.config.update({
region: "us-east-1",
endpoint: "https://dynamodb.us-east-1.amazonaws.com",
accessKeyId: pH.apiKeys.awsAccessKeyId,
secretAccessKey: pH.apiKeys.awsSecretAccessKey,
sessionToken: pH.apiKeys.awsSessionToken
});
//Create new DocumentClient
let dynamoDB = new AWS.DynamoDB({maxRetries: 13, retryDelayOptions: {base: 200}});
// let documentClient = AWS.DynamoDB.DocumentClient();
let batchNumber = 0;
let rowNumber = 25 * batchNumber;
for(batchNumber = 0; batchNumber < dynamoDBBatch.length && dynamoDBBatch[rowNumber] != undefined; batchNumber++) {
let batch = [];
for (rowNumber = 25 * batchNumber; rowNumber < ( (batchNumber + 1) * 25) && dynamoDBBatch[rowNumber] != undefined; rowNumber++) {
let row = dynamoDBBatch[rowNumber];
let item = {
PutRequest: {
Item : {
PriceTimeStamp: {N: (row.PriceTimeStamp + "")},
Currency: {S: row.Currency},
Price: {N: (row.Price + "")},
}
}
}
batch.push(item);
}
var params = {
RequestItems: {
"CryptoData": batch
}
};
//Store data in DynamoDB and handle errors
dynamoDB.batchWriteItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
}
//Call function to get historical data
getHistoricalData();
}