-
Notifications
You must be signed in to change notification settings - Fork 19
/
market_position.go
326 lines (284 loc) · 10.1 KB
/
market_position.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package positions
import (
"errors"
"fmt"
"math"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
)
// MarketPosition represents the position of a party inside a market.
type MarketPosition struct {
// Actual volume
size int64
// Potential volume (orders not yet accepted/rejected)
buy, sell int64
partyID string
price *num.Uint
// sum of size*price for party's buy/sell orders
buySumProduct, sellSumProduct *num.Uint
// this doesn't have to be included in checkpoints or snapshots
// yes, it's technically state, but the main reason for this field is to cut down on the number
// of events we send out.
distressed bool
averageEntryPrice *num.Uint
}
func NewMarketPosition(party string) *MarketPosition {
return &MarketPosition{
partyID: party,
price: num.UintZero(),
buySumProduct: num.UintZero(),
sellSumProduct: num.UintZero(),
averageEntryPrice: num.UintZero(),
}
}
func (p MarketPosition) Clone() *MarketPosition {
cpy := p
cpy.price = p.price.Clone()
cpy.buySumProduct = p.buySumProduct.Clone()
cpy.sellSumProduct = p.sellSumProduct.Clone()
cpy.averageEntryPrice = p.averageEntryPrice.Clone()
return &cpy
}
func (p *MarketPosition) Closed() bool {
// p.size can be negative
// p.buy and p.sell can be only positive
return p.size == 0 && p.buy+p.sell == 0
}
// UpdateInPlaceOnTrades takes a clone of the receiver position, and updates it with the position from the given trades.
func (p *MarketPosition) UpdateInPlaceOnTrades(log *logging.Logger, traderSide types.Side, trades []*types.Trade, order *types.Order) *MarketPosition {
pos := p.Clone()
for _, t := range trades {
pos.averageEntryPrice = CalcVWAP(pos.averageEntryPrice, pos.size, int64(t.Size), t.Price)
if traderSide == types.SideBuy {
pos.size += int64(t.Size)
} else {
pos.size -= int64(t.Size)
}
// if we bought then we want to decrease the order size for this side so add=false
// and vice versa for sell
pos.UpdateOnOrderChange(log, traderSide, order.Price, t.Size, false)
}
return pos
}
func (p *MarketPosition) SetParty(party string) { p.partyID = party }
func (p *MarketPosition) RegisterOrder(log *logging.Logger, order *types.Order) {
p.UpdateOnOrderChange(log, order.Side, order.Price, order.TrueRemaining(), true)
}
func (p *MarketPosition) UnregisterOrder(log *logging.Logger, order *types.Order) {
p.UpdateOnOrderChange(log, order.Side, order.Price, order.TrueRemaining(), false)
}
func (p *MarketPosition) UpdateOnOrderChange(log *logging.Logger, side types.Side, price *num.Uint, sizeChange uint64, add bool) {
if sizeChange == 0 {
return
}
iSizeChange := int64(sizeChange)
if side == types.SideBuy {
if !add && p.buy < iSizeChange {
log.Panic("cannot unregister order with potential buy + size change < 0",
logging.Int64("potential-buy", p.buy),
logging.Uint64("size-change", sizeChange))
}
if add && p.buy > math.MaxInt64-iSizeChange {
log.Panic("order too large to register, will overflow",
logging.Int64("potential-buy", p.buy),
logging.Uint64("size-change", sizeChange))
}
// recalculate sumproduct
if add {
p.buySumProduct.Add(p.buySumProduct, num.UintZero().Mul(price, num.NewUint(sizeChange)))
p.buy += iSizeChange
} else {
p.buySumProduct.Sub(p.buySumProduct, num.UintZero().Mul(price, num.NewUint(sizeChange)))
p.buy -= iSizeChange
}
if p.buy == 0 && !p.buySumProduct.IsZero() {
log.Panic("Non-zero buy sum-product with no buy orders",
logging.PartyID(p.partyID),
logging.BigUint("buy-sum-product", p.buySumProduct))
}
return
}
if !add && p.sell < iSizeChange {
log.Panic("cannot unregister order with potential sell + size change < 0",
logging.Int64("potential-sell", p.sell),
logging.Uint64("size-change", sizeChange))
}
if add && p.sell > math.MaxInt64-iSizeChange {
log.Panic("order too large to register, will overflow",
logging.Int64("potential-sell", p.sell),
logging.Uint64("size-change", sizeChange))
}
// recalculate sumproduct
if add {
p.sellSumProduct.Add(p.sellSumProduct, num.UintZero().Mul(price, num.NewUint(sizeChange)))
p.sell += iSizeChange
} else {
p.sellSumProduct.Sub(p.sellSumProduct, num.UintZero().Mul(price, num.NewUint(sizeChange)))
p.sell -= iSizeChange
}
if p.sell == 0 && !p.sellSumProduct.IsZero() {
log.Panic("Non-zero sell sum-product with no sell orders",
logging.PartyID(p.partyID),
logging.BigUint("sell-sum-product", p.sellSumProduct))
}
}
// AmendOrder unregisters the original order and then registers the newly amended order
// this method is a quicker way of handling separate unregister+register pairs.
func (p *MarketPosition) AmendOrder(log *logging.Logger, originalOrder, newOrder *types.Order) {
switch originalOrder.Side {
case types.SideBuy:
if uint64(p.buy) < originalOrder.TrueRemaining() {
log.Panic("cannot amend order with remaining > potential buy",
logging.Order(*originalOrder),
logging.Int64("potential-buy", p.buy))
}
case types.SideSell:
if uint64(p.sell) < originalOrder.TrueRemaining() {
log.Panic("cannot amend order with remaining > potential sell",
logging.Order(*originalOrder),
logging.Int64("potential-sell", p.sell))
}
}
p.UnregisterOrder(log, originalOrder)
p.RegisterOrder(log, newOrder)
}
// String returns a string representation of a market.
func (p MarketPosition) String() string {
return fmt.Sprintf("size:%v, buy:%v, sell:%v, price:%v, partyID:%v",
p.size, p.buy, p.sell, p.price, p.partyID)
}
// AverageEntryPrice returns the volume weighted average price.
func (p MarketPosition) AverageEntryPrice() *num.Uint {
return p.averageEntryPrice
}
// Buy will returns the potential buys for a given position.
func (p MarketPosition) Buy() int64 {
return p.buy
}
// Sell returns the potential sells for the position.
func (p MarketPosition) Sell() int64 {
return p.sell
}
// Size returns the current size of the position.
func (p MarketPosition) Size() int64 {
return p.size
}
// Party returns the party to which this positions is associated.
func (p MarketPosition) Party() string {
return p.partyID
}
// Price returns the current price for this position.
func (p MarketPosition) Price() *num.Uint {
if p.price != nil {
return p.price.Clone()
}
return num.UintZero()
}
// BuySumProduct - get sum of size * price of party's buy orders.
func (p MarketPosition) BuySumProduct() *num.Uint {
if p.buySumProduct != nil {
return p.buySumProduct.Clone()
}
return num.UintZero()
}
// SellSumProduct - get sum of size * price of party's sell orders.
func (p MarketPosition) SellSumProduct() *num.Uint {
if p.sellSumProduct != nil {
return p.sellSumProduct.Clone()
}
return num.UintZero()
}
// VWBuy - get volume weighted buy price for unmatched buy orders.
func (p MarketPosition) VWBuy() *num.Uint {
if p.buySumProduct != nil && p.buy != 0 {
vol := num.NewUint(uint64(p.buy))
return vol.Div(p.buySumProduct, vol)
}
return num.UintZero()
}
// VWSell - get volume weighted sell price for unmatched sell orders.
func (p MarketPosition) VWSell() *num.Uint {
if p.sellSumProduct != nil && p.sell != 0 {
vol := num.NewUint(uint64(p.sell))
return vol.Div(p.sellSumProduct, vol)
}
return num.UintZero()
}
// ValidateOrder returns an error is the order is so large that the position engine does not have the precision
// to register it.
func (p MarketPosition) ValidateOrderRegistration(s uint64, side types.Side) error {
size := int64(s)
if size == 0 {
return nil
}
// check that the cast to int64 hasn't pushed it backwards
if size < 0 {
return errors.New("cannot register position without causing overflow")
}
amt := p.buy
if side == types.SideSell {
amt = p.sell
}
if size > math.MaxInt64-amt {
return errors.New("cannot register position without causing overflow")
}
return nil
}
func (p MarketPosition) OrderReducesExposure(ord *types.Order) bool {
if ord == nil || p.Size() == 0 || ord.PeggedOrder != nil {
return false
}
// long position and short order
if p.Size() > 0 && ord.Side == types.SideSell {
// market order reduces exposure and doesn't flip position to the other side
if p.Size()-int64(ord.Remaining) >= 0 && ord.Type == types.OrderTypeMarket {
return true
}
// sum of all short limit orders wouldn't flip the position if filled (ord already included in pos)
if p.Size()-p.Sell() >= 0 && ord.Type == types.OrderTypeLimit {
return true
}
}
// short position and long order
if p.Size() < 0 && ord.Side == types.SideBuy {
// market order reduces exposure and doesn't flip position to the other side
if p.Size()+int64(ord.Remaining) <= 0 && ord.Type == types.OrderTypeMarket {
return true
}
// sum of all long limit orders wouldn't flip the position if filled (ord already included in pos)
if p.Size()+p.Buy() <= 0 && ord.Type == types.OrderTypeLimit {
return true
}
}
return false
}
// OrderReducesOnlyExposure returns true if the order reduce the position and the extra size if it was to flip the position side.
func (p MarketPosition) OrderReducesOnlyExposure(ord *types.Order) (reduce bool, extraSize uint64) {
// if already closed, or increasing position, we shortcut
if p.Size() == 0 || (p.Size() < 0 && ord.Side == types.SideSell) || (p.Size() > 0 && ord.Side == types.SideBuy) {
return false, 0
}
size := p.Size()
if size < 0 {
size = -size
}
if extraSizeI := size - int64(ord.Remaining); extraSizeI < 0 {
return true, uint64(-extraSizeI)
}
return true, 0
}