Skip to content

Commit

Permalink
feat(woo): bootstrap placeOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 31, 2023
1 parent aac0480 commit ee42293
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
57 changes: 54 additions & 3 deletions src/exchanges/woo/woo.exchange.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import type { Axios } from 'axios';
import rateLimit from 'axios-rate-limit';
import BigNumber from 'bignumber.js';
import { sumBy } from 'lodash';
import { forEachSeries } from 'p-iteration';
import { sumBy, times } from 'lodash';
import { forEachSeries, mapSeries } from 'p-iteration';

import type {
Candle,
ExchangeOptions,
Market,
OHLCVOptions,
Order,
PlaceOrderOpts,
Position,
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';
import { loop } from '../../utils/loop';
import { omitUndefined } from '../../utils/omit-undefined';
import { createWebSocket } from '../../utils/universal-ws';
Expand Down Expand Up @@ -543,6 +546,54 @@ export class Woo extends BaseExchange {
}
};

placeOrder = async (opts: PlaceOrderOpts) => {
const market = this.store.markets.find((m) => m.symbol === opts.symbol);

if (!market) {
throw new Error(`Market not found: ${opts.symbol}`);
}

const maxSize = market.limits.amount.max;
const pPrice = market.precision.price;
const pAmount = market.precision.amount;

const amount = adjust(opts.amount, pAmount);

const price = opts.price ? adjust(opts.price, pPrice) : null;

let type = inverseObj(ORDER_TYPE)[OrderType.Market];
if (opts.type === OrderType.Limit) {
type = opts.side === OrderSide.Buy ? 'ASK' : 'BID';
}

const req = omitUndefined({
symbol: reverseSymbol(opts.symbol),
order_type: type,
order_price: opts.type === OrderType.Limit ? price : undefined,
reduce_only: opts.reduceOnly,
side: inverseObj(ORDER_SIDE)[opts.side],
});

const lots = amount > maxSize ? Math.ceil(amount / maxSize) : 1;
const rest = amount > maxSize ? adjust(amount % maxSize, pAmount) : 0;

const lotSize = adjust((amount - rest) / lots, pAmount);
const payloads = times(lots, () => {
return { ...req, order_amount: lotSize };
});

if (rest) payloads.push({ ...req, order_amount: rest });

const responses = await mapSeries(payloads, async (p) => {
const { data } = await this.unlimitedXHR.post(ENDPOINTS.PLACE_ORDER, p);
return data;
});

console.log(responses);

return [];
};

private isAlgoOrder = (orderType: OrderType) => {
return (
orderType === OrderType.StopLoss ||
Expand Down
1 change: 1 addition & 0 deletions src/exchanges/woo/woo.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const ENDPOINTS = {
ORDERS: '/v1/orders',
KLINE: '/v1/kline',
CANCEL_ORDER: '/v1/order',
PLACE_ORDER: '/v1/order',
};

export const ORDER_TYPE: Record<string, OrderType> = {
Expand Down

0 comments on commit ee42293

Please sign in to comment.