mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
unmarshal price type
This commit is contained in:
parent
8f2d551399
commit
dae445ad5c
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user