Skip to content

Commit

Permalink
enable balancePercent field on (cross) market orders (#641)
Browse files Browse the repository at this point in the history
Co-authored-by: Rui <[email protected]>
  • Loading branch information
moo-onthelawn and ruixhuang authored Sep 11, 2024
1 parent 690e8c5 commit 660791b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/Input/TradeInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ The orderbook entries taken by the market order
data class TradeInputOptions(
&emsp;val needsSize: Boolean,
&emsp;val needsLeverage: Boolean,
&emsp;val needsBalancePercent: Boolean,
&emsp;val maxLeverage: Double?,
&emsp;val needsLimitPrice: Boolean,
&emsp;val needsTargetLeverage: Boolean,
Expand All @@ -248,6 +249,10 @@ UX should ask user for size

UX should ask user for leverage input

## needsBalancePercent

UX should ask user for balance percent input

## maxLeverage

Max leverage for the leverage slider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1066,13 +1066,15 @@ internal class TradeInputCalculator(
return when (MarginMode.invoke(marginMode)) {
MarginMode.Isolated -> listOf(
sizeField(),
// balancePercentField(), TODO: enable in CT-1180
bracketsField(),
marginModeField(market, account, subaccount),
reduceOnlyField(),
).filterNotNull()
else -> listOf(
sizeField(),
leverageField(),
balancePercentField(),
bracketsField(),
marginModeField(market, account, subaccount),
reduceOnlyField(),
Expand Down Expand Up @@ -1157,6 +1159,13 @@ internal class TradeInputCalculator(
)
}

private fun balancePercentField(): Map<String, Any> {
return mapOf(
"field" to "size.balancePercent",
"type" to "double",
)
}

private fun limitPriceField(): Map<String, Any> {
return mapOf(
"field" to "price.limitPrice",
Expand Down Expand Up @@ -1346,6 +1355,7 @@ internal class TradeInputCalculator(
val options = mutableMapOf<String, Any>(
"needsSize" to false,
"needsLeverage" to false,
"needsBalancePercent" to false,
"needsTargetLeverage" to false,
"needsTriggerPrice" to false,
"needsLimitPrice" to false,
Expand All @@ -1363,6 +1373,7 @@ internal class TradeInputCalculator(
when (parser.asString(field["field"])) {
"size.size" -> options["needsSize"] = true
"size.leverage" -> options["needsLeverage"] = true
"size.balancePercent" -> options["needsBalancePercent"] = true
"price.triggerPrice" -> options["needsTriggerPrice"] = true
"price.limitPrice" -> options["needsLimitPrice"] = true
"price.trailingPercent" -> options["needsTrailingPercent"] = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ internal class TradeInputOptionsCalculator(
return when (trade.marginMode) {
MarginMode.Isolated -> listOf(
sizeField(),
// balancePercentField(), TODO: enable in CT-1180
bracketsField(),
marginModeField(market, account, subaccount),
reduceOnlyField(),
Expand All @@ -89,6 +90,7 @@ internal class TradeInputOptionsCalculator(
else -> listOf(
sizeField(),
leverageField(),
balancePercentField(),
bracketsField(),
marginModeField(market, account, subaccount),
reduceOnlyField(),
Expand Down Expand Up @@ -181,6 +183,7 @@ internal class TradeInputOptionsCalculator(
when (parser.asString(field["field"])) {
"size.size" -> options.needsSize = true
"size.leverage" -> options.needsLeverage = true
"size.balancePercent" -> options.needsBalancePercent = true
"price.triggerPrice" -> options.needsTriggerPrice = true
"price.limitPrice" -> options.needsLimitPrice = true
"price.trailingPercent" -> options.needsTrailingPercent = true
Expand Down Expand Up @@ -404,6 +407,13 @@ internal class TradeInputOptionsCalculator(
)
}

private fun balancePercentField(): Map<String, Any> {
return mapOf(
"field" to "size.balancePercent",
"type" to "double",
)
}

private fun limitPriceField(): Map<String, Any> {
return mapOf(
"field" to "price.limitPrice",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ data class TradeInputOptions(
val needsMarginMode: Boolean,
val needsSize: Boolean,
val needsLeverage: Boolean,
val needsBalancePercent: Boolean,
val maxLeverage: Double?,
val needsLimitPrice: Boolean,
val needsTargetLeverage: Boolean,
Expand Down Expand Up @@ -184,6 +185,7 @@ data class TradeInputOptions(
needsMarginMode = state.needsMarginMode,
needsSize = state.needsSize,
needsLeverage = state.needsLeverage,
needsBalancePercent = state.needsBalancePercent,
maxLeverage = state.maxLeverage,
needsLimitPrice = state.needsLimitPrice,
needsTargetLeverage = state.needsTargetLeverage,
Expand Down Expand Up @@ -215,6 +217,7 @@ data class TradeInputOptions(
val needsMarginMode = parser.asBool(data["needsMarginMode"]) ?: true
val needsSize = parser.asBool(data["needsSize"]) ?: false
val needsLeverage = parser.asBool(data["needsLeverage"]) ?: false
val needsBalancePercent = parser.asBool(data["needsBalancePercent"]) ?: false
val maxLeverage = parser.asDouble(data["maxLeverage"])
val needsLimitPrice = parser.asBool(data["needsLimitPrice"]) ?: false
val needsTargetLeverage = parser.asBool(data["needsTargetLeverage"]) ?: false
Expand Down Expand Up @@ -282,6 +285,7 @@ data class TradeInputOptions(
existing?.needsMarginMode != needsMarginMode ||
existing.needsSize != needsSize ||
existing.needsLeverage != needsLeverage ||
existing.needsBalancePercent != needsBalancePercent ||
existing.maxLeverage != maxLeverage ||
existing.needsLimitPrice != needsLimitPrice ||
existing.needsTargetLeverage != needsTargetLeverage ||
Expand All @@ -302,6 +306,7 @@ data class TradeInputOptions(
needsMarginMode,
needsSize,
needsLeverage,
needsBalancePercent,
maxLeverage,
needsLimitPrice,
needsTargetLeverage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ internal data class InternalTradeInputOptions(
var needsMarginMode: Boolean = false,
var needsSize: Boolean = false,
var needsLeverage: Boolean = false,
var needsBalancePercent: Boolean = false,
var maxLeverage: Double? = null,
var needsLimitPrice: Boolean = false,
var needsTargetLeverage: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ open class BaseTests(
assertEquals(data.needsMarginMode, obj.needsMarginMode, "$trace.needsMarginMode $doesntMatchText")
assertEquals(data.needsSize, obj.needsSize, "$trace.needsSize $doesntMatchText")
assertEquals(data.needsLeverage, obj.needsLeverage, "$trace.needsLeverage $doesntMatchText")
assertEquals(data.needsBalancePercent, obj.needsBalancePercent, "$trace.needsBalancePercent $doesntMatchText")
assertEquals(data.needsBrackets, obj.needsBrackets, "$trace.needsBrackets $doesntMatchText")
assertEquals(data.needsGoodUntil, obj.needsGoodUntil, "$trace.needsGoodUntil $doesntMatchText")
assertEquals(data.needsLimitPrice, obj.needsLimitPrice, "$trace.needsLimitPrice $doesntMatchText")
Expand Down Expand Up @@ -771,6 +772,11 @@ open class BaseTests(
obj.needsLeverage,
"$trace.needsLeverage $doesntMatchText",
)
assertEquals(
parser.asBool(data["needsBalancePercent"]) ?: false,
obj.needsBalancePercent,
"$trace.needsBalancePercent $doesntMatchText",
)
assertEquals(
parser.asBool(data["needsBrackets"]) ?: false,
obj.needsBrackets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class TradeInputOptionsTests : V4BaseTests() {
val options = trade.options
assertEquals(options.needsSize, true)
assertEquals(options.needsLeverage, true)
assertEquals(options.needsBalancePercent, true)
assertEquals(options.needsTriggerPrice, false)
assertEquals(options.needsLimitPrice, false)
assertEquals(options.needsTrailingPercent, false)
Expand All @@ -145,6 +146,7 @@ class TradeInputOptionsTests : V4BaseTests() {
"options": {
"needsSize": true,
"needsLeverage": true,
"needsBalancePercent": true,
"needsTriggerPrice": false,
"needsLimitPrice": false,
"needsTrailingPercent": false,
Expand All @@ -168,6 +170,7 @@ class TradeInputOptionsTests : V4BaseTests() {
val options = trade.options
assertEquals(options.needsSize, true)
assertEquals(options.needsLeverage, false)
assertEquals(options.needsBalancePercent, false)
assertEquals(options.needsTriggerPrice, false)
assertEquals(options.needsLimitPrice, true)
assertEquals(options.needsTrailingPercent, false)
Expand All @@ -187,6 +190,7 @@ class TradeInputOptionsTests : V4BaseTests() {
"options": {
"needsSize": true,
"needsLeverage": false,
"needsBalancePercent": false,
"needsTriggerPrice": false,
"needsLimitPrice": true,
"needsTrailingPercent": false,
Expand All @@ -210,6 +214,7 @@ class TradeInputOptionsTests : V4BaseTests() {
val options = trade.options
assertEquals(options.needsSize, true)
assertEquals(options.needsLeverage, false)
assertEquals(options.needsBalancePercent, false)
assertEquals(options.needsTriggerPrice, false)
assertEquals(options.needsLimitPrice, true)
assertEquals(options.needsTrailingPercent, false)
Expand All @@ -229,6 +234,7 @@ class TradeInputOptionsTests : V4BaseTests() {
"options": {
"needsSize": true,
"needsLeverage": false,
"needsBalancePercent": false,
"needsTriggerPrice": false,
"needsLimitPrice": true,
"needsTrailingPercent": false,
Expand All @@ -252,6 +258,7 @@ class TradeInputOptionsTests : V4BaseTests() {
val options = trade.options
assertEquals(options.needsSize, true)
assertEquals(options.needsLeverage, false)
assertEquals(options.needsBalancePercent, false)
assertEquals(options.needsTriggerPrice, false)
assertEquals(options.needsLimitPrice, true)
assertEquals(options.needsTrailingPercent, false)
Expand Down Expand Up @@ -290,6 +297,7 @@ class TradeInputOptionsTests : V4BaseTests() {
"options": {
"needsSize": true,
"needsLeverage": false,
"needsBalancePercent": false,
"needsTriggerPrice": false,
"needsLimitPrice": true,
"needsTrailingPercent": false,
Expand Down

0 comments on commit 660791b

Please sign in to comment.