mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
bbgo: add two test cases for reflect
This commit is contained in:
parent
278aa026f2
commit
8961c940b2
|
@ -99,6 +99,24 @@ func filterStrategies(exchangeStrategies map[string]SingleExchangeStrategy, filt
|
||||||
return retStrategies, nil
|
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) {
|
func filterStrategiesByInterface(exchangeStrategies map[string]SingleExchangeStrategy, checkInterface interface{}) (map[string]SingleExchangeStrategy, error) {
|
||||||
rt := reflect.TypeOf(checkInterface).Elem()
|
rt := reflect.TypeOf(checkInterface).Elem()
|
||||||
return filterStrategies(exchangeStrategies, func(s SingleExchangeStrategy) bool {
|
return filterStrategies(exchangeStrategies, func(s SingleExchangeStrategy) bool {
|
||||||
|
@ -210,9 +228,11 @@ 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
|
strategies, err := filterStrategies(it.exchangeStrategies, func(s SingleExchangeStrategy) bool {
|
||||||
// send symbol options
|
return testInterface(s, (*PositionResetter)(nil)) || hasTypeField(s, &types.Position{})
|
||||||
if strategies, err := filterStrategiesByInterface(it.exchangeStrategies, (*PositionResetter)(nil)); err == nil && len(strategies) > 0 {
|
})
|
||||||
|
|
||||||
|
if 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 {
|
||||||
|
@ -481,7 +501,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, err := filterStrategiesByField(it.exchangeStrategies, "Position", reflect.TypeOf(types.NewPosition("", "", ""))); err == nil && len(strategies) > 0 {
|
if strategies, err := filterStrategiesByField(it.exchangeStrategies, "Position", reflect.TypeOf(&types.Position{})); 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 {
|
||||||
|
|
|
@ -6,20 +6,28 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type myStrategy struct {
|
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"
|
return "mystrategy"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m myStrategy) InstanceID() string {
|
func (m *myStrategy) InstanceID() string {
|
||||||
return fmt.Sprintf("%s:%s", m.ID(), m.Symbol)
|
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 {
|
func (m *myStrategy) Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -31,3 +39,20 @@ func Test_getStrategySignature(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "mystrategy:BTCUSDT", signature)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user