From 78644c14cea1357c48a60d00239227f26fcc7477 Mon Sep 17 00:00:00 2001 From: Rui Date: Fri, 8 Nov 2024 16:49:50 -0800 Subject: [PATCH] Fix isolated market position calculation --- build.gradle.kts | 2 +- .../calculator/V2/AccountCalculatorV2.kt | 8 +++++ .../output/account/SubaccountPosition.kt | 32 +++++++++---------- .../state/internalstate/InternalState.kt | 6 ++++ v4_abacus.podspec | 2 +- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index cd4830bfa..f8da0f237 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -52,7 +52,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.13.19" +version = "1.13.20" repositories { google() diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/AccountCalculatorV2.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/AccountCalculatorV2.kt index 93e19ef3b..43f1a9874 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/AccountCalculatorV2.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/AccountCalculatorV2.kt @@ -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 diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/output/account/SubaccountPosition.kt b/src/commonMain/kotlin/exchange.dydx.abacus/output/account/SubaccountPosition.kt index 6058b0020..7cf04c768 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/output/account/SubaccountPosition.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/output/account/SubaccountPosition.kt @@ -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( @@ -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( @@ -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( @@ -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( diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/internalstate/InternalState.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/internalstate/InternalState.kt index b6bf3da14..bda972c16 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/internalstate/InternalState.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/internalstate/InternalState.kt @@ -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( diff --git a/v4_abacus.podspec b/v4_abacus.podspec index 39564ff53..a0fa8fcfb 100644 --- a/v4_abacus.podspec +++ b/v4_abacus.podspec @@ -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 = ''