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

fix balancePercent calculations for trades on opposite side of current position #650

Merged
merged 10 commits into from
Sep 16, 2024

Conversation

moo-onthelawn
Copy link
Contributor

@moo-onthelawn moo-onthelawn commented Sep 13, 2024

fixed a couple bugs with balancePercent calculations. math will be the end of me I swear

  • Bug 1: Fix balancePercent size calculation for cross market trades on the opposite side of current position
    • The balance percent a user selects should include closing their whole current position and then apply the percentage off of their new free collateral given the entire closed position
  • Bug 2: Fix balancePercent size calculation for isolated market trades on opposite side of current position
    • It doesn't make sense to apply balancePercent on the freeCollateral since it doesn't impact their free collateral - make balance percent apply to the currentPosition size instead
  • Bug 3: Fix balancePercent seemingly being off for isolated market trades on same side of current position
    • It's cuz isolated markets need to do an additional calculation for the amount of additional collateral to trade when adding to current position size
    • Note: in ideal world I'd be using shared functions in MarginCalculator but I need to do a bit of refactoring to get the correct subaccount/childsubaccount variables. Filed a TODO to fix it.
    • Was able to adjust the collateral buffer percents after that fix

proof of concept help:

tada.mov

@moo-onthelawn moo-onthelawn marked this pull request as ready for review September 13, 2024 21:33
Comment on lines 643 to 645
if (freeCollateral <= Numeric.double.ZERO) {
return Numeric.double.ZERO
}
Copy link
Contributor

Choose a reason for hiding this comment

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

would it make sense to add a non-zero check for tradeLeverage as well?

@@ -558,7 +622,7 @@ internal class TradeInputMarketOrderCalculator() {
break@orderbookLoop
}
}
val balancePercentTotal = (usdcSizeTotal / tradeLeverage) / freeCollateral
val balancePercentTotal = calculateBalancePercentFromUsdcSize(usdcSize = usdcSizeTotal, freeCollateral = freeCollateral, positionSize = positionSizeNotional, tradeLeverage = tradeLeverage, isTradeSameSide = isTradeSameSide)
Copy link
Contributor

Choose a reason for hiding this comment

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

im surprised spotless doesn't format this to multiple lines

@@ -118,11 +119,28 @@ internal class TradeInputMarketOrderCalculator() {
maxMarketLeverage
}

val positions = subaccount.openPositions
val marketId = market?.perpetualMarket?.id
Copy link
Contributor

Choose a reason for hiding this comment

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

it looks like you are getting marketId from trade.marketId in the non-typed TradeInputCalculator. I'm assuming its interchangeable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yeah - they should be interchangeable, but lemme make them consistent for easier to read reasons

@moo-onthelawn moo-onthelawn merged commit a6485bb into main Sep 16, 2024
3 checks passed
@moo-onthelawn moo-onthelawn deleted the mulan-fixBalancePercent branch September 16, 2024 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants