mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
bbgo/interact: refactor strategy filter functions
This commit is contained in:
parent
442ca9287d
commit
278aa026f2
|
@ -86,33 +86,32 @@ func getStrategySignatures(exchangeStrategies map[string]SingleExchangeStrategy)
|
||||||
return strategies
|
return strategies
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterStrategyByInterface(checkInterface interface{}, exchangeStrategies map[string]SingleExchangeStrategy) (strategies map[string]SingleExchangeStrategy, found bool) {
|
// filterStrategies filters the exchange strategies by a filter tester function
|
||||||
found = false
|
// if filter() returns true, the strategy will be added to the returned map.
|
||||||
strategies = make(map[string]SingleExchangeStrategy)
|
func filterStrategies(exchangeStrategies map[string]SingleExchangeStrategy, filter func(s SingleExchangeStrategy) bool) (map[string]SingleExchangeStrategy, error) {
|
||||||
rt := reflect.TypeOf(checkInterface).Elem()
|
retStrategies := make(map[string]SingleExchangeStrategy)
|
||||||
for signature, strategy := range exchangeStrategies {
|
for signature, strategy := range exchangeStrategies {
|
||||||
if ok := reflect.TypeOf(strategy).Implements(rt); ok {
|
if ok := filter(strategy); ok {
|
||||||
strategies[signature] = strategy
|
retStrategies[signature] = strategy
|
||||||
found = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return strategies, found
|
return retStrategies, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterStrategyByField(fieldName string, fieldType reflect.Type, exchangeStrategies map[string]SingleExchangeStrategy) (strategies map[string]SingleExchangeStrategy, found bool) {
|
func filterStrategiesByInterface(exchangeStrategies map[string]SingleExchangeStrategy, checkInterface interface{}) (map[string]SingleExchangeStrategy, error) {
|
||||||
found = false
|
rt := reflect.TypeOf(checkInterface).Elem()
|
||||||
strategies = make(map[string]SingleExchangeStrategy)
|
return filterStrategies(exchangeStrategies, func(s SingleExchangeStrategy) bool {
|
||||||
for signature, strategy := range exchangeStrategies {
|
return reflect.TypeOf(s).Implements(rt)
|
||||||
r := reflect.ValueOf(strategy).Elem()
|
})
|
||||||
f := r.FieldByName(fieldName)
|
}
|
||||||
if !f.IsZero() && f.Type() == fieldType {
|
|
||||||
strategies[signature] = strategy
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return strategies, found
|
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 {
|
func generateStrategyButtonsForm(strategies map[string]SingleExchangeStrategy) [][3]string {
|
||||||
|
@ -172,7 +171,7 @@ 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 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.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||||
reply.Message("Please choose one strategy")
|
reply.Message("Please choose one strategy")
|
||||||
} else {
|
} else {
|
||||||
|
@ -213,7 +212,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
|
||||||
i.PrivateCommand("/resetposition", "Reset position", func(reply interact.Reply) error {
|
i.PrivateCommand("/resetposition", "Reset position", func(reply interact.Reply) error {
|
||||||
// it.trader.exchangeStrategies
|
// it.trader.exchangeStrategies
|
||||||
// send symbol options
|
// send symbol options
|
||||||
if strategies, found := filterStrategyByInterface((*PositionResetter)(nil), it.exchangeStrategies); found {
|
if strategies, err := filterStrategiesByInterface(it.exchangeStrategies, (*PositionResetter)(nil)); err == nil && len(strategies) > 0 {
|
||||||
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||||
reply.Message("Please choose one strategy")
|
reply.Message("Please choose one strategy")
|
||||||
} else {
|
} else {
|
||||||
|
@ -254,7 +253,7 @@ 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 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.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||||
reply.Message("Please choose one strategy")
|
reply.Message("Please choose one strategy")
|
||||||
} else {
|
} else {
|
||||||
|
@ -323,7 +322,7 @@ 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 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.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||||
reply.Message("Please choose a strategy")
|
reply.Message("Please choose a strategy")
|
||||||
} else {
|
} else {
|
||||||
|
@ -361,7 +360,7 @@ 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 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.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||||
reply.Message("Please choose one strategy")
|
reply.Message("Please choose one strategy")
|
||||||
} else {
|
} else {
|
||||||
|
@ -403,7 +402,7 @@ 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 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.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||||
reply.Message("Please choose one strategy")
|
reply.Message("Please choose one strategy")
|
||||||
} else {
|
} else {
|
||||||
|
@ -445,7 +444,7 @@ 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 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.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||||
reply.Message("Please choose one strategy")
|
reply.Message("Please choose one strategy")
|
||||||
} else {
|
} else {
|
||||||
|
@ -482,7 +481,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
|
||||||
i.PrivateCommand("/modifyposition", "Modify Strategy Position", func(reply interact.Reply) error {
|
i.PrivateCommand("/modifyposition", "Modify Strategy Position", func(reply interact.Reply) error {
|
||||||
// it.trader.exchangeStrategies
|
// it.trader.exchangeStrategies
|
||||||
// send symbol options
|
// 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.NewPosition("", "", ""))); err == nil && len(strategies) > 0 {
|
||||||
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
||||||
reply.Message("Please choose one strategy")
|
reply.Message("Please choose one strategy")
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user