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

[Bug]: Wrong collateralIncreaseEstimate for limit orders in isolated margin mode #10928

Closed
bglownia opened this issue Mar 19, 2024 · 2 comments · Fixed by #10936
Closed

[Bug]: Wrong collateralIncreaseEstimate for limit orders in isolated margin mode #10928

bglownia opened this issue Mar 19, 2024 · 2 comments · Fixed by #10936
Assignees
Labels

Comments

@bglownia
Copy link
Member

Problem encountered

When there is remaining limit order and collateral on orderMarginAccountBalance and user will try to place limit order in opposite direction collateralIncreaseEstimate is wrongly calculated.

Before placing order - additional margin required is 5.00
image

After placing order - no additional collateral was needed
image

Observed behaviour

Sum of pending orders is used to calculate collateral increase

Expected behaviour

Maximal risk exposure should be used to calculate collateral increase

Steps to reproduce

query EstimatePosition(
  $marketId: ID!
  $openVolume: String!
  $averageEntryPrice: String!
  $orders: [OrderInfo!]
  $marginAccountBalance: String!
  $generalAccountBalance: String!
  $orderMarginAccountBalance: String!
  $marginMode: MarginMode!
  $marginFactor: String
  $includeRequiredPositionMarginInAvailableCollateral: Boolean
) {
  estimatePosition(
    marketId: $marketId
    openVolume: $openVolume
    averageEntryPrice: $averageEntryPrice
    orders: $orders
    marginAccountBalance: $marginAccountBalance
    generalAccountBalance: $generalAccountBalance
    orderMarginAccountBalance: $orderMarginAccountBalance
    marginMode: $marginMode
    marginFactor: $marginFactor
    includeRequiredPositionMarginInAvailableCollateral: $includeRequiredPositionMarginInAvailableCollateral
    # Everywhere in the codebase we expect price values of the underlying to have the right
    # number of digits for formatting with market.decimalPlaces. By default the estimatePosition
    # query will return a full value requiring formatting using asset.decimals. For consistency
    # we can set this variable to true so that we can format with market.decimalPlaces
    scaleLiquidationPriceToMarketDecimals: true
  ) {
    collateralIncreaseEstimate {
      worstCase
      bestCase
    }
  }
}

{
  "marketId": "ac7fc86bddf26748c6ba32a67037fe13c623cd4b53480aac4eaf29fbbd22ac31",
  "openVolume": "0",
  "averageEntryPrice": "357417",
  "orders": [
    {
      "isMarketOrder": false,
      "price": "500000",
      "remaining": "10",
      "side": "SIDE_SELL"
    },
    {
      "isMarketOrder": false,
      "price": "250000",
      "remaining": "20",
      "side": "SIDE_BUY"
    }
  ],
  "marginAccountBalance": "0",
  "generalAccountBalance": "95000000",
  "orderMarginAccountBalance": "5000000",
  "marginFactor": "0.1",
  "marginMode": "MARGIN_MODE_ISOLATED_MARGIN",
  "includeRequiredPositionMarginInAvailableCollateral": true
}

Software version

v0.75.0-preview.8

Failing test

No response

Jenkins run

No response

Configuration used

No response

Relevant log output

No response

@bglownia
Copy link
Member Author

{
  "marketId": "ac7fc86bddf26748c6ba32a67037fe13c623cd4b53480aac4eaf29fbbd22ac31",
  "openVolume": "50",
  "averageEntryPrice": "351805",
  "orders": [
    {
      "isMarketOrder": false,
      "price": "200000",
      "remaining": "100000",
      "side": "SIDE_BUY"
    },
    {
      "isMarketOrder": false,
      "price": "400000",
      "remaining": "12000",
      "side": "SIDE_SELL"
    },
    {
      "isMarketOrder": false,
      "price": "400000",
      "remaining": "20000",
      "side": "SIDE_SELL"
    },
    {
      "isMarketOrder": false,
      "price": "400000",
      "remaining": "20000",
      "side": "SIDE_SELL"
    },
    {
      "isMarketOrder": true,
      "price": "0",
      "remaining": "832",
      "side": "SIDE_BUY"
    }
  ],
  "marginAccountBalance": "17488358",
  "generalAccountBalance": "307978467",
  "orderMarginAccountBalance": "20800000000",
  "marginFactor": "0.1",
  "marginMode": "MARGIN_MODE_ISOLATED_MARGIN",
  "includeRequiredPositionMarginInAvailableCollateral": true
}

returns

{
  "worstCase": "20292633924",
  "bestCase": "20292633924",
  "__typename": "CollateralIncreaseEstimate"
}

Which is totally wrong. Open orders collateral is covered by orderMarginAccountBalance, openVolume collateral is covered by marginAccountBalance. We should get estimate only for last tiny market order and we get also for remaining limit orders.

image image

@gordsport gordsport reopened this Mar 28, 2024
@witgaw
Copy link
Member

witgaw commented Mar 28, 2024

Issue is fixed, the problem is that it's not part of any tag yet.

@witgaw witgaw closed this as completed Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants