Skip to content

Commit

Permalink
feat(okx): update orders and algo orders
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Apr 20, 2023
1 parent dbc94ea commit 4d6447d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
54 changes: 48 additions & 6 deletions src/exchanges/okx/okx.exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
PlaceOrderOpts,
Position,
Ticker,
UpdateOrderOpts,
Writable,
} from '../../types';
import { inverseObj } from '../../utils/inverse-obj';
Expand All @@ -41,8 +42,6 @@ import {
import { OKXPrivateWebsocket } from './okx.ws-private';
import { OKXPublicWebsocket } from './okx.ws-public';

// TODO: Update orders
// TODO: Update algo orders
// TODO: Place trailing stops
// TODO: Place single TP/SL

Expand Down Expand Up @@ -512,6 +511,52 @@ export class OKXExchange extends BaseExchange {
return this.publicWebsocket.listenOrderBook(symbol, callback);
};

updateOrder = async ({ order, update }: UpdateOrderOpts) => {
if (order.type !== OrderType.Limit) {
return this.updateAlgoOrder({ order, update });
}

const market = this.store.markets.find((m) => m.symbol === order.symbol);
if (!market) throw new Error(`Market ${order.symbol} not found on OKX`);

const payload: Record<string, any> = { instId: market.id, ordId: order.id };
if ('price' in update) payload.newPx = `${update.price}`;
if ('amount' in update) {
const pAmount = market.precision.amount;
const amount = adjust(divide(update.amount, pAmount), pAmount);
payload.newSz = `${amount}`;
}

try {
await this.xhr.post(ENDPOINTS.UPDATE_ORDER, payload);
return [order.id];
} catch (err: any) {
this.emitter.emit('error', err?.response?.data?.msg || err?.message);
return [];
}
};

updateAlgoOrder = async ({ order, update }: UpdateOrderOpts) => {
const orders = this.store.orders.filter((o) =>
o.id.startsWith(order.id.replace(/_[a-zA-Z]+$/, ''))
);

const newOrder = {
symbol: order.symbol,
type: order.type,
side: order.side,
price: order.price,
amount: order.amount,
reduceOnly: order.reduceOnly || false,
};

if ('price' in update) newOrder.price = update.price;
if ('amount' in update) newOrder.amount = update.amount;

await this.cancelAlgoOrders(orders);
return await this.placeOrder(newOrder);
};

cancelOrders = async (orders: Order[]) => {
await this.cancelNormalOrders(orders);
await this.cancelAlgoOrders(orders);
Expand Down Expand Up @@ -545,10 +590,7 @@ export class OKXExchange extends BaseExchange {
if (!market) return acc;
return [
...acc,
{
instId: market.id,
algoId: o.id.replace('_sl', '').replace('_tp', ''),
},
{ instId: market.id, algoId: o.id.replace(/_[a-zA-Z]+$/, '') },
];
}, []);

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 @@ -33,6 +33,7 @@ export const ENDPOINTS = {
LEVERAGE: '/api/v5/account/leverage-info',
SET_POSITION_MODE: '/api/v5/account/set-position-mode',
ACCOUNT_CONFIG: '/api/v5/account/config',
UPDATE_ORDER: '/api/v5/trade/amend-order',
};

export const ORDER_STATUS: Record<string, OrderStatus> = {
Expand Down

0 comments on commit 4d6447d

Please sign in to comment.