From 367695ecaec3829a29f38879a86f2233f8b39af1 Mon Sep 17 00:00:00 2001 From: mulan xia Date: Tue, 9 Apr 2024 16:34:46 -0400 Subject: [PATCH 1/4] wip --- .../TriggerOrdersInputCalculator.kt | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt index b4b5c0470..705f1de6f 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt @@ -118,8 +118,8 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { modified.safeSet( "percentDiff", when (positionSide) { - "long" -> leverage.times(size.times(entryPrice.minus(triggerPrice))).div(notionalTotal).times(100) - "short" -> leverage.times(size.times(triggerPrice.minus(entryPrice))).div(notionalTotal).times(100) + "long" -> leverage.times(entryPrice.minus(triggerPrice)).div(notionalTotal).times(100) + "short" -> leverage.times(triggerPrice.minus(entryPrice)).div(notionalTotal).times(100) else -> null }, ) @@ -138,8 +138,8 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { modified.safeSet( "percentDiff", when (positionSide) { - "long" -> leverage.times(size.times(triggerPrice.minus(entryPrice))).div(notionalTotal).times(100) - "short" -> leverage.times(size.times(entryPrice.minus(triggerPrice))).div(notionalTotal).times(100) + "long" -> leverage.times(triggerPrice.minus(entryPrice)).div(notionalTotal).times(100) + "short" -> leverage.times(entryPrice.minus(triggerPrice)).div(notionalTotal).times(100) else -> null }, ) @@ -157,7 +157,7 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { ) modified.safeSet( "percentDiff", - leverage.times(usdcDiff).div(notionalTotal).times(100), + leverage.times(usdcDiff).div(size.times(notionalTotal)).times(100), ) } } @@ -173,7 +173,7 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { ) modified.safeSet( "percentDiff", - leverage.times(usdcDiff).div(notionalTotal).times(100), + leverage.times(usdcDiff).div(size.times(notionalTotal)).times(100), ) } } @@ -182,14 +182,14 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { modified.safeSet( "triggerPrice", when (positionSide) { - "long" -> entryPrice.minus(percentDiff.times(notionalTotal).div(size.times(leverage))) - "short" -> entryPrice.plus(percentDiff.times(notionalTotal).div(size.times(leverage))) + "long" -> entryPrice.minus(percentDiff.times(notionalTotal).div(leverage)) + "short" -> entryPrice.plus(percentDiff.times(notionalTotal).div(leverage)) else -> null }, ) modified.safeSet( "usdcDiff", - percentDiff.times(notionalTotal).div(leverage), + size.times(percentDiff.times(notionalTotal)).div(leverage), ) } } @@ -198,14 +198,14 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { modified.safeSet( "triggerPrice", when (positionSide) { - "long" -> entryPrice.plus(percentDiff.times(notionalTotal).div(size.times(leverage))) - "short" -> entryPrice.minus(percentDiff.times(notionalTotal).div(size.times(leverage))) + "long" -> entryPrice.plus(percentDiff.times(notionalTotal).div(leverage)) + "short" -> entryPrice.minus(percentDiff.times(notionalTotal).div(leverage)) else -> null }, ) modified.safeSet( "usdcDiff", - percentDiff.times(notionalTotal).div(leverage), + size.times(percentDiff.times(notionalTotal)).div(leverage), ) } } From 2841df8d491173ccfad4edf9dea94d6432e34ad8 Mon Sep 17 00:00:00 2001 From: mulan xia Date: Tue, 9 Apr 2024 16:50:45 -0400 Subject: [PATCH 2/4] test --- build.gradle.kts | 2 +- .../payload/TriggerOrdersInputTests.kt | 16 ++++++++-------- v4_abacus.podspec | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8bd75913f..d92b6f41b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -48,7 +48,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.6.36" +version = "1.6.37" repositories { google() diff --git a/src/commonTest/kotlin/exchange.dydx.abacus/payload/TriggerOrdersInputTests.kt b/src/commonTest/kotlin/exchange.dydx.abacus/payload/TriggerOrdersInputTests.kt index 915fd9751..e49aafb20 100644 --- a/src/commonTest/kotlin/exchange.dydx.abacus/payload/TriggerOrdersInputTests.kt +++ b/src/commonTest/kotlin/exchange.dydx.abacus/payload/TriggerOrdersInputTests.kt @@ -195,7 +195,7 @@ class TriggerOrderInputTests : V4BaseTests() { "limitPrice": "300.0", "triggerPrice": "400.0", "usdcDiff": "300", - "percentDiff": "35.15790352", + "percentDiff": "70.31580704", "input": "stopLossOrder.price.triggerPrice" }, "summary": { @@ -225,7 +225,7 @@ class TriggerOrderInputTests : V4BaseTests() { "limitPrice": "300.0", "triggerPrice": "200.0", "usdcDiff": "400", - "percentDiff": "46.87720469", + "percentDiff": "93.75440939", "input": "stopLossOrder.price.usdcDiff" }, "summary": { @@ -253,8 +253,8 @@ class TriggerOrderInputTests : V4BaseTests() { "side": "SELL", "price": { "limitPrice": "300.0", - "triggerPrice": "573.3534", - "usdcDiff": "213.3233", + "triggerPrice": "786.6767", + "usdcDiff": "106.6617", "percentDiff": "25.0", "input": "stopLossOrder.price.percentDiff" }, @@ -331,7 +331,7 @@ class TriggerOrderInputTests : V4BaseTests() { "limitPrice": "1600.0", "triggerPrice": "1800.0", "usdcDiff": "400", - "percentDiff": "46.87720469", + "percentDiff": "93.75440939", "input": "takeProfitOrder.price.triggerPrice" }, "summary": { @@ -361,7 +361,7 @@ class TriggerOrderInputTests : V4BaseTests() { "limitPrice": "1600.0", "triggerPrice": "1600.0", "usdcDiff": "300.0", - "percentDiff": "35.15790352", + "percentDiff": "70.31580704", "input": "takeProfitOrder.price.usdcDiff" }, "summary": { @@ -389,8 +389,8 @@ class TriggerOrderInputTests : V4BaseTests() { "side": "SELL", "price": { "limitPrice": "1600.0", - "triggerPrice": "1426.6466", - "usdcDiff": "213.3233", + "triggerPrice": "1213.3233", + "usdcDiff": "106.6617", "percentDiff": "25.0", "input": "takeProfitOrder.price.percentDiff" }, diff --git a/v4_abacus.podspec b/v4_abacus.podspec index bee25841f..d792e5a52 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.6.36' + spec.version = '1.6.37' spec.homepage = 'https://github.com/dydxprotocol/v4-abacus' spec.source = { :http=> ''} spec.authors = '' From 0183df8de2444f023f587a78f1ff059a14b4d6ac Mon Sep 17 00:00:00 2001 From: mulan xia Date: Tue, 9 Apr 2024 17:20:42 -0400 Subject: [PATCH 3/4] leverage is neg on short positions --- .../calculator/TriggerOrdersInputCalculator.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt index 705f1de6f..522bc825a 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt @@ -92,7 +92,7 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { val inputType = parser.asString(parser.value(modified, "input")) val positionSide = parser.asString(parser.value(position, "resources.indicator.current")) val notionalTotal = parser.asDouble(parser.value(position, "notionalTotal.current")) ?: return modified - val leverage = parser.asDouble(parser.value(position, "leverage.current")) ?: return modified + val leverage = parser.asDouble(parser.value(position, "leverage.current"))?.abs() ?: return modified if (size == null || size == Numeric.double.ZERO || notionalTotal == Numeric.double.ZERO || leverage == Numeric.double.ZERO) { // A valid position size should never have 0 size, notional value or leverage. From 826741187cdc0606102fdeddfc3cca17cc6828aa Mon Sep 17 00:00:00 2001 From: mulan xia Date: Tue, 9 Apr 2024 17:27:19 -0400 Subject: [PATCH 4/4] clear values when set to null --- .../TriggerOrdersInputCalculator.kt | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt index 522bc825a..3137394ff 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/TriggerOrdersInputCalculator.kt @@ -123,6 +123,15 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { else -> null }, ) + } else { + modified.safeSet( + "usdcDiff", + null, + ) + modified.safeSet( + "percentDiff", + null, + ) } } "takeProfitOrder.price.triggerPrice" -> { @@ -143,6 +152,15 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { else -> null }, ) + } else { + modified.safeSet( + "usdcDiff", + null, + ) + modified.safeSet( + "percentDiff", + null, + ) } } "stopLossOrder.price.usdcDiff" -> { @@ -159,6 +177,15 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { "percentDiff", leverage.times(usdcDiff).div(size.times(notionalTotal)).times(100), ) + } else { + modified.safeSet( + "triggerPrice", + null, + ) + modified.safeSet( + "percentDiff", + null, + ) } } "takeProfitOrder.price.usdcDiff" -> { @@ -175,6 +202,15 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { "percentDiff", leverage.times(usdcDiff).div(size.times(notionalTotal)).times(100), ) + } else { + modified.safeSet( + "triggerPrice", + null, + ) + modified.safeSet( + "percentDiff", + null, + ) } } "stopLossOrder.price.percentDiff" -> { @@ -191,6 +227,15 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { "usdcDiff", size.times(percentDiff.times(notionalTotal)).div(leverage), ) + } else { + modified.safeSet( + "triggerPrice", + null, + ) + modified.safeSet( + "usdcDiff", + null, + ) } } "takeProfitOrder.price.percentDiff" -> { @@ -207,6 +252,15 @@ internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) { "usdcDiff", size.times(percentDiff.times(notionalTotal)).div(leverage), ) + } else { + modified.safeSet( + "triggerPrice", + null, + ) + modified.safeSet( + "usdcDiff", + null, + ) } } else -> {}