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

TRCL-3644 Static Typing: TriggerOrder/SL/TP #597

Merged
merged 88 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 86 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
6b4c391
WIP
ruixhuang Aug 1, 2024
2d988f6
Merge branch 'main' into feature/markets_1
ruixhuang Aug 2, 2024
2522191
MarketProcessor
ruixhuang Aug 2, 2024
73868c0
Bump version
mobile-build-bot Aug 2, 2024
5b30eef
Sparklines
ruixhuang Aug 3, 2024
0cea168
Bump version
mobile-build-bot Aug 3, 2024
3d6fbb6
Lint
ruixhuang Aug 3, 2024
31c3acd
Merge branch 'feature/markets_1' into feature/markets_2
ruixhuang Aug 3, 2024
e4c712c
WIP
ruixhuang Aug 5, 2024
26f1630
Merge branch 'main' into feature/markets_2
ruixhuang Aug 5, 2024
cd79d5a
Merge branch 'feature/markets_2' into feature/markets_3
ruixhuang Aug 5, 2024
f03b424
Orderbook
ruixhuang Aug 6, 2024
e8e5602
Updated codegen for candles
ruixhuang Aug 6, 2024
4a6a15f
Market Candles
ruixhuang Aug 6, 2024
462a8ff
Bump version
mobile-build-bot Aug 6, 2024
d52adce
Add market trades to the output state.
ruixhuang Aug 7, 2024
60d8eb6
Merge branch 'feature/markets_4' of github.com:dydxprotocol/v4-abacus…
ruixhuang Aug 7, 2024
5ce9fdb
Revert "Merge branch 'feature/markets_4' of github.com:dydxprotocol/v…
ruixhuang Aug 7, 2024
c4c3730
Revert "Add market trades to the output state."
ruixhuang Aug 7, 2024
7a9ce0a
Bump version
mobile-build-bot Aug 7, 2024
a688b82
Add market trades to the output state.
ruixhuang Aug 7, 2024
0d1d2e6
Merge branch 'feature/markets_4' into feature/markets_6
ruixhuang Aug 7, 2024
10cf01e
Bump version
mobile-build-bot Aug 7, 2024
ab3fd0d
WIP
ruixhuang Aug 8, 2024
ea1c620
WIP
ruixhuang Aug 8, 2024
d46757a
WIP
ruixhuang Aug 9, 2024
0c76dfa
Fix tests
ruixhuang Aug 9, 2024
d954e21
Bump version
mobile-build-bot Aug 9, 2024
19dcd33
Merge fills and transfers
ruixhuang Aug 9, 2024
dc2e60a
Merge branch 'feature/calc_1' of github.com:dydxprotocol/v4-abacus in…
ruixhuang Aug 9, 2024
98a2df6
Merge branch 'feature/calc_1' into feature/trade_1
ruixhuang Aug 9, 2024
5fc6c60
WIP
ruixhuang Aug 10, 2024
a3916b5
WIP
ruixhuang Aug 12, 2024
222d283
WIP
ruixhuang Aug 12, 2024
aea8014
Merge branch 'main' into feature/calc_1
ruixhuang Aug 12, 2024
3eb966a
WIP
ruixhuang Aug 13, 2024
e829519
Merge branch 'feature/calc_1' into feature/trade_1
ruixhuang Aug 13, 2024
3c81c65
Working more or less
ruixhuang Aug 13, 2024
1fd42df
Updating tests
ruixhuang Aug 13, 2024
ab7858f
Bump version
mobile-build-bot Aug 13, 2024
88e1aa6
Merge branch 'main' into feature/calc_1
ruixhuang Aug 13, 2024
75007fc
Lint
ruixhuang Aug 13, 2024
1975e72
Lint
ruixhuang Aug 13, 2024
952c97f
Move sides and order types options to a single source
ruixhuang Aug 13, 2024
f5f4dfa
Add isOpen
ruixhuang Aug 13, 2024
545fe6b
Cleanup
ruixhuang Aug 13, 2024
15c0678
Merge branch 'feature/calc_1' into feature/trade_1
ruixhuang Aug 13, 2024
048a2dd
Bump version
mobile-build-bot Aug 13, 2024
5cb9afb
Clean up
ruixhuang Aug 13, 2024
b86eadc
Lint
ruixhuang Aug 13, 2024
3df5e4f
WIP
ruixhuang Aug 14, 2024
07cb4ea
Merge branch 'main' into feature/trade_1
ruixhuang Aug 15, 2024
d4ca43b
Merge branch 'feature/trade_1' into feature/trade_2
ruixhuang Aug 15, 2024
c27e381
AccountTransformer
ruixhuang Aug 15, 2024
af9b520
Trade receipts
ruixhuang Aug 15, 2024
7034e0a
Fix position list
ruixhuang Aug 15, 2024
033b44e
Fix a position bug
ruixhuang Aug 16, 2024
e25d771
Fix issue with null position
ruixhuang Aug 16, 2024
3b21a90
Deep-copy to groupedSubaccount
ruixhuang Aug 16, 2024
7b8791e
WIP
ruixhuang Aug 18, 2024
d50b093
TradeInputDataValidator
ruixhuang Aug 18, 2024
b847b40
TradeInputValidator
ruixhuang Aug 20, 2024
07b3cfb
Clean up
ruixhuang Aug 20, 2024
6227a52
Clean up
ruixhuang Aug 20, 2024
d412fa2
Merge branch 'main' into feature/trade_3
ruixhuang Aug 20, 2024
fbaf697
Bump version
mobile-build-bot Aug 20, 2024
8c1def9
TradeFieldsValidator
ruixhuang Aug 20, 2024
0a2bd04
AccountInputValidator
ruixhuang Aug 21, 2024
270b53b
Update integration tests
ruixhuang Aug 22, 2024
b24e08b
Merge branch 'main' into feature/trade_3
ruixhuang Aug 22, 2024
7f65928
Bump version
mobile-build-bot Aug 22, 2024
5d89f08
Merge branch 'feature/trade_3' into feature/trade_4
ruixhuang Aug 22, 2024
1469a14
Merge branch 'main' into feature/trade_4
ruixhuang Aug 22, 2024
6071494
Merge branch 'feature/trade_4' of github.com:dydxprotocol/v4-abacus i…
ruixhuang Aug 22, 2024
38f2b33
Bump version
mobile-build-bot Aug 22, 2024
1b425a2
ClosePositionInput
ruixhuang Aug 22, 2024
36b34f2
Validation
ruixhuang Aug 22, 2024
c3c49cd
Merge branch 'main' into feature/trade_4
ruixhuang Aug 22, 2024
4e0627d
Bump version
mobile-build-bot Aug 22, 2024
2698c1b
Merge branch 'feature/trade_4' into feature/trade_5
ruixhuang Aug 22, 2024
89fd283
Lint
ruixhuang Aug 22, 2024
ddbe267
Lint
ruixhuang Aug 22, 2024
c8ee7d8
Lint
ruixhuang Aug 22, 2024
6cd6cca
WIP
ruixhuang Aug 23, 2024
cb4580b
SL/TP
ruixhuang Aug 23, 2024
50185be
Bump version
mobile-build-bot Aug 23, 2024
4ddcf9b
Merge branch 'main' into feature/trade_6
ruixhuang Aug 26, 2024
4428e8c
Bump version
mobile-build-bot Aug 26, 2024
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.8.101"
version = "1.8.105"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import kotlin.math.min

internal object MarginCalculator {
fun findExistingPosition(
account: InternalAccountState,
account: InternalAccountState?,
marketId: String?,
subaccountNumber: Int,
): InternalPerpetualPosition? {
val position = account.groupedSubaccounts[subaccountNumber]?.openPositions?.get(marketId)
val position = account?.groupedSubaccounts?.get(subaccountNumber)?.openPositions?.get(marketId)
return if (
(position?.size ?: 0.0) != 0.0
) {
Expand Down Expand Up @@ -420,6 +420,16 @@ internal object MarginCalculator {
}

fun getChildSubaccountNumberForIsolatedMarginClosePosition(
account: InternalAccountState?,
subaccountNumber: Int,
tradeInput: InternalTradeInputState?
): Int {
val marketId = tradeInput?.marketId ?: return subaccountNumber
val position = findExistingPosition(account, marketId, subaccountNumber)
return position?.subaccountNumber ?: subaccountNumber
}

fun getChildSubaccountNumberForIsolatedMarginClosePositionDeprecated(
parser: ParserProtocol,
account: Map<String, Any>?,
subaccountNumber: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ internal class AccountCalculatorV2(
val modifiedOpenPositions = parentOpenPositions?.toMutableMap() ?: mutableMapOf()
val childOpenPositions = childSubaccount.openPositions
for ((market, childOpenPosition) in childOpenPositions ?: emptyMap()) {
// modifiedChildOpenPosition?.safeSet(
// "childSubaccountNumber",
// childSubaccountNumber,
// )
childOpenPosition.childSubaccountNumber = childSubaccountNumber

modifiedOpenPositions[market] = childOpenPosition
}
parentSubaccount.openPositions = modifiedOpenPositions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package exchange.dydx.abacus.calculator.v2.tradeinput

import abs
import exchange.dydx.abacus.calculator.CalculationPeriod
import exchange.dydx.abacus.calculator.TradeCalculation
import exchange.dydx.abacus.calculator.v2.AccountTransformerV2
import exchange.dydx.abacus.output.FeeTier
import exchange.dydx.abacus.output.input.MarginMode
import exchange.dydx.abacus.output.input.OrderSide
import exchange.dydx.abacus.output.input.OrderType
import exchange.dydx.abacus.output.input.TradeInputSize
import exchange.dydx.abacus.protocols.ParserProtocol
import exchange.dydx.abacus.state.internalstate.InternalAccountState
import exchange.dydx.abacus.state.internalstate.InternalConfigsState
Expand All @@ -16,12 +19,16 @@ import exchange.dydx.abacus.state.internalstate.InternalSubaccountState
import exchange.dydx.abacus.state.internalstate.InternalTradeInputState
import exchange.dydx.abacus.state.internalstate.InternalUserState
import exchange.dydx.abacus.state.internalstate.InternalWalletState
import exchange.dydx.abacus.state.internalstate.safeCreate
import exchange.dydx.abacus.state.model.ClosePositionInputField
import exchange.dydx.abacus.utils.Numeric
import exchange.dydx.abacus.utils.Rounder

internal class TradeInputCalculatorV2(
private val parser: ParserProtocol,
private val calculation: TradeCalculation,
private val marginModeCalculator: TradeInputMarginModeCalculator = TradeInputMarginModeCalculator(),
private val marketOrderCalculator: TradeInputMarketOrderCalculator = TradeInputMarketOrderCalculator(calculation),
private val marketOrderCalculator: TradeInputMarketOrderCalculator = TradeInputMarketOrderCalculator(),
private val nonMarketOrderCalculator: TradeInputNonMarketOrderCalculator = TradeInputNonMarketOrderCalculator(),
private val optionsCalculator: TradeInputOptionsCalculator = TradeInputOptionsCalculator(parser),
private val summaryCalculator: TradeInputSummaryCalculator = TradeInputSummaryCalculator(),
Expand Down Expand Up @@ -52,6 +59,9 @@ internal class TradeInputCalculatorV2(
)

if (input != null) {
if (calculation == TradeCalculation.closePosition) {
calculateClosePositionSize(trade, markets[trade.marketId], subaccount)
}
when (trade.type) {
OrderType.Market,
OrderType.StopMarket,
Expand Down Expand Up @@ -103,6 +113,41 @@ internal class TradeInputCalculatorV2(
return trade
}

private fun calculateClosePositionSize(
trade: InternalTradeInputState,
market: InternalMarketState?,
subaccount: InternalSubaccountState?,
): InternalTradeInputState {
val inputType = ClosePositionInputField.invoke(trade.size?.input)
val marketId = trade.marketId ?: return trade
val position = subaccount?.openPositions?.get(marketId) ?: return trade
val positionSize = position.calculated[CalculationPeriod.current]?.size ?: return trade
val positionSizeAbs = positionSize.abs()
trade.side = if (positionSize > Numeric.double.ZERO) OrderSide.Sell else OrderSide.Buy
when (inputType) {
ClosePositionInputField.percent -> {
val percent = trade.sizePercent ?: return trade
val size =
if (percent > Numeric.double.ONE) positionSizeAbs else positionSizeAbs * percent
val stepSize = market?.perpetualMarket?.configs?.stepSize ?: return trade
trade.size =
TradeInputSize.safeCreate(trade.size).copy(size = Rounder.round(size, stepSize))
return trade
}

ClosePositionInputField.size -> {
trade.sizePercent = null
val size = trade.size?.size ?: return trade
if (size > positionSizeAbs) {
trade.size = TradeInputSize.safeCreate(trade.size).copy(size = positionSizeAbs)
}
}

else -> {}
}
return trade
}

private fun finalize(
trade: InternalTradeInputState,
account: InternalAccountState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package exchange.dydx.abacus.calculator.v2.tradeinput

import abs
import exchange.dydx.abacus.calculator.CalculationPeriod
import exchange.dydx.abacus.calculator.TradeCalculation
import exchange.dydx.abacus.output.input.OrderSide
import exchange.dydx.abacus.output.input.OrderbookUsage
import exchange.dydx.abacus.output.input.TradeInputMarketOrder
Expand All @@ -16,24 +15,18 @@ import exchange.dydx.abacus.state.internalstate.InternalSubaccountState
import exchange.dydx.abacus.state.internalstate.InternalTradeInputState
import exchange.dydx.abacus.state.internalstate.InternalUserState
import exchange.dydx.abacus.state.internalstate.safeCreate
import exchange.dydx.abacus.state.model.ClosePositionInputField
import exchange.dydx.abacus.utils.Numeric
import exchange.dydx.abacus.utils.Rounder
import kollections.toIList

internal class TradeInputMarketOrderCalculator(
private val calculation: TradeCalculation,
) {
internal class TradeInputMarketOrderCalculator() {
fun calculate(
trade: InternalTradeInputState,
market: InternalMarketState?,
subaccount: InternalSubaccountState?,
user: InternalUserState?,
input: String?,
): InternalTradeInputState {
if (calculation == TradeCalculation.closePosition) {
calculateClosePositionSize(trade, market, subaccount)
}
val marketOrder = createMarketOrder(
trade = trade,
market = market,
Expand Down Expand Up @@ -87,41 +80,6 @@ internal class TradeInputMarketOrderCalculator(
}
}

private fun calculateClosePositionSize(
trade: InternalTradeInputState,
market: InternalMarketState?,
subaccount: InternalSubaccountState?,
): InternalTradeInputState {
val inputType = ClosePositionInputField.invoke(trade.size?.input)
val marketId = trade.marketId ?: return trade
val position = subaccount?.openPositions?.get(marketId) ?: return trade
val positionSize = position.calculated[CalculationPeriod.current]?.size ?: return trade
val positionSizeAbs = positionSize.abs()
trade.side = if (positionSize > Numeric.double.ZERO) OrderSide.Sell else OrderSide.Buy
when (inputType) {
ClosePositionInputField.percent -> {
val percent = trade.sizePercent ?: return trade
val size =
if (percent > Numeric.double.ONE) positionSizeAbs else positionSizeAbs * percent
val stepSize = market?.perpetualMarket?.configs?.stepSize ?: return trade
trade.size =
TradeInputSize.safeCreate(trade.size).copy(size = Rounder.round(size, stepSize))
return trade
}

ClosePositionInputField.size -> {
trade.sizePercent = null
val size = trade.size?.size ?: return trade
if (size > positionSizeAbs) {
trade.size = TradeInputSize.safeCreate(trade.size).copy(size = positionSizeAbs)
}
}

else -> {}
}
return trade
}

private fun createMarketOrder(
trade: InternalTradeInputState,
market: InternalMarketState?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class TradeInputNonMarketOrderCalculator {
val stepSize = market?.perpetualMarket?.configs?.stepSize ?: 0.001
val price = getNonMarketOrderPrice(tradePrices, market, trade.type, isBuying)
when (input) {
"size.size" -> {
"size.size", "size.percent" -> {
val size = tradeSize.size
val usdcSize =
if (price != null && size != null) (price * size) else null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ internal class TradeInputOptionsCalculator(
market = market,
)
if (options.executionOptions != null) {
if (trade.execution == null) {
val types = options.executionOptions?.map { it.type }
if (trade.execution == null || types?.contains(trade.execution) == false) {
trade.execution = options.executionOptions?.firstOrNull()?.type
}
}
Expand All @@ -289,7 +290,8 @@ internal class TradeInputOptionsCalculator(
market = market,
)
if (options.marginModeOptions != null) {
if (trade.marginMode == null) {
val types = options.marginModeOptions?.map { MarginMode.invoke(it.type) }
if (trade.marginMode == null || types?.contains(trade.marginMode) == false) {
trade.marginMode = MarginMode.invoke(options.marginModeOptions?.firstOrNull()?.type)
}
}
Expand Down
Loading
Loading