Skip to content
This repository has been archived by the owner on Sep 9, 2023. It is now read-only.

Implement OKEx v5 api #326

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
formating fix
  • Loading branch information
MarhiievHE committed Apr 25, 2022
commit 93d5c2cc5701dd70b165585ca47ded1a53c079aa
12 changes: 9 additions & 3 deletions __tests__/exchanges/OkexClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ testClient({
exchangeName: "OKEx",
clientName: "OKExClient - Futures",
fetchMarkets: async () => {
const results: any = await get("https://www.okx.com/api/v5/public/instruments?instType=FUTURES");
const results: any = await get(
"https://www.okx.com/api/v5/public/instruments?instType=FUTURES",
);
return results.data
.filter(p => p.settleCcy === "BTC")
.map(p => ({
Expand All @@ -97,7 +99,9 @@ testClient({
exchangeName: "OKEx",
clientName: "OKExClient - Swap",
fetchMarkets: async () => {
const results: any = await get("https://www.okx.com/api/v5/public/instruments?instType=SWAP");
const results: any = await get(
"https://www.okx.com/api/v5/public/instruments?instType=SWAP",
);
return results.data
.filter(p => ["BTC", "ETH", "LTC"].includes(p.settleCcy))
.map(p => ({
Expand All @@ -115,7 +119,9 @@ testClient({
exchangeName: "OKEx",
clientName: "OKExClient - Options",
fetchMarkets: async () => {
const results: any = await get("https://www.okx.com/api/v5/public/instruments?instType=OPTION&uly=BTC-USD");
const results: any = await get(
"https://www.okx.com/api/v5/public/instruments?instType=OPTION&uly=BTC-USD",
);
return results.data
.map(p => ({
id: p.instId,
Expand Down
44 changes: 24 additions & 20 deletions src/exchanges/OkexClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class OkexClient extends BasicClient {
*/
protected _marketArg(method: string, market: Market) {
const type: string = (market.type || "SPOT").toUpperCase();
return {"channel":method, "instId":market.id, "instType": type };
return { channel: method, instId: market.id, instType: type };
}

/**
Expand Down Expand Up @@ -230,7 +230,7 @@ export class OkexClient extends BasicClient {

protected _onMessage(json: string) {
// ignore pongs
if(json==="pong"){
if (json === "pong") {
return;
}

Expand Down Expand Up @@ -542,22 +542,14 @@ export class OkexClient extends BasicClient {
}
*/
protected _constructTicker(data, market) {
const {
last,
bidPx,
bidSz,
askPx,
askSz,
open24h,
high24h,
low24h,
vol24h,
ts,
} = data;
const { last, bidPx, bidSz, askPx, askSz, open24h, high24h, low24h, vol24h, ts } = data;

const change = parseFloat(last) - parseFloat(open24h);
const changePercent = change / parseFloat(open24h);
const timestamp = moment.unix(Math.ceil(ts/1000)).utc().valueOf();
const timestamp = moment
.unix(Math.ceil(ts / 1000))
.utc()
.valueOf();
return new Ticker({
exchange: this.name,
base: market.base,
Expand Down Expand Up @@ -590,7 +582,10 @@ export class OkexClient extends BasicClient {
*/
protected _constructTrade(datum, market) {
const { px, side, sz, ts, tradeId } = datum;
const unix = moment.unix(Math.ceil(ts/1000)).utc().valueOf();
const unix = moment
.unix(Math.ceil(ts / 1000))
.utc()
.valueOf();

return new Trade({
exchange: this.name,
Expand Down Expand Up @@ -618,8 +613,11 @@ export class OkexClient extends BasicClient {
* @param {*} datum
*/
protected _constructCandle(datum) {
const [datetime, open, high, low, close, volume,] = datum;
const ts = moment.unix(Math.ceil(datetime/1000)).utc().valueOf();
const [datetime, open, high, low, close, volume] = datum;
const ts = moment
.unix(Math.ceil(datetime / 1000))
.utc()
.valueOf();
return new Candle(ts, open, high, low, close, volume);
}

Expand Down Expand Up @@ -677,7 +675,10 @@ export class OkexClient extends BasicClient {
protected _constructLevel2Snapshot(datum, market) {
const asks = datum.asks.map(p => new Level2Point(p[0], p[1], p[3]));
const bids = datum.bids.map(p => new Level2Point(p[0], p[1], p[3]));
const ts = moment.unix(Math.ceil(datum.ts/1000)).utc().valueOf();
const ts = moment
.unix(Math.ceil(datum.ts / 1000))
.utc()
.valueOf();
const checksum = datum.checksum;
return new Level2Snapshot({
exchange: this.name,
Expand Down Expand Up @@ -722,7 +723,10 @@ export class OkexClient extends BasicClient {
_constructLevel2Update(datum, market) {
const asks = datum.asks.map(p => new Level2Point(p[0], p[1], p[3]));
const bids = datum.bids.map(p => new Level2Point(p[0], p[1], p[3]));
const ts = moment.unix(Math.ceil(datum.ts/1000)).utc().valueOf();
const ts = moment
.unix(Math.ceil(datum.ts / 1000))
.utc()
.valueOf();
const checksum = datum.checksum;
return new Level2Update({
exchange: this.name,
Expand Down