2022-04-14 05:44:34 +00:00
|
|
|
package bbgo
|
2022-04-15 08:06:19 +00:00
|
|
|
|
|
|
|
import (
|
2022-04-18 08:41:47 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2022-04-15 08:06:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StrategyController struct {
|
2022-04-18 09:26:43 +00:00
|
|
|
Status types.StrategyStatus
|
|
|
|
|
2022-04-15 09:02:00 +00:00
|
|
|
// Callbacks
|
2022-04-18 09:26:43 +00:00
|
|
|
SuspendCallback func() error
|
|
|
|
ResumeCallback func() error
|
|
|
|
EmergencyStopCallback func() error
|
2022-04-15 09:02:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:02:15 +00:00
|
|
|
func (s *StrategyController) GetStatus() types.StrategyStatus {
|
|
|
|
return s.Status
|
2022-04-15 09:02:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:02:15 +00:00
|
|
|
func (s *StrategyController) Suspend() error {
|
|
|
|
s.Status = types.StrategyStatusStopped
|
2022-04-15 09:02:00 +00:00
|
|
|
|
2022-04-20 11:02:15 +00:00
|
|
|
return s.EmitSuspend()
|
2022-04-15 09:02:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 09:26:43 +00:00
|
|
|
func (s *StrategyController) OnSuspend(cb func() error) {
|
2022-04-18 08:41:47 +00:00
|
|
|
s.SuspendCallback = cb
|
2022-04-15 09:02:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 08:41:47 +00:00
|
|
|
func (s *StrategyController) EmitSuspend() error {
|
|
|
|
if s.SuspendCallback != nil {
|
2022-04-18 09:26:43 +00:00
|
|
|
return s.SuspendCallback()
|
2022-04-18 08:41:47 +00:00
|
|
|
} else {
|
2022-04-20 11:02:15 +00:00
|
|
|
return nil
|
2022-04-18 08:41:47 +00:00
|
|
|
}
|
2022-04-15 09:02:00 +00:00
|
|
|
}
|
2022-04-15 08:06:19 +00:00
|
|
|
|
2022-04-20 11:02:15 +00:00
|
|
|
func (s *StrategyController) Resume() error {
|
|
|
|
s.Status = types.StrategyStatusRunning
|
|
|
|
|
|
|
|
return s.EmitResume()
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:26:43 +00:00
|
|
|
func (s *StrategyController) OnResume(cb func() error) {
|
2022-04-18 08:41:47 +00:00
|
|
|
s.ResumeCallback = cb
|
2022-04-15 08:06:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 08:41:47 +00:00
|
|
|
func (s *StrategyController) EmitResume() error {
|
|
|
|
if s.ResumeCallback != nil {
|
2022-04-18 09:26:43 +00:00
|
|
|
return s.ResumeCallback()
|
2022-04-18 08:41:47 +00:00
|
|
|
} else {
|
2022-04-20 11:02:15 +00:00
|
|
|
return nil
|
2022-04-18 08:41:47 +00:00
|
|
|
}
|
2022-04-15 08:06:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:02:15 +00:00
|
|
|
func (s *StrategyController) EmergencyStop() error {
|
|
|
|
s.Status = types.StrategyStatusStopped
|
|
|
|
|
|
|
|
return s.EmitEmergencyStop()
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:26:43 +00:00
|
|
|
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 {
|
2022-04-18 09:26:43 +00:00
|
|
|
return s.EmergencyStopCallback()
|
2022-04-18 08:41:47 +00:00
|
|
|
} else {
|
2022-04-20 11:02:15 +00:00
|
|
|
return nil
|
2022-04-18 08:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type StrategyControllerInterface interface {
|
2022-04-20 11:02:15 +00:00
|
|
|
GetStatus() types.StrategyStatus
|
|
|
|
Suspend() error
|
|
|
|
Resume() error
|
|
|
|
EmergencyStop() error
|
2022-04-15 08:06:19 +00:00
|
|
|
}
|