-
Notifications
You must be signed in to change notification settings - Fork 21
/
reference_price_moves.go
106 lines (90 loc) · 3.2 KB
/
reference_price_moves.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package execution
import (
"context"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/logging"
)
const (
// PriceMoveMid used to indicate that the mid price has moved.
PriceMoveMid = 1
// PriceMoveBestBid used to indicate that the best bid price has moved.
PriceMoveBestBid = 2
// PriceMoveBestAsk used to indicate that the best ask price has moved.
PriceMoveBestAsk = 4
// PriceMoveAll used to indicate everything has moved.
PriceMoveAll = PriceMoveMid + PriceMoveBestBid + PriceMoveBestAsk
)
type OrderReferenceCheck types.Order
func (o OrderReferenceCheck) HasMoved(changes uint8) bool {
return (o.PeggedOrder.Reference == types.PeggedReferenceMid &&
changes&PriceMoveMid > 0) ||
(o.PeggedOrder.Reference == types.PeggedReferenceBestBid &&
changes&PriceMoveBestBid > 0) ||
(o.PeggedOrder.Reference == types.PeggedReferenceBestAsk &&
changes&PriceMoveBestAsk > 0)
}
func (m *Market) checkForReferenceMoves(
ctx context.Context, orderUpdates []*types.Order, forceUpdate bool,
) {
if m.as.InAuction() {
return
}
// will be set to non-nil if a peg is missing
_, _, err := m.getBestStaticPricesDecimal()
newBestBid, _ := m.getBestStaticBidPrice()
newBestAsk, _ := m.getBestStaticAskPrice()
newMidBuy, _ := m.getStaticMidPrice(types.SideBuy)
newMidSell, _ := m.getStaticMidPrice(types.SideSell)
// Look for a move
var changes uint8
if !forceUpdate {
if newMidBuy.NEQ(m.lastMidBuyPrice) || newMidSell.NEQ(m.lastMidSellPrice) {
changes |= PriceMoveMid
}
if newBestBid.NEQ(m.lastBestBidPrice) {
changes |= PriceMoveBestBid
}
if newBestAsk.NEQ(m.lastBestAskPrice) {
changes |= PriceMoveBestAsk
}
} else {
changes = PriceMoveAll
}
// now we can start all special order repricing...
if err == nil {
orderUpdates = m.repriceAllSpecialOrders(ctx, changes, orderUpdates)
} else {
cancels := m.liquidity.UndeployLPs(ctx, orderUpdates)
orderUpdates, err = m.updateAndCreateLPOrders(ctx, nil, cancels, nil)
if err != nil {
m.log.Panic("missing static prices and error canceling LP orders",
logging.MarketID(m.mkt.GetID()),
logging.Error(err))
}
}
// Update the last price values
// no need to clone the prices, they're not used in calculations anywhere in this function
m.lastMidBuyPrice = newMidBuy
m.lastMidSellPrice = newMidSell
m.lastBestBidPrice = newBestBid
m.lastBestAskPrice = newBestAsk
// now we had new orderUpdates while processing those,
// that would means someone got distressed, so some order
// got uncrossed, so we need to check all these again.
// we do not use the forceUpdate ffield here as it's
// not required that prices moved though
if len(orderUpdates) > 0 {
m.checkForReferenceMoves(ctx, orderUpdates, false)
}
}