Skip to content

Commit

Permalink
feat: Grid trade feature (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisleekr authored Jul 29, 2021
1 parent 7229df4 commit 5df1cda
Show file tree
Hide file tree
Showing 77 changed files with 42,743 additions and 6,112 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ BINANCE_LOCAL_TUNNEL_SUBDOMAIN=default
## Feature Toggles
BINANCE_FEATURE_TOGGLE_NOTIFY_ORDER_CONFIRM=true
BINANCE_FEATURE_TOGGLE_NOTIFY_DEBUG=false
BINANCE_FEATURE_TOGGLE_NOTIFY_ORDER_EXECUTE=true
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
## Unreleased

- Make the local tunnel to be disabled by default. Thanks [@pedrohusky](https://github.com/pedrohusky)
- Support Grid strategy for buy/sell to mitigate loss/increasing profit - [#158](https://github.com/chrisleekr/binance-trading-bot/issues/158)
- Add frontend option to disable sorting or improve sorting - [#244](https://github.com/chrisleekr/binance-trading-bot/issues/244)

## [0.0.72] - 2021-07-07

Expand Down
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ FROM node:14-alpine AS dev-stage

RUN apk add --no-cache make gcc g++ python

# Add configuration files
COPY image-files/ /

WORKDIR /srv

COPY package*.json ./
Expand All @@ -23,6 +26,8 @@ ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}
LABEL com.chrisleekr.binance-trading-bot.node-env=${NODE_ENV}

ENTRYPOINT [ "docker-entrypoint.sh" ]

CMD [ "npm", "run", "dev" ]

# build stage
Expand All @@ -49,8 +54,13 @@ ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
LABEL com.chrisleekr.binance-trading-bot.node-env=${NODE_ENV}

# Add configuration files
COPY image-files/ /

WORKDIR /srv

COPY --from=build-stage /srv /srv

ENTRYPOINT [ "docker-entrypoint.sh" ]

CMD [ "npm", "start"]
205 changes: 137 additions & 68 deletions README.ko.md

Large diffs are not rendered by default.

215 changes: 144 additions & 71 deletions README.md

Large diffs are not rendered by default.

217 changes: 146 additions & 71 deletions README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,122 +33,197 @@
>
> 简单来说就是在价格变化时,按固定的数值或百分比进行委托。利用这个特性可以在下跌买入时买到尽可能低的价格,在上涨卖出时卖出最高的价格。
- 该机器人可以监测多个代币,并对每个代币的价格进行每秒监测。
- 这个机器人只在 USDT 交易对中测试过,比如 BTC/USDT,ETH/USDT。你也可以添加其他与法币锚定的稳定币,比如 BUSD、AUD。但是我并没有使用这些稳定币在生产环境测试过,请自行承担风险。
- 该机器人使用 MongoDB 数据库。然而,它并没有没有使用最新的 MongoDB 来支持Raspberry Pi 32bit,使用的 MongoDB 版本是3.2.20,这是由 [apcheamitru](https://hub.docker.com/r/apcheamitru/arm32v7-mongo) 提供的。
- The bot supports multiple buy/sell orders based on the configuration.
- The bot can monitor multiple symbols. All symbols will be monitored per second.
- The bot is using MongoDB to provide a persistence database. However, it does not use the latest MongoDB to support Raspberry Pi 32bit. Used MongoDB version
is 3.2.20, which is provided by [apcheamitru](https://hub.docker.com/r/apcheamitru/arm32v7-mongo).
- The bot is tested/working with Linux and Raspberry Pi 4 32bit. Other platforms are not tested.

#### 买入信号(Buy Signal)

该机器人将持续监测一段时间内的最低值。一旦当前价格达到最低价格,那么机器人将下达买入的 "止损限价单(STOP-LOSS-LIMIT)"指令。如果当前价格持续下跌。那么机器人将取消之前的订单,并重新放置新的"止损限价单(STOP-LOSS-LIMIT)"订单。
The bot will continuously monitor the coin based on the grid trade configuration.

- 当达到卖出的触发价格时,如果有足够的持仓(通常超过10美元),机器人就不会再下买单。
For grid trade #1, the bot will place a STOP-LOSS-LIMIT order to buy when the current price reaches the lowest price. If the current price continuously falls, then the bot will cancel the previous order and re-place the new STOP-LOSS-LIMIT order with the new price.

After grid trade #1, the bot will monitor the COIN based on the last buy price.

- The bot will not place a buy order of the grid trade #1 if has enough coin (typically over $10 worth) to sell when reaches the trigger price for selling.
- The bot will remove the last buy price if the estimated value is less than the last buy price removal threshold.

##### 买入方案

比方说,使用如下买入配置设置:
Let say, if the buy grid trade configurations are set as below:

- Number of grids: 2
- Grids
| No# | Trigger Percentage | Stop Price Percentage | Limit price percentage | USDT |
| --- | ------------------- | --------------------- | ---------------------- | ---- |
| 1 | 1 | 1.05 | 1.051 | 50 |
| 2 | 0.8 | 1.03 | 1.031 | 100 |

To make it easier to understand, I will use `$` as a USDT symbol. For the simple calculation, I do not take an account for the commission. In real trading, the quantity may be different.

Your 1st grid trading for buying is configured as below:

- Grid No#: 1
- Trigger percentage: 1
- Stop percentage: 1.05 (5.00%)
- Limit percentage: 1.051 (5.10%)
- Max purchase amount: $50

And the market is as below:

- 最大购买金额(Maximum purchase amount): $50
- 触发百分比(Trigger percentage): 1.005 (0.5%)
- 止损百分比(Stop price percentage): 1.01 (1.0%)
- 限价百分比(Limit price percentage): 1.011 (1.1%)
- Current price: $105
- Lowest price: $100
- Trigger price: $100

市场情况如下:
When the current price is falling to the lowest price ($100) and lower than ATH(All-Time High) restricted price if enabled, the bot will place new STOP-LOSS-LIMIT order for buying.

- 市场价: $101
- 限价: $100
- 触发价: $100.5
- Stop price: $100 * 1.05 = $105
- Limit price: $100 * 1.051 = $105.1
- Quantity: 0.47573

现在机器人不会下单,因为触发价(100.5)低于现价(101)
Let's assume the market changes as below:

在下一次市场成交后(next tick),市场情况现在如下:
- Current price: $95

- 市场价: $100
- 限价: $100
- 触发价: $100.5
Then the bot will follow the price fall and place new STOP-LOSS-LIMIT order as below:

现在,机器人会新建一个限价止损单来买入,因为现价(\$100)已经低于了触发价(\$100.5)。为了简化计算,这里没有考虑佣金。在真实的交易中,数量会有不同。新的买入订单会被执行如下:
- Stop price: $95 * 1.05 = $99.75
- Limit price: $95 * 1.051 = $99.845
- Quantity: 0.5

- 止损价 Stop price: \$100 \* 1.01 = $101
- 限价 Limit price: \$100 \* 1.011 = $101.1
- 数量: 0.49
Let's assume the market changes as below:

在下一次市场成交后(next tick),市场情况现在如下:
- Current price: $100

- 现价: $99
- 当前限价 Current limit price: $99 \* 1.011 = 100.089
- 未平仓止损价 Open order stop price: $101
Then the bot will execute 1st purchase for the coin. The last buy price will be recorded as `$99.845`. The purchased quantity will be `0.5`.

由于当前的未平仓止损价 101 高于当前限价 100.089,机器人会取消当前的订单并按以下方式重新下单
Once the coin is purchased, the bot will start monitoring the sell signal and at the same time, monitor the next grid trading for buying.

- 止损价 Stop price: \$99 \* 1.01 = $99.99
- 限价 Limit price: \$99 \* 1.011 = $100.089
- 数量 Quantity: 0.49
Your 2nd grid trading for buying is configured as below:

如果价格不断下跌,这个新的买单也会不断更新报价。
- Grid#: 2
- Current last buy price: $99.845
- Trigger percentage: 0.8 (20%)
- Stop percentage: 1.03 (3.00%)
- Limit percentage: 1.031 (3.10%)
- Max purchase amount: $100

接下来在一次市场成交后(next tick),市场情况如下:
And if the current price is continuously falling to `$79.876` (20% lower), then the bot will place new STOP-LOSS-LIMIT order for the 2nd grid trading for the coin.

- 现价 Current price: $100
Let's assume the market changes as below:

现在当前价格到了止损价(99.99),因此,买单将会按限价 100.089 报价下单。
- Current price: $75

Then the bot will follow the price fall and place new STOP-LOSS-LIMT order as below:

- Stop price: $75 * 1.03 = $77.25
- Limit price: $75 * 1.031 = $77.325
- Quantity: 1.29

Let's assume the market changes as below:

- Current price: $78

Then the bot will execute 2nd purchase for the coin. The last buy price will be automatically re-calculated as below:

- Final last buy price: ($50 + $100)/(0.5 COIN + 1.29 COIN) = $83.80

### 卖出信号

如果有足够的余额用于卖出,并且最后的买入价格已被记录,那么机器人将开始监测卖出信号。一旦当前价格达到触发价格,机器人将下达一个止损限价单来卖出。如果当前价格持续上涨,那么机器人将取消之前的订单,并重新以新的价格重新下新的止损限价单。
If there is enough balance for selling and the last buy price is recorded in the bot, then the bot will start monitoring the sell signal of the grid trade #1. Once the current price reaches the trigger price of the grid trade #1, then the bot will place a STOP-LOSS-LIMIT order to sell. If the current price continuously rises, then the bot will cancel the previous order and re-place the new STOP-LOSS-LIMIT order with the new price.

- 如果该币的价值低于10美元(最低名义价值),那么机器人将删除最后的买入价格,因为Binance不允许下低于10美元的订单。
- 如果机器人没有最后买入价格的记录,机器人将不会卖出该币。
- If the bot does not have a record for the last buy price, the bot will not sell the coin.
- If the coin is worth less than the last buy price removal threshold, then the bot will remove the last buy price.
- If the coin is not worth than the minimum notional value, then the bot will not place an order.

#### 卖出方案

比方说,使用如下卖出配置设置:
Let say, if the sell grid trade configurations are set as below:

- Number of grids: 2
- Grids
| No# | Trigger Percentage | Stop Price Percentage | Limit price percentage | Sell Quantity Percentage |
| --- | ------------------- | --------------------- | ---------------------- |------------------------- |
| 1st | 1.05 | 0.97 | 0.969 | 0.5 |
| 2nd | 1.08 | 0.95 | 0.949 | 1 |

Unlike buy, the sell configuration will use the percentage of a quantity. If you want to sell all of your coin quantity, then simply configure it as `1` (100%).

From the last buy actions, you now have the following balances:

- Current quantity: 1.79
- Current last buy price: $83.80

Your 1st grid trading for selling is configured as below:

- Grid No# 1
- Trigger percentage: 1.05
- Stop price percentage: 0.97
- Limit price percentage: 0.969
- Sell amount percentage: 0.5

Let's assume the market changes as below:

- Current price: $88

As the current price is higher than the sell trigger price($87.99), then the bot will place new STOP-LOSS-LIMIT order for selling.

- Stop price: $88 * 0.97 = $85.36
- Limit price: $88 * 0.969 = $85.272
- Quantity: 0.895

Let's assume the market changes as below:

- Current price: $90

Then the bot will follow the price rise and place new STOP-LOSS-LIMIT order as below:

- Stop price: $90 * 0.97 = $87.30
- Limit price: $90 * 0.969 = $87.21
- Quantity: 0.895

- 触发百分比 Trigger percentage: 1.05 (5.0%)
- 止损百分比 Stop price percentage: 0.98 (-2.0%)
- 限价百分比 Limit price percentage: 0.979 (-2.1%)
Let's assume the market changes as below:

同时市场行情如下:
- Current price: $87

- 剩余代币数量 Coin owned: 0.5
- 市场价 Current price: $100
- Last buy price: $100
- 触发价 Trigger price: \$100 \* 1.05 = $105
Then the bot will execute 1st sell for the coin. Then the bot will now wait for 2nd selling trigger price ($83.80 * 1.08 = $90.504).

这时机器人将不会提交订单,因为触发价(\$105)低于市场价(\$100)
- Current quantity: 0.895
- Current last buy price: $83.80

如果价格持续走低,机器会持续监控价格,直到价格达到触发价
Let's assume the market changes as below:

接下来在一次市场成交后(next tick),市场行情如下:
- Current price: $91

- 市场价 Current price: $105
- 触发价 Trigger price: $105
Then the current price($91) is higher than 2nd selling trigger price ($90.504), the bot will place new STOP-LOSS-LIMIT order as below:

机器人将提交新的止损限价单进行卖出,因为当前的价格(\$105)高于或等于触发价格(\$105)。
为了简化计算,我没有考虑佣金。在实际交易中,数量可能会有所不同。新的卖单将被提交如下:
- Stop price: $91 * 0.95 = $86.45
- Limit price: $91 * 0.949 = $86.359
- Quantity: 0.895

- 止损价 Stop price: \$105 \* 0.98 = $102.9
- 限价 Limit price: \$105 \* 0.979 = $102.795
- 数量 Quantity: 0.5
Let's assume the market changes as below:

接下来在一次市场成交后(next tick),市场情况如下:
- Current price: $100

- 市场价 Current price: $106
- 当前限价 Current limit price: $103.774
- 开仓止损价 Open order stop price: $102.29
Then the bot will follow the price rise and place new STOP-LOSS-LIMT order as below:

由于未平仓订单的止损价格(\$102.29)低于当前的限价(\$103.774),机器人将取消未平仓订单,并提交新的止损限价单,如下所示:
- Stop price: $100 * 0.95 = $95
- Limit price: $100 * 0.949 = $94.9
- Quantity: 0.895

- 止损价 Stop price: \$106 \* 0.98 = $103.88
- 限价 Limit price: \$106 \* 0.979 = $103.774
- 数量 Quantity: 0.5
Let's assume the market changes as below:

如果市场价不断升高,新的卖单也会更新报价。
- Current price: $94

如果市场价格变化:
Then the bot will execute 2nd sell for the coin.

- 市场价 Current price: $103
The final profit would be

目前的价格达到了止损价(\$103.88);因此,订单将以限价($103.774)执行。
- 1st sell: $94.9 * 0.895 = $84.9355
- 2nd sell: $87.21 * 0.895 = $78.05295
- Final profit: $162 (8% profit)

### [功能](https://github.com/chrisleekr/binance-trading-bot/wiki/Features)

Expand Down Expand Up @@ -246,11 +321,11 @@

| 前端 移动端 | 设置 | 手工交易 |
| --------- | ---- | ------ |
| ![Frontend Mobile](https://user-images.githubusercontent.com/5715919/124752399-262e5f00-df6b-11eb-9dc1-e8f06b98aa9a.png) | ![Setting](https://user-images.githubusercontent.com/5715919/124752414-2890b900-df6b-11eb-90f4-7fa79a84bf1d.png) | ![Manual Trade](https://user-images.githubusercontent.com/5715919/124752425-2c244000-df6b-11eb-97d9-d81e494d7e40.png) |
| ![Frontend Mobile](https://user-images.githubusercontent.com/5715919/127318555-31216c7e-f27c-4e05-a3b1-1ebda386e439.png) | ![Setting](https://user-images.githubusercontent.com/5715919/127318581-4e422ac9-b145-4e83-a90d-5c05c61d6e2f.png) | ![Manual Trade](https://user-images.githubusercontent.com/5715919/127318630-f2180e1b-3feb-48fa-a083-4cb7f90f743f.png) |

| Frontend Desktop |
| ------------------------------------------------------------------------------------------------------------------------- |
| ![Frontend Desktop](https://user-images.githubusercontent.com/5715919/124752605-668ddd00-df6b-11eb-887b-8cf79048d798.png) |
| ![Frontend Desktop](https://user-images.githubusercontent.com/5715919/127318831-1cbfab93-6300-4251-b757-7d51eb5fbc2d.png) |

### Sample Trade

Expand Down
Loading

0 comments on commit 5df1cda

Please sign in to comment.