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: support of manual trades in breakeven amount calculator #601

Conversation

rando128
Copy link
Contributor

@rando128 rando128 commented Feb 11, 2023

This PR provides multiple improvements to the suggested breakeven amount capability:

  • Manual trades are now supported in the calculator with 2 new input fields (totalBoughtQty and totalBoughtAmount). Since trades can happen outside of the bot, this gives flexibility to the user to test different scenarios
  • If the bot can detect obvious manual trades scenarios, the UI warns the user to update the 2 new input fields
  • Accessing the calculator without any active buy grid shows an informative message.

Changes:

  • nextBestBuyAmount calculation has been moved into the backend in get-indicators.js
  • Simplification of CoinWrapperBuySignal.js that now uses the server data
  • Fix (or improvement) to use the reduced sell trigger percentage in the nextBestBuyAmount calculation in case the conservative sell mode is applicable. The change is reflected in CoinWrapperBuySignal.js and SymbolGridCalculator.js
  • Obvious manual trade detections have been revisited in both get-indicators.js and SymbolGridCalculator.js

Caveat:

  • The calculator doesn't take into account fees. This might be addressed in another PR.

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


Warning information for obvious manual trade scenario
Screenshot 2023-02-12 at 15 22 36


Informative message for non active grid
Screenshot 2023-02-12 at 15 23 00

@rando128 rando128 changed the title Fix:nextBestBuyAmount on server with correct sell trigger percentage fix: nextBestBuyAmount on server with correct sell trigger percentage Feb 11, 2023
@rando128 rando128 changed the title fix: nextBestBuyAmount on server with correct sell trigger percentage feat: support of manual trades in breakeven amount calculator Feb 12, 2023
@chrisleekr
Copy link
Owner

Nice work @rando128

I will need to write test cases.

I will get it done when I have time.

@chrisleekr chrisleekr added the enhancement New feature or request label Feb 14, 2023
@chrisleekr chrisleekr self-requested a review February 14, 2023 07:11
@chrisleekr
Copy link
Owner

@rando128 I am still writing the test case. I found my old test case was just not right. It's very wrong.
I will need more time to refactor and make sure the test case is legit.

@rando128
Copy link
Contributor Author

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.

@chrisleekr
Copy link
Owner

@rando128 Ok no problem. I am refactoring the test to update quickly for changes.
About conservative mode, I had a question for you. But I assume that is the one you gonna fix.
Please commit any fixes. 👍

highestPrice: 10000,
lowestPrice: 8893.03,
athPrice: 9000,
athRestrictionPrice: 8100,
nextBestBuyAmount: 204983.33333333314,
Copy link
Owner

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?

Copy link
Contributor Author

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?

Copy link
Owner

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:

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:

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

Copy link
Contributor Author

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.

@chrisleekr
Copy link
Owner

@rando128 How's it going?
We good to merge?

@rando128
Copy link
Contributor Author

rando128 commented Mar 5, 2023

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

@rando128
Copy link
Contributor Author

rando128 commented Mar 5, 2023

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)

@rando128
Copy link
Contributor Author

rando128 commented Mar 5, 2023

@chrisleekr, with regards to the test scenario one, here is how to compute:

Grid Trade # Price Quantity Quote Amount Last buy price
Buy #1 9000,000000 1,000000 9000,000000 9000,000000
Next Best Buy 8900,050000 0,871712 7758,283333 8953,450300
Invested: 16758,283333
Next Sell 9007,171002 1,871712 16732,391786
Profit after sell −25,891548 (0,01% of loss)

With the following calculation data:

currentPrice 8900,050000
lastBuyPrice 9000,000000
totalAmount 9000,000000
totalQty 1,000000
buyTrigger 0,988894 8900,050000 <= buyTriggerPrice
sellTrigger 1,006000 9007,171002 <= sellTriggerPrice
nextBestBuyQuoteAmount 7758,283333
nextBestBuyQty 0,871712

@@ -124,6 +123,8 @@ const calculateNextBestBuyAmount = (
}
);

const buyTrigger = 1 + (currentPrice - lastBuyPrice) / lastBuyPrice;
Copy link
Owner

@chrisleekr chrisleekr Mar 19, 2023

Choose a reason for hiding this comment

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

@rando128

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?

Copy link
Contributor Author

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

Copy link
Owner

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? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix just pushed!

Copy link
Owner

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.

@chrisleekr chrisleekr merged commit 463181e into chrisleekr:master Mar 20, 2023
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.

2 participants