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

v1.6.37: Fix sltp percentage calculation #297

Merged
merged 4 commits into from
Apr 9, 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 @@ -48,7 +48,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.6.36"
version = "1.6.37"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -118,11 +118,20 @@ 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
},
)
} else {
modified.safeSet(
"usdcDiff",
null,
)
modified.safeSet(
"percentDiff",
null,
)
}
}
"takeProfitOrder.price.triggerPrice" -> {
Expand All @@ -138,11 +147,20 @@ 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
},
)
} else {
modified.safeSet(
"usdcDiff",
null,
)
modified.safeSet(
"percentDiff",
null,
)
}
}
"stopLossOrder.price.usdcDiff" -> {
Expand All @@ -157,7 +175,16 @@ 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),
)
} else {
modified.safeSet(
"triggerPrice",
null,
)
modified.safeSet(
"percentDiff",
null,
)
}
}
Expand All @@ -173,7 +200,16 @@ 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),
)
} else {
modified.safeSet(
"triggerPrice",
null,
)
modified.safeSet(
"percentDiff",
null,
)
}
}
Expand All @@ -182,14 +218,23 @@ 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),
)
} else {
modified.safeSet(
"triggerPrice",
null,
)
modified.safeSet(
"usdcDiff",
null,
)
}
}
Expand All @@ -198,14 +243,23 @@ 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),
)
} else {
modified.safeSet(
"triggerPrice",
null,
)
modified.safeSet(
"usdcDiff",
null,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
},
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
},
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.6.36'
spec.version = '1.6.37'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down
Loading