-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtickersaver.js
More file actions
58 lines (55 loc) · 1.89 KB
/
tickersaver.js
File metadata and controls
58 lines (55 loc) · 1.89 KB
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
const autobahn = require('autobahn');
const mongoose = require('mongoose');
const Tick = mongoose.model('Tick');
const gdax = require('gdax');
let poloniexconnection = new autobahn.Connection({
url: 'wss://api.poloniex.com',
realm: "realm1"
});
exports.start = () => {
poloniexconnection.onopen = (session) => {
function tickerEvent (args, kwargs) {
const tick = new Tick({
currencyPair: args[0],
last: args[1],
lowestAsk: args[2],
highestBid: args[3],
baseVolume: args[5],
quoteVolume: args[6],
platform: 'poloniex'
});
tick.save((err, tick) => {
if(err) console.log('Error saving tick from poloniex');
console.log(err);
});
}
session.subscribe('ticker', tickerEvent);
console.log('Poloniex websocket connection started');
};
poloniexconnection.onclose = () => {
console.log('Poloniex websocket connection closed');
console.log('Trying to reopen the poloniex connection');
poloniexconnection.open();
};
poloniexconnection.open();
let gdaxconnection = new gdax.WebsocketClient(['BTC-EUR']);
gdaxconnection.on('open', (data) => {
console.log('Gdax websocket connection started');
});
gdaxconnection.on('message', (data) => {
if(data.type === 'match'){
const tick = new Tick({
currencyPair: data.product_id.replace('-', '_'),
last: data.price,
platform: 'gdax'
});
tick.save((err, tick) => {
if(err) console.log('Error saving tick from gdax');
console.log(err);
});
}
});
gdaxconnection.on('close', (data) => {
console.log('Gdax websocket connection closed');
});
}