From 3be801ab6b0bb3b2a2b9e5a69dd1d95586698fb6 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 11 Sep 2022 02:41:59 +0800 Subject: [PATCH] bbgo/interact: add /resetposition command --- pkg/bbgo/interact.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/bbgo/interact.go b/pkg/bbgo/interact.go index f5b776861..d4acefaaf 100644 --- a/pkg/bbgo/interact.go +++ b/pkg/bbgo/interact.go @@ -18,6 +18,10 @@ type PositionCloser interface { ClosePosition(ctx context.Context, percentage fixedpoint.Value) error } +type PositionResetter interface { + ResetPosition() error +} + type PositionReader interface { CurrentPosition() *types.Position } @@ -172,7 +176,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { reply.AddMultipleButtons(generateStrategyButtonsForm(strategies)) reply.Message("Please choose one strategy") } else { - reply.Message("No strategy supports PositionReader") + reply.Message("No any strategy supports PositionReader") } return nil }).Cycle(func(signature string, reply interact.Reply) error { @@ -206,6 +210,33 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { 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 { // it.trader.exchangeStrategies // send symbol options @@ -213,7 +244,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { reply.AddMultipleButtons(generateStrategyButtonsForm(strategies)) reply.Message("Please choose one strategy") } else { - reply.Message("No strategy supports PositionCloser") + reply.Message("No strategy supports PositionCloser interface") } return nil }).Next(func(signature string, reply interact.Reply) error {