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

♻️ Refactoring exchanges services #15

Closed
wants to merge 4 commits into from
Closed
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
Next Next commit
🐛 Updating trade size on open / Fixing relative sizing with trading m…
…odes
  • Loading branch information
thibaultyou committed Aug 7, 2021
commit e03201e8fe3bd3e4ebb8b9351468efeee220ca9e
67 changes: 34 additions & 33 deletions src/services/exchanges/base/base.exchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,29 +221,13 @@ export abstract class BaseExchangeService implements IBaseExchange {
trade: Trade
): Promise<IOrderOptions> => {
const { symbol } = ticker;
const { size, max, direction } = trade;
const { size, direction } = trade;
const side = getSide(direction);
try {
// TODO refacto
let orderSize = Number(size);
if (size.includes('%') || max) {
const funds = await this.getAvailableFunds(account, ticker); // avoid this call if possible
if (size.includes('%')) {
orderSize = getRelativeOrderSize(funds, size);
}
if (max) {
await this.handleMaxBudget(account, ticker, trade, funds);
}
}
// if (isSpotExchange(ticker, this.exchangeId) && orderSize > funds) {
// // TODO create dedicated error
// throw new Error('Insufficient funds');
// }
///

const tickerPrice = getTickerPrice(ticker, this.exchangeId);
const orderSize = Number(size);
return {
size: getTokensAmount(symbol, tickerPrice, Number(orderSize)),
size: getTokensAmount(symbol, tickerPrice, orderSize),
side: side as 'buy' | 'sell'
};
} catch (err) {
Expand All @@ -254,24 +238,41 @@ export abstract class BaseExchangeService implements IBaseExchange {

openOrder = async (account: Account, trade: Trade): Promise<Order> => {
await this.refreshSession(account);
const { symbol, direction } = trade;
const { symbol, size, direction, max, mode } = trade;
const accountId = getAccountId(account);
try {
const ticker = await this.getTicker(symbol);
const isOrderAllowed = await this.handleOrderModes(
account,
ticker,
trade
);
if (isOrderAllowed) {
// TODO refacto
// close on sell spot order
if (
getSide(direction) === Side.Sell &&
isSpotExchange(ticker, this.exchangeId)
) {
return await this.closeOrder(account, trade, ticker);
// TODO refacto
// handling sell on spot
if (
getSide(direction) === Side.Sell &&
isSpotExchange(ticker, this.exchangeId)
) {
return await this.closeOrder(account, trade, ticker);
}
// handling size / budget
if (size.includes('%') || max) {
const funds = await this.getAvailableFunds(account, ticker); // avoid this call if possible
if (size.includes('%')) {
trade = {
...trade,
size: getRelativeOrderSize(funds, size).toString()
};
}
if (max) {
await this.handleMaxBudget(account, ticker, trade, funds);
}
}
// if (isSpotExchange(ticker, this.exchangeId) && orderSize > funds) {
// // TODO create dedicated error
// throw new Error('Insufficient funds');
// }
///
// handling modes
const isOrderAllowed = mode
? await this.handleOrderModes(account, ticker, trade)
: true;
if (isOrderAllowed) {
const { side, size } = await this.getOpenOrderOptions(
account,
ticker,
Expand Down
11 changes: 5 additions & 6 deletions src/services/exchanges/ftx.exchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ export class FTXExchangeService extends CompositeExchangeService {
const { symbol } = ticker;
const accountId = getAccountId(account);
try {
const position = (await this.getTickerPosition(
const { side, cost } = (await this.getTickerPosition(
account,
ticker
)) as IFTXFuturesPosition;
if (position && isSideDifferent(position.side as Side, direction)) {
if (isSideDifferent(side as Side, direction)) {
info(REVERSING_TRADE(this.exchangeId, accountId, symbol));
await this.closeOrder(account, trade, ticker);
const size = Math.abs(Number(cost)).toString();
await this.closeOrder(account, { ...trade, size }, ticker);
}
} catch (err) {
// ignore throw
Expand Down Expand Up @@ -154,13 +155,11 @@ export class FTXExchangeService extends CompositeExchangeService {
return true;
}
} else {
const position = (await this.getTickerPosition(
const { side, cost } = (await this.getTickerPosition(
account,
ticker
)) as IFTXFuturesPosition;
const { side, cost } = position;
if (
position &&
isSideDifferent(side as Side, direction) &&
Number(size) > Math.abs(Number(cost))
) {
Expand Down