2022-01-14 18:52:46 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/interact"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PositionCloser interface {
|
|
|
|
ClosePosition(ctx context.Context, percentage float64) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type PositionReader interface {
|
|
|
|
CurrentPosition() *types.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
type closePositionContext struct {
|
|
|
|
signature string
|
|
|
|
closer PositionCloser
|
|
|
|
percentage float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type CoreInteraction struct {
|
|
|
|
environment *Environment
|
|
|
|
trader *Trader
|
|
|
|
|
|
|
|
exchangeStrategies map[string]SingleExchangeStrategy
|
|
|
|
closePositionContext closePositionContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCoreInteraction(environment *Environment, trader *Trader) *CoreInteraction {
|
|
|
|
return &CoreInteraction{
|
|
|
|
environment: environment,
|
|
|
|
trader: trader,
|
|
|
|
exchangeStrategies: make(map[string]SingleExchangeStrategy),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
balances := session.Account.Balances()
|
|
|
|
for _, balance := range balances {
|
2022-01-15 17:06:47 +00:00
|
|
|
if balance.Total() == 0 {
|
|
|
|
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
|
|
|
|
found := false
|
|
|
|
for signature, strategy := range it.exchangeStrategies {
|
|
|
|
if _, ok := strategy.(PositionReader); ok {
|
2022-01-19 05:07:25 +00:00
|
|
|
reply.AddButton(signature, "strategy", signature)
|
2022-01-14 18:58:55 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
reply.Message("Please choose one strategy")
|
|
|
|
} else {
|
|
|
|
reply.Message("No any strategy supports PositionReader")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, implemented := strategy.(PositionReader)
|
|
|
|
if !implemented {
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support position close", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement PositionCloser interface", signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
position := reader.CurrentPosition()
|
|
|
|
if position != nil {
|
|
|
|
reply.Send("Your current position:")
|
2022-01-14 19:13:30 +00:00
|
|
|
reply.Send(position.PlainText())
|
2022-01-14 18:58:55 +00:00
|
|
|
|
|
|
|
if position.Base == 0 {
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %q has no opened position", signature))
|
|
|
|
return fmt.Errorf("strategy %T has no opened position", strategy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.RemoveKeyboard()
|
|
|
|
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
|
|
|
|
found := false
|
|
|
|
for signature, strategy := range it.exchangeStrategies {
|
|
|
|
if _, ok := strategy.(PositionCloser); ok {
|
2022-01-19 05:07:25 +00:00
|
|
|
reply.AddButton(signature, strategy, signature)
|
2022-01-14 18:52:46 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
reply.Message("Please choose your position from the current running strategies")
|
|
|
|
} else {
|
|
|
|
reply.Message("No any strategy supports PositionCloser")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
closer, implemented := strategy.(PositionCloser)
|
|
|
|
if !implemented {
|
|
|
|
reply.Message(fmt.Sprintf("Strategy %s does not support position close", signature))
|
|
|
|
return fmt.Errorf("strategy %s does not implement PositionCloser interface", signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
it.closePositionContext.closer = closer
|
|
|
|
it.closePositionContext.signature = signature
|
|
|
|
|
|
|
|
if reader, implemented := strategy.(PositionReader); implemented {
|
|
|
|
position := reader.CurrentPosition()
|
|
|
|
if position != nil {
|
|
|
|
reply.Send("Your current position:")
|
2022-01-14 19:13:30 +00:00
|
|
|
reply.Send(position.PlainText())
|
2022-01-14 18:52:46 +00:00
|
|
|
|
|
|
|
if position.Base == 0 {
|
|
|
|
reply.Message("No opened position")
|
|
|
|
reply.RemoveKeyboard()
|
|
|
|
return fmt.Errorf("no opened position")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
percentage, err := parseFloatPercent(percentageStr, 64)
|
|
|
|
if err != nil {
|
|
|
|
reply.Message(fmt.Sprintf("%q is not a valid percentage string", percentageStr))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.RemoveKeyboard()
|
|
|
|
|
|
|
|
err = it.closePositionContext.closer.ClosePosition(context.Background(), percentage)
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStrategySignature(strategy SingleExchangeStrategy) (string, error) {
|
|
|
|
rv := reflect.ValueOf(strategy).Elem()
|
|
|
|
if rv.Kind() != reflect.Struct {
|
|
|
|
return "", fmt.Errorf("strategy %T instance is not a struct", strategy)
|
|
|
|
}
|
|
|
|
|
|
|
|
var signature = path.Base(rv.Type().PkgPath())
|
|
|
|
|
|
|
|
var id = strategy.ID()
|
|
|
|
|
|
|
|
if !strings.EqualFold(id, signature) {
|
|
|
|
signature += "." + strings.ToLower(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < rv.NumField(); i++ {
|
|
|
|
field := rv.Field(i)
|
|
|
|
if field.Kind() == reflect.String {
|
|
|
|
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
|
|
|
|
}
|