interact: separate strategy filtering and button generation

This commit is contained in:
Andy Cheng 2022-03-28 12:37:42 +08:00
parent ee6377ab87
commit 63e8850cc3
No known key found for this signature in database
GPG Key ID: 936427CF651A9D28

View File

@ -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 found := false
var buttonsForm [][3]string var strategies []string
rt := reflect.TypeOf(checkInterface).Elem() rt := reflect.TypeOf(checkInterface).Elem()
for signature, strategy := range it.exchangeStrategies { for signature, strategy := range it.exchangeStrategies {
if ok := reflect.TypeOf(strategy).Implements(rt); ok { if ok := reflect.TypeOf(strategy).Implements(rt); ok {
buttonsForm = append(buttonsForm, [3]string{signature, "strategy", signature}) strategies = append(strategies, signature)
found = true 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) { 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 { i.PrivateCommand("/position", "Show Position", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
if buttonsForm, found := it.AddSupportedStrategyButtons((*PositionReader)(nil)); found { if strategies, found := it.FilterStrategyByInterface((*PositionReader)(nil)); found {
reply.AddMultipleButtons(buttonsForm) reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy") reply.Message("Please choose one strategy")
} else { } else {
reply.Message("No any strategy supports PositionReader") 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 { i.PrivateCommand("/closeposition", "Close position", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
if buttonsForm, found := it.AddSupportedStrategyButtons((*PositionCloser)(nil)); found { if strategies, found := it.FilterStrategyByInterface((*PositionCloser)(nil)); found {
reply.AddMultipleButtons(buttonsForm) reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy") reply.Message("Please choose one strategy")
} else { } else {
reply.Message("No any strategy supports PositionCloser") 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 { i.PrivateCommand("/status", "Strategy Status", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyStatusProvider)(nil)); found { if strategies, found := it.FilterStrategyByInterface((*StrategyStatusProvider)(nil)); found {
reply.AddMultipleButtons(buttonsForm) reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy") reply.Message("Please choose one strategy")
} else { } else {
reply.Message("No any strategy supports StrategyStatusProvider") 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 { i.PrivateCommand("/suspend", "Suspend Strategy", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyController)(nil)); found { if strategies, found := it.FilterStrategyByInterface((*StrategyController)(nil)); found {
reply.AddMultipleButtons(buttonsForm) reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy") reply.Message("Please choose one strategy")
} else { } else {
reply.Message("No any strategy supports StrategyController") 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 { i.PrivateCommand("/resume", "Resume Strategy", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyController)(nil)); found { if strategies, found := it.FilterStrategyByInterface((*StrategyController)(nil)); found {
reply.AddMultipleButtons(buttonsForm) reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy") reply.Message("Please choose one strategy")
} else { } else {
reply.Message("No any strategy supports StrategyController") 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 { i.PrivateCommand("/emergencystop", "Emergency Stop", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
if buttonsForm, found := it.AddSupportedStrategyButtons((*EmergencyStopper)(nil)); found { if strategies, found := it.FilterStrategyByInterface((*EmergencyStopper)(nil)); found {
reply.AddMultipleButtons(buttonsForm) reply.AddMultipleButtons(it.GenerateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy") reply.Message("Please choose one strategy")
} else { } else {
reply.Message("No any strategy supports EmergencyStopper") reply.Message("No any strategy supports EmergencyStopper")