type: add StrategyStatus type

This commit is contained in:
Andy Cheng 2022-03-21 15:01:15 +08:00
parent ce6efd9333
commit 5f7710103d
No known key found for this signature in database
GPG Key ID: 936427CF651A9D28
3 changed files with 21 additions and 12 deletions

View File

@ -24,7 +24,7 @@ type PositionReader interface {
type StrategyController interface {
SuspendStrategy(ctx context.Context) error
ResumeStrategy() error
GetStrategyStatus() bool
GetStrategyStatus() types.StrategyStatus
EmergencyStop(ctx context.Context) error
}
@ -254,9 +254,9 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
kc.RemoveKeyboard()
}
if status {
if status == types.StrategyStatusRunning {
reply.Message(fmt.Sprintf("Strategy %s is running.", signature))
} else {
} else if status == types.StrategyStatusStopped {
reply.Message(fmt.Sprintf("Strategy %s is not running.", signature))
}
@ -295,7 +295,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
// Check strategy status before suspend
status := controller.GetStrategyStatus()
if !status {
if status != types.StrategyStatusRunning {
reply.Message(fmt.Sprintf("Strategy %s is not running.", signature))
return nil
}
@ -347,7 +347,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
// Check strategy status before resume
status := controller.GetStrategyStatus()
if status {
if status != types.StrategyStatusStopped {
reply.Message(fmt.Sprintf("Strategy %s is running.", signature))
return nil
}

View File

@ -181,7 +181,7 @@ type Strategy struct {
trailingStopControl *TrailingStopControl
// StrategyController
status bool
status types.StrategyStatus
}
func (s *Strategy) ID() string {
@ -254,12 +254,12 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu
// StrategyController
func (s *Strategy) GetStrategyStatus() bool {
func (s *Strategy) GetStrategyStatus() types.StrategyStatus {
return s.status
}
func (s *Strategy) SuspendStrategy(ctx context.Context) error {
s.status = false
s.status = types.StrategyStatusStopped
var err error
// Cancel all order
@ -285,7 +285,7 @@ func (s *Strategy) SuspendStrategy(ctx context.Context) error {
}
func (s *Strategy) ResumeStrategy() error {
s.status = true
s.status = types.StrategyStatusRunning
return nil
}
@ -437,7 +437,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.orderExecutor = orderExecutor
// StrategyController
s.status = true
s.status = types.StrategyStatusRunning
// set default values
if s.Interval == "" {
@ -508,7 +508,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// Update trailing stop when the position changes
s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
// StrategyController
if !s.status {
if s.status != types.StrategyStatusRunning {
return
}
@ -558,7 +558,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
// StrategyController
if !s.status {
if s.status != types.StrategyStatusRunning {
return
}

View File

@ -0,0 +1,9 @@
package types
// StrategyStatus define strategy status
type StrategyStatus string
const (
StrategyStatusRunning StrategyStatus = "RUN"
StrategyStatusStopped StrategyStatus = "STOP"
)