add ID() to Strategy interface

This commit is contained in:
c9s 2021-02-03 09:08:05 +08:00
parent 8aa96c4546
commit 7904c6f4d0
12 changed files with 102 additions and 19 deletions

View File

@ -168,12 +168,32 @@ type Config struct {
RiskControls *RiskControls `json:"riskControls,omitempty" yaml:"riskControls,omitempty"`
ExchangeStrategies []ExchangeStrategyMount `json:"exchangeStrategies,omitempty" yaml:"exchangeStrategies,omitempty"`
CrossExchangeStrategies []CrossExchangeStrategy `json:"crossExchangeStrategies,omitempty" yaml:"crossExchangeStrategies,omitempty"`
ExchangeStrategies []ExchangeStrategyMount `json:"-" yaml:"-"`
CrossExchangeStrategies []CrossExchangeStrategy `json:"-" yaml:"-"`
PnLReporters []PnLReporterConfig `json:"reportPnL,omitempty" yaml:"reportPnL,omitempty"`
}
func (c *Config) Map() (map[string]interface{}, error) {
text, err := json.Marshal(c)
if err != nil {
return nil, err
}
var data map[string]interface{}
err = json.Unmarshal(text, &data)
if err != nil {
return nil, err
}
// convert strategy config back to the DSL format
for _, st := range c.ExchangeStrategies {
_ = st.Strategy
}
return data, err
}
type Stash map[string]interface{}
func loadStash(config []byte) (Stash, error) {

View File

@ -17,6 +17,7 @@ var SupportedExchanges = []types.ExchangeName{"binance", "max"}
// SingleExchangeStrategy represents the single Exchange strategy
type SingleExchangeStrategy interface {
ID() string
Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error
}
@ -29,6 +30,7 @@ type CrossExchangeSessionSubscriber interface {
}
type CrossExchangeStrategy interface {
ID() string
CrossRun(ctx context.Context, orderExecutionRouter OrderExecutionRouter, sessions map[string]*ExchangeSession) error
}

View File

@ -12,13 +12,15 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
var log = logrus.WithField("strategy", "bollgrid")
const ID = "bollgrid"
var log = logrus.WithField("strategy", ID)
func init() {
// Register the pointer of the strategy struct,
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
// Note: built-in strategies need to imported manually in the bbgo cmd package.
bbgo.RegisterStrategy("bollgrid", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -81,6 +83,10 @@ type Strategy struct {
boll *indicator.BOLL
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
// currently we need the 1m kline to update the last close price and indicators
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()})

View File

@ -12,10 +12,12 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
var log = logrus.WithField("strategy", "buyandhold")
const ID = "buyandhold"
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy("buyandhold", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -29,6 +31,10 @@ type Strategy struct {
MovingAverageWindow int `json:"movingAverageWindow"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)})
}

View File

@ -13,8 +13,10 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
const ID = "flashcrash"
func init() {
bbgo.RegisterStrategy("flashcrash", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -54,6 +56,10 @@ type Strategy struct {
ewma *indicator.EWMA
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
if err := session.Exchange.CancelOrders(context.Background(), s.activeOrders.Bids.Orders()...); err != nil {
log.WithError(err).Errorf("cancel order error")

View File

@ -12,13 +12,15 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
var log = logrus.WithField("strategy", "grid")
const ID = "grid"
var log = logrus.WithField("strategy", ID)
func init() {
// Register the pointer of the strategy struct,
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
// Note: built-in strategies need to imported manually in the bbgo cmd package.
bbgo.RegisterStrategy("grid", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -69,6 +71,10 @@ type Strategy struct {
orders map[uint64]types.Order
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) placeGridOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
log.Infof("placing grid orders...")

View File

@ -13,14 +13,16 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
const ID = "mirrormaker"
var defaultMargin = fixedpoint.NewFromFloat(0.01)
var defaultQuantity = fixedpoint.NewFromFloat(0.001)
var log = logrus.WithField("strategy", "mirrormaker")
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy("mirrormaker", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -58,6 +60,10 @@ type Strategy struct {
stopC chan struct{}
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
sourceSession, ok := sessions[s.SourceExchange]
if !ok {

View File

@ -8,8 +8,10 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
const ID = "pricealert"
func init() {
bbgo.RegisterStrategy("pricealert", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -22,6 +24,10 @@ type Strategy struct {
MinChange float64 `json:"minChange"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}

View File

@ -9,8 +9,10 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
const ID = "skeleton"
func init() {
bbgo.RegisterStrategy("skeleton", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -19,6 +21,11 @@ type Strategy struct {
types.Market
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
}

View File

@ -11,6 +11,8 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
const ID = "swing"
// The indicators (SMA and EWMA) that we want to use are returning float64 data.
type Float64Indicator interface {
Last() float64
@ -20,7 +22,7 @@ func init() {
// Register the pointer of the strategy struct,
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
// Note: built-in strategies need to imported manually in the bbgo cmd package.
bbgo.RegisterStrategy("swing", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -76,6 +78,10 @@ type Strategy struct {
MovingAverageWindow int `json:"movingAverageWindow"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}

View File

@ -13,7 +13,9 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
var log = logrus.WithField("strategy", "trailingstop")
const ID = "trailingstop"
var log = logrus.WithField("strategy", ID)
// The indicators (SMA and EWMA) that we want to use are returning float64 data.
type Float64Indicator interface {
@ -24,7 +26,7 @@ func init() {
// Register the pointer of the strategy struct,
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
// Note: built-in strategies need to imported manually in the bbgo cmd package.
bbgo.RegisterStrategy("trailingstop", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -71,6 +73,10 @@ type Strategy struct {
order types.Order
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.MovingAverageInterval.String()})

View File

@ -12,8 +12,10 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
const ID = "xpuremaker"
func init() {
bbgo.RegisterStrategy("xpuremaker", &Strategy{})
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
@ -29,6 +31,10 @@ type Strategy struct {
activeOrders map[string]types.Order
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{})
}