Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Grid trade feature #248

Merged
merged 20 commits into from
Jul 29, 2021
Merged

feat: Grid trade feature #248

merged 20 commits into from
Jul 29, 2021

Conversation

chrisleekr
Copy link
Owner

@chrisleekr chrisleekr commented Jul 23, 2021

Description

The bot is currently trading single buy/single sell.
There was a suggestion from @domnuprofesor to allow multiple buy/sell feature #158

This PR is to cover the feature

How to test:

$ git pull
$ git checkout feat/support-grid-trade
$ npm run docker:build
$ docker-compose -f docker-compose.server.yml up -d
$ docker ps -a
$ docker logs binance-bot -f --tail=1000 | bunyan

Note that when you use this branch, there is a migration running over your current configuration.
Once start using this PR, you cannot go back to use the previous version including the master branch.

Todo

This PR is not completed yet as require more features.

  • Save all filled orders to database
  • Allow removing the saved symbol grid trade manually

Related Issue

#158

Motivation and Context

To provide the feature, massive refactoring was required and changes in setting UI.

The features outlines as below:

Buy

Setting UI

  • Number of grids: 2
  • Grids
    No# Trigger Percentage Stop Price Percentage Limit price percentage USDT BTC BUSD
    1 1 1.05 1.051 50 0.001 50 X
    2 0.8 1.03 1.031 100 0.002 50 X

Validation

  • Buy amount cannot be less than the minimum notional value.
  • Only 1st trigger percentage can be above or equal to 1.

Scenario

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

  • Grid No#: 1
  • Trigger percentage: 1
  • Stop percentage: 1.05
  • Limit percentage: 1.051
  • Buy amount: 50 USDT

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.

Let's assume the current price is $105 for 1 COIN.

  • Current price: $105
  • Lowest price: $100
  • Trigger price: $100

When the current price is down 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.

  • Stop price: $100 * 1.05 = $105
  • Limit price: $100 * 1.051 = $105.1
  • Quantity: 0.47573

Let's assume the market changes as below:

  • Current price: $95

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

  • Stop price: $95 * 1.05 = $99.75
  • Limit price: $95 * 1.051 = $99.845
  • Quantity: 0.5

Let's assume the market changes as below:

  • Current price: $100

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.

Once the coin is purchased, the bot will start monitoring the sell signal or the next grid trading for buying.

Your 2nd grid trading for buying is configured as below:

  • Grid#: 2
  • Current last buy price: $99.845
  • Trigger percentage: 0.8
  • Stop percentage: 1.03
  • Limit percentage: 1.031
  • Buy amount: $100

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.

Let's assume the market changes as below:

  • 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

Sell

Setting UI

  • 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 X
    2nd 1.08 0.95 0.949 1 X

Unlike buy, the sell setting will use the percentage of a quantity. If you want to sell all of your coin quantity, then simply configure it as 100.

Validation

  • The trigger percentage cannot be lower than the previous trigger percentage.
  • If the grid's sell amount percentage is 1, then a new grid cannot be added.
  • If the last grid's sell amount percentage is not 1, then display the warning The bot will not sell all quantities. If you want to sell all quantities, configure as 1 for the last grid.

Sell Scenario

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 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

Let's assume the market changes as below:

  • Current price: $87

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).

  • Current quantity: 0.895
  • Current last buy price: 83.80 USDT

Let's assume the market changes as below:

  • Current price: $91

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:

  • Stop price: $91 * 0.95 = $86.45
  • Limit price: $91 * 0.949 = $86.359
  • Quantity: 0.895

Let's assume the market changes as below:

  • Current price: $100

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

  • Stop price: $100 * 0.95 = $95
  • Limit price: $100 * 0.949 = $94.9
  • Quantity: 0.895

Let's assume the market changes as below:

  • Current price: $94

Then the bot will execute 2nd sell for the coin.

The final profit would be

  • 1st sell: $94.9 * 0.895 = $84.9355
  • 2nd sell: $87.21 * 0.895 = $78.05295
  • 162 USDT (8% profit)

Symbol configuration

  • The symbol configuration should allow overriding the grid setting.
  • The symbol configuration should allow setting executed status manually.

How Has This Been Tested?

I am currently testing this feature and fixed some unforeseen issues.
It is now fairly stable. However, I will continue to test until I am confident with stability.

Welcome to test and suggest if you find any issue.

Screenshots (if appropriate):

Global Setting UI - Buy Global Setting UI - Sell Symbol Setting UI
image image image
Frontend 1 Frontend 2
image image

@chrisleekr chrisleekr added the enhancement New feature or request label Jul 23, 2021
@chrisleekr chrisleekr linked an issue Jul 23, 2021 that may be closed by this pull request
@chrisleekr chrisleekr self-assigned this Jul 23, 2021
Copy link
Contributor

@habibalkhabbaz habibalkhabbaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @chrisleekr for this big update!
I added some comments before testing, they are just enhancements and not issues.

app/cronjob/trailingTrade/step/determine-action.js Outdated Show resolved Hide resolved
app/cronjob/trailingTrade/step/place-buy-order.js Outdated Show resolved Hide resolved
Copy link
Contributor

@habibalkhabbaz habibalkhabbaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am testing the feature now, and it's working great until now.
However, I figured out these small issues that need to be fixed.

public/js/SymbolSettingIconGridBuy.js Outdated Show resolved Hide resolved
public/js/SymbolSettingIconGridBuy.js Outdated Show resolved Hide resolved
public/js/SettingIconGridBuy.js Outdated Show resolved Hide resolved
public/js/SettingIconGridBuy.js Outdated Show resolved Hide resolved
@habibalkhabbaz
Copy link
Contributor

habibalkhabbaz commented Jul 27, 2021

Thanks a lot @chrisleekr, I like your updates.
However, there is a bug when changing grid trades in the global settings, they will override the customized symbol grid trades.

Steps to reproduce the bug:

  1. Add (one) grid trade for one symbol for buy & sell and customize them.
  2. Add multiple grid trades for buy & sell in the global settings and apply them.
  3. Check the customized grid trades of that symbol, you will see they are overridden.

@chrisleekr
Copy link
Owner Author

chrisleekr commented Jul 27, 2021

@habibalkhabbaz
Another good find. Let me fix it up.

Updated:

I fixed up and added more tests for those cases.
Let me know if you find any issues.

@habibalkhabbaz
Copy link
Contributor

@habibalkhabbaz
Another good find. Let me fix it up.

Updated:

I fixed up and added more tests for those cases.
Let me know if you find any issues.

Just updated my local branch and it seems working very well!
I will let you know when I find something else.

@habibalkhabbaz
Copy link
Contributor

Finally we have sorting 😍 , let me test this

Copy link
Contributor

@habibalkhabbaz habibalkhabbaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested the latest update and I found these small issues.

public/js/AppSorting.js Outdated Show resolved Hide resolved
public/js/AppSorting.js Outdated Show resolved Hide resolved
if (s.sell.difference) {
return (s.sell.difference + 1000) * -10;
}
return s.buy.difference;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment in line 26

@habibalkhabbaz
Copy link
Contributor

habibalkhabbaz commented Jul 28, 2021

Oh, I didn't see the latest update.
However, why we don't use + for the numbers instead of conditions. It make sense, doesn't ?

@chrisleekr
Copy link
Owner Author

chrisleekr commented Jul 28, 2021

Oh, I didn't see the latest update.
However, why we don't use + for the numbers instead of conditions. It make sense, doesn't ?

The reason I don't use + is that it returns null as a value. It will give you the wrong order such as the below image. Below image is for the sort Sell Profit (ASC).

image

The correct order would be like below:

image

It is achieved by adding the condition as below:

s.sell.currentProfitPercentage !== null
        ? s.sell.currentProfitPercentage
        : direction === 'asc'
        ? 999
        : -999,

And lodash order by _.orderBy should handle string/number correctly if I am not wrong.

@chrisleekr chrisleekr linked an issue Jul 28, 2021 that may be closed by this pull request
@chrisleekr
Copy link
Owner Author

chrisleekr commented Jul 29, 2021

I think this PR is pretty stable. I will release it tomorrow or this weekend.
Actually, let me release now.

@chrisleekr chrisleekr merged commit 5df1cda into master Jul 29, 2021
@elvishp2006
Copy link

Hey @chrisleekr, it's already closed, but maybe I have found a strange situation, two buy orders have been executed to MATIC, but it's showing that only the first grid was executed

image

image

@chrisleekr
Copy link
Owner Author

@elvishp2006 Let's open new issue and follow from there.

@elvishp2006
Copy link

Done @chrisleekr #252

@chrisleekr chrisleekr deleted the feat/support-grid-trade branch July 30, 2021 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UX issue with coin sorting allow multiple orders per coin via configuration
3 participants