-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: support of manual trades in breakeven amount calculator #601
feat: support of manual trades in breakeven amount calculator #601
Conversation
Nice work @rando128 I will need to write test cases. I will get it done when I have time. |
@rando128 I am still writing the test case. I found my old test case was just not right. It's very wrong. |
No worries. I'll push an update because I was missing the conservative mode and one variable was not properly handled. I should commit before tomorrow. |
@rando128 Ok no problem. I am refactoring the test to update quickly for changes. |
highestPrice: 10000, | ||
lowestPrice: 8893.03, | ||
athPrice: 9000, | ||
athRestrictionPrice: 8100, | ||
nextBestBuyAmount: 204983.33333333314, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rando128
I have a question about the next best buy amount, but it seems your calculation is correct.
So the parameters were as below:
-
Last buy price: 9966.66666666667
-
Last closed: 8900
-
Buy trigger: 0.8929765886287623
-
Total bought quantity: 1.5
-
Total bought amount: 19900
-
Next best buy amount => 204983.33333333314
And I feel like this is a lot of money to buy for next. So I did a calculation.
Current price | 8900 | |||
---|---|---|---|---|
Grid Trade # | Amount | Quantity | Quote Amount | Last buy price |
#1 | 10000 | 1 | 10000 | |
#2 | 9900 | 0.5 | 4950 | |
Sum | 1.5 | 14950 | 9966.66666666667 | |
Next Best Buy amount | 8900 | 23.0318352059925 | 204983.333333333 | |
24.5318352059925 | 219933.333333333 | 8965.2213740458 |
Is my calculation correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chrisleekr, what is your sellTrigger (i.e expected market rebound) for this calculation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rando128 After your update, the calculation seems to be changed.
You can see the test cases how it's configured for the parameters.
All scenarios are now having nextBestBuyCalculation
.
Particularly, I was interested in the following scenarios.
when buy grid trade index is 1 after executing the 1st grid trade
Code:
binance-trading-bot/app/cronjob/trailingTrade/step/__tests__/get-indicators.test.js
Line 671 in 590e0f3
describe('when buy grid trade index is 1 after executing the 1st grid trade', () => { |
nextBestBuyAmount: -10589.999999999984,
nextBestBuyCalculation: {
buyTrigger: 1.01,
currentPrice: 8900.05,
hasObviousManualTrade: false,
isSingleSellGrid: true,
lastBuyPrice: 9000,
sellTrigger: 1.06,
totalBoughtAmount: 9000,
totalBoughtQty: 1
},
when buy grid trade index is 2 with conservative mode enabled
Code:
binance-trading-bot/app/cronjob/trailingTrade/step/__tests__/get-indicators.test.js
Line 2236 in 590e0f3
describe('when buy grid trade index is 2 with conservative mode enabled', () => { |
nextBestBuyAmount: 304933.8333333303,
nextBestBuyCalculation: {
buyTrigger: 1.01,
currentPrice: 8900,
hasObviousManualTrade: false,
isSingleSellGrid: true,
lastBuyPrice: 9966.66666666667,
sellTrigger: 1.0150000000000001,
totalBoughtAmount: 19900,
totalBoughtQty: 1.5
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chrisleekr, I noticed that some of your use cases have a buyTrigger superior to 1. Was it intentional? The calculations expect a buyTrigger inferior to 1.
I've checked this scenario which has a trigger inferior to 1 and the calculation is correct.
…o the front to simplify front code
…://github.com/rando128/binance-trading-bot-pr into fix/refactor-next-best-buy-amount-calculation
…nextBestBuy calculation at current price (instead of next configured grid to avoid confusion for the end-user).
@rando128 How's it going? |
@chrisleekr, I might improve the calculation algorithm to take into account the buy and sell spreads (stop limits) in a later PR. Nevertheless the current best buy quote amount calculation plays safe, so we are good to merge as far as I'm concerned. |
BTW I just realised how you built your test cases and how the buyTrigger and currentPrice relate to each other. My code was creating confusion so I'm pushing a last commit to clarify the implementation (it will also make the test cases simpler to handle) |
@chrisleekr, with regards to the test scenario one, here is how to compute:
With the following calculation data:
|
@@ -124,6 +123,8 @@ const calculateNextBestBuyAmount = ( | |||
} | |||
); | |||
|
|||
const buyTrigger = 1 + (currentPrice - lastBuyPrice) / lastBuyPrice; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found when the lastBuyPrice
is not configured, which will set as null
, it returns Infinity
for buyTrigger
.
currentPrice => 9000
lastBuyPrice => null
buyTrigger => Infinity
Is Infinity
intended or it should be 1?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. It doesn't make sense to even try to calculate it in this case. Maybe we could condition nextBestBuyCalculation code with a if(lastBuyPrice > 0)
statement on line 391
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, good. Do you want to update, or do you want me to update? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix just pushed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yay! I will check the tests and let's merge it.
This PR provides multiple improvements to the suggested breakeven amount capability:
totalBoughtQty
andtotalBoughtAmount
). Since trades can happen outside of the bot, this gives flexibility to the user to test different scenariosChanges:
nextBestBuyAmount
calculation has been moved into the backend inget-indicators.js
CoinWrapperBuySignal.js
that now uses the server datanextBestBuyAmount
calculation in case the conservative sell mode is applicable. The change is reflected inCoinWrapperBuySignal.js
andSymbolGridCalculator.js
get-indicators.js
andSymbolGridCalculator.js
Caveat:
Motivation and Context
How Has This Been Tested?
This is working on my production server.
Screenshots
Bought amount/quantity fields without obvious manual trade scenario
![Screenshot 2023-02-12 at 15 22 24](https://user-images.githubusercontent.com/1491835/218317792-2b36ec96-31a0-4cf7-bdb3-3bccfc18d81f.png)
Warning information for obvious manual trade scenario
![Screenshot 2023-02-12 at 15 22 36](https://user-images.githubusercontent.com/1491835/218317861-4c6a5c76-0798-4642-b367-b977c6797962.png)
Informative message for non active grid
![Screenshot 2023-02-12 at 15 23 00](https://user-images.githubusercontent.com/1491835/218317897-b05d650e-da7e-40e7-b99e-c4ad0a548f38.png)