2020-07-10 13:34:39 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-27 05:54:39 +00:00
|
|
|
"reflect"
|
2020-07-13 04:57:18 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-10-12 09:33:02 +00:00
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2020-10-16 02:09:30 +00:00
|
|
|
flag "github.com/spf13/pflag"
|
2020-07-10 13:34:39 +00:00
|
|
|
)
|
|
|
|
|
2020-10-16 02:09:30 +00:00
|
|
|
var SupportedExchanges = []types.ExchangeName{"binance", "max"}
|
|
|
|
|
|
|
|
// PersistentFlags defines the flags for environments
|
|
|
|
func PersistentFlags(flags *flag.FlagSet) {
|
|
|
|
flags.String("binance-api-key", "", "binance api key")
|
|
|
|
flags.String("binance-api-secret", "", "binance api secret")
|
|
|
|
flags.String("max-api-key", "", "max api key")
|
|
|
|
flags.String("max-api-secret", "", "max api secret")
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
// SingleExchangeStrategy represents the single Exchange strategy
|
|
|
|
type SingleExchangeStrategy interface {
|
2020-10-25 16:26:17 +00:00
|
|
|
Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error
|
2020-07-14 06:54:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
type CrossExchangeStrategy interface {
|
2020-10-25 16:26:17 +00:00
|
|
|
Run(ctx context.Context, orderExecutionRouter OrderExecutionRouter, sessions map[string]*ExchangeSession) error
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 02:06:15 +00:00
|
|
|
type Trader struct {
|
|
|
|
Notifiability
|
2020-10-22 02:54:03 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
environment *Environment
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-26 13:36:47 +00:00
|
|
|
riskControls *RiskControls
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
crossExchangeStrategies []CrossExchangeStrategy
|
|
|
|
exchangeStrategies map[string][]SingleExchangeStrategy
|
2020-10-14 02:06:15 +00:00
|
|
|
|
2020-10-27 00:17:42 +00:00
|
|
|
tradeReporter *TradeReporter
|
2020-10-14 02:06:15 +00:00
|
|
|
// reportTimer *time.Timer
|
|
|
|
// ProfitAndLossCalculator *accounting.ProfitAndLossCalculator
|
2020-07-13 05:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
func NewTrader(environ *Environment) *Trader {
|
2020-09-07 06:20:03 +00:00
|
|
|
return &Trader{
|
2020-10-12 14:46:06 +00:00
|
|
|
environment: environ,
|
|
|
|
exchangeStrategies: make(map[string][]SingleExchangeStrategy),
|
2020-09-07 06:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 00:17:42 +00:00
|
|
|
func (trader *Trader) ReportTrade() *TradeReporter {
|
|
|
|
trader.tradeReporter = NewTradeReporter(&trader.Notifiability)
|
|
|
|
return trader.tradeReporter
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:46:34 +00:00
|
|
|
// AttachStrategyOn attaches the single exchange strategy on an exchange session.
|
2020-10-12 14:46:06 +00:00
|
|
|
// Single exchange strategy is the default behavior.
|
2020-10-19 14:46:34 +00:00
|
|
|
func (trader *Trader) AttachStrategyOn(session string, strategies ...SingleExchangeStrategy) *Trader {
|
2020-10-12 14:46:06 +00:00
|
|
|
if _, ok := trader.environment.sessions[session]; !ok {
|
2020-10-13 06:50:59 +00:00
|
|
|
log.Panicf("session %s is not defined", session)
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-09-19 03:25:48 +00:00
|
|
|
|
2020-10-16 02:26:45 +00:00
|
|
|
for _, s := range strategies {
|
|
|
|
trader.exchangeStrategies[session] = append(trader.exchangeStrategies[session], s)
|
|
|
|
}
|
2020-10-16 05:52:18 +00:00
|
|
|
|
|
|
|
return trader
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
// AttachCrossExchangeStrategy attaches the cross exchange strategy
|
2020-10-16 05:52:18 +00:00
|
|
|
func (trader *Trader) AttachCrossExchangeStrategy(strategy CrossExchangeStrategy) *Trader {
|
2020-10-12 14:46:06 +00:00
|
|
|
trader.crossExchangeStrategies = append(trader.crossExchangeStrategies, strategy)
|
2020-10-16 05:52:18 +00:00
|
|
|
|
|
|
|
return trader
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-27 01:58:21 +00:00
|
|
|
// TODO: provide a more DSL way to configure risk controls
|
2020-10-26 13:36:47 +00:00
|
|
|
func (trader *Trader) SetRiskControls(riskControls *RiskControls) {
|
|
|
|
trader.riskControls = riskControls
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
func (trader *Trader) Run(ctx context.Context) error {
|
|
|
|
if err := trader.environment.Init(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
// load and run session strategies
|
2020-10-16 02:26:45 +00:00
|
|
|
for sessionName, strategies := range trader.exchangeStrategies {
|
2020-10-25 10:26:10 +00:00
|
|
|
session := trader.environment.sessions[sessionName]
|
2020-10-26 13:36:47 +00:00
|
|
|
|
2020-10-27 00:17:42 +00:00
|
|
|
if session.tradeReporter != nil {
|
|
|
|
session.Stream.OnTrade(func(trade types.Trade) {
|
|
|
|
session.tradeReporter.Report(trade)
|
|
|
|
})
|
|
|
|
} else if trader.tradeReporter != nil {
|
|
|
|
session.Stream.OnTrade(func(trade types.Trade) {
|
|
|
|
trader.tradeReporter.Report(trade)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-27 01:58:21 +00:00
|
|
|
var baseOrderExecutor = &ExchangeOrderExecutor{
|
2020-10-27 02:00:41 +00:00
|
|
|
// copy the parent notifiers and session
|
2020-10-14 02:06:15 +00:00
|
|
|
Notifiability: trader.Notifiability,
|
2020-10-25 16:26:17 +00:00
|
|
|
session: session,
|
2020-10-14 02:06:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 01:58:21 +00:00
|
|
|
// default to base order executor
|
|
|
|
var orderExecutor OrderExecutor = baseOrderExecutor
|
|
|
|
|
2020-10-26 13:36:47 +00:00
|
|
|
// Since the risk controls are loaded from the config file
|
2020-10-27 05:54:39 +00:00
|
|
|
if riskControls := trader.riskControls; riskControls != nil {
|
2020-10-27 01:58:21 +00:00
|
|
|
if trader.riskControls.SessionBasedRiskControl != nil {
|
|
|
|
control, ok := trader.riskControls.SessionBasedRiskControl[sessionName]
|
|
|
|
if ok {
|
|
|
|
control.SetBaseOrderExecutor(baseOrderExecutor)
|
|
|
|
|
|
|
|
// pick the order executor
|
|
|
|
if control.OrderExecutor != nil {
|
|
|
|
orderExecutor = control.OrderExecutor
|
|
|
|
}
|
2020-10-26 13:36:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
for _, strategy := range strategies {
|
2020-10-27 05:54:39 +00:00
|
|
|
rs := reflect.ValueOf(strategy)
|
|
|
|
if rs.Elem().Kind() == reflect.Struct {
|
2020-10-27 11:33:11 +00:00
|
|
|
// get the struct element
|
2020-10-27 05:54:39 +00:00
|
|
|
rs = rs.Elem()
|
2020-10-27 11:33:11 +00:00
|
|
|
|
2020-10-27 11:37:11 +00:00
|
|
|
trader.fillStrategyNotifiability(strategy, rs)
|
2020-10-27 05:54:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
err := strategy.Run(ctx, orderExecutor, session)
|
2020-09-28 07:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-14 02:06:15 +00:00
|
|
|
router := &ExchangeOrderExecutionRouter{
|
|
|
|
// copy the parent notifiers
|
|
|
|
Notifiability: trader.Notifiability,
|
2020-10-16 02:09:30 +00:00
|
|
|
sessions: trader.environment.sessions,
|
2020-10-14 02:06:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
for _, strategy := range trader.crossExchangeStrategies {
|
2020-10-14 02:06:15 +00:00
|
|
|
if err := strategy.Run(ctx, router, trader.environment.sessions); err != nil {
|
2020-09-19 02:59:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-09-19 02:59:43 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
return trader.environment.Connect(ctx)
|
2020-09-07 06:20:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 11:37:11 +00:00
|
|
|
func (trader *Trader) fillStrategyNotifiability(strategy SingleExchangeStrategy, rs reflect.Value) {
|
|
|
|
field := rs.FieldByName("Notifiability")
|
|
|
|
if !field.IsValid() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("found Notifiability in strategy %T, configuring...", strategy)
|
|
|
|
if !field.CanSet() {
|
|
|
|
log.Panicf("strategy %T field Notifiability can not be set", strategy)
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Type() == reflect.PtrTo(reflect.TypeOf(trader.Notifiability)) {
|
|
|
|
field.Set(reflect.ValueOf(&trader.Notifiability))
|
|
|
|
} else {
|
|
|
|
field.Set(reflect.ValueOf(trader.Notifiability))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
/*
|
2020-10-13 10:08:02 +00:00
|
|
|
func (trader *OrderExecutor) RunStrategyWithHotReload(ctx context.Context, strategy SingleExchangeStrategy, configFile string) (chan struct{}, error) {
|
2020-09-08 06:56:08 +00:00
|
|
|
var done = make(chan struct{})
|
|
|
|
var configWatcherDone = make(chan struct{})
|
|
|
|
|
|
|
|
log.Infof("watching config file: %v", configFile)
|
|
|
|
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
if err := watcher.Add(configFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
strategyContext, strategyCancel := context.WithCancel(ctx)
|
|
|
|
defer strategyCancel()
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
traderDone, err := trader.RunStrategy(strategyContext, strategy)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var configReloadTimer *time.Timer = nil
|
|
|
|
defer close(configWatcherDone)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-traderDone:
|
|
|
|
log.Infof("reloading config file %s", configFile)
|
|
|
|
if err := config.LoadConfigFile(configFile, strategy); err != nil {
|
|
|
|
log.WithError(err).Error("error load config file")
|
|
|
|
}
|
|
|
|
|
2020-10-22 02:47:54 +00:00
|
|
|
trader.NotifyTo("config reloaded, restarting trader")
|
2020-09-08 06:56:08 +00:00
|
|
|
|
|
|
|
traderDone, err = trader.RunStrategy(strategyContext, strategy)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("[trader] error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case event := <-watcher.Events:
|
|
|
|
log.Infof("[fsnotify] event: %+v", event)
|
|
|
|
|
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
|
|
|
log.Info("[fsnotify] modified file:", event.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if configReloadTimer != nil {
|
|
|
|
configReloadTimer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
configReloadTimer = time.AfterFunc(3*time.Second, func() {
|
|
|
|
strategyCancel()
|
|
|
|
})
|
|
|
|
|
|
|
|
case err := <-watcher.Errors:
|
|
|
|
log.WithError(err).Error("[fsnotify] error:", err)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return done, nil
|
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
*/
|
2020-09-08 06:56:08 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
/*
|
2020-10-13 10:08:02 +00:00
|
|
|
func (trader *OrderExecutor) RunStrategy(ctx context.Context, strategy SingleExchangeStrategy) (chan struct{}, error) {
|
2020-07-13 16:20:15 +00:00
|
|
|
trader.reportTimer = time.AfterFunc(1*time.Second, func() {
|
2020-09-07 06:33:58 +00:00
|
|
|
trader.reportPnL()
|
2020-07-13 05:25:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
stream.OnTrade(func(trade *types.Trade) {
|
2020-09-19 03:09:20 +00:00
|
|
|
trader.NotifyTrade(trade)
|
2020-07-16 07:36:02 +00:00
|
|
|
trader.ProfitAndLossCalculator.AddTrade(*trade)
|
2020-09-05 08:22:46 +00:00
|
|
|
_, err := trader.Context.StockManager.AddTrades([]types.Trade{*trade})
|
2020-08-04 11:05:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("stock manager load trades error")
|
|
|
|
}
|
2020-07-13 05:25:48 +00:00
|
|
|
|
2020-07-13 16:20:15 +00:00
|
|
|
if trader.reportTimer != nil {
|
|
|
|
trader.reportTimer.Stop()
|
2020-07-13 05:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 05:27:28 +00:00
|
|
|
trader.reportTimer = time.AfterFunc(1*time.Minute, func() {
|
2020-09-07 06:33:58 +00:00
|
|
|
trader.reportPnL()
|
2020-07-13 05:25:48 +00:00
|
|
|
})
|
|
|
|
})
|
2020-07-10 13:34:39 +00:00
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
*/
|
2020-07-10 13:34:39 +00:00
|
|
|
|
2020-10-26 08:44:05 +00:00
|
|
|
// ReportPnL configure and set the PnLReporter with the given notifier
|
2020-10-27 05:54:39 +00:00
|
|
|
func (trader *Trader) ReportPnL() *PnLReporterManager {
|
|
|
|
return NewPnLReporter(&trader.Notifiability)
|
2020-10-22 07:57:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
type OrderExecutor interface {
|
|
|
|
SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders []types.Order, err error)
|
2020-10-14 02:06:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
type OrderExecutionRouter interface {
|
|
|
|
// SubmitOrderTo submit order to a specific exchange session
|
|
|
|
SubmitOrdersTo(ctx context.Context, session string, orders ...types.SubmitOrder) (createdOrders []types.Order, err error)
|
2020-10-14 02:06:15 +00:00
|
|
|
}
|