-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
price_type.go
77 lines (63 loc) · 1.49 KB
/
price_type.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
package types
import (
"encoding/json"
"strings"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors"
)
type PriceType string
const (
PriceTypeLast PriceType = "LAST"
PriceTypeBuy PriceType = "BUY" // BID
PriceTypeSell PriceType = "SELL" // ASK
PriceTypeMid PriceType = "MID"
PriceTypeMaker PriceType = "MAKER"
PriceTypeTaker PriceType = "TAKER"
)
var ErrInvalidPriceType = errors.New("invalid price type")
func ParsePriceType(s string) (p PriceType, err error) {
p = PriceType(strings.ToUpper(s))
switch p {
case PriceTypeLast, PriceTypeBuy, PriceTypeSell, PriceTypeMid, PriceTypeMaker, PriceTypeTaker:
return p, err
}
return p, ErrInvalidPriceType
}
func (p *PriceType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
t, err := ParsePriceType(s)
if err != nil {
return err
}
*p = t
return nil
}
func (p PriceType) Map(ticker *Ticker, side SideType) fixedpoint.Value {
price := ticker.Last
switch p {
case PriceTypeLast:
price = ticker.Last
case PriceTypeBuy:
price = ticker.Buy
case PriceTypeSell:
price = ticker.Sell
case PriceTypeMid:
price = ticker.Buy.Add(ticker.Sell).Div(fixedpoint.NewFromInt(2))
case PriceTypeMaker:
if side == SideTypeBuy {
price = ticker.Buy
} else if side == SideTypeSell {
price = ticker.Sell
}
case PriceTypeTaker:
if side == SideTypeBuy {
price = ticker.Sell
} else if side == SideTypeSell {
price = ticker.Buy
}
}
return price
}