2022-01-14 18:52:46 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
2022-04-20 11:03:06 +00:00
|
|
|
"context"
|
2022-01-14 18:52:46 +00:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-02-15 05:55:19 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-01-14 18:52:46 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/interact"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
type PositionCloser interface {
|
|
|
|
ClosePosition(ctx context.Context, percentage fixedpoint.Value) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type PositionReader interface {
|
|
|
|
CurrentPosition() *types.Position
|
|
|
|
}
|
|
|
|
|
2022-01-14 18:52:46 +00:00
|
|
|
type closePositionContext struct {
|
|
|
|
signature string
|
2022-04-20 11:03:06 +00:00
|
|
|
closer PositionCloser
|
2022-02-03 11:19:56 +00:00
|
|
|
percentage fixedpoint.Value
|
2022-01-14 18:52:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 06:40:54 +00:00
|
|
|
type modifyPositionContext struct {
|
2022-07-29 03:52:20 +00:00
|
|
|
signature string
|
2022-07-29 06:40:54 +00:00
|
|
|
modifier *types.Position
|
2022-07-29 05:45:33 +00:00
|
|
|
target string
|
2022-07-29 03:52:20 +00:00
|
|
|
value fixedpoint.Value
|
|
|
|
}
|
|
|
|
|
2022-01-14 18:52:46 +00:00
|
|
|
type CoreInteraction struct {
|
|
|
|
environment *Environment
|
|
|
|
trader *Trader
|
|
|
|
|
2022-07-29 03:52:20 +00:00
|
|
|
exchangeStrategies map[string]SingleExchangeStrategy
|
|
|
|
closePositionContext closePositionContext
|
2022-07-29 06:40:54 +00:00
|
|
|
modifyPositionContext modifyPositionContext
|
2022-01-14 18:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCoreInteraction(environment *Environment, trader *Trader) *CoreInteraction {
|
|
|
|
return &CoreInteraction{
|
|
|
|
environment: environment,
|
|
|
|
trader: trader,
|
|
|
|
exchangeStrategies: make(map[string]SingleExchangeStrategy),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 08:34:51 +00:00
|
|
|
type SimpleInteraction struct {
|
|
|
|
Command string
|
|
|
|
Description string
|
|
|
|
F interface{}
|
|
|
|
Cmd *interact.Command
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *SimpleInteraction) Commands(i *interact.Interact) {
|
|
|
|
it.Cmd = i.PrivateCommand(it.Command, it.Description, it.F)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterCommand(command, desc string, f interface{}) *interact.Command {
|
|
|
|
it := &SimpleInteraction{
|
|
|
|
Command: command,
|
|
|
|
Description: desc,
|
|
|
|
F: f,
|
|
|
|
}
|
|
|
|
interact.AddCustomInteraction(it)
|
|
|
|
return it.Cmd
|
|
|
|
}
|
|
|
|
|
2022-04-26 10:32:41 +00:00
|
|
|
func getStrategySignatures(exchangeStrategies map[string]SingleExchangeStrategy) []string {
|
2022-04-25 10:37:15 +00:00
|
|
|
var strategies []string
|
|
|
|
for signature := range exchangeStrategies {
|
|
|
|
strategies = append(strategies, signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strategies
|
|
|
|
}
|
|
|
|
|
2022-04-26 10:32:41 +00:00
|
|
|
func filterStrategyByInterface(checkInterface interface{}, exchangeStrategies map[string]SingleExchangeStrategy) (strategies map[string]SingleExchangeStrategy, found bool) {
|
2022-04-20 11:03:06 +00:00
|
|
|
found = false
|
2022-06-06 07:36:09 +00:00
|
|
|
strategies = make(map[string]SingleExchangeStrategy)
|
2022-04-20 11:03:06 +00:00
|
|
|
rt := reflect.TypeOf(checkInterface).Elem()
|
2022-04-21 09:03:01 +00:00
|
|
|
for signature, strategy := range exchangeStrategies {
|
2022-04-20 11:03:06 +00:00
|
|
|
if ok := reflect.TypeOf(strategy).Implements(rt); ok {
|
2022-04-25 10:37:15 +00:00
|
|
|
strategies[signature] = strategy
|
2022-04-20 11:03:06 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strategies, found
|
|
|
|
}
|
|
|
|
|
2022-07-29 06:40:54 +00:00
|
|
|
func filterStrategyByField(fieldName string, fieldType reflect.Type, exchangeStrategies map[string]SingleExchangeStrategy) (strategies map[string]SingleExchangeStrategy, found bool) {
|
|
|
|
found = false
|
|
|
|
strategies = make(map[string]SingleExchangeStrategy)
|
|
|
|
for signature, strategy := range exchangeStrategies {
|
|
|
|
r := reflect.ValueOf(strategy).Elem()
|
|
|
|
f := r.FieldByName(fieldName)
|
|
|
|
if !f.IsZero() && f.Type() == fieldType {
|
|
|
|
strategies[signature] = strategy
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strategies, found
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:11:50 +00:00
|
|
|
func generateStrategyButtonsForm(strategies map[string]SingleExchangeStrategy) [][3]string {
|
2022-03-28 04:37:42 +00:00
|
|
|
var buttonsForm [][3]string
|
2022-04-26 11:11:50 +00:00
|
|
|
signatures := getStrategySignatures(strategies)
|
|
|
|
for _, signature := range signatures {
|
|
|
|
buttonsForm = append(buttonsForm, [3]string{signature, "strategy", signature})
|
2022-03-28 04:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buttonsForm
|
2022-03-21 09:49:18 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 18:52:46 +00:00
|
|
|
func (it *CoreInteraction) Commands(i *interact.Interact) {
|
2022-01-15 16:25:11 +00:00
|
|
|
i.PrivateCommand("/sessions", "List Exchange Sessions", func(reply interact.Reply) error {
|
2022-01-19 05:07:25 +00:00
|
|
|
switch r := reply.(type) {
|
|
|
|
case *interact.SlackReply:
|
|
|
|
// call slack specific api to build the reply object
|
|
|
|
_ = r
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:25:11 +00:00
|
|
|
message := "Your connected sessions:\n"
|
|
|
|
for name, session := range it.environment.Sessions() {
|
|
|
|
message += "- " + name + " (" + session.ExchangeName.String() + ")\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Message(message)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
i.PrivateCommand("/balances", "Show balances", func(reply interact.Reply) error {
|
|
|
|
reply.Message("Please select an exchange session")
|
|
|
|
for name := range it.environment.Sessions() {
|
2022-01-19 05:07:25 +00:00
|
|
|
reply.AddButton(name, "session", name)
|
2022-01-15 16:25:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).Next(func(sessionName string, reply interact.Reply) error {
|
|
|
|
session, ok := it.environment.Session(sessionName)
|
|
|
|
if !ok {
|
|
|
|
reply.Message(fmt.Sprintf("Session %s not found", sessionName))
|
|
|
|
return fmt.Errorf("session %s not found", sessionName)
|
|
|
|
}
|
|
|
|
|
|
|
|
message := "Your balances\n"
|
2022-04-23 07:43:11 +00:00
|
|
|
balances := session.GetAccount().Balances()
|
2022-01-15 16:25:11 +00:00
|
|
|
for _, balance := range balances {
|
2022-02-03 11:19:56 +00:00
|
|
|
if balance.Total().IsZero() {
|
2022-01-15 17:06:47 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:25:11 +00:00
|
|
|
message += "- " + balance.String() + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Message(message)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
i.PrivateCommand("/position", "Show Position", func(reply interact.Reply) error {
|
2022-01-14 18:58:55 +00:00
|
|
|
// it.trader.exchangeStrategies
|
|
|
|
// send symbol options
|
2022-04-26 10:32:41 +00:00
|
|
|
if strategies, found := filterStrategyByInterface((*PositionReader)(nil), it.exchangeStrategies); found {
|
2022-04-26 11:11:50 +00:00
|
|
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
2022-03-23 04:05:35 +00:00
|
|
|
reply.Message("Please choose one strategy")
|
|
|
|
} else {
|
2022-04-20 11:03:06 +00:00
|
|
|
reply.Message("No strategy supports PositionReader")
|
2022-03-23 04:05:35 +00:00
|
|
|
}
|
2022-01-14 18:58:55 +00:00
|
|
|
return nil
|
2022-01-14 19:06:36 +00:00
|
|
|
}).Cycle(func(signature string, reply interact.Reply) error {
|
2022-01-14 18:58:55 +00:00
|
|
|
strategy, ok := it.exchangeStrategies[signature]
|
|
|
|
if !ok {
|
|
|
|
reply.Message("Strategy not found")
|
|
|
|
return fmt.Errorf("strategy %s not found", signature)
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
reader, implemented := strategy.(PositionReader)
|
2022-01-14 18:58:55 +00:00
|
|
|
if !implemented {
|
2022-04-20 11:03:06 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support position close", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement PositionCloser interface", signature)
|
2022-01-14 18:58:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
position := reader.CurrentPosition()
|
|
|
|
if position != nil {
|
|
|
|
reply.Send("Your current position:")
|
|
|
|
reply.Send(position.PlainText())
|
2022-01-14 18:58:55 +00:00
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
if position.Base.IsZero() {
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %q has no opened position", signature))
|
|
|
|
return fmt.Errorf("strategy %T has no opened position", strategy)
|
|
|
|
}
|
2022-01-23 06:13:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
if kc, ok := reply.(interact.KeyboardController); ok {
|
|
|
|
kc.RemoveKeyboard()
|
2022-04-18 08:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 18:58:55 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-01-15 16:25:11 +00:00
|
|
|
i.PrivateCommand("/closeposition", "Close position", func(reply interact.Reply) error {
|
2022-01-14 18:52:46 +00:00
|
|
|
// it.trader.exchangeStrategies
|
|
|
|
// send symbol options
|
2022-04-26 10:32:41 +00:00
|
|
|
if strategies, found := filterStrategyByInterface((*PositionCloser)(nil), it.exchangeStrategies); found {
|
2022-04-26 11:11:50 +00:00
|
|
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
2022-03-23 04:05:35 +00:00
|
|
|
reply.Message("Please choose one strategy")
|
|
|
|
} else {
|
2022-04-20 11:03:06 +00:00
|
|
|
reply.Message("No strategy supports PositionCloser")
|
2022-03-23 04:05:35 +00:00
|
|
|
}
|
2022-01-14 18:52:46 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
closer, implemented := strategy.(PositionCloser)
|
2022-01-14 18:52:46 +00:00
|
|
|
if !implemented {
|
2022-04-20 11:03:06 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support position close", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement PositionCloser interface", signature)
|
2022-01-14 18:52:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
it.closePositionContext.closer = closer
|
2022-01-14 18:52:46 +00:00
|
|
|
it.closePositionContext.signature = signature
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
if reader, implemented := strategy.(PositionReader); implemented {
|
|
|
|
position := reader.CurrentPosition()
|
|
|
|
if position != nil {
|
|
|
|
reply.Send("Your current position:")
|
|
|
|
reply.Send(position.PlainText())
|
2022-01-14 18:52:46 +00:00
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
if position.Base.IsZero() {
|
|
|
|
reply.Message("No opened position")
|
|
|
|
if kc, ok := reply.(interact.KeyboardController); ok {
|
|
|
|
kc.RemoveKeyboard()
|
|
|
|
}
|
|
|
|
return fmt.Errorf("no opened position")
|
2022-01-14 18:52:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Message("Choose or enter the percentage to close")
|
2022-01-19 05:07:25 +00:00
|
|
|
for _, p := range []string{"5%", "25%", "50%", "80%", "100%"} {
|
|
|
|
reply.AddButton(p, "percentage", p)
|
2022-01-14 18:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}).Next(func(percentageStr string, reply interact.Reply) error {
|
2022-02-03 11:19:56 +00:00
|
|
|
percentage, err := fixedpoint.NewFromString(percentageStr)
|
2022-01-14 18:52:46 +00:00
|
|
|
if err != nil {
|
|
|
|
reply.Message(fmt.Sprintf("%q is not a valid percentage string", percentageStr))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-15 05:55:19 +00:00
|
|
|
if kc, ok := reply.(interact.KeyboardController); ok {
|
2022-01-23 06:13:47 +00:00
|
|
|
kc.RemoveKeyboard()
|
|
|
|
}
|
2022-01-14 18:52:46 +00:00
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
err = it.closePositionContext.closer.ClosePosition(context.Background(), percentage)
|
2022-01-14 18:52:46 +00:00
|
|
|
if err != nil {
|
|
|
|
reply.Message(fmt.Sprintf("Failed to close the position, %s", err.Error()))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:25:11 +00:00
|
|
|
reply.Message("Done")
|
2022-01-14 18:52:46 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-03-18 10:43:07 +00:00
|
|
|
|
|
|
|
i.PrivateCommand("/status", "Strategy Status", func(reply interact.Reply) error {
|
|
|
|
// it.trader.exchangeStrategies
|
|
|
|
// send symbol options
|
2022-04-26 10:32:41 +00:00
|
|
|
if strategies, found := filterStrategyByInterface((*StrategyStatusReader)(nil), it.exchangeStrategies); found {
|
2022-04-26 11:11:50 +00:00
|
|
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
2022-04-18 08:42:29 +00:00
|
|
|
reply.Message("Please choose a strategy")
|
2022-03-23 04:05:35 +00:00
|
|
|
} else {
|
2022-04-21 08:55:58 +00:00
|
|
|
reply.Message("No strategy supports StrategyStatusReader")
|
2022-03-23 04:05:35 +00:00
|
|
|
}
|
2022-03-18 10:43:07 +00:00
|
|
|
return nil
|
2022-03-28 03:58:01 +00:00
|
|
|
}).Next(func(signature string, reply interact.Reply) error {
|
2022-03-18 10:43:07 +00:00
|
|
|
strategy, ok := it.exchangeStrategies[signature]
|
|
|
|
if !ok {
|
|
|
|
reply.Message("Strategy not found")
|
|
|
|
return fmt.Errorf("strategy %s not found", signature)
|
|
|
|
}
|
|
|
|
|
2022-04-21 08:55:58 +00:00
|
|
|
controller, implemented := strategy.(StrategyStatusReader)
|
2022-03-18 10:43:07 +00:00
|
|
|
if !implemented {
|
2022-04-21 08:55:58 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support StrategyStatusReader", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement StrategyStatusReader", signature)
|
2022-03-18 10:43:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
status := controller.GetStatus()
|
2022-03-18 10:43:07 +00:00
|
|
|
|
|
|
|
if kc, ok := reply.(interact.KeyboardController); ok {
|
|
|
|
kc.RemoveKeyboard()
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
if status == types.StrategyStatusRunning {
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %s is running.", signature))
|
|
|
|
} else if status == types.StrategyStatusStopped {
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %s is not running.", signature))
|
2022-03-18 10:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
i.PrivateCommand("/suspend", "Suspend Strategy", func(reply interact.Reply) error {
|
|
|
|
// it.trader.exchangeStrategies
|
|
|
|
// send symbol options
|
2022-04-26 10:32:41 +00:00
|
|
|
if strategies, found := filterStrategyByInterface((*StrategyToggler)(nil), it.exchangeStrategies); found {
|
2022-04-26 11:11:50 +00:00
|
|
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
2022-03-23 04:05:35 +00:00
|
|
|
reply.Message("Please choose one strategy")
|
|
|
|
} else {
|
2022-04-21 08:55:58 +00:00
|
|
|
reply.Message("No strategy supports StrategyToggler")
|
2022-03-23 04:05:35 +00:00
|
|
|
}
|
2022-03-18 10:43:07 +00:00
|
|
|
return nil
|
2022-03-28 03:58:01 +00:00
|
|
|
}).Next(func(signature string, reply interact.Reply) error {
|
2022-03-18 10:43:07 +00:00
|
|
|
strategy, ok := it.exchangeStrategies[signature]
|
|
|
|
if !ok {
|
|
|
|
reply.Message("Strategy not found")
|
|
|
|
return fmt.Errorf("strategy %s not found", signature)
|
|
|
|
}
|
|
|
|
|
2022-04-21 08:55:58 +00:00
|
|
|
controller, implemented := strategy.(StrategyToggler)
|
2022-03-18 10:43:07 +00:00
|
|
|
if !implemented {
|
2022-04-21 08:55:58 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support StrategyToggler", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement StrategyToggler", signature)
|
2022-03-18 10:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check strategy status before suspend
|
2022-04-20 11:03:06 +00:00
|
|
|
if controller.GetStatus() != types.StrategyStatusRunning {
|
2022-03-18 10:43:07 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s is not running.", signature))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if kc, ok := reply.(interact.KeyboardController); ok {
|
|
|
|
kc.RemoveKeyboard()
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
if err := controller.Suspend(); err != nil {
|
2022-03-18 10:43:07 +00:00
|
|
|
reply.Message(fmt.Sprintf("Failed to suspend the strategy, %s", err.Error()))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %s suspended.", signature))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
i.PrivateCommand("/resume", "Resume Strategy", func(reply interact.Reply) error {
|
|
|
|
// it.trader.exchangeStrategies
|
|
|
|
// send symbol options
|
2022-04-26 10:32:41 +00:00
|
|
|
if strategies, found := filterStrategyByInterface((*StrategyToggler)(nil), it.exchangeStrategies); found {
|
2022-04-26 11:11:50 +00:00
|
|
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
2022-03-23 04:05:35 +00:00
|
|
|
reply.Message("Please choose one strategy")
|
|
|
|
} else {
|
2022-04-21 08:55:58 +00:00
|
|
|
reply.Message("No strategy supports StrategyToggler")
|
2022-03-23 04:05:35 +00:00
|
|
|
}
|
2022-03-18 10:43:07 +00:00
|
|
|
return nil
|
2022-03-28 03:58:01 +00:00
|
|
|
}).Next(func(signature string, reply interact.Reply) error {
|
2022-03-18 10:43:07 +00:00
|
|
|
strategy, ok := it.exchangeStrategies[signature]
|
|
|
|
if !ok {
|
|
|
|
reply.Message("Strategy not found")
|
|
|
|
return fmt.Errorf("strategy %s not found", signature)
|
|
|
|
}
|
|
|
|
|
2022-04-21 08:55:58 +00:00
|
|
|
controller, implemented := strategy.(StrategyToggler)
|
2022-03-18 10:43:07 +00:00
|
|
|
if !implemented {
|
2022-04-21 08:55:58 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support StrategyToggler", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement StrategyToggler", signature)
|
2022-03-18 10:43:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
// Check strategy status before suspend
|
|
|
|
if controller.GetStatus() != types.StrategyStatusStopped {
|
2022-03-18 10:43:07 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s is running.", signature))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if kc, ok := reply.(interact.KeyboardController); ok {
|
|
|
|
kc.RemoveKeyboard()
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
if err := controller.Resume(); err != nil {
|
2022-03-18 10:43:07 +00:00
|
|
|
reply.Message(fmt.Sprintf("Failed to resume the strategy, %s", err.Error()))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %s resumed.", signature))
|
|
|
|
return nil
|
|
|
|
})
|
2022-03-21 03:42:54 +00:00
|
|
|
|
|
|
|
i.PrivateCommand("/emergencystop", "Emergency Stop", func(reply interact.Reply) error {
|
|
|
|
// it.trader.exchangeStrategies
|
|
|
|
// send symbol options
|
2022-04-26 10:32:41 +00:00
|
|
|
if strategies, found := filterStrategyByInterface((*EmergencyStopper)(nil), it.exchangeStrategies); found {
|
2022-04-26 11:11:50 +00:00
|
|
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
2022-03-23 04:05:35 +00:00
|
|
|
reply.Message("Please choose one strategy")
|
|
|
|
} else {
|
2022-04-21 08:55:58 +00:00
|
|
|
reply.Message("No strategy supports EmergencyStopper")
|
2022-03-23 04:05:35 +00:00
|
|
|
}
|
2022-03-21 03:42:54 +00:00
|
|
|
return nil
|
2022-03-28 03:58:01 +00:00
|
|
|
}).Next(func(signature string, reply interact.Reply) error {
|
2022-03-21 03:42:54 +00:00
|
|
|
strategy, ok := it.exchangeStrategies[signature]
|
|
|
|
if !ok {
|
|
|
|
reply.Message("Strategy not found")
|
|
|
|
return fmt.Errorf("strategy %s not found", signature)
|
|
|
|
}
|
|
|
|
|
2022-04-21 08:55:58 +00:00
|
|
|
controller, implemented := strategy.(EmergencyStopper)
|
2022-03-21 03:42:54 +00:00
|
|
|
if !implemented {
|
2022-04-21 08:55:58 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support EmergencyStopper", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement EmergencyStopper", signature)
|
2022-03-21 03:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if kc, ok := reply.(interact.KeyboardController); ok {
|
|
|
|
kc.RemoveKeyboard()
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:03:06 +00:00
|
|
|
if err := controller.EmergencyStop(); err != nil {
|
2022-04-18 08:42:29 +00:00
|
|
|
reply.Message(fmt.Sprintf("Failed to emergency stop the strategy, %s", err.Error()))
|
2022-03-21 03:42:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %s stopped and the position closed.", signature))
|
|
|
|
return nil
|
|
|
|
})
|
2022-07-29 03:52:20 +00:00
|
|
|
|
2022-07-29 05:45:33 +00:00
|
|
|
// Position updater
|
2022-07-29 06:40:54 +00:00
|
|
|
i.PrivateCommand("/modifyposition", "Modify Strategy Position", func(reply interact.Reply) error {
|
2022-07-29 03:52:20 +00:00
|
|
|
// it.trader.exchangeStrategies
|
|
|
|
// send symbol options
|
2022-07-29 06:40:54 +00:00
|
|
|
if strategies, found := filterStrategyByField("Position", reflect.TypeOf(types.NewPosition("", "", "")), it.exchangeStrategies); found {
|
2022-07-29 03:52:20 +00:00
|
|
|
reply.AddMultipleButtons(generateStrategyButtonsForm(strategies))
|
|
|
|
reply.Message("Please choose one strategy")
|
|
|
|
} else {
|
2022-07-29 06:40:54 +00:00
|
|
|
reply.Message("No strategy supports Position Modify")
|
2022-07-29 03:52:20 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-29 06:40:54 +00:00
|
|
|
r := reflect.ValueOf(strategy).Elem()
|
|
|
|
f := r.FieldByName("Position")
|
|
|
|
positionModifier, implemented := f.Interface().(*types.Position)
|
2022-07-29 03:52:20 +00:00
|
|
|
if !implemented {
|
2022-07-29 06:40:54 +00:00
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support Position Modify", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement Position Modify", signature)
|
2022-07-29 03:52:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 06:40:54 +00:00
|
|
|
it.modifyPositionContext.modifier = positionModifier
|
|
|
|
it.modifyPositionContext.signature = signature
|
2022-07-29 03:52:20 +00:00
|
|
|
|
2022-07-29 05:45:33 +00:00
|
|
|
reply.Message("Please choose what you want to change")
|
|
|
|
reply.AddButton("base", "Base", "base")
|
|
|
|
reply.AddButton("quote", "Quote", "quote")
|
|
|
|
reply.AddButton("cost", "Average Cost", "cost")
|
2022-07-29 03:52:20 +00:00
|
|
|
|
|
|
|
return nil
|
2022-07-29 05:45:33 +00:00
|
|
|
}).Next(func(target string, reply interact.Reply) error {
|
|
|
|
if target != "base" && target != "quote" && target != "cost" {
|
|
|
|
reply.Message(fmt.Sprintf("%q is not a valid target string", target))
|
|
|
|
return fmt.Errorf("%q is not a valid target string", target)
|
2022-07-29 03:52:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 06:40:54 +00:00
|
|
|
it.modifyPositionContext.target = target
|
2022-07-29 04:00:11 +00:00
|
|
|
|
|
|
|
reply.Message("Enter the amount to change")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}).Next(func(valueStr string, reply interact.Reply) error {
|
2022-07-29 06:40:54 +00:00
|
|
|
value, err := fixedpoint.NewFromString(valueStr)
|
2022-07-29 04:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
reply.Message(fmt.Sprintf("%q is not a valid value string", valueStr))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if kc, ok := reply.(interact.KeyboardController); ok {
|
|
|
|
kc.RemoveKeyboard()
|
|
|
|
}
|
|
|
|
|
2022-07-29 06:40:54 +00:00
|
|
|
if it.modifyPositionContext.target == "base" {
|
|
|
|
err = it.modifyPositionContext.modifier.ModifyBase(value)
|
|
|
|
} else if it.modifyPositionContext.target == "quote" {
|
|
|
|
err = it.modifyPositionContext.modifier.ModifyQuote(value)
|
|
|
|
} else if it.modifyPositionContext.target == "cost" {
|
|
|
|
err = it.modifyPositionContext.modifier.ModifyAverageCost(value)
|
2022-07-29 04:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-07-29 05:45:33 +00:00
|
|
|
reply.Message(fmt.Sprintf("Failed to modify position of the strategy, %s", err.Error()))
|
2022-07-29 04:05:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-29 06:40:54 +00:00
|
|
|
reply.Message(fmt.Sprintf("Position of strategy %s modified.", it.modifyPositionContext.signature))
|
2022-07-29 04:05:39 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-01-14 18:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *CoreInteraction) Initialize() error {
|
|
|
|
// re-map exchange strategies into the signature-object map
|
|
|
|
for sessionID, strategies := range it.trader.exchangeStrategies {
|
|
|
|
for _, strategy := range strategies {
|
|
|
|
signature, err := getStrategySignature(strategy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := sessionID + "." + signature
|
|
|
|
it.exchangeStrategies[key] = strategy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 02:40:15 +00:00
|
|
|
// getStrategySignature returns strategy instance unique signature
|
2022-01-14 18:52:46 +00:00
|
|
|
func getStrategySignature(strategy SingleExchangeStrategy) (string, error) {
|
2022-06-07 02:40:15 +00:00
|
|
|
// Returns instance ID
|
|
|
|
var signature = callID(strategy)
|
|
|
|
if signature != "" {
|
|
|
|
return signature, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use reflect to build instance signature
|
2022-01-14 18:52:46 +00:00
|
|
|
rv := reflect.ValueOf(strategy).Elem()
|
|
|
|
if rv.Kind() != reflect.Struct {
|
|
|
|
return "", fmt.Errorf("strategy %T instance is not a struct", strategy)
|
|
|
|
}
|
|
|
|
|
2022-06-07 02:40:15 +00:00
|
|
|
signature = path.Base(rv.Type().PkgPath())
|
2022-01-14 18:52:46 +00:00
|
|
|
for i := 0; i < rv.NumField(); i++ {
|
|
|
|
field := rv.Field(i)
|
2022-04-26 13:05:26 +00:00
|
|
|
fieldName := rv.Type().Field(i).Name
|
|
|
|
if field.Kind() == reflect.String && fieldName != "Status" {
|
2022-01-14 18:52:46 +00:00
|
|
|
str := field.String()
|
|
|
|
if len(str) > 0 {
|
|
|
|
signature += "." + field.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return signature, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFloatPercent(s string, bitSize int) (f float64, err error) {
|
|
|
|
i := strings.Index(s, "%")
|
|
|
|
if i < 0 {
|
|
|
|
return strconv.ParseFloat(s, bitSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = strconv.ParseFloat(s[:i], bitSize)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return f / 100.0, nil
|
|
|
|
}
|