Commit bf7b1e85 by ken

depth

parent 8bc20132
...@@ -14,7 +14,8 @@ const accountInfoChannel = "getaccountinfo"; ...@@ -14,7 +14,8 @@ const accountInfoChannel = "getaccountinfo";
const APIKEY = "6b07db0c-dc4f-4a90-9955-665041bdbdbf"; const APIKEY = "6b07db0c-dc4f-4a90-9955-665041bdbdbf";
const APISECRET = "7ac9bcc3-5f0c-4eaf-99c9-80cb6a45165a"; const APISECRET = "7ac9bcc3-5f0c-4eaf-99c9-80cb6a45165a";
const lastEventTimeMap = {}; const lastEventTimeMap = {};
const lastDataMap = {} const lastDataMap = {};
const lastLastDataMap = {};
let balanceByRestOk = true; let balanceByRestOk = true;
let balanceByWebsocketOk = true; let balanceByWebsocketOk = true;
let currentIpIndex = 0; let currentIpIndex = 0;
...@@ -124,23 +125,24 @@ function subscribe() { ...@@ -124,23 +125,24 @@ function subscribe() {
} }
} else { } else {
const depth = data; const depth = data;
const key = depth.channel.replace('_', '').toUpperCase() + '@zb'; // const key = depth.channel.replace('_', '').toUpperCase() + '@zb';
const symbol = depth.channel.replace('_depth', '').toUpperCase(); const symbol = depth.channel.replace('_depth', '').toUpperCase();
const lastEventTime = lastEventTimeMap[symbol] || 0; _handleDepthData(symbol, data, "websocket");
if (depth.timestamp > lastEventTime) { // const lastEventTime = lastEventTimeMap[symbol] || 0;
lastEventTimeMap[symbol] = depth.timestamp; // if (depth.timestamp > lastEventTime) {
// console.log(symbol + '@' + depth.timestamp * 1000 + ' from websocket'); // lastEventTimeMap[symbol] = depth.timestamp;
const asks = depth.asks.reverse(); // // console.log(symbol + '@' + depth.timestamp * 1000 + ' from websocket');
const bids = depth.bids; // const asks = depth.asks.reverse();
const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000}); // const bids = depth.bids;
const depthJson = JSON.stringify({asks, bids, symbol}); // const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000});
if (asks[0][0] >= bids[0][0]) { // const depthJson = JSON.stringify({asks, bids, symbol});
redisClient.set(key, depthJson, 'EX', 2); // if (asks[0][0] >= bids[0][0]) {
redisClient.publish(publishKey, publishContent); // redisClient.set(key, depthJson, 'EX', 2);
} else { // redisClient.publish(publishKey, publishContent);
console.error("买一大于卖一,数据有误") // } else {
} // console.error("买一大于卖一,数据有误")
} // }
// }
} }
}); });
} }
...@@ -171,25 +173,21 @@ function _request(method, url, callback) { ...@@ -171,25 +173,21 @@ function _request(method, url, callback) {
}); });
} }
function depthByRest(symbol,callback) { function _handleDepthData(symbol, data, source) {
const url = `http://api.zb.com/data/v1/depth?market=${symbol}&size=5`;
_request("GET", url, (err, data) => {
if (err) {
console.error(url + "出错");
} else {
const key = symbol.replace('_', '').toUpperCase() + 'DEPTH@zb'; const key = symbol.replace('_', '').toUpperCase() + 'DEPTH@zb';
symbol = symbol.toUpperCase(); symbol = symbol.toUpperCase();
const lastEventTime = lastEventTimeMap[symbol] || 0; const lastEventTime = lastEventTimeMap[symbol] || 0;
if (data.timestamp > lastEventTime || (data.timestamp === lastEventTime && JSON.stringify(data) !== lastDataMap[symbol])) { if (data.timestamp > lastEventTime || (data.timestamp === lastEventTime && JSON.stringify(data) !== lastDataMap[symbol] && JSON.stringify(data) !== lastLastDataMap[symbol])) {
if (symbol === "BTCUSDT") { if (symbol === "BTCUSDT") {
let consoleMethod = console.log; let consoleMethod = console.log;
if (data.timestamp === lastEventTime) { if (data.timestamp === lastEventTime) {
consoleMethod = console.error consoleMethod = console.warn;
} }
consoleMethod(symbol + '@' + data.timestamp * 1000 + ' from rest'); consoleMethod(symbol + '@' + data.timestamp * 1000 + ' from ' + source);
consoleMethod(JSON.stringify(data)); consoleMethod(JSON.stringify(data));
} }
lastEventTimeMap[symbol] = data.timestamp; lastEventTimeMap[symbol] = data.timestamp;
lastLastDataMap[symbol] = lastDataMap[symbol];
lastDataMap[symbol] = JSON.stringify(data); lastDataMap[symbol] = JSON.stringify(data);
const publishContent = JSON.stringify({symbol, time: data.timestamp * 1000}); const publishContent = JSON.stringify({symbol, time: data.timestamp * 1000});
const asks = data.asks.reverse(); const asks = data.asks.reverse();
...@@ -202,6 +200,15 @@ function depthByRest(symbol,callback) { ...@@ -202,6 +200,15 @@ function depthByRest(symbol,callback) {
console.error("买一大于卖一,数据有误") console.error("买一大于卖一,数据有误")
} }
} }
}
function depthByRest(symbol,callback) {
const url = `http://api.zb.com/data/v1/depth?market=${symbol}&size=5`;
_request("GET", url, (err, data) => {
if (err) {
console.error(url + "出错");
} else {
_handleDepthData(symbol, data, "rest");
} }
if (callback) { if (callback) {
callback(err, data); callback(err, data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment