From 63e8850cc3ec6d3dff95417a54f745773319677a Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Mon, 28 Mar 2022 12:37:42 +0800 Subject: [PATCH] interact: separate strategy filtering and button generation --- pkg/bbgo/interact.go | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/pkg/bbgo/interact.go b/pkg/bbgo/interact.go index 40b98aa17..9fdf34b54 100644 --- a/pkg/bbgo/interact.go +++ b/pkg/bbgo/interact.go @@ -61,18 +61,27 @@ func NewCoreInteraction(environment *Environment, trader *Trader) *CoreInteracti } } -func (it *CoreInteraction) AddSupportedStrategyButtons(checkInterface interface{}) ([][3]string, bool) { +func (it *CoreInteraction) FilterStrategyByInterface(checkInterface interface{}) ([]string, bool) { found := false - var buttonsForm [][3]string + var strategies []string rt := reflect.TypeOf(checkInterface).Elem() for signature, strategy := range it.exchangeStrategies { if ok := reflect.TypeOf(strategy).Implements(rt); ok { - buttonsForm = append(buttonsForm, [3]string{signature, "strategy", signature}) + strategies = append(strategies, signature) found = true } } - return buttonsForm, found + return strategies, found +} + +func (it *CoreInteraction) GenerateStrategyButtonsForm(strategies []string) [][3]string { + var buttonsForm [][3]string + for _, strategy := range strategies { + buttonsForm = append(buttonsForm, [3]string{strategy, "strategy", strategy}) + } + + return buttonsForm } func (it *CoreInteraction) Commands(i *interact.Interact) { @@ -122,8 +131,8 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { i.PrivateCommand("/position", "Show Position", func(reply interact.Reply) error { // it.trader.exchangeStrategies // send symbol options - if buttonsForm, found := it.AddSupportedStrategyButtons((*PositionReader)(nil)); found { - reply.AddMultipleButtons(buttonsForm) + if strategies, found := it.FilterStrategyByInterface((*PositionReader)(nil)); found { + reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies)) reply.Message("Please choose one strategy") } else { reply.Message("No any strategy supports PositionReader") @@ -163,8 +172,8 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { i.PrivateCommand("/closeposition", "Close position", func(reply interact.Reply) error { // it.trader.exchangeStrategies // send symbol options - if buttonsForm, found := it.AddSupportedStrategyButtons((*PositionCloser)(nil)); found { - reply.AddMultipleButtons(buttonsForm) + if strategies, found := it.FilterStrategyByInterface((*PositionCloser)(nil)); found { + reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies)) reply.Message("Please choose one strategy") } else { reply.Message("No any strategy supports PositionCloser") @@ -232,8 +241,8 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { i.PrivateCommand("/status", "Strategy Status", func(reply interact.Reply) error { // it.trader.exchangeStrategies // send symbol options - if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyStatusProvider)(nil)); found { - reply.AddMultipleButtons(buttonsForm) + if strategies, found := it.FilterStrategyByInterface((*StrategyStatusProvider)(nil)); found { + reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies)) reply.Message("Please choose one strategy") } else { reply.Message("No any strategy supports StrategyStatusProvider") @@ -270,8 +279,8 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { i.PrivateCommand("/suspend", "Suspend Strategy", func(reply interact.Reply) error { // it.trader.exchangeStrategies // send symbol options - if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyController)(nil)); found { - reply.AddMultipleButtons(buttonsForm) + if strategies, found := it.FilterStrategyByInterface((*StrategyController)(nil)); found { + reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies)) reply.Message("Please choose one strategy") } else { reply.Message("No any strategy supports StrategyController") @@ -315,8 +324,8 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { i.PrivateCommand("/resume", "Resume Strategy", func(reply interact.Reply) error { // it.trader.exchangeStrategies // send symbol options - if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyController)(nil)); found { - reply.AddMultipleButtons(buttonsForm) + if strategies, found := it.FilterStrategyByInterface((*StrategyController)(nil)); found { + reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies)) reply.Message("Please choose one strategy") } else { reply.Message("No any strategy supports StrategyController") @@ -360,8 +369,8 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { i.PrivateCommand("/emergencystop", "Emergency Stop", func(reply interact.Reply) error { // it.trader.exchangeStrategies // send symbol options - if buttonsForm, found := it.AddSupportedStrategyButtons((*EmergencyStopper)(nil)); found { - reply.AddMultipleButtons(buttonsForm) + if strategies, found := it.FilterStrategyByInterface((*EmergencyStopper)(nil)); found { + reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies)) reply.Message("Please choose one strategy") } else { reply.Message("No any strategy supports EmergencyStopper")