Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for the position values before calculating subaccount #705

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.12.21"
version = "1.12.22"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,20 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
) {
for (period in periods) {
val quoteBalance = parser.asDouble(value(subaccount, "quoteBalance", period))
if (quoteBalance != null) {

var hasPositionCalculated = false
positions?.let {
for ((key, position) in positions) {
val valueTotal = parser.asDouble(value(position, "valueTotal", period))
if (valueTotal != null) {
hasPositionCalculated = true
break
}
}
}
val positionsReady = positions.isNullOrEmpty() || hasPositionCalculated

if (quoteBalance != null && positionsReady) {
var notionalTotal = Numeric.double.ZERO
var valueTotal = Numeric.double.ZERO
var initialRiskTotal = Numeric.double.ZERO
Expand Down Expand Up @@ -464,12 +477,9 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
) {
for (period in periods) {
val quoteBalance = parser.asDouble(value(subaccount, "quoteBalance", period))
if (quoteBalance != null) {
val equity =
parser.asDouble(value(subaccount, "equity", period)) ?: Numeric.double.ZERO
val initialRiskTotal =
parser.asDouble(value(subaccount, "initialRiskTotal", period))
?: Numeric.double.ZERO
val equity = parser.asDouble(value(subaccount, "equity", period))
val initialRiskTotal = parser.asDouble(value(subaccount, "initialRiskTotal", period))
if (quoteBalance != null && equity != null && initialRiskTotal != null) {
val imf =
parser.asDouble(configs?.get("initialMarginFraction"))
?: parser.asDouble(0.05)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,21 @@ internal class SubaccountCalculatorV2(
val calculated = subaccount.calculated[period] ?: InternalSubaccountCalculated()
subaccount.calculated[period] = calculated

var hasPositionCalculated = false
for (position in positions?.values ?: emptyList()) {
if (position.calculated[period] != null) {
hasPositionCalculated = true
}
}
val positionsReady = positions.isNullOrEmpty() || hasPositionCalculated

val quoteBalance = calculated.quoteBalance
if (quoteBalance != null) {
if (quoteBalance != null && positionsReady) {
var notionalTotal = Numeric.double.ZERO
var valueTotal = Numeric.double.ZERO
var initialRiskTotal = Numeric.double.ZERO

for ((key, position) in positions ?: emptyMap()) {
for (position in positions?.values ?: emptyList()) {
val positionCalculated = position.calculated[period]
notionalTotal += positionCalculated?.notionalTotal ?: Numeric.double.ZERO
valueTotal += positionCalculated?.valueTotal ?: Numeric.double.ZERO
Expand Down Expand Up @@ -169,9 +177,9 @@ internal class SubaccountCalculatorV2(
for (period in periods) {
val calculated = subaccount?.calculated?.get(period)
val quoteBalance = calculated?.quoteBalance
if (quoteBalance != null) {
val equity = calculated.equity ?: Numeric.double.ZERO
val initialRiskTotal = calculated.initialRiskTotal ?: Numeric.double.ZERO
val equity = calculated?.equity
val initialRiskTotal = calculated?.initialRiskTotal
if (quoteBalance != null && equity != null && initialRiskTotal != null) {
val imf = configs?.initialMarginFraction ?: 0.05

calculated.buyingPower = calculateBuyingPower(
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.12.21'
spec.version = '1.12.22'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down