unmarshal price type

This commit is contained in:
narumi 2024-02-23 16:29:26 +08:00
parent 8f2d551399
commit dae445ad5c

View File

@ -1,7 +1,11 @@
package types
import (
"encoding/json"
"strings"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors"
)
type PriceType string
@ -15,6 +19,44 @@ const (
PriceTypeTaker PriceType = "TAKER"
)
var ErrInvalidPriceType = errors.New("invalid price type")
func StrToPriceType(s string) (price PriceType, err error) {
switch strings.ToLower(s) {
case "last":
price = PriceTypeLast
case "buy":
price = PriceTypeBuy
case "sell":
price = PriceTypeSell
case "mid":
price = PriceTypeMid
case "maker":
price = PriceTypeMaker
case "taker":
price = PriceTypeTaker
default:
err = ErrInvalidPriceType
}
return price, err
}
func (p *PriceType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
t, err := StrToPriceType(s)
if err != nil {
return err
}
*p = t
return nil
}
func (p PriceType) Map(ticker *Ticker, side SideType) fixedpoint.Value {
price := ticker.Last