Skip to content

Commit

Permalink
getting closer hm
Browse files Browse the repository at this point in the history
  • Loading branch information
moo-onthelawn committed Aug 29, 2024
1 parent 5faa4b3 commit 5cd31a8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS
import exchange.dydx.abacus.utils.Numeric
import kollections.iListOf
import kotlin.math.max
import kotlin.math.min

internal object MarginCalculator {
fun findExistingPosition(
Expand Down Expand Up @@ -775,8 +776,7 @@ internal object MarginCalculator {

// Cap targetLeverage to 98% of max leverage
val adjustedTargetLeverage = if (maxLeverageForMarket != null) {
val cappedLeverage = maxLeverageForMarket * MAX_LEVERAGE_BUFFER_PERCENT
cappedLeverage
maxLeverageForMarket * MAX_LEVERAGE_BUFFER_PERCENT
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import exchange.dydx.abacus.utils.Numeric
import exchange.dydx.abacus.utils.mutable
import exchange.dydx.abacus.utils.safeSet
import kotlin.math.max
import exchange.dydx.abacus.utils.Logger

internal enum class CalculationPeriod(val rawValue: String) {
current("current"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ internal class TradeInputCalculator(
): Map<String, Any> {
val account = parser.asNativeMap(state["account"])

val subaccount = parser.asNativeMap(parser.value(account, "subaccounts.$subaccountNumber"))

val user = parser.asNativeMap(state["user"]) ?: mapOf()
val markets = parser.asNativeMap(state["markets"])
val rewardsParams = parser.asNativeMap(state["rewardsParams"])
Expand All @@ -72,6 +70,14 @@ internal class TradeInputCalculator(
subaccountNumber,
)

val marginMode = parser.asString(parser.value(trade, "marginMode"))?.let { MarginMode.invoke(it) }
val subaccount = if (marginMode == MarginMode.Cross) {
parser.asNativeMap(parser.value(account, "subaccounts.$subaccountNumber"))
} else {
// TODO: incorrect for isolated trades; fix CT-1092
parser.asNativeMap(parser.value(account, "groupedSubaccounts.$subaccountNumber"))
}

val marketId = parser.asString(trade?.get("marketId"))
val type = parser.asString(trade?.get("type"))
val market = if (marketId != null) parser.asNativeMap(markets?.get(marketId)) else null
Expand Down Expand Up @@ -1691,6 +1697,8 @@ internal class TradeInputCalculator(
val freeCollateral = parser.asDouble(parser.value(subaccount, "freeCollateral.current")) ?: Numeric.double.ZERO
val positionNotionalTotal = parser.asDouble(parser.value(position, "notionalTotal.current")) ?: Numeric.double.ZERO

// xcxc update

return if (equity != null && equity > Numeric.double.ZERO) {
(freeCollateral + positionNotionalTotal / maxMarketLeverage) * maxMarketLeverage / equity
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fun TradingStateMachine.trade(
changes = StateChanges(
iListOf(Changes.subaccount, Changes.input),
null,
iListOf(subaccountNumber),
subaccountNumbers,
)
} else {
error = ParsingError(
Expand Down Expand Up @@ -386,7 +386,7 @@ fun TradingStateMachine.trade(
changes = StateChanges(
iListOf(Changes.wallet, Changes.subaccount, Changes.input),
null,
subaccountNumbers,
iListOf(subaccountNumber),
)
}
if (sizeChanged) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import kotlinx.serialization.json.jsonObject
import kotlin.math.max
import kotlin.math.min
import kotlin.time.Duration.Companion.days
import exchange.dydx.abacus.output.input.MarginMode

@Suppress("UNCHECKED_CAST")
@JsExport
Expand Down Expand Up @@ -628,7 +629,6 @@ open class TradingStateMachine(
internal fun updateStateChanges(changes: StateChanges): StateChanges {
if (changes.changes.contains(Changes.input)) {
val subaccountNumber = changes.subaccountNumbers?.firstOrNull()
val childSubaccountNumber = changes.subaccountNumbers?.lastOrNull()

val subaccount = if (subaccountNumber != null) {
parser.asNativeMap(
Expand Down Expand Up @@ -657,11 +657,11 @@ open class TradingStateMachine(
)
}

if (subaccountNumber != null && childSubaccountNumber != null) {
if (subaccountNumber != null) {
if (staticTyping) {
when (internalState.input.currentType) {
InputType.TRADE -> {
calculateTrade(childSubaccountNumber)
calculateTrade(subaccountNumber)
}
InputType.TRANSFER -> {
calculateTransfer(subaccountNumber)
Expand All @@ -680,7 +680,7 @@ open class TradingStateMachine(
} else {
when (this.input?.get("current")) {
"trade" -> {
calculateTrade(childSubaccountNumber)
calculateTrade(subaccountNumber)
}

"closePosition" -> {
Expand Down Expand Up @@ -1071,13 +1071,20 @@ open class TradingStateMachine(
val marketId =
parser.asString(parser.value(modified, "trade.marketId"))
if (subaccountNumber != null && marketId != null) {
val marginMode = parser.asString(parser.value(modified, "marginMode"))?.let { MarginMode.invoke(it) }
val subaccount = if (marginMode == MarginMode.Cross) {
parser.asNativeMap(parser.value(this.account, "subaccounts.$subaccountNumber"))
} else {
parser.asNativeMap(parser.value(this.account, "groupedSubaccounts.$subaccountNumber"))
}
val leverage =
parser.asDouble(
parser.value(
this.account,
"subaccounts.$subaccountNumber.openPositions.$marketId.leverage.postOrder",
subaccount,
"openPositions.$marketId.leverage.postOrder",
),
)
Logger.e { "xcxc $marketId on $subaccountNumber has leverage $leverage"}
modified.safeSet("trade.size.leverage", leverage)
} else {
modified.safeSet("trade.size.leverage", null)
Expand Down

0 comments on commit 5cd31a8

Please sign in to comment.