qbtrade/pkg/types/positionSide.go

85 lines
1.4 KiB
Go
Raw Normal View History

2024-07-21 14:42:15 +00:00
package types
import (
"encoding/json"
"strings"
"github.com/pkg/errors"
"git.qtrade.icu/lychiyu/qbtrade/pkg/style"
)
// 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()
}