-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
position.go
583 lines (482 loc) · 15.5 KB
/
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package types
import (
"fmt"
"sync"
"time"
"github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/util/templateutil"
)
type PositionType string
const (
PositionShort = PositionType("Short")
PositionLong = PositionType("Long")
PositionClosed = PositionType("Closed")
)
type ExchangeFee struct {
MakerFeeRate fixedpoint.Value
TakerFeeRate fixedpoint.Value
}
type PositionRisk struct {
Leverage fixedpoint.Value `json:"leverage"`
LiquidationPrice fixedpoint.Value `json:"liquidationPrice"`
}
type Position struct {
Symbol string `json:"symbol" db:"symbol"`
BaseCurrency string `json:"baseCurrency" db:"base"`
QuoteCurrency string `json:"quoteCurrency" db:"quote"`
Market Market `json:"market,omitempty"`
Base fixedpoint.Value `json:"base" db:"base"`
Quote fixedpoint.Value `json:"quote" db:"quote"`
AverageCost fixedpoint.Value `json:"averageCost" db:"average_cost"`
// ApproximateAverageCost adds the computed fee in quote in the average cost
// This is used for calculating net profit
ApproximateAverageCost fixedpoint.Value `json:"approximateAverageCost"`
FeeRate *ExchangeFee `json:"feeRate,omitempty"`
ExchangeFeeRates map[ExchangeName]ExchangeFee `json:"exchangeFeeRates"`
// TotalFee stores the fee currency -> total fee quantity
TotalFee map[string]fixedpoint.Value `json:"totalFee" db:"-"`
OpenedAt time.Time `json:"openedAt,omitempty" db:"-"`
ChangedAt time.Time `json:"changedAt,omitempty" db:"changed_at"`
Strategy string `json:"strategy,omitempty" db:"strategy"`
StrategyInstanceID string `json:"strategyInstanceID,omitempty" db:"strategy_instance_id"`
AccumulatedProfit fixedpoint.Value `json:"accumulatedProfit,omitempty" db:"accumulated_profit"`
sync.Mutex
// Modify position callbacks
modifyCallbacks []func(baseQty fixedpoint.Value, quoteQty fixedpoint.Value, price fixedpoint.Value)
}
func (p *Position) CsvHeader() []string {
return []string{
"symbol",
"time",
"average_cost",
"base",
"quote",
"accumulated_profit",
}
}
func (p *Position) CsvRecords() [][]string {
if p.AverageCost.IsZero() && p.Base.IsZero() {
return nil
}
return [][]string{
{
p.Symbol,
p.ChangedAt.UTC().Format(time.RFC1123),
p.AverageCost.String(),
p.Base.String(),
p.Quote.String(),
p.AccumulatedProfit.String(),
},
}
}
// NewProfit generates the profit object from the current position
func (p *Position) NewProfit(trade Trade, profit, netProfit fixedpoint.Value) Profit {
return Profit{
Symbol: p.Symbol,
QuoteCurrency: p.QuoteCurrency,
BaseCurrency: p.BaseCurrency,
AverageCost: p.AverageCost,
// profit related fields
Profit: profit,
NetProfit: netProfit,
ProfitMargin: profit.Div(trade.QuoteQuantity),
NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
// trade related fields
Trade: &trade,
TradeID: trade.ID,
OrderID: trade.OrderID,
Side: trade.Side,
IsBuyer: trade.IsBuyer,
IsMaker: trade.IsMaker,
Price: trade.Price,
Quantity: trade.Quantity,
QuoteQuantity: trade.QuoteQuantity,
// FeeInUSD: 0,
Fee: trade.Fee,
FeeCurrency: trade.FeeCurrency,
Exchange: trade.Exchange,
IsMargin: trade.IsMargin,
IsFutures: trade.IsFutures,
IsIsolated: trade.IsIsolated,
TradedAt: trade.Time.Time(),
Strategy: p.Strategy,
StrategyInstanceID: p.StrategyInstanceID,
PositionOpenedAt: p.OpenedAt,
}
}
// ROI -- Return on investment (ROI) is a performance measure used to evaluate the efficiency or profitability of an investment
// or compare the efficiency of a number of different investments.
// ROI tries to directly measure the amount of return on a particular investment, relative to the investment's cost.
func (p *Position) ROI(price fixedpoint.Value) fixedpoint.Value {
unrealizedProfit := p.UnrealizedProfit(price)
cost := p.AverageCost.Mul(p.Base.Abs())
return unrealizedProfit.Div(cost)
}
func (p *Position) NewMarketCloseOrder(percentage fixedpoint.Value) *SubmitOrder {
base := p.GetBase()
quantity := base.Abs()
if percentage.Compare(fixedpoint.One) < 0 {
quantity = quantity.Mul(percentage)
}
if quantity.Compare(p.Market.MinQuantity) < 0 {
return nil
}
side := SideTypeSell
sign := base.Sign()
if sign == 0 {
return nil
} else if sign < 0 {
side = SideTypeBuy
}
return &SubmitOrder{
Symbol: p.Symbol,
Market: p.Market,
Type: OrderTypeMarket,
Side: side,
Quantity: quantity,
MarginSideEffect: SideEffectTypeAutoRepay,
}
}
func (p *Position) IsDust(price fixedpoint.Value) bool {
base := p.Base.Abs()
return p.Market.IsDustQuantity(base, price)
}
// GetBase locks the mutex and return the base quantity
// The base quantity can be negative
func (p *Position) GetBase() (base fixedpoint.Value) {
p.Lock()
base = p.Base
p.Unlock()
return base
}
func (p *Position) GetQuantity() fixedpoint.Value {
base := p.GetBase()
return base.Abs()
}
func (p *Position) UnrealizedProfit(price fixedpoint.Value) fixedpoint.Value {
quantity := p.GetBase().Abs()
if p.IsLong() {
return price.Sub(p.AverageCost).Mul(quantity)
} else if p.IsShort() {
return p.AverageCost.Sub(price).Mul(quantity)
}
return fixedpoint.Zero
}
func (p *Position) OnModify(cb func(baseQty fixedpoint.Value, quoteQty fixedpoint.Value, price fixedpoint.Value)) {
p.modifyCallbacks = append(p.modifyCallbacks, cb)
}
func (p *Position) EmitModify(baseQty fixedpoint.Value, quoteQty fixedpoint.Value, price fixedpoint.Value) {
for _, cb := range p.modifyCallbacks {
cb(baseQty, quoteQty, price)
}
}
// ModifyBase modifies position base quantity with `qty`
func (p *Position) ModifyBase(qty fixedpoint.Value) error {
p.Base = qty
p.EmitModify(p.Base, p.Quote, p.AverageCost)
return nil
}
// ModifyQuote modifies position quote quantity with `qty`
func (p *Position) ModifyQuote(qty fixedpoint.Value) error {
p.Quote = qty
p.EmitModify(p.Base, p.Quote, p.AverageCost)
return nil
}
// ModifyAverageCost modifies position average cost with `price`
func (p *Position) ModifyAverageCost(price fixedpoint.Value) error {
p.AverageCost = price
p.EmitModify(p.Base, p.Quote, p.AverageCost)
return nil
}
type FuturesPosition struct {
Symbol string `json:"symbol"`
BaseCurrency string `json:"baseCurrency"`
QuoteCurrency string `json:"quoteCurrency"`
Market Market `json:"market"`
Base fixedpoint.Value `json:"base"`
Quote fixedpoint.Value `json:"quote"`
AverageCost fixedpoint.Value `json:"averageCost"`
// ApproximateAverageCost adds the computed fee in quote in the average cost
// This is used for calculating net profit
ApproximateAverageCost fixedpoint.Value `json:"approximateAverageCost"`
FeeRate *ExchangeFee `json:"feeRate,omitempty"`
ExchangeFeeRates map[ExchangeName]ExchangeFee `json:"exchangeFeeRates"`
// Futures data fields
Isolated bool `json:"isolated"`
UpdateTime int64 `json:"updateTime"`
PositionRisk *PositionRisk
}
func NewPositionFromMarket(market Market) *Position {
if len(market.BaseCurrency) == 0 || len(market.QuoteCurrency) == 0 {
panic("logical exception: missing market information, base currency or quote currency is empty")
}
return &Position{
Symbol: market.Symbol,
BaseCurrency: market.BaseCurrency,
QuoteCurrency: market.QuoteCurrency,
Market: market,
TotalFee: make(map[string]fixedpoint.Value),
}
}
func NewPosition(symbol, base, quote string) *Position {
return &Position{
Symbol: symbol,
BaseCurrency: base,
QuoteCurrency: quote,
TotalFee: make(map[string]fixedpoint.Value),
}
}
func (p *Position) addTradeFee(trade Trade) {
if p.TotalFee == nil {
p.TotalFee = make(map[string]fixedpoint.Value)
}
p.TotalFee[trade.FeeCurrency] = p.TotalFee[trade.FeeCurrency].Add(trade.Fee)
}
func (p *Position) Reset() {
p.Base = fixedpoint.Zero
p.Quote = fixedpoint.Zero
p.AverageCost = fixedpoint.Zero
p.TotalFee = make(map[string]fixedpoint.Value)
}
func (p *Position) SetFeeRate(exchangeFee ExchangeFee) {
p.FeeRate = &exchangeFee
}
func (p *Position) SetExchangeFeeRate(ex ExchangeName, exchangeFee ExchangeFee) {
if p.ExchangeFeeRates == nil {
p.ExchangeFeeRates = make(map[ExchangeName]ExchangeFee)
}
p.ExchangeFeeRates[ex] = exchangeFee
}
func (p *Position) IsShort() bool {
return p.Base.Sign() < 0
}
func (p *Position) IsLong() bool {
return p.Base.Sign() > 0
}
func (p *Position) IsClosed() bool {
return p.Base.Sign() == 0
}
func (p *Position) IsOpened(currentPrice fixedpoint.Value) bool {
return !p.IsClosed() && !p.IsDust(currentPrice)
}
func (p *Position) Type() PositionType {
if p.Base.Sign() > 0 {
return PositionLong
} else if p.Base.Sign() < 0 {
return PositionShort
}
return PositionClosed
}
func (p *Position) SlackAttachment() slack.Attachment {
p.Lock()
defer p.Unlock()
averageCost := p.AverageCost
base := p.Base
quote := p.Quote
var posType = p.Type()
var color = ""
sign := p.Base.Sign()
if sign == 0 {
color = "#cccccc"
} else if sign > 0 {
color = "#228B22"
} else if sign < 0 {
color = "#DC143C"
}
title := templateutil.Render(string(posType)+` Position {{ .Symbol }} `, p)
fields := []slack.AttachmentField{
{Title: "Average Cost", Value: averageCost.String() + " " + p.QuoteCurrency, Short: true},
{Title: p.BaseCurrency, Value: base.String(), Short: true},
{Title: p.QuoteCurrency, Value: quote.String()},
}
if p.TotalFee != nil {
for feeCurrency, fee := range p.TotalFee {
if fee.Sign() > 0 {
fields = append(fields, slack.AttachmentField{
Title: fmt.Sprintf("Fee (%s)", feeCurrency),
Value: fee.String(),
Short: true,
})
}
}
}
return slack.Attachment{
// Pretext: "",
// Text: text,
Title: title,
Color: color,
Fields: fields,
Footer: templateutil.Render("update time {{ . }}", time.Now().Format(time.RFC822)),
// FooterIcon: "",
}
}
func (p *Position) PlainText() (msg string) {
posType := p.Type()
msg = fmt.Sprintf("%s Position %s: average cost = %v, base = %v, quote = %v",
posType,
p.Symbol,
p.AverageCost,
p.Base,
p.Quote,
)
if p.TotalFee != nil {
for feeCurrency, fee := range p.TotalFee {
msg += fmt.Sprintf("\nfee (%s) = %v", feeCurrency, fee)
}
}
return msg
}
func (p *Position) String() string {
return fmt.Sprintf("POSITION %s: average cost = %v, base = %v, quote = %v",
p.Symbol,
p.AverageCost,
p.Base,
p.Quote,
)
}
func (p *Position) BindStream(stream Stream) {
stream.OnTradeUpdate(func(trade Trade) {
if p.Symbol == trade.Symbol {
p.AddTrade(trade)
}
})
}
func (p *Position) AddTrades(trades []Trade) (fixedpoint.Value, fixedpoint.Value, bool) {
var totalProfitAmount, totalNetProfit fixedpoint.Value
for _, trade := range trades {
if profit, netProfit, madeProfit := p.AddTrade(trade); madeProfit {
totalProfitAmount = totalProfitAmount.Add(profit)
totalNetProfit = totalNetProfit.Add(netProfit)
}
}
return totalProfitAmount, totalNetProfit, !totalProfitAmount.IsZero()
}
func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedpoint.Value, madeProfit bool) {
price := td.Price
quantity := td.Quantity
quoteQuantity := td.QuoteQuantity
fee := td.Fee
// calculated fee in quote (some exchange accounts may enable platform currency fee discount, like BNB)
// convert platform fee token into USD values
var feeInQuote = fixedpoint.Zero
switch td.FeeCurrency {
case p.BaseCurrency:
if !td.IsFutures {
quantity = quantity.Sub(fee)
}
case p.QuoteCurrency:
if !td.IsFutures {
quoteQuantity = quoteQuantity.Sub(fee)
}
default:
if !td.Fee.IsZero() {
if p.ExchangeFeeRates != nil {
if exchangeFee, ok := p.ExchangeFeeRates[td.Exchange]; ok {
if td.IsMaker {
feeInQuote = feeInQuote.Add(exchangeFee.MakerFeeRate.Mul(quoteQuantity))
} else {
feeInQuote = feeInQuote.Add(exchangeFee.TakerFeeRate.Mul(quoteQuantity))
}
}
} else if p.FeeRate != nil {
if td.IsMaker {
feeInQuote = feeInQuote.Add(p.FeeRate.MakerFeeRate.Mul(quoteQuantity))
} else {
feeInQuote = feeInQuote.Add(p.FeeRate.TakerFeeRate.Mul(quoteQuantity))
}
}
}
}
p.Lock()
defer p.Unlock()
// update changedAt field before we unlock in the defer func
defer func() {
p.ChangedAt = td.Time.Time()
}()
p.addTradeFee(td)
// Base > 0 means we're in long position
// Base < 0 means we're in short position
switch td.Side {
case SideTypeBuy:
// was short position, now trade buy should cover the position
if p.Base.Sign() < 0 {
// convert short position to long position
if p.Base.Add(quantity).Sign() > 0 {
profit = p.AverageCost.Sub(price).Mul(p.Base.Neg())
netProfit = p.ApproximateAverageCost.Sub(price).Mul(p.Base.Neg()).Sub(feeInQuote)
p.Base = p.Base.Add(quantity)
p.Quote = p.Quote.Sub(quoteQuantity)
p.AverageCost = price
p.ApproximateAverageCost = price
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
p.OpenedAt = td.Time.Time()
return profit, netProfit, true
} else {
// after adding quantity it's still short position
p.Base = p.Base.Add(quantity)
p.Quote = p.Quote.Sub(quoteQuantity)
profit = p.AverageCost.Sub(price).Mul(quantity)
netProfit = p.ApproximateAverageCost.Sub(price).Mul(quantity).Sub(feeInQuote)
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
return profit, netProfit, true
}
}
// before adding the quantity, it's already a dust position
// then we should set the openedAt time
if p.IsDust(td.Price) {
p.OpenedAt = td.Time.Time()
}
// here the case is: base == 0 or base > 0
divisor := p.Base.Add(quantity)
p.ApproximateAverageCost = p.ApproximateAverageCost.Mul(p.Base).
Add(quoteQuantity).
Add(feeInQuote).
Div(divisor)
p.AverageCost = p.AverageCost.Mul(p.Base).Add(quoteQuantity).Div(divisor)
p.Base = p.Base.Add(quantity)
p.Quote = p.Quote.Sub(quoteQuantity)
return fixedpoint.Zero, fixedpoint.Zero, false
case SideTypeSell:
// was long position, the sell trade should reduce the base amount
if p.Base.Sign() > 0 {
// convert long position to short position
if p.Base.Compare(quantity) < 0 {
profit = price.Sub(p.AverageCost).Mul(p.Base)
netProfit = price.Sub(p.ApproximateAverageCost).Mul(p.Base).Sub(feeInQuote)
p.Base = p.Base.Sub(quantity)
p.Quote = p.Quote.Add(quoteQuantity)
p.AverageCost = price
p.ApproximateAverageCost = price
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
p.OpenedAt = td.Time.Time()
return profit, netProfit, true
} else {
p.Base = p.Base.Sub(quantity)
p.Quote = p.Quote.Add(quoteQuantity)
profit = price.Sub(p.AverageCost).Mul(quantity)
netProfit = price.Sub(p.ApproximateAverageCost).Mul(quantity).Sub(feeInQuote)
p.AccumulatedProfit = p.AccumulatedProfit.Add(profit)
return profit, netProfit, true
}
}
// before subtracting the quantity, it's already a dust position
// then we should set the openedAt time
if p.IsDust(td.Price) {
p.OpenedAt = td.Time.Time()
}
// handling short position, since Base here is negative we need to reverse the sign
divisor := quantity.Sub(p.Base)
p.ApproximateAverageCost = p.ApproximateAverageCost.Mul(p.Base.Neg()).
Add(quoteQuantity).
Sub(feeInQuote).
Div(divisor)
p.AverageCost = p.AverageCost.Mul(p.Base.Neg()).
Add(quoteQuantity).
Div(divisor)
p.Base = p.Base.Sub(quantity)
p.Quote = p.Quote.Add(quoteQuantity)
return fixedpoint.Zero, fixedpoint.Zero, false
}
return fixedpoint.Zero, fixedpoint.Zero, false
}