feature: prototype of strategy controller struct

This commit is contained in:
Andy Cheng 2022-04-15 17:02:00 +08:00
parent 73c2c84cab
commit 85ffe9a2de

View File

@ -2,21 +2,65 @@ package bbgo
import ( import (
"context" "context"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/fixedpoint"
) )
type StrategyController struct { type StrategyController struct {
status types.StrategyStatus // Callbacks
GetStatusCallbacks map[string]func(ctx context.Context)
SuspendCallbacks []func(ctx context.Context) GetPositionCallbacks map[string]func(ctx context.Context)
ClosePositionCallbacks map[string]func(ctx context.Context, percentage fixedpoint.Value)
SuspendCallbacks map[string]func(ctx context.Context)
ResumeCallbacks map[string]func(ctx context.Context)
EmergencyStopCallbacks map[string]func(ctx context.Context)
} }
func (s *StrategyController) OnSuspend(cb func(ctx context.Context)) { // TODO: strategy no found
s.SuspendCallbacks = append(s.SuspendCallbacks, cb)
func (s *StrategyController) OnGetStatus(signature string, cb func(ctx context.Context)) {
s.GetStatusCallbacks[signature] = cb
} }
func (s *StrategyController) EmitKLineClosed(ctx context.Context) { func (s *StrategyController) EmitGetStatus(signature string, ctx context.Context) {
for _, cb := range s.SuspendCallbacks { s.GetStatusCallbacks[signature](ctx)
cb(ctx) }
}
func (s *StrategyController) OnGetPosition(signature string, cb func(ctx context.Context)) {
s.GetPositionCallbacks[signature] = cb
}
func (s *StrategyController) OnClosePosition(signature string, cb func(ctx context.Context, percentage fixedpoint.Value)) {
s.ClosePositionCallbacks[signature] = cb
}
func (s *StrategyController) EmitClosePosition(signature string, ctx context.Context, percentage fixedpoint.Value) {
s.ClosePositionCallbacks[signature](ctx, percentage)
}
func (s *StrategyController) EmitGetPosition(signature string, ctx context.Context) {
s.GetPositionCallbacks[signature](ctx)
}
func (s *StrategyController) OnSuspend(signature string, cb func(ctx context.Context)) {
s.SuspendCallbacks[signature] = cb
}
func (s *StrategyController) EmitSuspend(signature string, ctx context.Context) {
s.SuspendCallbacks[signature](ctx)
}
func (s *StrategyController) OnResume(signature string, cb func(ctx context.Context)) {
s.ResumeCallbacks[signature] = cb
}
func (s *StrategyController) EmitResume(signature string, ctx context.Context) {
s.ResumeCallbacks[signature](ctx)
}
func (s *StrategyController) OnEmergencyStop(signature string, cb func(ctx context.Context)) {
s.EmergencyStopCallbacks[signature] = cb
}
func (s *StrategyController) EmitEmergencyStop(signature string, ctx context.Context) {
s.EmergencyStopCallbacks[signature](ctx)
} }