bbgo/interact: add /resetposition command

This commit is contained in:
c9s 2022-09-11 02:41:59 +08:00
parent 9474c2779c
commit 3be801ab6b
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -18,6 +18,10 @@ type PositionCloser interface {
ClosePosition(ctx context.Context, percentage fixedpoint.Value) error ClosePosition(ctx context.Context, percentage fixedpoint.Value) error
} }
type PositionResetter interface {
ResetPosition() error
}
type PositionReader interface { type PositionReader interface {
CurrentPosition() *types.Position CurrentPosition() *types.Position
} }
@ -172,7 +176,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies)) reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy") reply.Message("Please choose one strategy")
} else { } else {
reply.Message("No strategy supports PositionReader") reply.Message("No any strategy supports PositionReader")
} }
return nil return nil
}).Cycle(func(signature string, reply interact.Reply) error { }).Cycle(func(signature string, reply interact.Reply) error {
@ -206,6 +210,33 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
return nil return nil
}) })
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 {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy")
} else {
reply.Message("No strategy supports PositionResetter interface")
}
return nil
}).Next(func(signature string, reply interact.Reply) error {
strategy, ok := it.exchangeStrategies[signature]
if !ok {
reply.Message("Strategy not found")
return fmt.Errorf("strategy %s not found", signature)
}
resetter, implemented := strategy.(PositionResetter)
if !implemented {
reply.Message(fmt.Sprintf("Strategy %s does not support PositionResetter", signature))
return fmt.Errorf("strategy %s does not implement PositionResetter interface", signature)
}
err := resetter.ResetPosition()
return err
})
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
@ -213,7 +244,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies)) reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
reply.Message("Please choose one strategy") reply.Message("Please choose one strategy")
} else { } else {
reply.Message("No strategy supports PositionCloser") reply.Message("No strategy supports PositionCloser interface")
} }
return nil return nil
}).Next(func(signature string, reply interact.Reply) error { }).Next(func(signature string, reply interact.Reply) error {