Commit 52a3cc3a by zihan

kucoin更改

parent ad7e5447
...@@ -2,13 +2,14 @@ const WebSocket = require('ws'); ...@@ -2,13 +2,14 @@ const WebSocket = require('ws');
const SocksProxyAgent = require('socks-proxy-agent'); const SocksProxyAgent = require('socks-proxy-agent');
const proxy = process.env.agent; const proxy = process.env.agent;
const agent = proxy ? new SocksProxyAgent(proxy) : null; const agent = proxy ? new SocksProxyAgent(proxy) : null;
const wsUrl = 'wss://push1.kucoin.com/endpoint';
const request = require('request'); const request = require('request');
const host = 'https://api.kucoin.com'; const host = 'https://api.kucoin.com';
const CryptoJS = require('crypto-js'); const CryptoJS = require('crypto-js');
const constants = require('./constants'); const constants = require('./constants');
const {IPs, mergeDepth} = require('./util'); const {IPs, mergeDepthAsk, mergeDepthBids, mergeDepth} = require('./util');
let totalOrderbook = {}; let totalOrderbook = {};
let cachedUpdate = {};
const fetchingMap = {};
let getUpdatedData = function (currentData, response) { let getUpdatedData = function (currentData, response) {
...@@ -24,7 +25,7 @@ let getUpdatedData = function (currentData, response) { ...@@ -24,7 +25,7 @@ let getUpdatedData = function (currentData, response) {
} }
else if (response.data.action === 'CANCEL') { else if (response.data.action === 'CANCEL') {
newCount -= response.data.count; newCount -= response.data.count;
if(newCount <0){ if (newCount < 0) {
newCount = 0; newCount = 0;
} }
} }
...@@ -32,7 +33,7 @@ let getUpdatedData = function (currentData, response) { ...@@ -32,7 +33,7 @@ let getUpdatedData = function (currentData, response) {
} }
} }
if (!has) { if (!has) {
if(response.data.action !== 'CANCEL') if (response.data.action !== 'CANCEL')
updateData.push([response.data.price, response.data.count]); updateData.push([response.data.price, response.data.count]);
} else { } else {
// updateData = [[response.data.price, newCount]]; // updateData = [[response.data.price, newCount]];
...@@ -46,66 +47,80 @@ class biboxApi { ...@@ -46,66 +47,80 @@ class biboxApi {
this.apiKey = '5ba054d2e0abb830dee81e1d'; this.apiKey = '5ba054d2e0abb830dee81e1d';
this.apiSecret = 'baa55ba9-7290-431e-9fd4-42aa4598887f'; this.apiSecret = 'baa55ba9-7290-431e-9fd4-42aa4598887f';
this.allowRequest = true; this.allowRequest = true;
this.index = 0;
}
_selectIp(){
const ip = IPs[this.index % IPs.length];
this.index++;
return ip;
} }
subscribeSymbols(symbols, depth, callback) { subscribeSymbols(symbols, depth, callback) {
this._publicRequest('/v1/bullet/usercenter/loginUser',{protocol:'websocket',encrypt:true},(error,result)=>{ const ip = this._selectIp();
if(error){ this._publicRequest('/api/v1/bullet-public', {}, (error, result) => {
if (error) {
console.log("fetch ws token error"); console.log("fetch ws token error");
this.subscribeSymbols(symbols, depth, callback); this.subscribeSymbols(symbols, depth, callback);
}else{ } else {
// for(let i=0;i<2;i++){ this._openWs(result, symbols, depth, ip, callback);
const ip = IPs[0];
// setTimeout(()=>{
this._openWs(result, symbols, depth,ip, callback);
// },i*10000);
// }
} }
}); }, ip, 'POST');
} }
_openWs(result, symbols, depth, ipAddress, callback) { _openWs(result, symbols, depth, ipAddress, callback) {
const token = result.data.bulletToken; const token = result.data.token;
const reqURL = `${wsUrl}?bulletToken=${token}&format=json&resource=api`; const serverInfo = result.data.instanceServers[0];
const wss = new WebSocket(reqURL, {agent,localAddress:ipAddress}); const pingInterval = serverInfo.pingInterval;
const wsUrl = serverInfo.endpoint;
const reqURL = `${wsUrl}?token=${token}&connectId=${Date.now()}`;
const wss = new WebSocket(reqURL, {agent, localAddress: ipAddress});
wss.on('open', () => { wss.on('open', () => {
console.log("websocket on open"); console.log("websocket on open");
}); });
wss.on('message', (data) => { wss.on('message', (data) => {
const response = JSON.parse(data); const response = JSON.parse(data);
if (response.type === 'ack') { if (response.type === 'welcome') {
const id = response.id; const id = response.id;
setInterval(() => { setInterval(() => {
if (wss.readyState === constants.SocketReadyStateOpen) { if (wss.readyState === constants.SocketReadyStateOpen) {
wss.send(JSON.stringify({id, type: 'ping'})) wss.send(JSON.stringify({id, type: 'ping'}))
} }
}, 40000); }, pingInterval);
for (let i=0;i<symbols.length;i++) { const topics = symbols.join(",");
const symbol = symbols[i]; wss.send(JSON.stringify({id,type:"subscribe",topic:`/market/level2:${topics}`,response:true}));
setTimeout(()=>{ } else if (response.topic && response.topic.startsWith('/market/level2')) {
this._subscribeSymbol(wss, id, symbol, depth) const symbol = response.data.symbol;
},i*10); const snapshot = totalOrderbook[symbol];
} if(!snapshot ){
} else if (response.topic && response.topic.startsWith('/trade')) { if(!fetchingMap[symbol]){
const timeStamp = response.data.time; fetchingMap[symbol] = true;
const symbol = response.topic.replace('/trade/', '').replace('_TRADE', ''); setTimeout(()=>{
let asks = totalOrderbook[symbol].asks || []; this.getOrderbook(symbol,100,ipAddress,this._handleOrderbookSnapshot.bind(this,symbol));
let bids = totalOrderbook[symbol].bids || []; },100);
if (response.data.type === 'SELL') { }
const updateData = getUpdatedData(asks, response); let array = cachedUpdate[symbol];
asks = mergeDepth(asks, updateData, true); if(!array){
} else if (response.data.type === 'BUY') { array = [];
const updateData = getUpdatedData(bids, response); cachedUpdate[symbol] = array;
bids = mergeDepth(bids, updateData, false); }
array.push(response.data);
}else{
const updatedAsks = response.data.changes.asks;
const updatedBids = response.data.changes.bids;
const mergedAsks = mergeDepth(snapshot.asks,updatedAsks,true);
const mergedBids = mergeDepth(snapshot.bids, updatedBids, false);
// if(symbol === 'BCHSV-BTC'){
// console.log(updatedBids.toString())
// console.log(snapshot.bids.toString());
// console.log(mergedBids.toString());
// console.log("====================")
// }
snapshot.asks = mergedAsks.slice(0,50);
snapshot.bids = mergedBids.slice(0,50);
callback(null,{symbol,asks:mergedAsks, bids:mergedBids, timestamp:Date.now()});
} }
totalOrderbook[symbol].bids = bids;
totalOrderbook[symbol].asks = asks;
const ret = {data: {SELL: asks, BUY: bids}, timestamp: timeStamp, symbol: symbol,seq:response.seq};
callback(null, ret);
} }
// else {
// // console.log(data);
// }
}); });
wss.on('error', (error) => { wss.on('error', (error) => {
console.log("websocket error:"); console.log("websocket error:");
...@@ -114,32 +129,60 @@ class biboxApi { ...@@ -114,32 +129,60 @@ class biboxApi {
wss.on('close', () => { wss.on('close', () => {
console.log("websocket closed"); console.log("websocket closed");
setTimeout(() => { setTimeout(() => {
// this.subscribeSymbols(symbols, depth, callback); this._openWs(result, symbols, depth, ipAddress, callback);
this._openWs(result,symbols,depth,ipAddress,callback);
}, 2000); }, 2000);
}) })
} }
_handleOrderbookSnapshot(symbol,error,data){
fetchingMap[symbol] = false;
if(error){
console.error("get snapshot error:"+symbol);
console.error(error);
return;
}
totalOrderbook[symbol] = data.data;
const lupdates = cachedUpdate[symbol];
for(const update of lupdates){
const updatedAsks = update.changes.asks;
const updatedBids = update.changes.bids;
const filteredAsks = updatedAsks.filter((item)=>{
return item[2] > data.data.sequence;
});
const filteredBids = updatedBids.filter((item)=>{
return item[2] > data.data.sequence;
})
const mergedAsks = mergeDepth(totalOrderbook[symbol].asks,filteredAsks,true);
const mergedBids = mergeDepth(totalOrderbook[symbol].bids, filteredBids, false);
totalOrderbook[symbol].asks = mergedAsks;
totalOrderbook[symbol].bids = mergedBids;
}
_subscribeSymbol(wss, id, symbol, depth){
const ipAddress = IPs[Math.round(Math.random()*(IPs.length-1))];
this.getOrderbook(symbol,depth,ipAddress,(error,result)=>{
if(error){
console.error("get symbol by rest error:");
console.error(error);
setTimeout(()=>{
this._subscribeSymbol(wss,id,symbol,depth);
},2000);
return;
}
const data = result.data;
const oldData = totalOrderbook[symbol];
if(!oldData || data.timestamp > oldData.timestamp){
totalOrderbook[symbol]={asks:data.SELL.map((item)=>[item[0],item[1]]),bids:data.BUY.map((item)=>[item[0],item[1]]),timestamp:data.timestamp};
}
wss.send(JSON.stringify({id,type:'subscribe','topic':`/trade/${symbol}_TRADE`}))
});
} }
// _subscribeSymbol(wss, id, symbol, depth) {
// const ipAddress = IPs[Math.round(Math.random() * (IPs.length - 1))];
// this.getOrderbook(symbol, depth, ipAddress, (error, result) => {
// if (error) {
// console.error("get symbol by rest error:");
// console.error(error);
// setTimeout(() => {
// this._subscribeSymbol(wss, id, symbol, depth);
// }, 2000);
// return;
// }
// const data = result.data;
// const oldData = totalOrderbook[symbol];
// if (!oldData || data.timestamp > oldData.timestamp) {
// totalOrderbook[symbol] = {
// asks: data.SELL.map((item) => [item[0], item[1]]),
// bids: data.BUY.map((item) => [item[0], item[1]]),
// timestamp: data.timestamp
// };
// }
// wss.send(JSON.stringify({id, type: 'subscribe', 'topic': `/trade/${symbol}_TRADE`}))
// });
// }
transform(obj) { transform(obj) {
var str = []; var str = [];
for (var p in obj) for (var p in obj)
...@@ -157,13 +200,13 @@ class biboxApi { ...@@ -157,13 +200,13 @@ class biboxApi {
return newObj;//返回排好序的新对象 return newObj;//返回排好序的新对象
} }
_publicRequest(path, params,callback,bindIP) { _publicRequest(path, params, callback, bindIP, method = 'GET') {
if(!this.allowRequest){ if (!this.allowRequest) {
callback({code:"-2",message:"出现超频情况,暂停提交请求"}); callback({code: "-2", message: "出现超频情况,暂停提交请求"});
return; return;
} }
let url = host + path; let url = host + path;
if (params) { if (params && method === 'GET') {
url += "?"; url += "?";
const keys = Object.keys(params); const keys = Object.keys(params);
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
...@@ -175,10 +218,10 @@ class biboxApi { ...@@ -175,10 +218,10 @@ class biboxApi {
} }
const options = { const options = {
url, url,
method: 'GET', method: method,
timeout: 8000, timeout: 8000,
forever: true, forever: true,
localAddress:bindIP, localAddress: bindIP,
}; };
request(options, (error, response, body) => { request(options, (error, response, body) => {
...@@ -187,7 +230,7 @@ class biboxApi { ...@@ -187,7 +230,7 @@ class biboxApi {
} else { } else {
try { try {
const result = JSON.parse(body); const result = JSON.parse(body);
if (result.success) { if (result.code === '200000') {
callback(null, result); callback(null, result);
} else { } else {
callback(result, null); callback(result, null);
...@@ -195,24 +238,25 @@ class biboxApi { ...@@ -195,24 +238,25 @@ class biboxApi {
} catch (e) { } catch (e) {
// console.error(e); // console.error(e);
// console.error(body); // console.error(body);
if(response.statusCode == 200){ if (response.statusCode == 200) {
console.log(body); console.log(body);
console.log(e);
} }
callback({statusCode:response.statusCode}, null); callback({statusCode: response.statusCode}, null);
} }
} }
}); });
} }
_request(method, path, params, callback) { _request(method, path, params, callback) {
if(!this.allowRequest){ if (!this.allowRequest) {
callback({code:"-2",message:"出现超频情况,暂停提交请求"}); callback({code: "-2", message: "出现超频情况,暂停提交请求"});
return; return;
} }
let url = host + path; let url = host + path;
const nonce = new Date().getTime(); const nonce = new Date().getTime();
const strParams = this.transform(this.objKeySort(params)); const strParams = this.transform(this.objKeySort(params));
const strForSign = path + '/' + nonce + '/' + strParams; const strForSign = path + '/' + nonce + '/' + strParams;
const strForSign2 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(strForSign)) const strForSign2 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(strForSign))
const sign = CryptoJS.HmacSHA256(strForSign2, this.apiSecret).toString(CryptoJS.enc.Hex); const sign = CryptoJS.HmacSHA256(strForSign2, this.apiSecret).toString(CryptoJS.enc.Hex);
const headers = { const headers = {
...@@ -223,7 +267,7 @@ class biboxApi { ...@@ -223,7 +267,7 @@ class biboxApi {
} }
const requestParams = {}; const requestParams = {};
let form = params let form = params
if (method === 'GET'){ if (method === 'GET') {
url += '?' + this.transform(params); url += '?' + this.transform(params);
form = {} form = {}
} }
...@@ -247,7 +291,7 @@ class biboxApi { ...@@ -247,7 +291,7 @@ class biboxApi {
} catch (e) { } catch (e) {
console.error("parse body时出错"); console.error("parse body时出错");
console.error("status code:" + response.statusCode); console.error("status code:" + response.statusCode);
callback({statusCode:response.statusCode}, null) callback({statusCode: response.statusCode}, null)
// if (response.statusCode === 429) { // if (response.statusCode === 429) {
// callback({statusCode: response.statusCode}) // callback({statusCode: response.statusCode})
// } else { // } else {
...@@ -260,7 +304,7 @@ class biboxApi { ...@@ -260,7 +304,7 @@ class biboxApi {
fetchSymbols(callback) { fetchSymbols(callback) {
this._publicRequest('/v1/market/open/symbols', {}, callback); this._publicRequest('/api/v1/symbols', {}, callback);
} }
order(price, amount, symbol, side, callback) { order(price, amount, symbol, side, callback) {
...@@ -279,11 +323,11 @@ class biboxApi { ...@@ -279,11 +323,11 @@ class biboxApi {
} }
balance(limit, page, callback) { balance(limit, page, callback) {
this._request("GET", "/v1/account/balances",{limit: limit || 20, page: page || 1}, callback); this._request("GET", "/v1/account/balances", {limit: limit || 20, page: page || 1}, callback);
} }
coins_info(callback) { coins_info(callback) {
this._publicRequest("/v1/market/open/coins",{}, callback); this._publicRequest("/v1/market/open/coins", {}, callback);
} }
getTrades(symbol, callback) { getTrades(symbol, callback) {
...@@ -296,7 +340,7 @@ class biboxApi { ...@@ -296,7 +340,7 @@ class biboxApi {
type: side === constants.OrderSideBuy ? 'BUY' : 'SELL', type: side === constants.OrderSideBuy ? 'BUY' : 'SELL',
orderOid: orderId orderOid: orderId
} }
this._request("GET", "/v1/order/detail", params, callback); this._request("GET", "/v1/order/detail", params, callback);
} }
cancelOrder(orderId, symbol, side, callback) { cancelOrder(orderId, symbol, side, callback) {
...@@ -308,19 +352,20 @@ class biboxApi { ...@@ -308,19 +352,20 @@ class biboxApi {
this._request("POST", "/v1/cancel-order", params, callback); this._request("POST", "/v1/cancel-order", params, callback);
} }
fetchHistorOrders(page,size,symbol,side,callback){ fetchHistorOrders(page, size, symbol, side, callback) {
const params = { const params = {
pair:symbol, pair: symbol,
account_type:0, account_type: 0,
page, page,
size, size,
order_side:side === constants.OrderSideBuy ? 1 : 2, order_side: side === constants.OrderSideBuy ? 1 : 2,
hide_cancel:0 hide_cancel: 0
}; };
this._request("POST","/v1/orderpending","orderpending/pendingHistoryList",params,callback); this._request("POST", "/v1/orderpending", "orderpending/pendingHistoryList", params, callback);
} }
getOrderbook(symbol,depth,bindIP,callback){
this._publicRequest("/v1/open/orders",{"symbol":symbol,"limit":25},callback,bindIP); getOrderbook(symbol, depth, bindIP, callback) {
this._publicRequest("/api/v1/market/orderbook/level2_100", {"symbol": symbol}, callback, bindIP);
} }
} }
......
...@@ -234,7 +234,17 @@ class BaseCollector { ...@@ -234,7 +234,17 @@ class BaseCollector {
this.currencySymbolMap[midCurrency] = [symbol]; this.currencySymbolMap[midCurrency] = [symbol];
} }
} }
this._subscribeSymbols(symbolNames,this._publishDataForStrategy3.bind(this),5);
const filteredSymbols = symbolNames.filter((item)=>{
const keySymbol = this._convertSymbolName(item);
const mid = this.getMidCurrency(keySymbol);
if(this.baseCurrencies.includes(mid)){
return true;
}
const array = this.getSymbols(mid);
return array.length >1
});
this._subscribeSymbols(filteredSymbols,this._publishDataForStrategy3.bind(this),5);
} }
runStrategy3(){ runStrategy3(){
......
...@@ -13,7 +13,7 @@ const Strategy3MaxAmountMap = { ...@@ -13,7 +13,7 @@ const Strategy3MaxAmountMap = {
KCS: 600, KCS: 600,
} }
class BiboxCollector extends BaseCollector{ class KucoinCollector extends BaseCollector{
constructor(wantedSymbols){ constructor(wantedSymbols){
super("KUCOIN",baseCurrencies,machine,wantedSymbols); super("KUCOIN",baseCurrencies,machine,wantedSymbols);
this.api = new biboxApi('',''); this.api = new biboxApi('','');
...@@ -31,7 +31,7 @@ class BiboxCollector extends BaseCollector{ ...@@ -31,7 +31,7 @@ class BiboxCollector extends BaseCollector{
const symbolDetails = data.data; const symbolDetails = data.data;
const symbolMap = {}; const symbolMap = {};
for(let detail of symbolDetails){ for(let detail of symbolDetails){
if(baseCurrencies.includes(detail.coinTypePair)){ if(baseCurrencies.includes(detail.quoteCurrency) && detail.enableTrading){
symbolMap[detail.symbol] = detail; symbolMap[detail.symbol] = detail;
} }
} }
...@@ -45,7 +45,7 @@ class BiboxCollector extends BaseCollector{ ...@@ -45,7 +45,7 @@ class BiboxCollector extends BaseCollector{
} }
_subscribeSymbols(symbols,callback,subscribeDepth){ _subscribeSymbols(symbols,callback,subscribeDepth){
this._fetchDepthByWebsocket(symbols,subscribeDepth,callback) this._fetchDepthByWebsocket(symbols.slice(0,50),subscribeDepth,callback)
// setTimeout(()=>{ // setTimeout(()=>{
// this._fetchDepthByRest(symbols,subscribeDepth,callback); // this._fetchDepthByRest(symbols,subscribeDepth,callback);
// },20000); // },20000);
...@@ -80,17 +80,29 @@ class BiboxCollector extends BaseCollector{ ...@@ -80,17 +80,29 @@ class BiboxCollector extends BaseCollector{
_fetchDepthByWebsocket(symbols, depth, callback){ _fetchDepthByWebsocket(symbols, depth, callback){
this.api.subscribeSymbols(symbols, depth, (error, result)=>{ function subsCallback(error,result){
if(error){ if(error){
console.error("subscribe error"); console.error("subscribe error");
console.error(error); console.error(error);
}else{ }else{
const data = result.data; callback(result.asks.slice(0,depth), result.bids.slice(0,depth), result.symbol, result.timestamp);
const timeStamp = result.timestamp;
const symbol = result.symbol;
callback(data.SELL.slice(0, depth), data.BUY.slice(0, depth), symbol, timeStamp,result.seq);
} }
}) }
let buffer = [];
for (let i = 0; i < symbols.length; i++) {
buffer.push(symbols[i]);
if (buffer.length === 95) {
const subsBuffer = buffer;
setTimeout(() => {
this.api.subscribeSymbols(subsBuffer, depth, subsCallback)
}, ((i + 1) / 10 - 1) * 50);
buffer = [];
}else if(i === symbols.length-1){
setTimeout(() => {
this.api.subscribeSymbols(buffer, depth, subsCallback)
}, ((i + 1) / 10 - 1) * 50);
}
}
} }
_getCoinsInfo(){ _getCoinsInfo(){
...@@ -111,11 +123,14 @@ class BiboxCollector extends BaseCollector{ ...@@ -111,11 +123,14 @@ class BiboxCollector extends BaseCollector{
}); });
} }
_runMonitor(callback){ _runMonitor(callback){
callback({});
return;
//todo 接口也改了
let balanceMap = {}; let balanceMap = {};
let need = 1; let need = 1;
if(!coinInfoMap){ // if(!coinInfoMap){
this._getCoinsInfo() // this._getCoinsInfo()
} // }
this.api.balance(20, 1, (error,result)=>{ this.api.balance(20, 1, (error,result)=>{
if(error){ if(error){
console.error("get balance by rest error:"); console.error("get balance by rest error:");
...@@ -206,5 +221,5 @@ class BiboxCollector extends BaseCollector{ ...@@ -206,5 +221,5 @@ class BiboxCollector extends BaseCollector{
} }
module.exports = BiboxCollector; module.exports = KucoinCollector;
...@@ -20,6 +20,7 @@ class BiboxStrategy3 extends Strategy3 { ...@@ -20,6 +20,7 @@ class BiboxStrategy3 extends Strategy3 {
} }
_doTrade(baseCurrency1, midCurrency, baseCurrency2, buyPrice, sellPrice, returnPrice, amount, returnAmount, doSaveOrder) { _doTrade(baseCurrency1, midCurrency, baseCurrency2, buyPrice, sellPrice, returnPrice, amount, returnAmount, doSaveOrder) {
return;
const buySymbol = this.collector.getSymbol(baseCurrency1, midCurrency); const buySymbol = this.collector.getSymbol(baseCurrency1, midCurrency);
const buyStartTime = Date.now(); const buyStartTime = Date.now();
const collector = this.collector; const collector = this.collector;
......
...@@ -43,7 +43,7 @@ function testCollector(){ ...@@ -43,7 +43,7 @@ function testCollector(){
// const strategy3 = new BiboxStrategy3(collector); // const strategy3 = new BiboxStrategy3(collector);
// strategy3.run(); // strategy3.run();
} }
// testCollector(); testCollector();
function printCurrency(){ function printCurrency(){
const currentArray = [,'ETH','BTC','LTC','BCH','USDT','USD','RMB',"RCN","WINGS","TRX","LEND","CMT","POWR","HSR","GAS","RDN","TNT","OAX" const currentArray = [,'ETH','BTC','LTC','BCH','USDT','USD','RMB',"RCN","WINGS","TRX","LEND","CMT","POWR","HSR","GAS","RDN","TNT","OAX"
...@@ -121,7 +121,7 @@ function printCurrency(){ ...@@ -121,7 +121,7 @@ function printCurrency(){
} }
}) })
} }
printCurrency() // printCurrency()
function testOrder(){ function testOrder(){
const order = new Order(); const order = new Order();
......
...@@ -39,8 +39,66 @@ function mergeDepth(oldDepth, updateDepth, isAsk) { ...@@ -39,8 +39,66 @@ function mergeDepth(oldDepth, updateDepth, isAsk) {
return i return i
} }
function mergeDepthAsk(oldDepth, updateDepth,oldVersion) {
const results = [];
const cal = {old: 0, updated: 0};
for (; cal.old < oldDepth.length || cal.updated < updateDepth.length;) {
if (oldDepth[cal.old] === undefined) {
updateDepth[cal.updated].size > 1e-8 && results.push(updateDepth[cal.updated]);
cal.updated++;
} else if (updateDepth[cal.updated] === undefined) {
results.push(oldDepth[cal.old]);
cal.old++;
} else if (parseFloat(oldDepth[cal.old].price) > parseFloat(updateDepth[cal.updated].price)) {
updateDepth[cal.updated].size > 1e-8 && results.push(updateDepth[cal.updated]);
cal.updated++;
} else if (parseFloat(oldDepth[cal.old].price) === parseFloat(updateDepth[cal.updated].price)) {
if (parseFloat(updateDepth[cal.updated].size) > 1e-8) {
results.push(updateDepth[cal.updated]);
}
cal.updated++;
cal.old++;
} else {
results.push(oldDepth[cal.old]);
cal.old++;
}
}
return results
}
function mergeDepthBids(oldDepth, updateDepth, oldVersion) {
const result = [];
const cal = {old: 0, updated: 0};
for (; cal.old < oldDepth.length || cal.updated < updateDepth.length;) {
if (oldDepth[cal.old] === undefined) {
updateDepth[cal.updated].size > 1e-8 && result.push(updateDepth[cal.updated]);
cal.updated++;
} else if (updateDepth[cal.updated] === undefined) {
result.push(oldDepth[cal.old]);
cal.old++;
} else if (parseFloat(oldDepth[cal.old].price) < parseFloat(updateDepth[cal.updated].price)) {
updateDepth[cal.updated].size > 1e-8 && result.push(updateDepth[cal.updated]);
cal.updated++;
} else if (parseFloat(oldDepth[cal.old].price) === parseFloat(updateDepth[cal.updated].price)) {
if (parseFloat(updateDepth[cal.updated].size) > 1e-8) {
result.push(updateDepth[cal.updated++]);
cal.old++;
} else {
cal.updated++;
cal.old++;
}
} else {
result.push(oldDepth[cal.old++])
}
}
return result;
}
module.exports = { module.exports = {
IPs: ips, IPs: ips,
mergeDepth: mergeDepth mergeDepthBids:mergeDepthBids,
mergeDepthAsk:mergeDepthAsk,
mergeDepth:mergeDepth
} }
\ No newline at end of file
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