2020-07-10 13:34:39 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-27 05:54:39 +00:00
|
|
|
"reflect"
|
2020-11-12 06:50:08 +00:00
|
|
|
"sync"
|
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-07-10 13:34:39 +00:00
|
|
|
)
|
|
|
|
|
2020-10-16 02:09:30 +00:00
|
|
|
var SupportedExchanges = []types.ExchangeName{"binance", "max"}
|
|
|
|
|
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-28 08:27:25 +00:00
|
|
|
type ExchangeSessionSubscriber interface {
|
|
|
|
Subscribe(session *ExchangeSession)
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:27:33 +00:00
|
|
|
type CrossExchangeSessionSubscriber interface {
|
|
|
|
Subscribe(sessions map[string]*ExchangeSession)
|
|
|
|
}
|
|
|
|
|
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-11-12 06:50:08 +00:00
|
|
|
//go:generate callbackgen -type Graceful
|
|
|
|
type Graceful struct {
|
|
|
|
shutdownCallbacks []func(ctx context.Context, wg *sync.WaitGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graceful) Shutdown(ctx context.Context) {
|
|
|
|
var wg sync.WaitGroup
|
2020-11-12 06:59:47 +00:00
|
|
|
wg.Add(len(g.shutdownCallbacks))
|
|
|
|
|
|
|
|
go g.EmitShutdown(ctx, &wg)
|
|
|
|
|
2020-11-12 06:50:08 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2020-11-09 08:34:35 +00:00
|
|
|
type Logging interface {
|
|
|
|
EnableLogging()
|
|
|
|
DisableLogging()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Logger interface {
|
|
|
|
Warnf(message string, args ...interface{})
|
|
|
|
Errorf(message string, args ...interface{})
|
|
|
|
Infof(message string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type SilentLogger struct{}
|
|
|
|
|
|
|
|
func (logger *SilentLogger) Infof(message string, args ...interface{}) {}
|
|
|
|
func (logger *SilentLogger) Warnf(message string, args ...interface{}) {}
|
|
|
|
func (logger *SilentLogger) Errorf(message string, args ...interface{}) {}
|
|
|
|
|
2020-10-14 02:06:15 +00:00
|
|
|
type Trader struct {
|
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-11-09 08:34:35 +00:00
|
|
|
|
|
|
|
logger Logger
|
2020-11-12 06:50:08 +00:00
|
|
|
|
|
|
|
Graceful Graceful
|
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-11-09 08:34:35 +00:00
|
|
|
logger: log.StandardLogger(),
|
2020-09-07 06:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 08:34:35 +00:00
|
|
|
func (trader *Trader) EnableLogging() {
|
|
|
|
trader.logger = log.StandardLogger()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trader *Trader) DisableLogging() {
|
|
|
|
trader.logger = &SilentLogger{}
|
|
|
|
}
|
|
|
|
|
2020-11-17 00:19:22 +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-11-17 00:19:22 +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 {
|
2020-10-28 08:27:25 +00:00
|
|
|
// pre-subscribe the data
|
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-28 08:27:25 +00:00
|
|
|
for _, strategy := range strategies {
|
|
|
|
if subscriber, ok := strategy.(ExchangeSessionSubscriber); ok {
|
|
|
|
subscriber.Subscribe(session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 13:36:47 +00:00
|
|
|
|
2020-12-02 14:44:41 +00:00
|
|
|
for _, strategy := range trader.crossExchangeStrategies {
|
|
|
|
if subscriber, ok := strategy.(CrossExchangeSessionSubscriber); ok {
|
|
|
|
subscriber.Subscribe(trader.environment.sessions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
if err := trader.environment.Init(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
// load and run Session strategies
|
2020-10-28 08:27:25 +00:00
|
|
|
for sessionName, strategies := range trader.exchangeStrategies {
|
|
|
|
var session = trader.environment.sessions[sessionName]
|
2020-10-27 00:17:42 +00:00
|
|
|
|
2020-10-27 01:58:21 +00:00
|
|
|
var baseOrderExecutor = &ExchangeOrderExecutor{
|
2020-10-30 21:21:17 +00:00
|
|
|
// copy the environment notification system so that we can route
|
|
|
|
Notifiability: trader.environment.Notifiability,
|
2020-11-17 00:19:22 +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-11-12 06:59:47 +00:00
|
|
|
if err := injectField(rs, "Graceful", &trader.Graceful, true); err != nil {
|
2020-11-12 06:50:08 +00:00
|
|
|
log.WithError(err).Errorf("strategy Graceful injection failed")
|
|
|
|
}
|
|
|
|
|
2020-11-09 08:47:29 +00:00
|
|
|
if err := injectField(rs, "Logger", &trader.logger, false); err != nil {
|
|
|
|
log.WithError(err).Errorf("strategy Logger injection failed")
|
|
|
|
}
|
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
if err := injectField(rs, "Notifiability", &trader.environment.Notifiability, false); err != nil {
|
2020-10-28 08:27:25 +00:00
|
|
|
log.WithError(err).Errorf("strategy Notifiability injection failed")
|
2020-10-27 12:13:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 23:49:06 +00:00
|
|
|
if err := injectField(rs, "OrderExecutor", orderExecutor, false); err != nil {
|
2020-10-28 08:27:25 +00:00
|
|
|
log.WithError(err).Errorf("strategy OrderExecutor injection failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if symbol, ok := isSymbolBasedStrategy(rs); ok {
|
2020-10-28 23:49:06 +00:00
|
|
|
log.Infof("found symbol based strategy from %s", rs.Type())
|
2020-10-28 08:27:25 +00:00
|
|
|
if hasField(rs, "Market") {
|
|
|
|
if market, ok := session.Market(symbol); ok {
|
|
|
|
// let's make the market object passed by pointer
|
2020-10-28 23:49:06 +00:00
|
|
|
if err := injectField(rs, "Market", &market, false); err != nil {
|
|
|
|
log.WithError(err).Errorf("strategy %T Market injection failed", strategy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StandardIndicatorSet
|
|
|
|
if hasField(rs, "StandardIndicatorSet") {
|
|
|
|
if indicatorSet, ok := session.StandardIndicatorSet(symbol); ok {
|
|
|
|
if err := injectField(rs, "StandardIndicatorSet", indicatorSet, true); err != nil {
|
|
|
|
log.WithError(err).Errorf("strategy %T StandardIndicatorSet injection failed", strategy)
|
2020-10-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasField(rs, "MarketDataStore") {
|
|
|
|
if store, ok := session.MarketDataStore(symbol); ok {
|
2020-10-28 23:49:06 +00:00
|
|
|
if err := injectField(rs, "MarketDataStore", store, true); err != nil {
|
|
|
|
log.WithError(err).Errorf("strategy %T MarketDataStore injection failed", strategy)
|
2020-10-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 12:13:10 +00:00
|
|
|
}
|
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{
|
2020-10-30 21:21:17 +00:00
|
|
|
Notifiability: trader.environment.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-11-17 00:19:22 +00:00
|
|
|
rs := reflect.ValueOf(strategy)
|
|
|
|
if rs.Elem().Kind() == reflect.Struct {
|
|
|
|
// get the struct element
|
|
|
|
rs = rs.Elem()
|
2020-09-08 06:56:08 +00:00
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
if err := injectField(rs, "Graceful", &trader.Graceful, true); err != nil {
|
|
|
|
log.WithError(err).Errorf("strategy Graceful injection failed")
|
|
|
|
}
|
2020-09-08 06:56:08 +00:00
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
if err := injectField(rs, "Logger", &trader.logger, false); err != nil {
|
|
|
|
log.WithError(err).Errorf("strategy Logger injection failed")
|
|
|
|
}
|
2020-09-08 06:56:08 +00:00
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
if err := injectField(rs, "Notifiability", &trader.environment.Notifiability, false); err != nil {
|
|
|
|
log.WithError(err).Errorf("strategy Notifiability injection failed")
|
2020-09-08 06:56:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:05:20 +00:00
|
|
|
}
|
2020-07-13 05:25:48 +00:00
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
if err := strategy.Run(ctx, router, trader.environment.sessions); err != nil {
|
|
|
|
return err
|
2020-07-13 05:25:48 +00:00
|
|
|
}
|
2020-11-17 00:19:22 +00:00
|
|
|
}
|
2020-07-13 05:25:48 +00:00
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
return trader.environment.Connect(ctx)
|
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 {
|
2020-10-30 21:21:17 +00:00
|
|
|
return NewPnLReporter(&trader.environment.Notifiability)
|
2020-10-22 07:57:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
type OrderExecutor interface {
|
2020-10-31 11:54:05 +00:00
|
|
|
SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error)
|
2020-10-14 02:06:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
type OrderExecutionRouter interface {
|
2020-11-17 00:19:22 +00:00
|
|
|
// SubmitOrderTo submit order to a specific exchange Session
|
2020-10-31 11:54:05 +00:00
|
|
|
SubmitOrdersTo(ctx context.Context, session string, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error)
|
2020-10-14 02:06:15 +00:00
|
|
|
}
|