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

various wash trade fixes. #2262

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
37 changes: 8 additions & 29 deletions execution/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ func (m *Market) SubmitOrder(ctx context.Context, order *types.Order) (*types.Or
}

// Perform check and allocate margin
newOrderMarginRiskRollback, err := m.checkMarginForOrder(ctx, pos, order)
_, err = m.checkMarginForOrder(ctx, pos, order)
if err != nil {
_, err1 := m.position.UnregisterOrder(order)
if err1 != nil {
Expand Down Expand Up @@ -766,42 +766,21 @@ func (m *Market) SubmitOrder(ctx context.Context, order *types.Order) (*types.Or
}

// if order was FOK or IOC some or all of it may have not be consumed, so we need to
// or if the order was stopped because of a wash trade
// remove them from the potential orders,
// then we should be able to process the rest of the order properly.
if (order.TimeInForce == types.Order_TIF_FOK || order.TimeInForce == types.Order_TIF_IOC || order.Status == types.Order_STATUS_STOPPED) &&
confirmation.Order.Remaining != 0 {
if ((order.TimeInForce == types.Order_TIF_FOK ||
order.TimeInForce == types.Order_TIF_IOC ||
order.Status == types.Order_STATUS_STOPPED) &&
confirmation.Order.Remaining != 0) ||
// Also do it if specifically we went against a wash trade
(order.Status == types.Order_STATUS_REJECTED &&
order.Reason == types.OrderError_ORDER_ERROR_SELF_TRADING) {
_, err := m.position.UnregisterOrder(order)
if err != nil {
m.log.Error("Unable to unregister potential trader positions",
logging.String("market-id", m.GetID()),
logging.Error(err))
}

// if the specific case we are in and FOK or IOC
// we moved some margin, which may never be released
// as we never create a position in the settlement engine.
// to be fair the monies would be released later on if the
// party place an order which stay in the book / trade.
// but in the case the party is actually neve used again
// the funds woulds be locked on the margin account until
// the market is being closed.
// we also check the margin risk update was not nil, as it's not
// guaranteed the trader had to pay any margin
asset, _ := m.mkt.GetAsset()
if order.Remaining == order.Size && newOrderMarginRiskRollback != nil {
transfers, err := m.collateral.RollbackMarginUpdateOnOrder(
ctx, m.GetID(), asset, newOrderMarginRiskRollback)
if err != nil {
m.log.Error("Unable to rollback risk updates",
logging.String("market-id", m.GetID()),
logging.Error(err))
}
evt := events.NewTransferResponse(
ctx, []*types.TransferResponse{transfers})
m.broker.Send(evt)
}

}

// Insert aggressive remaining order
Expand Down
40 changes: 37 additions & 3 deletions integration/features/ensure-funds-are-released.feature
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ Feature: Test margins releases on position = 0
| name | asset |
| traderGuy | BTC |

# setup previous mark price
Then traders place following orders:
| trader | id | type | volume | price | resulting trades | type | tif |

Then traders place following orders with references:
| trader | id | type | volume | price | resulting trades | type | tif | reference |
Expand All @@ -90,3 +87,40 @@ Feature: Test margins releases on position = 0
Then I expect the trader to have a margin:
| trader | asset | id | margin | general |
| traderGuy | BTC | ETH/DEC19 | 0 | 1000000000 |

Scenario: No margin left for wash trade after cancelling first order
# setup accounts
Given the following traders:
| name | amount |
| traderGuy | 1000000000 |
Then I Expect the traders to have new general account:
| name | asset |
| traderGuy | BTC |

Then traders place following orders with references:
| trader | id | type | volume | price | resulting trades | type | tif | reference |
| traderGuy | ETH/DEC19 | buy | 13 | 15000 | 0 | TYPE_LIMIT | TIF_GTC | ref-1 |

# checking margins
Then I expect the trader to have a margin:
| trader | asset | id | margin | general |
| traderGuy | BTC | ETH/DEC19 | 980 | 999999020 |

# now we place an order which would wash trade and see
Then traders place following orders:
| trader | id | type | volume | price | resulting trades | type | tif |
| traderGuy | ETH/DEC19 | sell | 13 | 15000 | 0 | TYPE_LIMIT | TIF_GTC |

# checking margins, should have the margins required for the current order
Then I expect the trader to have a margin:
| trader | asset | id | margin | general |
| traderGuy | BTC | ETH/DEC19 | 980 | 999999020 |

# cancel the first order
Then traders cancels the following orders reference:
| trader | reference |
| traderGuy | ref-1 |

Then I expect the trader to have a margin:
| trader | asset | id | margin | general |
| traderGuy | BTC | ETH/DEC19 | 0 | 1000000000 |
4 changes: 2 additions & 2 deletions matching/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ func (b *OrderBook) GetTrades(order *types.Order) ([]*types.Trade, error) {

if err != nil {
if err == ErrWashTrade {
// we still want to submit this order, but know there will be no trades coming out of it
return nil, nil
// we still want to submit this order, there might be trades coming out of it
return trades, nil
}
// some random error happened, return both trades and error
// this is a case that isn't covered by the current SubmitOrder call
Expand Down