Skip to content

Commit

Permalink
feat(woo): place position SL/TP on create
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 31, 2023
1 parent 0df8856 commit 157d010
Showing 1 changed file with 67 additions and 4 deletions.
71 changes: 67 additions & 4 deletions src/exchanges/woo/woo.exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
Ticker,
UpdateOrderOpts,
} from '../../types';
import { OrderType, OrderStatus, PositionSide } from '../../types';
import { OrderSide, OrderType, OrderStatus, PositionSide } from '../../types';
import { adjust } from '../../utils/adjust';
import { v } from '../../utils/get-key';
import { inverseObj } from '../../utils/inverse-obj';
Expand Down Expand Up @@ -541,7 +541,23 @@ export class Woo extends BaseExchange {
};

cancelAlgoOrder = async (order: Order) => {
await this.unlimitedXHR.delete(`${ENDPOINTS.ALGO_ORDER}/${order.parentId}`);
const sibling = this.store.orders.find(
(o) => o.parentId === order.parentId && o.id !== order.id
);

const id = order.parentId || order.id;
await this.unlimitedXHR.delete(`${ENDPOINTS.ALGO_ORDER}/${id}`);

if (sibling) {
const priceKey =
sibling.type === OrderType.StopLoss ? 'stopLoss' : 'takeProfit';

await this.placePositionalAlgoOrder({
symbol: sibling.symbol,
side: sibling.side,
[priceKey]: sibling.price,
});
}
};

placeOrder = async (opts: PlaceOrderOpts) => {
Expand Down Expand Up @@ -592,8 +608,9 @@ export class Woo extends BaseExchange {
return data.order_id as string;
});

if (opts.stopLoss) {
// TODO: Place POSITIONAL_TP_SL algo order
if (opts.stopLoss || opts.takeProfit) {
const algoIds = await this.placePositionalAlgoOrder(opts);
orderIds.push(...algoIds);
}

return orderIds;
Expand Down Expand Up @@ -642,6 +659,52 @@ export class Woo extends BaseExchange {
}
};

placePositionalAlgoOrder = async (
opts: Pick<PlaceOrderOpts, 'side' | 'stopLoss' | 'symbol' | 'takeProfit'>
) => {
const req: Record<string, any> = {
symbol: reverseSymbol(opts.symbol),
reduceOnly: false,
algoType: 'POSITIONAL_TP_SL',
childOrders: [],
};

if (opts.stopLoss) {
req.childOrders.push({
algoType: 'STOP_LOSS',
type: 'CLOSE_POSITION',
side: opts.side === OrderSide.Buy ? 'SELL' : 'BUY',
reduceOnly: true,
triggerPrice: `${opts.stopLoss}`,
});
}

if (opts.takeProfit) {
req.childOrders.push({
algoType: 'TAKE_PROFIT',
type: 'CLOSE_POSITION',
side: opts.side === OrderSide.Buy ? 'SELL' : 'BUY',
reduceOnly: true,
triggerPrice: `${opts.takeProfit}`,
});
}

try {
const {
data: {
data: { rows },
},
} = await this.unlimitedXHR.post<{
data: { rows: Array<Record<string, any>> };
}>(ENDPOINTS.ALGO_ORDER, req);

return rows.map((r) => r.orderId);
} catch (err: any) {
this.emitter.emit('error', err?.response?.data?.message || err?.message);
return [];
}
};

cancelAllOrders = async () => {
try {
await this.unlimitedXHR.delete(ENDPOINTS.CANCEL_ALGO_ORDERS);
Expand Down

0 comments on commit 157d010

Please sign in to comment.