bbgo_origin/pkg/bbgo/strategy_controller.go

80 lines
1.5 KiB
Go
Raw Normal View History

package bbgo
import (
2022-04-18 08:41:47 +00:00
"github.com/c9s/bbgo/pkg/types"
)
type StrategyController struct {
Status types.StrategyStatus
// Callbacks
SuspendCallback func() error
ResumeCallback func() error
EmergencyStopCallback func() error
}
func (s *StrategyController) GetStatus() types.StrategyStatus {
return s.Status
}
func (s *StrategyController) Suspend() error {
s.Status = types.StrategyStatusStopped
return s.EmitSuspend()
}
func (s *StrategyController) OnSuspend(cb func() error) {
2022-04-18 08:41:47 +00:00
s.SuspendCallback = cb
}
2022-04-18 08:41:47 +00:00
func (s *StrategyController) EmitSuspend() error {
if s.SuspendCallback != nil {
return s.SuspendCallback()
2022-04-18 08:41:47 +00:00
} else {
return nil
2022-04-18 08:41:47 +00:00
}
}
func (s *StrategyController) Resume() error {
s.Status = types.StrategyStatusRunning
return s.EmitResume()
}
func (s *StrategyController) OnResume(cb func() error) {
2022-04-18 08:41:47 +00:00
s.ResumeCallback = cb
}
2022-04-18 08:41:47 +00:00
func (s *StrategyController) EmitResume() error {
if s.ResumeCallback != nil {
return s.ResumeCallback()
2022-04-18 08:41:47 +00:00
} else {
return nil
2022-04-18 08:41:47 +00:00
}
}
func (s *StrategyController) EmergencyStop() error {
s.Status = types.StrategyStatusStopped
return s.EmitEmergencyStop()
}
func (s *StrategyController) OnEmergencyStop(cb func() error) {
2022-04-18 08:41:47 +00:00
s.EmergencyStopCallback = cb
}
func (s *StrategyController) EmitEmergencyStop() error {
if s.EmergencyStopCallback != nil {
return s.EmergencyStopCallback()
2022-04-18 08:41:47 +00:00
} else {
return nil
2022-04-18 08:41:47 +00:00
}
}
type StrategyControllerInterface interface {
GetStatus() types.StrategyStatus
Suspend() error
Resume() error
EmergencyStop() error
}