From 85ffe9a2de5a56190e205856d33252dbabd4df91 Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Fri, 15 Apr 2022 17:02:00 +0800 Subject: [PATCH] feature: prototype of strategy controller struct --- pkg/bbgo/strategy_controller.go | 64 +++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/pkg/bbgo/strategy_controller.go b/pkg/bbgo/strategy_controller.go index 99d19982f..087c50e9d 100644 --- a/pkg/bbgo/strategy_controller.go +++ b/pkg/bbgo/strategy_controller.go @@ -2,21 +2,65 @@ package bbgo import ( "context" - "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/fixedpoint" ) type StrategyController struct { - status types.StrategyStatus - - SuspendCallbacks []func(ctx context.Context) + // Callbacks + GetStatusCallbacks map[string]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)) { - s.SuspendCallbacks = append(s.SuspendCallbacks, cb) +// TODO: strategy no found + +func (s *StrategyController) OnGetStatus(signature string, cb func(ctx context.Context)) { + s.GetStatusCallbacks[signature] = cb } -func (s *StrategyController) EmitKLineClosed(ctx context.Context) { - for _, cb := range s.SuspendCallbacks { - cb(ctx) - } +func (s *StrategyController) EmitGetStatus(signature string, ctx context.Context) { + s.GetStatusCallbacks[signature](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) }