Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Z
zbNode
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ken
zbNode
Commits
bf7b1e85
Commit
bf7b1e85
authored
Sep 10, 2018
by
ken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
depth
parent
8bc20132
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
42 deletions
+49
-42
zb.js
zb.js
+49
-42
No files found.
zb.js
View file @
bf7b1e85
...
...
@@ -14,7 +14,8 @@ const accountInfoChannel = "getaccountinfo";
const
APIKEY
=
"6b07db0c-dc4f-4a90-9955-665041bdbdbf"
;
const
APISECRET
=
"7ac9bcc3-5f0c-4eaf-99c9-80cb6a45165a"
;
const
lastEventTimeMap
=
{};
const
lastDataMap
=
{}
const
lastDataMap
=
{};
const
lastLastDataMap
=
{};
let
balanceByRestOk
=
true
;
let
balanceByWebsocketOk
=
true
;
let
currentIpIndex
=
0
;
...
...
@@ -124,23 +125,24 @@ function subscribe() {
}
}
else
{
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
lastEventTime
=
lastEventTimeMap
[
symbol
]
||
0
;
if
(
depth
.
timestamp
>
lastEventTime
)
{
lastEventTimeMap
[
symbol
]
=
depth
.
timestamp
;
// console.log(symbol + '@' + depth.timestamp * 1000 + ' from websocket');
const
asks
=
depth
.
asks
.
reverse
();
const
bids
=
depth
.
bids
;
const
publishContent
=
JSON
.
stringify
({
symbol
,
time
:
depth
.
timestamp
*
1000
});
const
depthJson
=
JSON
.
stringify
({
asks
,
bids
,
symbol
});
if
(
asks
[
0
][
0
]
>=
bids
[
0
][
0
])
{
redisClient
.
set
(
key
,
depthJson
,
'EX'
,
2
);
redisClient
.
publish
(
publishKey
,
publishContent
);
}
else
{
console
.
error
(
"买一大于卖一,数据有误"
)
}
}
_handleDepthData
(
symbol
,
data
,
"websocket"
);
// const lastEventTime = lastEventTimeMap[symbol] || 0;
// if (depth.timestamp > lastEventTime) {
// lastEventTimeMap[symbol] = depth.timestamp;
// // console.log(symbol + '@' + depth.timestamp * 1000 + ' from websocket');
// const asks = depth.asks.reverse();
// const bids = depth.bids;
// const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000});
// const depthJson = JSON.stringify({asks, bids, symbol});
// if (asks[0][0] >= bids[0][0]) {
// redisClient.set(key, depthJson, 'EX', 2);
// redisClient.publish(publishKey, publishContent);
// } else {
// console.error("买一大于卖一,数据有误")
// }
// }
}
});
}
...
...
@@ -171,37 +173,42 @@ function _request(method, url, callback) {
});
}
function
_handleDepthData
(
symbol
,
data
,
source
)
{
const
key
=
symbol
.
replace
(
'_'
,
''
).
toUpperCase
()
+
'DEPTH@zb'
;
symbol
=
symbol
.
toUpperCase
();
const
lastEventTime
=
lastEventTimeMap
[
symbol
]
||
0
;
if
(
data
.
timestamp
>
lastEventTime
||
(
data
.
timestamp
===
lastEventTime
&&
JSON
.
stringify
(
data
)
!==
lastDataMap
[
symbol
]
&&
JSON
.
stringify
(
data
)
!==
lastLastDataMap
[
symbol
]))
{
if
(
symbol
===
"BTCUSDT"
)
{
let
consoleMethod
=
console
.
log
;
if
(
data
.
timestamp
===
lastEventTime
)
{
consoleMethod
=
console
.
warn
;
}
consoleMethod
(
symbol
+
'@'
+
data
.
timestamp
*
1000
+
' from '
+
source
);
consoleMethod
(
JSON
.
stringify
(
data
));
}
lastEventTimeMap
[
symbol
]
=
data
.
timestamp
;
lastLastDataMap
[
symbol
]
=
lastDataMap
[
symbol
];
lastDataMap
[
symbol
]
=
JSON
.
stringify
(
data
);
const
publishContent
=
JSON
.
stringify
({
symbol
,
time
:
data
.
timestamp
*
1000
});
const
asks
=
data
.
asks
.
reverse
();
const
bids
=
data
.
bids
;
const
depthJson
=
JSON
.
stringify
({
asks
,
bids
,
symbol
});
if
(
asks
[
0
][
0
]
>=
bids
[
0
][
0
])
{
redisClient
.
set
(
key
,
depthJson
,
'EX'
,
2
);
redisClient
.
publish
(
publishKey
,
publishContent
);
}
else
{
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
{
const
key
=
symbol
.
replace
(
'_'
,
''
).
toUpperCase
()
+
'DEPTH@zb'
;
symbol
=
symbol
.
toUpperCase
();
const
lastEventTime
=
lastEventTimeMap
[
symbol
]
||
0
;
if
(
data
.
timestamp
>
lastEventTime
||
(
data
.
timestamp
===
lastEventTime
&&
JSON
.
stringify
(
data
)
!==
lastDataMap
[
symbol
]))
{
if
(
symbol
===
"BTCUSDT"
)
{
let
consoleMethod
=
console
.
log
;
if
(
data
.
timestamp
===
lastEventTime
)
{
consoleMethod
=
console
.
error
}
consoleMethod
(
symbol
+
'@'
+
data
.
timestamp
*
1000
+
' from rest'
);
consoleMethod
(
JSON
.
stringify
(
data
));
}
lastEventTimeMap
[
symbol
]
=
data
.
timestamp
;
lastDataMap
[
symbol
]
=
JSON
.
stringify
(
data
);
const
publishContent
=
JSON
.
stringify
({
symbol
,
time
:
data
.
timestamp
*
1000
});
const
asks
=
data
.
asks
.
reverse
();
const
bids
=
data
.
bids
;
const
depthJson
=
JSON
.
stringify
({
asks
,
bids
,
symbol
});
if
(
asks
[
0
][
0
]
>=
bids
[
0
][
0
])
{
redisClient
.
set
(
key
,
depthJson
,
'EX'
,
2
);
redisClient
.
publish
(
publishKey
,
publishContent
);
}
else
{
console
.
error
(
"买一大于卖一,数据有误"
)
}
}
_handleDepthData
(
symbol
,
data
,
"rest"
);
}
if
(
callback
)
{
callback
(
err
,
data
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment