diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TradeInputCalculator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TradeInputCalculator.kt index 674d6ca69..86f769989 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TradeInputCalculator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TradeInputCalculator.kt @@ -1688,8 +1688,6 @@ internal class TradeInputCalculator( "openPositions.$marketId", ), ) - val equity = parser.asDouble(parser.value(subaccount, "equity.current")) ?: return null - val freeCollateral = parser.asDouble(parser.value(subaccount, "freeCollateral.current")) ?: return null val initialMarginFraction = parser.asDouble(parser.value(market, "configs.effectiveInitialMarginFraction")) @@ -1699,9 +1697,12 @@ internal class TradeInputCalculator( } else { Numeric.double.ONE / initialMarginFraction } + + val equity = parser.asDouble(parser.value(subaccount, "equity.current")) + val freeCollateral = parser.asDouble(parser.value(subaccount, "freeCollateral.current")) ?: Numeric.double.ZERO val positionNotionalTotal = parser.asDouble(parser.value(position, "notionalTotal.current")) ?: Numeric.double.ZERO - return if (equity > Numeric.double.ZERO) { + return if (equity != null && equity > Numeric.double.ZERO) { (freeCollateral + positionNotionalTotal / maxMarketLeverage) * maxMarketLeverage / equity } else { maxMarketLeverage diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/TradeInput/TradeInputOptionsCalculator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/TradeInput/TradeInputOptionsCalculator.kt index 3f91b5e84..f945c3382 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/TradeInput/TradeInputOptionsCalculator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/TradeInput/TradeInputOptionsCalculator.kt @@ -352,8 +352,6 @@ internal class TradeInputOptionsCalculator( return null } - val equity = subaccount.calculated[CalculationPeriod.current]?.equity ?: return null - val freeCollateral = subaccount.calculated[CalculationPeriod.current]?.freeCollateral ?: return null val initialMarginFraction = market.perpetualMarket?.configs?.effectiveInitialMarginFraction ?: return null @@ -362,9 +360,12 @@ internal class TradeInputOptionsCalculator( } else { Numeric.double.ONE / initialMarginFraction } + + val equity = subaccount.calculated[CalculationPeriod.current]?.equity + val freeCollateral = subaccount.calculated[CalculationPeriod.current]?.freeCollateral ?: Numeric.double.ZERO val positionNotionalTotal = position?.calculated?.get(CalculationPeriod.current)?.notionalTotal ?: Numeric.double.ZERO - return if (equity > Numeric.double.ZERO) { + return if (equity != null && equity > Numeric.double.ZERO) { (freeCollateral + positionNotionalTotal / maxMarketLeverage) * maxMarketLeverage / equity } else { maxMarketLeverage