package types import ( "encoding/json" "git.qtrade.icu/lychiyu/bbgo/pkg/style" "strings" "github.com/pkg/errors" ) // PositionSideType define side type of order type PositionSideType string const ( PositionSideTypeShort = PositionSideType("SHORT") PositionSideTypeLong = PositionSideType("LONG") ) var ErrInvalidPositionSideType = errors.New("invalid position side type") func StrToPositionSideType(s string) (side PositionSideType, err error) { switch strings.ToLower(s) { case "LONG": side = PositionSideTypeLong case "SHORT": side = PositionSideTypeShort default: err = ErrInvalidSideType return side, err } return side, err } func (side *PositionSideType) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil { return err } ss, err := StrToPositionSideType(s) if err != nil { return err } *side = ss return nil } func (side PositionSideType) Reverse() PositionSideType { switch side { case PositionSideTypeShort: return PositionSideTypeShort case PositionSideTypeLong: return PositionSideTypeLong } return side } func (side PositionSideType) String() string { return string(side) } func (side PositionSideType) Color() string { if side == PositionSideTypeShort { return style.GreenColor } if side == PositionSideTypeLong { return style.RedColor } return style.GrayColor } func PositionSideToColorName(side PositionSideType) string { return side.Color() }