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
♻️ Adding back trading modes for FTX exchange
  • Loading branch information
thibaultyou committed Aug 7, 2021
commit feaf5de7f95983e96847bb0a0fd438ceeaa0032d
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

>
> This project is under construction 🚧
> Only USD based markets are supported for now
>

Minimalist service designed to execute [TradingView](https://www.tradingview.com/) webhooks and process them to cryptocurrencies exchanges from [AWS lightsail](https://lightsail.aws.amazon.com/).
Expand All @@ -27,13 +26,17 @@ You can use the bot with :

### 🏦 Supported exchanges

| logo | id | name | doc |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|-----------------------------------------------------------------------------|:------------------------------------------------------------------:|
>
> Only USD based markets are supported for now
>

| logo | id | name | doc |
|-|-|-|:-:|
| [![binance](https://user-images.githubusercontent.com/1294454/29604020-d5483cdc-87ee-11e7-94c7-d1a8d9169293.jpg)](https://www.binance.com/) | binance | [Binance (spot)](https://www.binance.com/) | [API](https://binance-docs.github.io/apidocs/spot/en) |
| [![binanceusdm](https://user-images.githubusercontent.com/1294454/117738721-668c8d80-b205-11eb-8c49-3fad84c4a07f.jpg)](https://www.binance.com/) | binanceusdm | [Binance USDⓈ-M (futures)](https://www.binance.com/) | [API](https://binance-docs.github.io/apidocs/spot/en) |
| [![ftx](https://user-images.githubusercontent.com/1294454/67149189-df896480-f2b0-11e9-8816-41593e17f9ec.jpg)](https://ftx.com/) | ftx | [FTX (spot & futures)](https://ftx.com/) | [API](https://github.com/ftexchange/ftx) |
| [![kraken](https://user-images.githubusercontent.com/51840849/76173629-fc67fb00-61b1-11ea-84fe-f2de582f58a3.jpg)](https://www.kraken.com) | kraken | [Kraken (spot)](https://www.kraken.com) | [API](https://www.kraken.com/features/api) |
| [![kucoin](https://user-images.githubusercontent.com/51840849/87295558-132aaf80-c50e-11ea-9801-a2fb0c57c799.jpg)](https://www.kucoin.com/) | kucoin | [KuCoin (spot)](https://www.kucoin.com/) | [API](https://docs.kucoin.com) |
| [![kraken](https://user-images.githubusercontent.com/51840849/76173629-fc67fb00-61b1-11ea-84fe-f2de582f58a3.jpg)](https://www.kraken.com) | kraken | [Kraken (spot)](https://www.kraken.com) | [API](https://www.kraken.com/features/api) |
<!-- | [![binancecoinm](https://user-images.githubusercontent.com/1294454/117738721-668c8d80-b205-11eb-8c49-3fad84c4a07f.jpg)](https://www.binance.com/) | binancecoinm | [Binance COIN-M](https://www.binance.com/) | * | [API](https://binance-docs.github.io/apidocs/spot/en) |-->

### 🚧 Features
Expand All @@ -46,10 +49,11 @@ You can use the bot with :
- list account balances
- list exchange markets
- process multiple trades at once

<!-- - close a position and open another in the opposite direction (Futures only)
- close a position while reducing on oversell / overbuy (Futures only) -->
- close a position and open another in the opposite direction (Futures only)
- close a position while reducing on oversell / overbuy (Futures only)

### 💡 Contributions

Feel free to submit [Github issues](https://github.com/thibaultyou/tradingview-alerts-processor/issues) if you find anything you want me to add, fix or improve.

Best way to show your support to this tool is by hitting the star button [![Stars](https://img.shields.io/github/stars/thibaultyou/tradingview-alerts-processor?style=social)](https://github.com/thibaultyou/tradingview-alerts-processor/stargazers), you can also [!["Buy Me A Coffee"](https://img.shields.io/badge/-buy_me_a%C2%A0coffee-gray?logo=buy-me-a-coffee)](https://www.buymeacoffee.com/thibaultyou).
6 changes: 2 additions & 4 deletions docs/2_Alerts.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
[{ "stub": "dev", "direction": "long", "symbol": "ETH-PERP", "size": "50" }, { "stub": "dev", "direction": "long", "symbol": "BTC-PERP", "size": "50" }]
```

<!--
- Short position and close opened Long position if any :

```json
Expand All @@ -73,7 +72,7 @@
```json
{ "stub": "dev", "direction": "long", "symbol": "ETH-PERP", "size": "50", "mode": "reverse" }
```

- Close Long position if Short size is greater than opened position :

```json
Expand All @@ -84,5 +83,4 @@

```json
{ "stub": "dev", "direction": "long", "symbol": "ETH-PERP", "size": "50", "mode": "overflow" }
``` -->

```
3 changes: 1 addition & 2 deletions docs/3_Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
curl -d '[{"stub": "test", "symbol": "ETH-PERP", "size": "11", "direction": "long" }, {"stub": "test", "symbol": "BTC-PERP", "size": "11", "direction": "long" }]' -X POST http:https://YOUR.STATIC.IP.ADDRESS/trades -H 'Content-Type: application/json; charset=utf-8'
```

<!--
- __Close a short position__ and __open a long position__ on ETH-PERP using `test` account :

```sh
Expand All @@ -105,4 +104,4 @@

```sh
curl -d '{"stub": "test", "symbol": "ETH-PERP", "direction": "short", "size": "100", "mode": "overflow" }' -X POST http:https://YOUR.STATIC.IP.ADDRESS/trades -H 'Content-Type: application/json; charset=utf-8'
``` -->
```
12 changes: 7 additions & 5 deletions src/services/exchanges/base/futures.exchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
POSITIONS_READ_SUCCESS,
POSITION_READ_SUCCESS
} from '../../../messages/exchanges.messages';
import { OPEN_TRADE_ERROR_MAX_SIZE } from '../../../messages/trading.messages';
import {
OPEN_TRADE_ERROR_MAX_SIZE,
REVERSING_TRADE,
TRADE_OVERFLOW
} from '../../../messages/trading.messages';
import { FuturesPosition } from '../../../types/exchanges.types';
import { getAccountId } from '../../../utils/account.utils';
import { getRelativeOrderSize } from '../../../utils/trading/conversion.utils';
Expand Down Expand Up @@ -98,10 +102,8 @@ export abstract class FuturesExchangeService
if (mode === TradingMode.Reverse) {
await this.handleReverseOrder(account, ticker, trade);
} else if (mode === TradingMode.Overflow) {
const isOverflowing = await this.handleOverflow(account, ticker, trade);
if (isOverflowing) {
return false; // on overflow we only close position
}
// on overflow we only close position so we don't need to open a new trade
return !(await this.handleOverflow(account, ticker, trade));
}
return true;
};
Expand Down
144 changes: 69 additions & 75 deletions src/services/exchanges/ftx.exchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { getAccountId } from '../../utils/account.utils';
import { Exchange, Ticker } from 'ccxt';
import { Side } from '../../constants/trading.constants';
import { IOrderOptions } from '../../interfaces/trading.interfaces';
import { error } from '../logger.service';
import { error, info } from '../logger.service';
import { Trade } from '../../entities/trade.entities';
import { isFTXSpot } from '../../utils/exchanges/ftx.utils';
import { OPEN_TRADE_ERROR_MAX_SIZE } from '../../messages/trading.messages';
import {
OPEN_TRADE_ERROR_MAX_SIZE,
REVERSING_TRADE,
TRADE_OVERFLOW
} from '../../messages/trading.messages';
import { OpenPositionError } from '../../errors/trading.errors';
import { CompositeExchangeService } from './base/composite.exchange.service';
import { IFTXFuturesPosition } from '../../interfaces/exchanges/ftx.exchange.interfaces';
Expand All @@ -17,7 +21,11 @@ import {
getOrderCost,
getRelativeOrderSize
} from '../../utils/trading/conversion.utils';
import { getInvertedSide, getSide } from '../../utils/trading/side.utils';
import {
getInvertedSide,
getSide,
isSideDifferent
} from '../../utils/trading/side.utils';

export class FTXExchangeService extends CompositeExchangeService {
constructor() {
Expand Down Expand Up @@ -102,82 +110,68 @@ export class FTXExchangeService extends CompositeExchangeService {
}
};

handleReverseOrder(
handleReverseOrder = async (
account: Account,
ticker: Ticker,
trade: Trade
): Promise<void> {
throw new Error('Method not implemented.');
}
handleOverflow(
): Promise<void> => {
const { direction } = trade;
const { symbol } = ticker;
const accountId = getAccountId(account);
try {
const position = (await this.getTickerPosition(
account,
ticker
)) as IFTXFuturesPosition;
if (position && isSideDifferent(position.side as Side, direction)) {
info(REVERSING_TRADE(this.exchangeId, accountId, symbol));
await this.closeOrder(account, trade, ticker);
}
} catch (err) {
// ignore throw
}
};

handleOverflow = async (
account: Account,
ticker: Ticker,
trade: Trade
): Promise<boolean> {
throw new Error('Method not implemented.');
}

// handleReverseOrder = async (
// account: Account,
// ticker: Ticker,
// trade: Trade
// ): Promise<void> => {
// const { direction } = trade;
// const accountId = getAccountId(account);
// try {
// const position = (await this.getTickerPosition(
// account,
// ticker
// )) as IFTXFuturesPosition;
// if (position && isSideDifferent(position.side as Side, direction)) {
// info(REVERSING_TRADE(this.exchangeId, accountId, ticker.symbol));
// await this.closeOrder(account, trade, ticker);
// }
// } catch (err) {
// // ignore throw
// }
// };

// handleOverflow = async (
// account: Account,
// ticker: Ticker,
// trade: Trade
// ): Promise<boolean> => {
// const { direction, size } = trade;
// const { symbol, info } = ticker;
// const accountId = getAccountId(account);
// try {
// if (isFTXSpot(ticker)) {
// const balance = await this.getTickerBalance(account, ticker);
// const cost = getOrderCost(ticker, this.exchangeId, balance);
// if (cost && getSide(direction) === Side.Sell && cost < Number(size)) {
// info(TRADE_OVERFLOW(this.exchangeId, accountId, symbol));
// await this.closeOrder(
// account,
// { ...trade, size: balance.toString() },
// ticker
// );
// return true;
// }
// } else {
// const position = (await this.getTickerPosition(
// account,
// ticker
// )) as IFTXFuturesPosition;
// const { side, cost } = position;
// if (
// position &&
// isSideDifferent(side as Side, direction) &&
// Number(size) > Math.abs(Number(cost))
// ) {
// info(TRADE_OVERFLOW(this.exchangeId, accountId, symbol));
// await this.closeOrder(account, trade, ticker);
// return true;
// }
// }
// } catch (err) {
// // ignore throw
// }
// return false;
// };
): Promise<boolean> => {
const { direction, size } = trade;
const { symbol } = ticker;
const accountId = getAccountId(account);
try {
if (isFTXSpot(ticker)) {
const balance = await this.getTickerBalance(account, ticker);
const cost = getOrderCost(ticker, this.exchangeId, balance);
if (cost && getSide(direction) === Side.Sell && cost < Number(size)) {
info(TRADE_OVERFLOW(this.exchangeId, accountId, symbol));
await this.closeOrder(
account,
{ ...trade, size: balance.toString() },
ticker
);
return true;
}
} else {
const position = (await this.getTickerPosition(
account,
ticker
)) as IFTXFuturesPosition;
const { side, cost } = position;
if (
position &&
isSideDifferent(side as Side, direction) &&
Number(size) > Math.abs(Number(cost))
) {
info(TRADE_OVERFLOW(this.exchangeId, accountId, symbol));
await this.closeOrder(account, trade, ticker);
return true;
}
}
} catch (err) {
// ignore throw
}
return false;
};
}