Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor interact strategy filter functions #928

Merged
merged 3 commits into from Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
135 changes: 77 additions & 58 deletions pkg/bbgo/interact.go
Expand Up @@ -77,54 +77,6 @@ func RegisterCommand(command, desc string, f interface{}) *interact.Command {
return it.Cmd
}

func getStrategySignatures(exchangeStrategies map[string]SingleExchangeStrategy) []string {
var strategies []string
for signature := range exchangeStrategies {
strategies = append(strategies, signature)
}

return strategies
}

func filterStrategyByInterface(checkInterface interface{}, exchangeStrategies map[string]SingleExchangeStrategy) (strategies map[string]SingleExchangeStrategy, found bool) {
found = false
strategies = make(map[string]SingleExchangeStrategy)
rt := reflect.TypeOf(checkInterface).Elem()
for signature, strategy := range exchangeStrategies {
if ok := reflect.TypeOf(strategy).Implements(rt); ok {
strategies[signature] = strategy
found = true
}
}

return strategies, found
}

func filterStrategyByField(fieldName string, fieldType reflect.Type, exchangeStrategies map[string]SingleExchangeStrategy) (strategies map[string]SingleExchangeStrategy, found bool) {
found = false
strategies = make(map[string]SingleExchangeStrategy)
for signature, strategy := range exchangeStrategies {
r := reflect.ValueOf(strategy).Elem()
f := r.FieldByName(fieldName)
if !f.IsZero() && f.Type() == fieldType {
strategies[signature] = strategy
found = true
}
}

return strategies, found
}

func generateStrategyButtonsForm(strategies map[string]SingleExchangeStrategy) [][3]string {
var buttonsForm [][3]string
signatures := getStrategySignatures(strategies)
for _, signature := range signatures {
buttonsForm = append(buttonsForm, [3]string{signature, "strategy", signature})
}

return buttonsForm
}

func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/sessions", "List Exchange Sessions", func(reply interact.Reply) error {
switch r := reply.(type) {
Expand Down Expand Up @@ -172,7 +124,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/position", "Show Position", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
if strategies, found := filterStrategyByInterface((*PositionReader)(nil), it.exchangeStrategies); found {
if strategies, err := filterStrategiesByInterface(it.exchangeStrategies, (*PositionReader)(nil)); err == nil && len(strategies) > 0 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy")
} else {
Expand Down Expand Up @@ -211,9 +163,11 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
})

i.PrivateCommand("/resetposition", "Reset position", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
if strategies, found := filterStrategyByInterface((*PositionResetter)(nil), it.exchangeStrategies); found {
strategies, err := filterStrategies(it.exchangeStrategies, func(s SingleExchangeStrategy) bool {
return testInterface(s, (*PositionResetter)(nil)) || hasTypeField(s, &types.Position{})
})

if err == nil && len(strategies) > 0 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy")
} else {
Expand Down Expand Up @@ -254,7 +208,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/closeposition", "Close position", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
if strategies, found := filterStrategyByInterface((*PositionCloser)(nil), it.exchangeStrategies); found {
if strategies, err := filterStrategiesByInterface(it.exchangeStrategies, (*PositionCloser)(nil)); err == nil && len(strategies) > 0 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy")
} else {
Expand Down Expand Up @@ -323,7 +277,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/status", "Strategy Status", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
if strategies, found := filterStrategyByInterface((*StrategyStatusReader)(nil), it.exchangeStrategies); found {
if strategies, err := filterStrategiesByInterface(it.exchangeStrategies, (*StrategyStatusReader)(nil)); err == nil && len(strategies) > 0 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose a strategy")
} else {
Expand Down Expand Up @@ -361,7 +315,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/suspend", "Suspend Strategy", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
if strategies, found := filterStrategyByInterface((*StrategyToggler)(nil), it.exchangeStrategies); found {
if strategies, err := filterStrategiesByInterface(it.exchangeStrategies, (*StrategyToggler)(nil)); err == nil && len(strategies) > 0 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy")
} else {
Expand Down Expand Up @@ -403,7 +357,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/resume", "Resume Strategy", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
if strategies, found := filterStrategyByInterface((*StrategyToggler)(nil), it.exchangeStrategies); found {
if strategies, err := filterStrategiesByInterface(it.exchangeStrategies, (*StrategyToggler)(nil)); err == nil && len(strategies) > 0 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy")
} else {
Expand Down Expand Up @@ -445,7 +399,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/emergencystop", "Emergency Stop", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
if strategies, found := filterStrategyByInterface((*EmergencyStopper)(nil), it.exchangeStrategies); found {
if strategies, err := filterStrategiesByInterface(it.exchangeStrategies, (*EmergencyStopper)(nil)); err == nil && len(strategies) > 0 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy")
} else {
Expand Down Expand Up @@ -482,7 +436,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/modifyposition", "Modify Strategy Position", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
if strategies, found := filterStrategyByField("Position", reflect.TypeOf(types.NewPosition("", "", "")), it.exchangeStrategies); found {
if strategies, err := filterStrategiesByField(it.exchangeStrategies, "Position", reflect.TypeOf(&types.Position{})); err == nil && len(strategies) > 0 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy")
} else {
Expand Down Expand Up @@ -610,3 +564,68 @@ func parseFloatPercent(s string, bitSize int) (f float64, err error) {
}
return f / 100.0, nil
}

func getStrategySignatures(exchangeStrategies map[string]SingleExchangeStrategy) []string {
var strategies []string
for signature := range exchangeStrategies {
strategies = append(strategies, signature)
}

return strategies
}

// filterStrategies filters the exchange strategies by a filter tester function
// if filter() returns true, the strategy will be added to the returned map.
func filterStrategies(exchangeStrategies map[string]SingleExchangeStrategy, filter func(s SingleExchangeStrategy) bool) (map[string]SingleExchangeStrategy, error) {
retStrategies := make(map[string]SingleExchangeStrategy)
for signature, strategy := range exchangeStrategies {
if ok := filter(strategy); ok {
retStrategies[signature] = strategy
}
}

return retStrategies, nil
}

func hasTypeField(obj interface{}, typ interface{}) bool {
targetType := reflect.TypeOf(typ)
found := false
_ = dynamic.IterateFields(obj, func(ft reflect.StructField, fv reflect.Value) error {
if fv.Type() == targetType {
found = true
}

return nil
})
return found
}

func testInterface(obj interface{}, checkType interface{}) bool {
rt := reflect.TypeOf(checkType).Elem()
return reflect.TypeOf(obj).Implements(rt)
}

func filterStrategiesByInterface(exchangeStrategies map[string]SingleExchangeStrategy, checkInterface interface{}) (map[string]SingleExchangeStrategy, error) {
rt := reflect.TypeOf(checkInterface).Elem()
return filterStrategies(exchangeStrategies, func(s SingleExchangeStrategy) bool {
return reflect.TypeOf(s).Implements(rt)
})
}

func filterStrategiesByField(exchangeStrategies map[string]SingleExchangeStrategy, fieldName string, fieldType reflect.Type) (map[string]SingleExchangeStrategy, error) {
return filterStrategies(exchangeStrategies, func(s SingleExchangeStrategy) bool {
r := reflect.ValueOf(s).Elem()
f := r.FieldByName(fieldName)
return !f.IsZero() && f.Type() == fieldType
})
}

func generateStrategyButtonsForm(strategies map[string]SingleExchangeStrategy) [][3]string {
var buttonsForm [][3]string
signatures := getStrategySignatures(strategies)
for _, signature := range signatures {
buttonsForm = append(buttonsForm, [3]string{signature, "strategy", signature})
}

return buttonsForm
}
31 changes: 28 additions & 3 deletions pkg/bbgo/interact_test.go
Expand Up @@ -6,20 +6,28 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

type myStrategy struct {
Symbol string `json:"symbol"`
Symbol string `json:"symbol"`
Position *types.Position
}

func (m myStrategy) ID() string {
func (m *myStrategy) ID() string {
return "mystrategy"
}

func (m myStrategy) InstanceID() string {
func (m *myStrategy) InstanceID() string {
return fmt.Sprintf("%s:%s", m.ID(), m.Symbol)
}

func (m *myStrategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
return nil
}

func (m *myStrategy) Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error {
return nil
}
Expand All @@ -31,3 +39,20 @@ func Test_getStrategySignature(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "mystrategy:BTCUSDT", signature)
}

func Test_hasTypeField(t *testing.T) {
s := &myStrategy{
Symbol: "BTCUSDT",
}
ok := hasTypeField(s, &types.Position{})
assert.True(t, ok)
}

func Test_testInterface(t *testing.T) {
s := &myStrategy{
Symbol: "BTCUSDT",
}

ok := testInterface(s, (*PositionCloser)(nil))
assert.True(t, ok)
}