-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
153 lines (144 loc) · 4.56 KB
/
app.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
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
'use strict';
// src/app.coffee
// import binance from 'node-binance-api'
var baseUrl;
var binance;
var go;
var initOrMerge;
var periods;
binance = require('node-binance-api');
binance.options({
APIKEY: 'KdVTR61jfhhOj26FXTLDfg4CcvpVlkGV4Zj4COgh1xwdHY3Sug21o96g0MkeR8YN',
APISECRET: '5jT3Klj4TXlNunhcKOKOjyMjBLacxMKkJCdbpcw5KdcUtT0JE5MBWOvOtnZGq3N4'
});
// initialize a member of the dict as a struct or update a specific field in the
// struct
initOrMerge = function(dict, key, field, value) {
if (dict[key] == null) {
dict[key] = {};
}
dict[key][field] = value;
return dict;
};
// baseUrl for manual request
baseUrl = 'https://api.binance.com/api/';
// Period list
periods = ['1m', '3m', '5m', '15m', '30m', '1h', '1d', '3d', '1w', '1M'];
// '2h'
// '4h'
// '6h'
// '8h'
// '12h'
// Main loop
go = function() {
var dict, millis, p1s;
// Phase 1 promises
p1s = [];
// Time
millis = (new Date).getTime();
// Dictionary
dict = {};
// time: millis
p1s.push(new Promise(function(resolve, reject) {
// Instantaneous Price of Stock
return binance.prices(function(prices) {
var code, price;
for (code in prices) {
price = prices[code];
initOrMerge(dict, code, 'price', parseFloat(price));
}
// console.log 'prices()', dict
return resolve(dict);
});
}).catch(function(err) {
return console.log('prices() error', err);
}));
p1s.push(new Promise(function(resolve, reject) {
// Instantenous Bid/Ask Summary
return binance.bookTickers(function(tickers) {
var code, k, ticker, v;
for (code in tickers) {
ticker = tickers[code];
for (k in ticker) {
v = ticker[k];
ticker[k] = parseFloat(v);
}
initOrMerge(dict, code, 'ticker', ticker);
}
// console.log 'bookTickers()', dict
return resolve(dict);
});
}).catch(function(err) {
return console.log('bookTickers() error', err);
}));
return Promise.all(p1s).then(function() {
var code, obj, offset, p2s, period, results;
console.log('Phase 1', dict);
// Phase 2 promises
p2s = [];
results = [];
for (code in dict) {
obj = dict[code];
p2s.push(new Promise(function(resolve, reject) {
// Instantenous Bid/Ask List
return binance.depth(code, function(depth, symbol) {
dict[symbol].depth = depth;
// console.log 'depth()', dict
return resolve(dict);
});
}).catch(function(err) {
return console.log('depth() error', err);
}));
offset = 0;
results.push((function() {
var i, len, results1;
results1 = [];
// Seems like overkill, we
for (i = 0, len = periods.length; i < len; i++) {
period = periods[i];
results1.push(p2s.push(new Promise(function(resolve, reject) {
// stagget the calls so we don't spam it to death
setTimeout(function() {
// Historic Candle Sticks
console.log(offset, code, period);
return binance.publicRequest(baseUrl + 'v1/klines', {
symbol: code,
interval: period,
limit: 1
}, function(ticks) {
var assetVolume, buyAssetVolume, buyBaseVolume, close, closeTime, high, ignored, lastTick, low, open, time, trades, volume;
lastTick = ticks[ticks.length - 1];
[time, open, high, low, close, volume, closeTime, assetVolume, trades, buyBaseVolume, buyAssetVolume, ignored] = lastTick;
dict[code]['candlestick' + period] = {
time: time,
open: parseFloat(open),
high: parseFloat(high),
low: parseFloat(low),
close: parseFloat(close),
volume: parseFloat(volume),
closeTime: closeTime,
assetVolume: parseFloat(assetVolume),
trades: trades,
buyBaseVolume: parseFloat(buyBaseVolume),
buyAssetVolume: parseFloat(buyAssetVolume),
ignored: parseFloat(ignored)
};
console.log('candlesticks()', dict[code]);
return resolve(dict);
});
}, offset * 40);
// increment offset
return offset++;
}).catch(function(err) {
return console.log('candlesticks() error', err);
})));
}
return results1;
})());
}
return results;
});
};
// setTimeout go, 10000
go();
//# sourceMappingURL=app.js.map