Skip to content

Commit

Permalink
Static Typing: Fix isolated market position calculation (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang authored Nov 11, 2024
1 parent 6aa4ecf commit 30bc0d9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.13.19"
version = "1.13.20"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ internal class AccountCalculatorV2(
for ((market, childOpenPosition) in childOpenPositions ?: emptyMap()) {
childOpenPosition.childSubaccountNumber = childSubaccountNumber

// copying from subaccount to position
for (period in CalculationPeriod.entries) {
childOpenPosition.calculated[period]?.equity = childSubaccount.calculated[period]?.equity
childOpenPosition.calculated[period]?.freeCollateral = childSubaccount.calculated[period]?.freeCollateral
childOpenPosition.calculated[period]?.quoteBalance = childSubaccount.calculated[period]?.quoteBalance
childOpenPosition.calculated[period]?.marginUsage = childSubaccount.calculated[period]?.marginUsage
}

modifiedOpenPositions[market] = childOpenPosition
}
parentSubaccount.openPositions = modifiedOpenPositions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ data class SubaccountPosition(
)
}

val freeCollateral = if (subaccount != null) {
val freeCollateral = if (position != null) {
TradeStatesWithDoubleValues(
current = subaccount.calculated[CalculationPeriod.current]?.freeCollateral,
postOrder = subaccount.calculated[CalculationPeriod.post]?.freeCollateral,
postAllOrders = subaccount.calculated[CalculationPeriod.settled]?.freeCollateral,
current = position.calculated[CalculationPeriod.current]?.freeCollateral,
postOrder = position.calculated[CalculationPeriod.post]?.freeCollateral,
postAllOrders = position.calculated[CalculationPeriod.settled]?.freeCollateral,
)
} else {
TradeStatesWithDoubleValues.create(
Expand All @@ -309,11 +309,11 @@ data class SubaccountPosition(
)
}

val marginUsage = if (subaccount != null) {
val marginUsage = if (position != null) {
TradeStatesWithDoubleValues(
current = subaccount.calculated[CalculationPeriod.current]?.marginUsage,
postOrder = subaccount.calculated[CalculationPeriod.post]?.marginUsage,
postAllOrders = subaccount.calculated[CalculationPeriod.settled]?.marginUsage,
current = position.calculated[CalculationPeriod.current]?.marginUsage,
postOrder = position.calculated[CalculationPeriod.post]?.marginUsage,
postAllOrders = position.calculated[CalculationPeriod.settled]?.marginUsage,
)
} else {
TradeStatesWithDoubleValues.create(
Expand All @@ -323,11 +323,11 @@ data class SubaccountPosition(
)
}

val quoteBalance = if (subaccount != null) {
val quoteBalance = if (position != null) {
TradeStatesWithDoubleValues(
current = subaccount.calculated[CalculationPeriod.current]?.quoteBalance,
postOrder = subaccount.calculated[CalculationPeriod.post]?.quoteBalance,
postAllOrders = subaccount.calculated[CalculationPeriod.settled]?.quoteBalance,
current = position.calculated[CalculationPeriod.current]?.quoteBalance,
postOrder = position.calculated[CalculationPeriod.post]?.quoteBalance,
postAllOrders = position.calculated[CalculationPeriod.settled]?.quoteBalance,
)
} else {
TradeStatesWithDoubleValues.create(
Expand All @@ -337,11 +337,11 @@ data class SubaccountPosition(
)
}

val equity = if (subaccount != null) {
val equity = if (position != null) {
TradeStatesWithDoubleValues(
current = subaccount.calculated[CalculationPeriod.current]?.equity,
postOrder = subaccount.calculated[CalculationPeriod.post]?.equity,
postAllOrders = subaccount.calculated[CalculationPeriod.settled]?.equity,
current = position.calculated[CalculationPeriod.current]?.equity,
postOrder = position.calculated[CalculationPeriod.post]?.equity,
postAllOrders = position.calculated[CalculationPeriod.settled]?.equity,
)
} else {
TradeStatesWithDoubleValues.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ internal data class InternalPositionCalculated(
var size: Double? = null,
var liquidationPrice: Double? = null,
var buyingPower: Double? = null,
// the following four fields are copied from the subaccount calculated.. this is needed
// when grouping subaccounts for isolated margin
var equity: Double? = null,
var freeCollateral: Double? = null,
var marginUsage: Double? = null,
var quoteBalance: Double? = null,
)

internal data class InternalAccountBalanceState(
Expand Down
2 changes: 1 addition & 1 deletion v4_abacus.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'v4_abacus'
spec.version = '1.13.19'
spec.version = '1.13.20'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down

0 comments on commit 30bc0d9

Please sign in to comment.