2020-10-21 07:57:14 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-24 07:38:13 +00:00
|
|
|
"bytes"
|
2020-10-21 07:57:14 +00:00
|
|
|
"context"
|
2020-10-24 07:38:13 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2020-10-21 07:57:14 +00:00
|
|
|
"syscall"
|
2020-10-24 07:38:13 +00:00
|
|
|
"text/template"
|
2020-10-21 07:57:14 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-10-24 07:43:55 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-21 07:57:14 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-10-24 07:38:13 +00:00
|
|
|
flag "github.com/spf13/pflag"
|
2020-10-21 07:57:14 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2020-10-21 07:58:58 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
2020-10-21 07:57:14 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/config"
|
2020-10-21 09:10:47 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/notifier/slacknotifier"
|
2020-10-21 07:57:14 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/slack/slacklog"
|
2020-10-24 07:38:13 +00:00
|
|
|
|
|
|
|
// import built-in strategies
|
|
|
|
_ "github.com/c9s/bbgo/pkg/strategy/buyandhold"
|
2020-10-21 07:57:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errSlackTokenUndefined = errors.New("slack token is not defined.")
|
|
|
|
|
|
|
|
func init() {
|
2020-10-24 07:38:13 +00:00
|
|
|
RunCmd.Flags().Bool("no-compile", false, "do not compile wrapper binary")
|
2020-10-23 06:28:07 +00:00
|
|
|
RunCmd.Flags().String("config", "config/bbgo.yaml", "strategy config file")
|
|
|
|
RunCmd.Flags().String("since", "", "pnl since time")
|
|
|
|
RootCmd.AddCommand(RunCmd)
|
2020-10-21 07:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
var runTemplate = template.Must(template.New("main").Parse(`package main
|
|
|
|
// DO NOT MODIFY THIS FILE. THIS FILE IS GENERATED FOR IMPORTING STRATEGIES
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/cmd"
|
|
|
|
|
|
|
|
{{- range .Imports }}
|
|
|
|
_ "{{ . }}"
|
|
|
|
{{- end }}
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd.Execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
`))
|
|
|
|
|
|
|
|
func compileRunFile(filepath string, config *config.Config) error {
|
|
|
|
var buf = bytes.NewBuffer(nil)
|
|
|
|
if err := runTemplate.Execute(buf, config); err != nil {
|
2020-10-23 06:49:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
return ioutil.WriteFile(filepath, buf.Bytes(), 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runConfig(ctx context.Context, config *config.Config) error {
|
2020-10-23 06:49:54 +00:00
|
|
|
slackToken := viper.GetString("slack-token")
|
|
|
|
if len(slackToken) == 0 {
|
|
|
|
return errSlackTokenUndefined
|
|
|
|
}
|
|
|
|
|
2020-10-24 07:43:55 +00:00
|
|
|
log.AddHook(slacklog.NewLogHook(slackToken, viper.GetString("slack-error-channel")))
|
2020-10-23 06:49:54 +00:00
|
|
|
|
|
|
|
var notifier = slacknotifier.New(slackToken, viper.GetString("slack-channel"))
|
|
|
|
|
|
|
|
db, err := cmdutil.ConnectMySQL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
environ := bbgo.NewDefaultEnvironment(db)
|
|
|
|
environ.ReportTrade(notifier)
|
|
|
|
|
|
|
|
trader := bbgo.NewTrader(environ)
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
for _, entry := range config.ExchangeStrategies {
|
2020-10-23 06:49:54 +00:00
|
|
|
for _, mount := range entry.Mounts {
|
2020-10-24 07:43:55 +00:00
|
|
|
log.Infof("attaching strategy %T on %s...", entry.Strategy, mount)
|
2020-10-23 06:49:54 +00:00
|
|
|
trader.AttachStrategyOn(mount, entry.Strategy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
for _, strategy := range config.CrossExchangeStrategies {
|
2020-10-24 07:43:55 +00:00
|
|
|
log.Infof("attaching strategy %T", strategy)
|
2020-10-23 06:49:54 +00:00
|
|
|
trader.AttachCrossExchangeStrategy(strategy)
|
|
|
|
}
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
for _, report := range config.PnLReporters {
|
2020-10-23 06:49:54 +00:00
|
|
|
if len(report.AverageCostBySymbols) > 0 {
|
|
|
|
trader.ReportPnL(notifier).
|
|
|
|
AverageCostBySymbols(report.AverageCostBySymbols...).
|
|
|
|
Of(report.Of...).
|
|
|
|
When(report.When...)
|
|
|
|
} else {
|
|
|
|
return errors.Errorf("unsupported PnL reporter: %+v", report)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return trader.Run(ctx)
|
|
|
|
}
|
|
|
|
|
2020-10-23 06:28:07 +00:00
|
|
|
var RunCmd = &cobra.Command{
|
2020-10-21 07:57:14 +00:00
|
|
|
Use: "run",
|
2020-10-23 06:49:54 +00:00
|
|
|
Short: "run strategies from config file",
|
2020-10-21 07:57:14 +00:00
|
|
|
|
|
|
|
// SilenceUsage is an option to silence usage when an error occurs.
|
|
|
|
SilenceUsage: true,
|
|
|
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configFile) == 0 {
|
2020-10-23 06:49:54 +00:00
|
|
|
return errors.New("--config option is required")
|
2020-10-21 07:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
noCompile, err := cmd.Flags().GetBool("no-compile")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-21 07:57:14 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
userConfig, err := config.Load(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-21 07:57:14 +00:00
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
if noCompile {
|
|
|
|
if err := runConfig(ctx, userConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
buildDir := filepath.Join("build", "bbgow")
|
|
|
|
if _, err := os.Stat(buildDir); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(buildDir, 0777); err != nil {
|
|
|
|
return errors.Wrapf(err, "can not create build directory: %s", buildDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mainFile := filepath.Join(buildDir, "main.go")
|
|
|
|
if err := compileRunFile(mainFile, userConfig); err != nil {
|
|
|
|
return errors.Wrap(err, "compile error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use "\" for Windows
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTarget := filepath.Join(cwd, buildDir)
|
2020-10-24 07:43:55 +00:00
|
|
|
log.Infof("building binary from %s...", buildTarget)
|
2020-10-24 07:38:13 +00:00
|
|
|
|
|
|
|
buildCmd := exec.CommandContext(ctx, "go", "build", "-tags", "wrapper", "-o", "bbgow", buildTarget)
|
|
|
|
buildCmd.Stdout = os.Stdout
|
|
|
|
buildCmd.Stderr = os.Stderr
|
|
|
|
if err := buildCmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var flagsArgs = []string{"run", "--no-compile"}
|
|
|
|
cmd.Flags().Visit(func(flag *flag.Flag) {
|
|
|
|
flagsArgs = append(flagsArgs, flag.Name, flag.Value.String())
|
|
|
|
})
|
|
|
|
flagsArgs = append(flagsArgs, args...)
|
|
|
|
|
|
|
|
executePath := filepath.Join(cwd, "bbgow")
|
|
|
|
runCmd := exec.CommandContext(ctx, executePath, flagsArgs...)
|
|
|
|
runCmd.Stdout = os.Stdout
|
|
|
|
runCmd.Stderr = os.Stderr
|
|
|
|
if err := runCmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-10-21 07:57:14 +00:00
|
|
|
},
|
|
|
|
}
|