interact: add EmergencyStop() to StrategyController interface

This commit is contained in:
Andy Cheng 2022-03-21 11:42:54 +08:00
parent b6aff9674c
commit 69a02f1664
No known key found for this signature in database
GPG Key ID: 936427CF651A9D28

View File

@ -25,6 +25,7 @@ type StrategyController interface {
SuspendStrategy(ctx context.Context) error
ResumeStrategy() error
GetStrategyStatus() bool
EmergencyStop(ctx context.Context) error
}
type closePositionContext struct {
@ -365,6 +366,51 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
reply.Message(fmt.Sprintf("Strategy %s resumed.", signature))
return nil
})
i.PrivateCommand("/emergencystop", "Emergency Stop", func(reply interact.Reply) error {
// it.trader.exchangeStrategies
// send symbol options
found := false
for signature, strategy := range it.exchangeStrategies {
if _, ok := strategy.(StrategyController); ok {
reply.AddButton(signature, "strategy", signature)
found = true
}
}
if found {
reply.Message("Please choose one strategy")
} else {
reply.Message("No any strategy supports StrategyController")
}
return nil
}).Cycle(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)
}
controller, implemented := strategy.(StrategyController)
if !implemented {
reply.Message(fmt.Sprintf("Strategy %s does not support strategy resume", signature))
return fmt.Errorf("strategy %s does not implement StrategyController interface", signature)
}
err := controller.EmergencyStop(context.Background())
if kc, ok := reply.(interact.KeyboardController); ok {
kc.RemoveKeyboard()
}
if err != nil {
reply.Message(fmt.Sprintf("Failed to stop the strategy, %s", err.Error()))
return err
}
reply.Message(fmt.Sprintf("Strategy %s stopped and the position closed.", signature))
return nil
})
}
func (it *CoreInteraction) Initialize() error {