mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
compile local strategies into the wrapper binary
This commit is contained in:
parent
cd28fb8771
commit
9ce9ecc910
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/c9s/bbgo/pkg/cmd"
|
"github.com/c9s/bbgo/pkg/cmd"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
---
|
---
|
||||||
|
imports:
|
||||||
|
- github.com/c9s/bbgo/pkg/strategy/buyandhold
|
||||||
notifications:
|
notifications:
|
||||||
slack:
|
slack:
|
||||||
defaultChannel: "bbgo"
|
defaultChannel: "bbgo"
|
||||||
|
@ -12,13 +14,13 @@ reportTrades:
|
||||||
"sxpusdt": "bbgo-sxpusdt"
|
"sxpusdt": "bbgo-sxpusdt"
|
||||||
|
|
||||||
reportPnL:
|
reportPnL:
|
||||||
- averageCostBySymbols:
|
- averageCostBySymbols:
|
||||||
- "BTCUSDT"
|
- "BTCUSDT"
|
||||||
- "BNBUSDT"
|
- "BNBUSDT"
|
||||||
of: binance
|
of: binance
|
||||||
when:
|
when:
|
||||||
- "@daily"
|
- "@daily"
|
||||||
- "@hourly"
|
- "@hourly"
|
||||||
|
|
||||||
sessions:
|
sessions:
|
||||||
max:
|
max:
|
||||||
|
@ -31,9 +33,9 @@ sessions:
|
||||||
secretVar: BINANCE_API_SECRET
|
secretVar: BINANCE_API_SECRET
|
||||||
|
|
||||||
exchangeStrategies:
|
exchangeStrategies:
|
||||||
- on: binance
|
- on: binance
|
||||||
buyandhold:
|
buyandhold:
|
||||||
symbol: "BTCUSDT"
|
symbol: "BTCUSDT"
|
||||||
interval: "1m"
|
interval: "1m"
|
||||||
baseQuantity: 0.01
|
baseQuantity: 0.01
|
||||||
minDropPercentage: -0.02
|
minDropPercentage: -0.02
|
||||||
|
|
107
pkg/cmd/run.go
107
pkg/cmd/run.go
|
@ -1,12 +1,19 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
flag "github.com/spf13/pflag"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/bbgo"
|
"github.com/c9s/bbgo/pkg/bbgo"
|
||||||
|
@ -14,22 +21,46 @@ import (
|
||||||
"github.com/c9s/bbgo/pkg/config"
|
"github.com/c9s/bbgo/pkg/config"
|
||||||
"github.com/c9s/bbgo/pkg/notifier/slacknotifier"
|
"github.com/c9s/bbgo/pkg/notifier/slacknotifier"
|
||||||
"github.com/c9s/bbgo/pkg/slack/slacklog"
|
"github.com/c9s/bbgo/pkg/slack/slacklog"
|
||||||
|
|
||||||
|
// import built-in strategies
|
||||||
|
_ "github.com/c9s/bbgo/pkg/strategy/buyandhold"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errSlackTokenUndefined = errors.New("slack token is not defined.")
|
var errSlackTokenUndefined = errors.New("slack token is not defined.")
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
RunCmd.Flags().Bool("no-compile", false, "do not compile wrapper binary")
|
||||||
RunCmd.Flags().String("config", "config/bbgo.yaml", "strategy config file")
|
RunCmd.Flags().String("config", "config/bbgo.yaml", "strategy config file")
|
||||||
RunCmd.Flags().String("since", "", "pnl since time")
|
RunCmd.Flags().String("since", "", "pnl since time")
|
||||||
RootCmd.AddCommand(RunCmd)
|
RootCmd.AddCommand(RunCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runConfig(ctx context.Context, configFile string) error {
|
var runTemplate = template.Must(template.New("main").Parse(`package main
|
||||||
userConfig, err := config.Load(configFile)
|
// DO NOT MODIFY THIS FILE. THIS FILE IS GENERATED FOR IMPORTING STRATEGIES
|
||||||
if err != nil {
|
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 {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ioutil.WriteFile(filepath, buf.Bytes(), 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runConfig(ctx context.Context, config *config.Config) error {
|
||||||
slackToken := viper.GetString("slack-token")
|
slackToken := viper.GetString("slack-token")
|
||||||
if len(slackToken) == 0 {
|
if len(slackToken) == 0 {
|
||||||
return errSlackTokenUndefined
|
return errSlackTokenUndefined
|
||||||
|
@ -49,19 +80,19 @@ func runConfig(ctx context.Context, configFile string) error {
|
||||||
|
|
||||||
trader := bbgo.NewTrader(environ)
|
trader := bbgo.NewTrader(environ)
|
||||||
|
|
||||||
for _, entry := range userConfig.ExchangeStrategies {
|
for _, entry := range config.ExchangeStrategies {
|
||||||
for _, mount := range entry.Mounts {
|
for _, mount := range entry.Mounts {
|
||||||
logrus.Infof("attaching strategy %T on %s...", entry.Strategy, mount)
|
logrus.Infof("attaching strategy %T on %s...", entry.Strategy, mount)
|
||||||
trader.AttachStrategyOn(mount, entry.Strategy)
|
trader.AttachStrategyOn(mount, entry.Strategy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, strategy := range userConfig.CrossExchangeStrategies {
|
for _, strategy := range config.CrossExchangeStrategies {
|
||||||
logrus.Infof("attaching strategy %T", strategy)
|
logrus.Infof("attaching strategy %T", strategy)
|
||||||
trader.AttachCrossExchangeStrategy(strategy)
|
trader.AttachCrossExchangeStrategy(strategy)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, report := range userConfig.PnLReporters {
|
for _, report := range config.PnLReporters {
|
||||||
if len(report.AverageCostBySymbols) > 0 {
|
if len(report.AverageCostBySymbols) > 0 {
|
||||||
trader.ReportPnL(notifier).
|
trader.ReportPnL(notifier).
|
||||||
AverageCostBySymbols(report.AverageCostBySymbols...).
|
AverageCostBySymbols(report.AverageCostBySymbols...).
|
||||||
|
@ -92,12 +123,70 @@ var RunCmd = &cobra.Command{
|
||||||
return errors.New("--config option is required")
|
return errors.New("--config option is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
noCompile, err := cmd.Flags().GetBool("no-compile")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err = runConfig(ctx, configFile)
|
userConfig, err := config.Load(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
if noCompile {
|
||||||
return err
|
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)
|
||||||
|
logrus.Infof("building binary from %s...", buildTarget)
|
||||||
|
|
||||||
|
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
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user