-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrades.js
68 lines (58 loc) · 2.15 KB
/
trades.js
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
"use strict";
const fs = require("fs");
const dotenv = require("dotenv").config();
const ccxt = require("ccxt");
const exchangeClasses = [];
const exchanges = [];
const prefExchanges = ["binance", "bitfinex", "bitflyer", "bithumb", "bitmart", "bitmax", "bitmex", "bitstamp", "bittrex", "coinbase", "coinbasepro", "coincheck", "deribit", "ftx", "gateio", "huobipro", "indodax", "itbit", "kraken", "kucoin", "liquid", "okex", "poloniex", "upbit"];
const bitmax = new ccxt.bitmax();
const TRADE_COUNT = process.env.TRADE_COUNT;
const _ = require("lodash");
function initExchanges() {
let exchangeIDs = ccxt.exchanges;
//get exchange classes
Object.keys(exchangeIDs).forEach((key) => {
if (prefExchanges.includes(exchangeIDs[key])) {
exchangeClasses.push(ccxt[exchangeIDs[key]]);
}
});
//store instances of exchanges
Object.keys(exchangeClasses).forEach((key) => {
exchanges.push(exchangeClasses[key]);
});
}
//fetching and exporting trades to json files /exports .
async function loadTrades() {
for (let i = 0; i < exchanges.length; i++) {
try {
let exchange = new exchanges[i]({ 'enableRateLimit': true });
let markets = await exchange.loadMarkets();
let symbols = exchange.symbols;
Object.keys(symbols).forEach(async (key) => {
if (exchange.hasFetchTrades) {
let trades = await exchange.fetchTrades(symbols[key]); //we can not limit to a certain amount of trades as it breaks pagination on some exchanges
let slicedTrades = _.takeRight(trades, TRADE_COUNT);
let output = {
exchange: exchange.id,
symbol: key,
trades: trades,
};
let fileName = exchange.id + "-" + markets[symbols[key]].base + markets[symbols[key]].quote + "-trades.json";
let data = JSON.stringify(output, null, 4);
fs.writeFileSync("exports/trades/" + fileName, data);
} else {
console.log(exchange.id + " does not support fetching trades");
}
});
} catch (err) {
console.log(err);
}
}
}
//application start function
function run() {
initExchanges();
loadTrades();
}
//app entry point
run();