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