chrisleekr / binance-trading-bot

Automated Binance trading bot - Trade multiple cryptocurrencies. Buy low/sell high with Grid Trading. Integrated with TradingView technical analysis
MIT License
5.03k stars 1.1k forks source link

allow multiple orders per coin via configuration #158

Closed domnuprofesor closed 3 years ago

domnuprofesor commented 3 years ago

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

chrisleekr commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@elvishp2006 @piscui @domnuprofesor

Sometimes I find myself doing this manually.

I do have a plan to add a manual buy/sell feature - https://github.com/chrisleekr/binance-trading-bot/issues/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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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

Things to consider would be:

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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

Validation

Scenario

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

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.

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.

Let's assume the market changes as below:

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

Let's assume the market changes as below:

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:

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:

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

Let's assume the market changes as below:

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

Sell

Setting UI

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

Sell Scenario

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

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

Let's assume the market changes as below:

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

Let's assume the market changes as below:

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

Let's assume the market changes as below:

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

Let's assume the market changes as below:

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:

Let's assume the market changes as below:

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

Let's assume the market changes as below:

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

The final profit would be

Symbol configuration

elvishp2006 commented 3 years ago

@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 commented 3 years ago

@elvishp2006

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

andferno commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

@andferno

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