feature: mix embeded struct and callback in strategy controller

This commit is contained in:
Andy Cheng 2022-04-20 19:02:15 +08:00
parent 57fdc9b120
commit 78a8c2aaaf

View File

@ -1,69 +1,26 @@
package bbgo package bbgo
import ( import (
"fmt"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"reflect"
) )
type StrategyController struct { type StrategyController struct {
Status types.StrategyStatus Status types.StrategyStatus
// Callbacks // Callbacks
GetStatusCallback func() types.StrategyStatus
GetPositionCallback func() *types.Position
ClosePositionCallback func(percentage fixedpoint.Value) error
SuspendCallback func() error SuspendCallback func() error
ResumeCallback func() error ResumeCallback func() error
EmergencyStopCallback func() error EmergencyStopCallback func() error
} }
func (s *StrategyController) HasCallback(callback string) bool { func (s *StrategyController) GetStatus() types.StrategyStatus {
callbackV := reflect.ValueOf(s).Elem().FieldByName(callback) return s.Status
if callbackV.IsValid() {
if !callbackV.IsNil() {
return true
}
}
return false
} }
func (s *StrategyController) OnGetStatus(cb func() types.StrategyStatus) { func (s *StrategyController) Suspend() error {
s.GetStatusCallback = cb s.Status = types.StrategyStatusStopped
}
func (s *StrategyController) EmitGetStatus() (status types.StrategyStatus, err error) { return s.EmitSuspend()
if s.GetStatusCallback != nil {
return s.GetStatusCallback(), nil
} else {
return types.StrategyStatusUnknown, fmt.Errorf("no GetStatus callback registered")
}
}
func (s *StrategyController) OnGetPosition(cb func() *types.Position) {
s.GetPositionCallback = cb
}
func (s *StrategyController) EmitGetPosition() (position *types.Position, err error) {
if s.GetPositionCallback != nil {
return s.GetPositionCallback(), nil
} else {
return nil, fmt.Errorf("no GetPosition callback registered")
}
}
func (s *StrategyController) OnClosePosition(cb func(percentage fixedpoint.Value) error) {
s.ClosePositionCallback = cb
}
func (s *StrategyController) EmitClosePosition(percentage fixedpoint.Value) error {
if s.ClosePositionCallback != nil {
return s.ClosePositionCallback(percentage)
} else {
return fmt.Errorf("no ClosePosition callback registered")
}
} }
func (s *StrategyController) OnSuspend(cb func() error) { func (s *StrategyController) OnSuspend(cb func() error) {
@ -74,10 +31,16 @@ func (s *StrategyController) EmitSuspend() error {
if s.SuspendCallback != nil { if s.SuspendCallback != nil {
return s.SuspendCallback() return s.SuspendCallback()
} else { } else {
return fmt.Errorf("no Suspend callback registered") return nil
} }
} }
func (s *StrategyController) Resume() error {
s.Status = types.StrategyStatusRunning
return s.EmitResume()
}
func (s *StrategyController) OnResume(cb func() error) { func (s *StrategyController) OnResume(cb func() error) {
s.ResumeCallback = cb s.ResumeCallback = cb
} }
@ -86,10 +49,16 @@ func (s *StrategyController) EmitResume() error {
if s.ResumeCallback != nil { if s.ResumeCallback != nil {
return s.ResumeCallback() return s.ResumeCallback()
} else { } else {
return fmt.Errorf("no Resume callback registered") return nil
} }
} }
func (s *StrategyController) EmergencyStop() error {
s.Status = types.StrategyStatusStopped
return s.EmitEmergencyStop()
}
func (s *StrategyController) OnEmergencyStop(cb func() error) { func (s *StrategyController) OnEmergencyStop(cb func() error) {
s.EmergencyStopCallback = cb s.EmergencyStopCallback = cb
} }
@ -98,22 +67,13 @@ func (s *StrategyController) EmitEmergencyStop() error {
if s.EmergencyStopCallback != nil { if s.EmergencyStopCallback != nil {
return s.EmergencyStopCallback() return s.EmergencyStopCallback()
} else { } else {
return fmt.Errorf("no EmergencyStop callback registered") return nil
} }
} }
type StrategyControllerInterface interface { type StrategyControllerInterface interface {
HasCallback(callback string) bool GetStatus() types.StrategyStatus
OnGetStatus(cb func() types.StrategyStatus) Suspend() error
EmitGetStatus() (status types.StrategyStatus, err error) Resume() error
OnGetPosition(cb func() *types.Position) EmergencyStop() error
EmitGetPosition() (position *types.Position, err error)
OnClosePosition(cb func(percentage fixedpoint.Value) error)
EmitClosePosition(percentage fixedpoint.Value) error
OnSuspend(cb func() error)
EmitSuspend() error
OnResume(cb func() error)
EmitResume() error
OnEmergencyStop(cb func() error)
EmitEmergencyStop() error
} }