Skip to content

Commit

Permalink
use optimistic execution price
Browse files Browse the repository at this point in the history
  • Loading branch information
aforaleka committed Apr 11, 2024
1 parent ec506bd commit 1718d08
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class AccountTransformer() {
market: Map<String, Any>?,
parser: ParserProtocol,
period: String,
usePessimisticCollateralCheck: Boolean
usePessimisticCollateralCheck: Boolean,
useOptimisticCollateralCheck: Boolean
): Map<String, Any>? {
val modified = account?.mutable() ?: return null
val subaccount = if (subaccountNumber != null) {
Expand All @@ -27,7 +28,15 @@ class AccountTransformer() {
null
}
val modifiedSubaccount =
subaccountTransformer.applyTradeToSubaccount(subaccount, trade, market, parser, period, usePessimisticCollateralCheck)
subaccountTransformer.applyTradeToSubaccount(
subaccount,
trade,
market,
parser,
period,
usePessimisticCollateralCheck,
useOptimisticCollateralCheck,
)
modified.safeSet("subaccounts.$subaccountNumber", modifiedSubaccount)
return modified
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ internal class SubaccountTransformer {
limitPrice: Double?,
isBuying: Boolean,
usePessimisticPrice: Boolean,
useOptimisticPrice: Boolean,
): Double? {
if (usePessimisticPrice) {
oraclePrice?.let { oraclePrice ->
limitPrice?.let { limitPrice ->
oraclePrice?.let { oraclePrice ->
limitPrice?.let { limitPrice ->
if (usePessimisticPrice) {
return if (isBuying) {
max(oraclePrice, limitPrice)
} else {
min(oraclePrice, limitPrice)
}
} else if (useOptimisticPrice) {
return if (isBuying) {
min(oraclePrice, limitPrice)
} else {
max(oraclePrice, limitPrice)
}
}
}
}
Expand All @@ -49,6 +56,7 @@ internal class SubaccountTransformer {
trade: Map<String, Any>,
market: Map<String, Any>?,
usePessimisticCollateralCheck: Boolean,
useOptimisticCollateralCheck: Boolean,
): Map<String, Any>? {
val marketId = parser.asString(trade["marketId"])
val side = parser.asString(trade["side"])
Expand All @@ -64,6 +72,7 @@ internal class SubaccountTransformer {
originalPrice,
side == "BUY",
usePessimisticCollateralCheck,
useOptimisticCollateralCheck,
)
} ?: originalPrice
val size = (
Expand Down Expand Up @@ -196,10 +205,17 @@ internal class SubaccountTransformer {
market: Map<String, Any>?,
parser: ParserProtocol,
period: String,
usePessimisticCollateralCheck: Boolean
usePessimisticCollateralCheck: Boolean,
useOptimisticCollateralCheck: Boolean,
): Map<String, Any>? {
if (subaccount != null) {
val delta = deltaFromTrade(parser, trade, market, usePessimisticCollateralCheck)
val delta = deltaFromTrade(
parser,
trade,
market,
usePessimisticCollateralCheck,
useOptimisticCollateralCheck,
)
return applyDeltaToSubaccount(subaccount, delta, parser, period)
}
return subaccount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ internal class TradeInputCalculator(
parser,
"postOrder",
featureFlags.usePessimisticCollateralCheck,
featureFlags.useOptimisticCollateralCheck,
),
)
modified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ data class EnvironmentLinks(
data class EnvironmentFeatureFlags(
val reduceOnlySupported: Boolean,
val usePessimisticCollateralCheck: Boolean,
val useOptimisticCollateralCheck: Boolean,
val withdrawalSafetyEnabled: Boolean,
) {
companion object {
Expand All @@ -112,11 +113,13 @@ data class EnvironmentFeatureFlags(
): EnvironmentFeatureFlags {
val reduceOnlySupported = parser.asBool(data?.get("reduceOnlySupported")) ?: false
val usePessimisticCollateralCheck = parser.asBool(data?.get("usePessimisticCollateralCheck")) ?: false
val useOptimisticCollateralCheck = parser.asBool(data?.get("useOptimisticCollateralCheck")) ?: false
val withdrawalSafetyEnabled = parser.asBool(data?.get("withdrawalSafetyEnabled")) ?: false

return EnvironmentFeatureFlags(
reduceOnlySupported,
usePessimisticCollateralCheck,
useOptimisticCollateralCheck,
withdrawalSafetyEnabled,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class AnalyticsUtils {
* @param payload HumanReadableCancelOrderPayload
* @param fromSlTpDialog Boolean
*/
fun formatCancelOrderPayload(payload: HumanReadableCancelOrderPayload, fromSlTpDialog: Boolean? = false,): IMap<String, Any>? {
fun formatCancelOrderPayload(
payload: HumanReadableCancelOrderPayload,
fromSlTpDialog: Boolean? = false,
): IMap<String, Any>? {
return iMapOf(
"fromSlTpDialog" to fromSlTpDialog,
"subaccountNumber" to payload.subaccountNumber,
Expand Down
Loading

0 comments on commit 1718d08

Please sign in to comment.