-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
side.go
91 lines (67 loc) · 1.38 KB
/
side.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
package types
import (
"encoding/json"
"strings"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/style"
)
// SideType define side type of order
type SideType string
const (
SideTypeBuy = SideType("BUY")
SideTypeSell = SideType("SELL")
SideTypeSelf = SideType("SELF")
// SideTypeBoth is only used for the configuration context
SideTypeBoth = SideType("BOTH")
)
var ErrInvalidSideType = errors.New("invalid side type")
func StrToSideType(s string) (side SideType, err error) {
switch strings.ToLower(s) {
case "buy":
side = SideTypeBuy
case "sell":
side = SideTypeSell
case "both":
side = SideTypeBoth
default:
err = ErrInvalidSideType
return side, err
}
return side, err
}
func (side *SideType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
ss, err := StrToSideType(s)
if err != nil {
return err
}
*side = ss
return nil
}
func (side SideType) Reverse() SideType {
switch side {
case SideTypeBuy:
return SideTypeSell
case SideTypeSell:
return SideTypeBuy
}
return side
}
func (side SideType) String() string {
return string(side)
}
func (side SideType) Color() string {
if side == SideTypeBuy {
return style.GreenColor
}
if side == SideTypeSell {
return style.RedColor
}
return style.GrayColor
}
func SideToColorName(side SideType) string {
return side.Color()
}