Skip to content

Commit

Permalink
feat(okx): cancel algo orders
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Apr 20, 2023
1 parent e8954cc commit dbc94ea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
50 changes: 40 additions & 10 deletions src/exchanges/okx/okx.exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
import { OKXPrivateWebsocket } from './okx.ws-private';
import { OKXPublicWebsocket } from './okx.ws-public';

// TODO: Cancel algo orders
// TODO: Update orders
// TODO: Update algo orders
// TODO: Place trailing stops
Expand Down Expand Up @@ -514,6 +513,11 @@ export class OKXExchange extends BaseExchange {
};

cancelOrders = async (orders: Order[]) => {
await this.cancelNormalOrders(orders);
await this.cancelAlgoOrders(orders);
};

cancelNormalOrders = async (orders: Order[]) => {
const batches = orders
.filter((o) => o.type === OrderType.Limit)
.reduce((acc: Array<Record<string, any>>, o) => {
Expand All @@ -523,22 +527,48 @@ export class OKXExchange extends BaseExchange {
}, []);

await forEachSeries(chunk(batches, 20), async (batch) => {
await this.unlimitedXHR.post(ENDPOINTS.CANCEL_ORDERS, batch);
const {
data: { data },
} = await this.unlimitedXHR.post(ENDPOINTS.CANCEL_ORDERS, batch);

data.forEach((d: Record<string, any>) => {
if (d.sMsg) this.emitter.emit('error', d.sMsg);
});
});
};

cancelAlgoOrders = async (orders: Order[]) => {
const batches = orders
.filter((o) => o.type !== OrderType.Limit)
.reduce((acc: Array<Record<string, any>>, o) => {
const market = this.store.markets.find((m) => m.symbol === o.symbol);
if (!market) return acc;
return [
...acc,
{
instId: market.id,
algoId: o.id.replace('_sl', '').replace('_tp', ''),
},
];
}, []);

await forEachSeries(chunk(batches, 20), async (batch) => {
const {
data: { data },
} = await this.unlimitedXHR.post(ENDPOINTS.CANCEL_ALGO_ORDERS, batch);

data.forEach((d: Record<string, any>) => {
if (d.sMsg) this.emitter.emit('error', d.sMsg);
});
});
};

cancelSymbolOrders = async (symbol: string) => {
const market = this.store.markets.find((m) => m.symbol === symbol);
if (!market) return;

const orders = this.store.orders.filter(
(o) => o.symbol === symbol && o.type === OrderType.Limit
);

const batches = orders.map((o) => ({ instId: market.id, ordId: o.id }));
await forEachSeries(chunk(batches, 20), async (batch) => {
await this.unlimitedXHR.post(ENDPOINTS.CANCEL_ORDERS, batch);
});
const orders = this.store.orders.filter((o) => o.symbol === symbol);
await this.cancelOrders(orders);
};

setLeverage = async (symbol: string, inputLeverage: number) => {
Expand Down
1 change: 1 addition & 0 deletions src/exchanges/okx/okx.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const ENDPOINTS = {
UNFILLED_ORDERS: '/api/v5/trade/orders-pending',
UNFILLED_ALGO_ORDERS: '/api/v5/trade/orders-algo-pending',
CANCEL_ORDERS: '/api/v5/trade/cancel-batch-orders',
CANCEL_ALGO_ORDERS: '/api/v5/trade/cancel-algos',
PLACE_ORDERS: '/api/v5/trade/batch-orders',
SET_LEVERAGE: '/api/v5/account/set-leverage',
LEVERAGE: '/api/v5/account/leverage-info',
Expand Down

0 comments on commit dbc94ea

Please sign in to comment.