Skip to content

Instantly share code, notes, and snippets.

@Anderson-Juhasc
Last active March 9, 2016 15:28
Show Gist options
  • Save Anderson-Juhasc/2b62efde04df84cde6a0 to your computer and use it in GitHub Desktop.
Save Anderson-Juhasc/2b62efde04df84cde6a0 to your computer and use it in GitHub Desktop.
// constructor
function Bot(api_key, secret) {
var self = this;
self.ticker = null;
if (channels) {
self.subscribe(channels);
}
}
Bot.prototype.subscribe = function(channels) {
var self = this;
var socketURL = 'wss://real.okcoin.cn:10440/websocket/okcoinapi';
var ws = new WebSocket.Client(socketURL);
self.ws = ws;
ws.on('open', function() {
console.log('websocket connected to: ' + socketURL);
var connected = new self.log({
message: 'websocket connected to: ' + socketURL,
created: Date.now()
});
// Saving it to the database.
connected.save(function (err) {
if (err) {
console.log ('Error on save!');
}
});
var data = [];
Object.keys(channels).forEach(function(name) {
switch (name) {
case 'ok_spotusd_trade':
case 'ok_spotusd_cancel_order':
case 'ok_spotcny_trade':
case 'ok_spotcny_cancel_order':
break;
case 'ok_spotusd_userinfo':
case 'ok_spotcny_userinfo':
self.userInfo();
break;
case 'ok_usd_realtrades':
case 'ok_cny_realtrades':
case 'ok_usd_future_realtrades':
self.send(name);
break;
default:
data.push({'event': 'addChannel','channel': name});
}
});
ws.send(JSON.stringify(data));
});
ws.on('message', function(data) {
var messages = JSON.parse(data.data)
, channel = messages[0].channel
, data = messages[0].data;
messages.forEach(function(message) {
var callback = channels[message['channel']];
if (!callback) return;
if (message['errorcode']) {
message['errormessage'] = errorMessage(message['errorcode']);
callback(null, message);
} else if (message['data']) {
callback(message['data']);
}
});
if (channel === 'ok_spotusd_userinfo' || channel === 'ok_spotcny_userinfo') {
if (data !== undefined) {
self.walletFiat = self.coin === 'usd'
? parseFloat(data.info.funds.free.usd)
: parseFloat(data.info.funds.free.cny);
self.walletBTC = parseFloat(data.info.funds.free.btc);
if (self.buying === false) {
self.buying = true;
}
if (self.lot_value === null) {
if (self.walletBTC > 0.01) {
self.lot_value = self.walletBTC.toFixed(8);
self.lot3 = self.lot_value;
}
if ((self.walletBTC / 2) > 0.01) {
self.lot_value = (self.walletBTC / 2).toFixed(8);
self.lot3 = self.lot_value;
self.lot2 = self.lot_value;
}
if ((self.walletBTC / 3) > 0.01) {
self.lot_value = (self.walletBTC / 3).toFixed(8);
self.lot3 = self.lot_value;
self.lot2 = self.lot_value;
self.lot1 = self.lot_value;
}
}
if (self.orders !== null && self.orders !== undefined) {
self.buy();
self.sell();
}
}
}
if (channel === 'ok_btccny_ticker' || channel === 'ok_btcusd_ticker') {
self.ticker = data;
}
if (channel === 'ok_btccny_depth' || channel === 'ok_btcusd_depth') {
self.orders = data;
self.userInfo();
}
if (channel === 'ok_spotusd_trade' || channel === 'ok_spotcny_trade') {
console.log(data);
//[ { channel: 'ok_spotcny_trade',
// data: { order_id: '1005936763', result: 'true' } } ]
}
});
ws.on('close', function() {
console.log('websocket disconnected: ' + socketURL);
var disconnected = new self.log({
message: 'websocket disconnected: ' + socketURL,
created: Date.now()
});
// Saving it to the database.
disconnected.save(function (err) {
if (err) {
console.log ('Error on save!');
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment