Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
moo-onthelawn committed Aug 16, 2024
1 parent 84a53e0 commit 1eded7b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
8 changes: 7 additions & 1 deletion docs/Account.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ Leverage of the position

## maxLeverage

Max leverage allowed for the position, calculated from adjustedImf
Max leverage allowed for the position

```
max_market_leverage = 1 / adjustedImf
max_leverage = subaccount.freeCollateral * max_market_leverage / suabccount.equity
```

## buyingPower

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exchange.dydx.abacus.calculator

import abs
import exchange.dydx.abacus.output.input.MarginMode
import exchange.dydx.abacus.protocols.ParserProtocol
import exchange.dydx.abacus.utils.Numeric
import exchange.dydx.abacus.utils.mutable
Expand Down Expand Up @@ -133,7 +134,6 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
set(null, modified, "adjustedImf", period)
set(null, modified, "adjustedMmf", period)
set(null, modified, "initialRiskTotal", period)
set(null, modified, "maxLeverage", period)
set(null, modified, "unrealizedPnl", period)
set(null, modified, "unrealizedPnlPercent", period)
set(null, modified, "marginValue", period)
Expand All @@ -150,12 +150,12 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
configs,
notional,
)
val maxLeverage =
if (adjustedImf != Numeric.double.ZERO) Numeric.double.ONE / adjustedImf else null

val marginMode = parser.asString(parser.value(position, "marginMode"))

set(adjustedImf, modified, "adjustedImf", period)
set(adjustedMmf, modified, "adjustedMmf", period)
set(adjustedImf * notional, modified, "initialRiskTotal", period)
set(maxLeverage, modified, "maxLeverage", period)

if (entryPrice != null) {
val entryValue = size * entryPrice
Expand All @@ -167,7 +167,6 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
set(unrealizedPnlPercent, modified, "unrealizedPnlPercent", period)
}

val marginMode = parser.asString(parser.value(position, "marginMode"))
when (marginMode) {
"ISOLATED" -> {
val equity = parser.asDouble(value(subaccount, "equity", period))
Expand All @@ -190,7 +189,6 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
set(null, modified, "adjustedImf", period)
set(null, modified, "adjustedMmf", period)
set(null, modified, "initialRiskTotal", period)
set(null, modified, "maxLeverage", period)
set(null, modified, "unrealizedPnl", period)
set(null, modified, "unrealizedPnlPercent", period)
set(null, modified, "marginValue", period)
Expand All @@ -204,7 +202,6 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
set(null, modified, "adjustedImf", period)
set(null, modified, "adjustedMmf", period)
set(null, modified, "initialRiskTotal", period)
set(null, modified, "maxLeverage", period)
set(null, modified, "marginValue", period)
}
}
Expand Down Expand Up @@ -320,12 +317,27 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
val initialRiskTotal =
parser.asDouble(value(subaccount, "initialRiskTotal", period))
val equity = parser.asDouble(value(subaccount, "equity", period))
val freeCollateral = parser.asDouble(value(subaccount, "freeCollateral", period))

for ((key, position) in positions) {
val marginMode = parser.asString(parser.value(position, "marginMode"))
val adjustedImf = parser.asDouble(value(position, "adjustedImf", period))
val marketLeverage = if (adjustedImf != null && adjustedImf != Numeric.double.ZERO) Numeric.double.ONE / adjustedImf else Numeric.double.ONE

val leverage = calculatePositionLeverage(
equity = equity,
notionalValue = parser.asDouble(value(position, "valueTotal", period)),
)
set(leverage, position, "leverage", period)

val maxLeverage = calculatePositionMaxLeverage(
marginMode = marginMode,
adjustedImf = adjustedImf,
freeCollateral = freeCollateral,
equity = equity,
)
set(maxLeverage, position, "maxLeverage", period)

val liquidationPrice = calculatePositionLiquidationPrice(
equity = equity ?: Numeric.double.ZERO,
marketId = key,
Expand All @@ -337,7 +349,7 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
val buyingPower = calculatePositionBuyingPower(
equity = equity,
initialRiskTotal = initialRiskTotal,
imf = parser.asDouble(value(position, "adjustedImf", period)),
imf = adjustedImf,
)
set(buyingPower, position, "buyingPower", period)
}
Expand All @@ -356,6 +368,25 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
}
}

private fun calculatePositionMaxLeverage(
marginMode: MarginMode?,
adjustedImf: Double?,
freeCollateral: Double?,
equity: Double?,
): Double? {
val marketMaxLeverage = if (adjustedImf != null && adjustedImf != Numeric.double.ZERO) {
Numeric.double.ONE / adjustedImf
} else {
Numeric.double.ONE
}
return if (marginMode == MarginMode.Isolated) {
// TODO: CT-1111
marketMaxLeverage
} else {
(freeCollateral ?: Numeric.double.ZERO) * marketMaxLeverage / (equity ?: Numeric.double.ONE)
}
}

private fun calculatePositionLiquidationPrice(
equity: Double,
marketId: String,
Expand Down

0 comments on commit 1eded7b

Please sign in to comment.