Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bybit l2updates #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions src/exchanges/BybitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class BybitClient extends BasicClient {
protected _sendUnsubLevel3Updates = NotImplementedFn;

constructor({
wssPath = "wss://stream.bybit.com/spot/quote/ws/v2",
wssPath = "wss://stream.bybit.com/spot/quote/ws/v1",
watcherMs,
}: BybitClientOptions = {}) {
super(wssPath, "Bybit", undefined, watcherMs);
Expand Down Expand Up @@ -56,8 +56,8 @@ export class BybitClient extends BasicClient {
JSON.stringify({
topic: "realtimes",
event: "sub",
symbol: remoteId,
params: {
symbol: remoteId,
binary: false,
},
}),
Expand All @@ -69,8 +69,8 @@ export class BybitClient extends BasicClient {
JSON.stringify({
topic: "realtimes",
event: "cancel",
symbol: remoteId,
params: {
symbol: remoteId,
binary: false,
},
}),
Expand All @@ -82,8 +82,8 @@ export class BybitClient extends BasicClient {
JSON.stringify({
topic: "trade",
event: "sub",
symbol: remoteId,
params: {
symbol: remoteId,
binary: false,
},
}),
Expand All @@ -106,10 +106,10 @@ export class BybitClient extends BasicClient {
protected _sendSubLevel2Updates(remoteId: string) {
this._wss.send(
JSON.stringify({
topic: "depth",
topic: "diffDepth",
event: "sub",
symbol: remoteId,
params: {
symbol: remoteId,
binary: false,
},
}),
Expand All @@ -119,10 +119,10 @@ export class BybitClient extends BasicClient {
protected _sendUnsubLevel2Updates(remoteId: string) {
this._wss.send(
JSON.stringify({
topic: "depth",
topic: "diffDepth",
event: "cancel",
symbol: remoteId,
params: {
symbol: remoteId,
binary: false,
},
}),
Expand Down Expand Up @@ -150,24 +150,26 @@ export class BybitClient extends BasicClient {
return;
}

if (msg.topic === "depth") {
if (msg.topic === "diffDepth") {
this._onL2Update(msg);
return;
}
}

protected _onTicker(msg) {
const {
s, // symbol
t, // timestamp
o, // open
c, // close
h, // high
l, // low
v, // volume
qv, // quote volume
m, // change
} = msg.data;
const [
{
s, // symbol
t, // timestamp
o, // open
c, // close
h, // high
l, // low
v, // volume
qv, // quote volume
m, // change
},
] = msg.data;

const market = this._tickerSubs.get(s);
if (!market) return;
Expand All @@ -190,14 +192,16 @@ export class BybitClient extends BasicClient {
}

protected _onTrade(msg) {
const { symbol } = msg.params;
const {
v, // trade ID
t, // timestamp
p, // price
q, // quantity
m, // isBuy
} = msg.data;
const { symbol } = msg;
const [
{
v, // trade ID
t, // timestamp
p, // price
q, // quantity
m, // isBuy
},
] = msg.data;

const market = this._tradeSubs.get(symbol);
if (!market) return;
Expand All @@ -217,12 +221,14 @@ export class BybitClient extends BasicClient {
}

protected _onL2Update(msg) {
const {
s, // symbol
t, // timestamp
b, // bids
a, // asks
} = msg.data;
const [
{
s, // symbol
t, // timestamp
b, // bids
a, // asks
},
] = msg.data;

const bids = b.map(([price, amount]) => new Level2Point(price, amount));
const asks = a.map(([price, amount]) => new Level2Point(price, amount));
Expand Down