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

allow multiple orders per coin via configuration #158

Closed
domnuprofesor opened this issue May 7, 2021 · 19 comments · Fixed by #248
Closed

allow multiple orders per coin via configuration #158

domnuprofesor opened this issue May 7, 2021 · 19 comments · Fixed by #248
Labels
enhancement New feature or request

Comments

@domnuprofesor
Copy link

right now the bot operates with only one BUY order per coin followed by an immediate SELL order. If the SELL order gets stuck then potential BUY opportunities may be missed because the bot waits to SELL the previous BUY order.

So I propose to allow for multiple concurrent BUY|SELL orders per coin.

Looking forward to your opinion.

Thanks,
Francisc

@domnuprofesor domnuprofesor added the enhancement New feature or request label May 7, 2021
@chrisleekr
Copy link
Owner

Hmm, interesting.

But wouldn't be really risky?

If the coin is falling continuously, then you end up using all your money to the single coin, rather than buying multiple coins to ease the risk.

What do you think?

@domnuprofesor
Copy link
Author

I don't think it will be risky and of course the user is in control.
The user can enable this behavior.

As I said, I think that in-between BUY-SELL cycle there are opportunities that are lost and we can prevent this with multiple orders per coin.

I would really like to have this option and congrats for your work.

@chrisleekr
Copy link
Owner

Hmm... This feature will be very interesting.

Let's say, the bot purchased the coin because it reached the lowest price
In the next tick, the coin price falls, the bot will still purchase because it is still the lowest price.
In the next tick, again, the bot will buy again.

I can add a cool time for the next buy, but it also should automatically disable stop-loss.

Hmm, will this feature be required by many people since you can do it manually?

@elvishp2006
Copy link

elvishp2006 commented May 9, 2021

I support this idea, for example, im holding BNB, and it has a $ 100,00 as max purchase, but it's used as transaction discount, and I wanna keep buying it even if I already have $ 90,00, if the price drops, buy another $ 10,00

@piscui
Copy link

piscui commented May 10, 2021

Sometimes I find myself doing this manually. This is, once the bot has executed a buy order, I place a manual order directly on Binance and then override the buy price on the bot with the new average.

If you really think the coin (or whatever you're trading for that matter) will go up in price eventually, then it is not uncommon to keep buying the dips to average down.

I'm not sure if I'd like the bot to do this fully automated though, but it would be cool to have a button or something to place an additional order (perhaps with an option to override the amount) where the bot will repeat the process of chasing down the price and, once executed, then automate the calculation of the new average as I described above.

@chrisleekr
Copy link
Owner

@elvishp2006 @piscui @domnuprofesor

Sometimes I find myself doing this manually.

I do have a plan to add a manual buy/sell feature - #100

I support this idea, for example, im holding BNB, and it has a $ 100,00 as max purchase, but it's used as transaction discount, and I wanna keep buying it even if I already have $ 90,00, if the price drops, buy another $ 10,00

Interesting, so basically you calculate the last buy price manually or just leave it to the last buy price as the initial buy price?

I'm not sure if I'd like the bot to do this fully automated though

If the feature is added, then it would be optional.

I just have few concerns to develop this feature.

  1. I will need to track the buy order is executed. Currently, it is not tracking the order status due to the API limit. Need to find a smart way to track it.
  2. How to make it configurable for this feature? I mean will this be like?
  • Initial purchase options
    • Trigger percentage
    • Stop price percentage
    • LImit price percentage
  • Interval trading options:
    • Trigger percentage from the last buy price: 0.9
    • Purchase amount: 100

Should the interval trading options be multiple? or just single?
(I just found on Google this strategy is called Interval Trading(Scale Trading)

I will need more advice from you guys.

@piscui
Copy link

piscui commented May 10, 2021

@chrisleekr I believe what you're referring to is also called grid trading. Binance has this built in for futures, but with other services like Bitsgap you can do it with spot as well.

I've actually been playing with it a bit (you get a 14 day trial) and although it is more hands off, it may take a much longer time to get any significant profits. It makes tons of very small transactions and requires you to allocate greater amounts of money since it places a whole bunch of limit orders (both buy and sell) when the bot starts; anything less than $500 per bot/pair is not really worth it.

For me, honestly, your bot works much better. I don't let it run completely in auto-pilot but with a little supervision you can get better results in my opinion.

Also:

I will need to track the buy order is executed.

Probably not. You already track the last buy price, and you know the quantity. With that, plus the new buy's price and quantity you can calculate the overall price average.

For example say you initially bought 100 coins at 1.50 USDT.
Then you place a new order and buy 200 coins at 1.30 USDT.
When the order is executed then:

  1. Fetch the current balance, and stored last buy price
  2. Calculate the new averaged buy price:
    new averaged buy price = ((previous balance * stored buy price) + (amount of coins bought * new buy price)) / (previous balance + amount of coins bought)
    eg.: ((100 * 1.50) + (200 * 1.30)) / (100 + 200) = 1.37 USDT
  3. Replace last buy price with new average price in the db

The only way it wouldn't work is if you added manual buys from the binance interface like I outlined in my previous comment (which I would no longer do if I could do it from within the bot, of course).
There may also be a very tiny additional balance for "previous balance" left over from previous transactions, but that's probably not a big deal.

I hope that helps.

@andferno
Copy link

First, thank you for your great work. It's amazing and very useful.
This is a great feature to include in this project. Some times I'm forced to use Binance trading system (like @piscui does) to buy when price drops abruptly after a small increase that triggers the stop-limit order. In real life (like 2 hours ago), when all cryptos went crazy and dropped like 10% to 15%. I missed some buys during that period and was unable to track "support price" manually.
Grid trading is good, but slow increasing profit, and it's because selling orders are placed above current price and buy orders under current price. That strategy doesn't wait for "support" price or "resistance" one (I mean the lower and upper prices). Mixing your bot strategy with a grid would be awesome.

Should the interval trading options be multiple? or just single?

You could request Trigger percentage, Purchase amount and Number of grids. That last one would be the number of times that trigger will be executed, and Purchase amount could be per grid or total buying amount for all the grids (it should be divided by number of grids to place each order).

Maybe you can include this same behavior for selling orders. Requesting Trigger percentage, Selling amount and Number of grids. It would work as buying case, but Selling amount (or total amount for the grid) should be reserved or locked to be able to sell something. I realized that selling orders are for the total amount you have in that currency (limit isn't respected).

Of course this options should be optional.

@chrisleekr
Copy link
Owner

chrisleekr commented May 11, 2021

@piscui @andferno Thanks for your input, guys.

Fetch the current balance, and stored last buy price
Calculate the new averaged buy price:
new averaged buy price = ((previous balance * stored buy price) + (amount of coins bought * new buy price)) / (previous balance + amount of coins bought)
eg.: ((100 * 1.50) + (200 * 1.30)) / (100 + 200) = 1.37 USDT
Replace last buy price with new average price in the db

This sounds right. Even for manual recording the last buy price, I can save the balance of the time you record the last buy price.

You could request Trigger percentage, Purchase amount and Number of grids. That last one would be the number of times that trigger will be executed, and Purchase amount could be per grid or total buying amount for all the grids (it should be divided by number of grids to place each order).

Maybe you can include this same behavior for selling orders. Requesting Trigger percentage, Selling amount and Number of grids. It would work as buying case, but Selling amount (or total amount for the grid) should be reserved or locked to be able to sell something. I realized that selling orders are for the total amount you have in that currency (limit isn't respected).

Good good. So by setting Number of grids, you may configure something like below:

  • Number of grid: 3
  • First grid
    • Trigger percentage: 1
    • Purchase amount: 50
  • Second grid
    • trigger percentage: 0.8 (-20%) of the last buy price
    • Purchase amount: 50
  • Third grid
    • trigger percentage: 0.8 (-20%) of the average of first and second grid buy.
    • Purchase amount: 50

Things to consider would be:

  • Must be optional
  • Default number of grid will be 1, which is effectively same as now.
  • Record each purchase
  • When you don't have enough balance, grid purchase will not be executed.
  • Display grid purchase history
  • This strategy can apply to sell as well.

Sounds right? Let me know if I misunderstood.

I will list up all Todo list and make a vote for the next development once I merge in PR 77.
Then I would know what feature to develop next.

@andferno
Copy link

Happy to contribute @chrisleekr .

Good good. So by setting Number of grids, you may configure something like below:

Number of grid: 3
First grid
Trigger percentage: 1
Purchase amount: 50
Second grid
trigger percentage: 0.8 (-20%) of the last buy price
Purchase amount: 50
Third grid
trigger percentage: 0.8 (-20%) of the average of first and second grid buy.
Purchase amount: 50

That's more advanced that my proposal, but awesome. I was thinking in arithmetic or geometric slicing of the grid, so a number of grids are provided and total amount reserved for the grid is divided automatically into pieces. Percentage used could be the same for all grids (the lower the price is, more cryptos are bought for the same money) or customizable. But being able to edit each grid amount and percentage is great.

Sounds right? Let me know if I miss understood.

As I said, sounds awesome. You have my vote for this.

@piscui
Copy link

piscui commented May 11, 2021

I agree with @andferno, this really sounds awesome. It's similar to Bitsgap but with a different approach which in my opinion works better and brings the better of both worlds.

Thanks for your input, guys.

Thank YOU @chrisleekr for you hard work. I can't wait to try all the goodies you have lined up.

By the way I've been running #77 for a while and it's looking great. I had to stop using it at first because it crashed a few times and was using a ton of CPU, but whatever you did on the last update fixed both and it's been smooth sailing since.

@chrisleekr
Copy link
Owner

chrisleekr commented May 11, 2021

Thanks for your input, guys. I really appreciated it.

@piscui Interesting. The last commit was just updating the frontend for showing API limit. But hey, it is fixed :yay:

@piscui
Copy link

piscui commented May 11, 2021

@chrisleekr I'm sorry it probably wasn't the very last commit, but several commits back. The crashy version didn't have the stop-loss feature so it must have been a commit somewhere between before and after that feature was added that fixed the issues I was having.
Anyway yeah it's running smooth now so let's ride with that!

@chrisleekr
Copy link
Owner

Hey guys,

I am still trying to design how to implement this strategy.

I did some brainstorming as below. I tried to think easier way to implement, I guess it should be complex because the strategy itself is complex.

If you find any issue with the below calculation, please let me know.

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

@elvishp2006
Copy link

@chrisleekr sorry for the late reply, it seems good to me, we can implement like this and test. Since the market is falling, my last buy was more that 14 days ago, and none sell since that, I think with that strategy we can make some profit even with a falling market

@chrisleekr
Copy link
Owner

chrisleekr commented Jul 21, 2021

@elvishp2006

I finished development and I am currently testing the feature. I will push the commit once I think fairly stable.

@andferno
Copy link

@chrisleekr I believe its a very good description of this feature. We'll have to test.

I was thinking about some improvements like filling buying and selling table automatically. The only needed information for this would be number of grids, starting percentage, final percentage/spacing/common ratio and arithmetic or geometrical spacing between grids. So if arithmetic, it will calculate percentage this way startPercentage+((finalPercentage-startPercentage)/numGrids)(gridNumber-1) or spacing(commonRatio) could be provided and it will calculate startPercentage+commonRatio(gridNumber-1). If geometric, calculating commonRatio (exp(finalPercentage/startPercentage, numGrids-1)) and multiplying it by current grid percentage (actually it would be the difference from 1 or current price). It's numGrids-1 because we already have startPercentage and finalPercentage values.

Don't know if my explanation has any sense. Please comment.

@chrisleekr chrisleekr linked a pull request Jul 23, 2021 that will close this issue
2 tasks
@chrisleekr
Copy link
Owner

Hi guys,

I have committed the feature to PR #248.

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.

All the details are described in the PR.

@chrisleekr
Copy link
Owner

@andferno

Let's discuss your strategy after this feature is merged.

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 a pull request may close this issue.

5 participants