2020-07-10 13:34:39 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-07 03:43:17 +00:00
|
|
|
"fmt"
|
2020-10-27 05:54:39 +00:00
|
|
|
"reflect"
|
2022-07-19 09:13:35 +00:00
|
|
|
"sync"
|
2020-07-13 04:57:18 +00:00
|
|
|
|
2021-02-16 07:58:21 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-07-13 04:57:18 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2022-01-14 18:52:46 +00:00
|
|
|
|
2022-06-29 10:49:42 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/dynamic"
|
2022-01-14 18:52:46 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/interact"
|
2020-07-10 13:34:39 +00:00
|
|
|
)
|
|
|
|
|
2022-07-19 09:13:35 +00:00
|
|
|
// Strategy method calls:
|
|
|
|
// -> Defaults() (optional method)
|
|
|
|
// -> Initialize() (optional method)
|
|
|
|
// -> Validate() (optional method)
|
|
|
|
// -> Run() (optional method)
|
|
|
|
// -> Shutdown(shutdownCtx context.Context, wg *sync.WaitGroup)
|
2022-05-10 05:25:03 +00:00
|
|
|
type StrategyID interface {
|
|
|
|
ID() string
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
// SingleExchangeStrategy represents the single Exchange strategy
|
|
|
|
type SingleExchangeStrategy interface {
|
2022-05-10 05:25:03 +00:00
|
|
|
StrategyID
|
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
|
|
|
}
|
|
|
|
|
2022-07-19 08:59:56 +00:00
|
|
|
// StrategyInitializer's Initialize method is called before the Subscribe method call.
|
2022-01-28 18:22:20 +00:00
|
|
|
type StrategyInitializer interface {
|
|
|
|
Initialize() error
|
|
|
|
}
|
|
|
|
|
2022-07-19 08:59:56 +00:00
|
|
|
type StrategyDefaulter interface {
|
|
|
|
Defaults() error
|
|
|
|
}
|
|
|
|
|
2022-07-19 09:13:35 +00:00
|
|
|
type StrategyValidator interface {
|
|
|
|
Validate() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type StrategyShutdown interface {
|
|
|
|
Shutdown(ctx context.Context, wg *sync.WaitGroup)
|
|
|
|
}
|
|
|
|
|
2021-10-17 14:23:21 +00:00
|
|
|
// ExchangeSessionSubscriber provides an interface for collecting subscriptions from different strategies
|
|
|
|
// Subscribe method will be called before the user data stream connection is created.
|
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 {
|
2020-12-03 01:31:40 +00:00
|
|
|
CrossSubscribe(sessions map[string]*ExchangeSession)
|
2020-11-15 05:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
type CrossExchangeStrategy interface {
|
2022-05-10 05:25:03 +00:00
|
|
|
StrategyID
|
2020-12-03 01:31:40 +00:00
|
|
|
CrossRun(ctx context.Context, orderExecutionRouter OrderExecutionRouter, sessions map[string]*ExchangeSession) error
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
|
2021-05-02 15:47:57 +00:00
|
|
|
func (logger *SilentLogger) Infof(string, ...interface{}) {}
|
|
|
|
func (logger *SilentLogger) Warnf(string, ...interface{}) {}
|
|
|
|
func (logger *SilentLogger) Errorf(string, ...interface{}) {}
|
2020-11-09 08:34:35 +00:00
|
|
|
|
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
|
|
|
|
2023-03-15 14:47:40 +00:00
|
|
|
// gracefulShutdown is used for registering strategy's Shutdown calls
|
|
|
|
// when strategy implements Shutdown(ctx), the func ref will be stored in the callback.
|
2022-10-02 11:34:52 +00:00
|
|
|
gracefulShutdown GracefulShutdown
|
2022-09-30 07:46:26 +00:00
|
|
|
|
2020-11-09 08:34:35 +00:00
|
|
|
logger Logger
|
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{}
|
|
|
|
}
|
|
|
|
|
2021-02-20 04:23:31 +00:00
|
|
|
func (trader *Trader) Configure(userConfig *Config) error {
|
|
|
|
if userConfig.RiskControls != nil {
|
|
|
|
trader.SetRiskControls(userConfig.RiskControls)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range userConfig.ExchangeStrategies {
|
|
|
|
for _, mount := range entry.Mounts {
|
|
|
|
log.Infof("attaching strategy %T on %s...", entry.Strategy, mount)
|
2021-03-15 10:51:29 +00:00
|
|
|
if err := trader.AttachStrategyOn(mount, entry.Strategy); err != nil {
|
2021-02-20 04:23:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, strategy := range userConfig.CrossExchangeStrategies {
|
|
|
|
log.Infof("attaching cross exchange strategy %T", strategy)
|
|
|
|
trader.AttachCrossExchangeStrategy(strategy)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
2021-01-09 11:47:21 +00:00
|
|
|
func (trader *Trader) AttachStrategyOn(session string, strategies ...SingleExchangeStrategy) error {
|
|
|
|
if len(trader.environment.sessions) == 0 {
|
|
|
|
return fmt.Errorf("you don't have any session configured, please check your environment variable or config file")
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
if _, ok := trader.environment.sessions[session]; !ok {
|
2021-01-09 11:47:21 +00:00
|
|
|
var keys []string
|
|
|
|
for k := range trader.environment.sessions {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("session %s is not defined, valid sessions are: %v", session, keys)
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-09-19 03:25:48 +00:00
|
|
|
|
2022-06-17 11:19:51 +00:00
|
|
|
trader.exchangeStrategies[session] = append(
|
|
|
|
trader.exchangeStrategies[session], strategies...)
|
2020-10-16 05:52:18 +00:00
|
|
|
|
2021-01-09 11:47:21 +00:00
|
|
|
return nil
|
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
|
|
|
|
2021-05-02 15:48:53 +00:00
|
|
|
// SetRiskControls sets the risk controller
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-18 07:36:53 +00:00
|
|
|
func (trader *Trader) RunSingleExchangeStrategy(
|
|
|
|
ctx context.Context, strategy SingleExchangeStrategy, session *ExchangeSession, orderExecutor OrderExecutor,
|
|
|
|
) error {
|
2022-07-19 09:13:35 +00:00
|
|
|
if v, ok := strategy.(StrategyValidator); ok {
|
2021-04-02 02:12:55 +00:00
|
|
|
if err := v.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("failed to validate the config: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 09:13:35 +00:00
|
|
|
if shutdown, ok := strategy.(StrategyShutdown); ok {
|
2022-09-30 08:47:04 +00:00
|
|
|
trader.gracefulShutdown.OnShutdown(shutdown.Shutdown)
|
2022-07-19 09:13:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 08:12:00 +00:00
|
|
|
return strategy.Run(ctx, orderExecutor, session)
|
|
|
|
}
|
2020-10-28 08:27:25 +00:00
|
|
|
|
2021-02-16 08:12:00 +00:00
|
|
|
func (trader *Trader) getSessionOrderExecutor(sessionName string) OrderExecutor {
|
|
|
|
var session = trader.environment.sessions[sessionName]
|
2020-10-28 23:49:06 +00:00
|
|
|
|
2021-02-16 08:12:00 +00:00
|
|
|
// default to base order executor
|
2021-05-12 10:58:20 +00:00
|
|
|
var orderExecutor OrderExecutor = session.OrderExecutor
|
2020-10-28 08:27:25 +00:00
|
|
|
|
2021-02-16 08:12:00 +00:00
|
|
|
// Since the risk controls are loaded from the config file
|
|
|
|
if trader.riskControls != nil && trader.riskControls.SessionBasedRiskControl != nil {
|
2021-02-16 08:13:52 +00:00
|
|
|
if control, ok := trader.riskControls.SessionBasedRiskControl[sessionName]; ok {
|
2021-05-12 10:58:20 +00:00
|
|
|
control.SetBaseOrderExecutor(session.OrderExecutor)
|
2021-02-16 08:12:00 +00:00
|
|
|
|
|
|
|
// pick the wrapped order executor
|
|
|
|
if control.OrderExecutor != nil {
|
|
|
|
return control.OrderExecutor
|
2020-10-27 05:54:39 +00:00
|
|
|
}
|
2021-02-16 08:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return orderExecutor
|
|
|
|
}
|
2020-10-27 05:54:39 +00:00
|
|
|
|
2021-02-16 08:12:00 +00:00
|
|
|
func (trader *Trader) RunAllSingleExchangeStrategy(ctx context.Context) error {
|
|
|
|
// load and run Session strategies
|
|
|
|
for sessionName, strategies := range trader.exchangeStrategies {
|
|
|
|
var session = trader.environment.sessions[sessionName]
|
|
|
|
var orderExecutor = trader.getSessionOrderExecutor(sessionName)
|
|
|
|
for _, strategy := range strategies {
|
|
|
|
if err := trader.RunSingleExchangeStrategy(ctx, strategy, session, orderExecutor); err != nil {
|
2020-09-28 07:01:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2021-02-16 08:12:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-19 08:59:00 +00:00
|
|
|
func (trader *Trader) injectFieldsAndSubscribe(ctx context.Context) error {
|
2022-07-21 04:18:09 +00:00
|
|
|
// load and run Session strategies
|
|
|
|
for sessionName, strategies := range trader.exchangeStrategies {
|
|
|
|
var session = trader.environment.sessions[sessionName]
|
|
|
|
for _, strategy := range strategies {
|
|
|
|
rs := reflect.ValueOf(strategy)
|
|
|
|
|
|
|
|
// get the struct element
|
|
|
|
rs = rs.Elem()
|
|
|
|
|
|
|
|
if rs.Kind() != reflect.Struct {
|
|
|
|
return errors.New("strategy object is not a struct")
|
|
|
|
}
|
|
|
|
|
2023-04-25 16:37:13 +00:00
|
|
|
if err := trader.injectCommonServices(ctx, strategy); err != nil {
|
2022-07-21 04:18:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:17:44 +00:00
|
|
|
if defaulter, ok := strategy.(StrategyDefaulter); ok {
|
|
|
|
if err := defaulter.Defaults(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if initializer, ok := strategy.(StrategyInitializer); ok {
|
|
|
|
if err := initializer.Initialize(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if subscriber, ok := strategy.(ExchangeSessionSubscriber); ok {
|
|
|
|
subscriber.Subscribe(session)
|
|
|
|
} else {
|
|
|
|
log.Errorf("strategy %s does not implement ExchangeSessionSubscriber", strategy.ID())
|
|
|
|
}
|
|
|
|
|
2023-10-18 07:36:53 +00:00
|
|
|
if symbol, ok := dynamic.LookupSymbolField(rs); ok && symbol != "" {
|
2022-10-19 04:17:44 +00:00
|
|
|
log.Infof("found symbol %s based strategy from %s", symbol, rs.Type())
|
|
|
|
|
|
|
|
if err := session.initSymbol(ctx, trader.environment, symbol); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to inject object into %T when initSymbol", strategy)
|
|
|
|
}
|
2022-07-21 04:18:09 +00:00
|
|
|
|
|
|
|
market, ok := session.Market(symbol)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("market of symbol %s not found", symbol)
|
|
|
|
}
|
|
|
|
|
2022-07-26 10:35:50 +00:00
|
|
|
indicatorSet := session.StandardIndicatorSet(symbol)
|
2022-07-21 04:33:29 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("standardIndicatorSet of symbol %s not found", symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
store, ok := session.MarketDataStore(symbol)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("marketDataStore of symbol %s not found", symbol)
|
|
|
|
}
|
|
|
|
|
2022-07-21 04:18:09 +00:00
|
|
|
if err := dynamic.ParseStructAndInject(strategy,
|
|
|
|
market,
|
|
|
|
session,
|
|
|
|
session.OrderExecutor,
|
2022-07-21 04:33:29 +00:00
|
|
|
indicatorSet,
|
|
|
|
store,
|
2022-07-21 04:18:09 +00:00
|
|
|
); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to inject object into %T", strategy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, strategy := range trader.crossExchangeStrategies {
|
|
|
|
rs := reflect.ValueOf(strategy)
|
|
|
|
|
|
|
|
// get the struct element from the struct pointer
|
|
|
|
rs = rs.Elem()
|
|
|
|
if rs.Kind() != reflect.Struct {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-25 16:37:13 +00:00
|
|
|
if err := trader.injectCommonServices(ctx, strategy); err != nil {
|
2022-07-21 04:18:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-19 08:59:00 +00:00
|
|
|
|
|
|
|
if defaulter, ok := strategy.(StrategyDefaulter); ok {
|
|
|
|
if err := defaulter.Defaults(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if initializer, ok := strategy.(StrategyInitializer); ok {
|
|
|
|
if err := initializer.Initialize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if subscriber, ok := strategy.(CrossExchangeSessionSubscriber); ok {
|
|
|
|
subscriber.CrossSubscribe(trader.environment.sessions)
|
|
|
|
} else {
|
|
|
|
log.Errorf("strategy %s does not implement CrossExchangeSessionSubscriber", strategy.ID())
|
|
|
|
}
|
2022-07-21 04:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-16 08:12:00 +00:00
|
|
|
func (trader *Trader) Run(ctx context.Context) error {
|
2022-01-14 18:52:46 +00:00
|
|
|
// before we start the interaction,
|
|
|
|
// register the core interaction, because we can only get the strategies in this scope
|
|
|
|
// trader.environment.Connect will call interact.Start
|
|
|
|
interact.AddCustomInteraction(NewCoreInteraction(trader.environment, trader))
|
|
|
|
|
2022-10-19 08:59:00 +00:00
|
|
|
if err := trader.injectFieldsAndSubscribe(ctx); err != nil {
|
2022-07-21 04:18:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:45:24 +00:00
|
|
|
if err := trader.environment.Start(ctx); err != nil {
|
2021-02-16 08:12:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := trader.RunAllSingleExchangeStrategy(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:06:15 +00:00
|
|
|
router := &ExchangeOrderExecutionRouter{
|
2022-06-19 04:29:36 +00:00
|
|
|
sessions: trader.environment.sessions,
|
|
|
|
executors: make(map[string]OrderExecutor),
|
2021-05-12 10:58:20 +00:00
|
|
|
}
|
|
|
|
for sessionID := range trader.environment.sessions {
|
|
|
|
var orderExecutor = trader.getSessionOrderExecutor(sessionID)
|
|
|
|
router.executors[sessionID] = orderExecutor
|
2020-10-14 02:06:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
for _, strategy := range trader.crossExchangeStrategies {
|
2022-05-05 06:28:42 +00:00
|
|
|
if err := strategy.CrossRun(ctx, router, trader.environment.sessions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return trader.environment.Connect(ctx)
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:29:07 +00:00
|
|
|
func (trader *Trader) LoadState(ctx context.Context) error {
|
2022-05-05 06:28:42 +00:00
|
|
|
if trader.environment.BacktestService != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:29:07 +00:00
|
|
|
isolation := GetIsolationFromContext(ctx)
|
2022-05-05 06:28:42 +00:00
|
|
|
|
2023-04-25 10:29:07 +00:00
|
|
|
ps := isolation.persistenceServiceFacade.Get()
|
2022-05-05 06:28:42 +00:00
|
|
|
|
|
|
|
log.Infof("loading strategies states...")
|
2022-05-10 05:25:03 +00:00
|
|
|
|
|
|
|
return trader.IterateStrategies(func(strategy StrategyID) error {
|
2022-08-22 18:12:26 +00:00
|
|
|
id := dynamic.CallID(strategy)
|
2022-06-02 19:10:50 +00:00
|
|
|
return loadPersistenceFields(strategy, id, ps)
|
2022-05-10 05:25:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trader *Trader) IterateStrategies(f func(st StrategyID) error) error {
|
2022-05-05 06:28:42 +00:00
|
|
|
for _, strategies := range trader.exchangeStrategies {
|
|
|
|
for _, strategy := range strategies {
|
2022-05-10 05:25:03 +00:00
|
|
|
if err := f(strategy); err != nil {
|
2022-05-05 06:28:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 05:25:03 +00:00
|
|
|
|
2022-05-05 06:28:42 +00:00
|
|
|
for _, strategy := range trader.crossExchangeStrategies {
|
2022-05-10 05:25:03 +00:00
|
|
|
if err := f(strategy); err != nil {
|
2022-05-05 06:04:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-05 06:28:42 +00:00
|
|
|
}
|
2022-05-05 06:04:44 +00:00
|
|
|
|
2022-05-05 06:28:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:50:50 +00:00
|
|
|
// NOTICE: the ctx here is the trading context, which could already be canceled.
|
2023-03-15 14:47:40 +00:00
|
|
|
func (trader *Trader) SaveState(ctx context.Context) error {
|
2022-05-05 06:28:42 +00:00
|
|
|
if trader.environment.BacktestService != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:47:40 +00:00
|
|
|
isolation := GetIsolationFromContext(ctx)
|
2022-05-05 06:28:42 +00:00
|
|
|
|
2023-03-15 14:47:40 +00:00
|
|
|
ps := isolation.persistenceServiceFacade.Get()
|
2022-05-05 06:28:42 +00:00
|
|
|
|
2023-03-15 14:47:40 +00:00
|
|
|
log.Debugf("saving strategy persistence states...")
|
2022-05-10 05:25:03 +00:00
|
|
|
return trader.IterateStrategies(func(strategy StrategyID) error {
|
2022-08-22 18:12:26 +00:00
|
|
|
id := dynamic.CallID(strategy)
|
2022-05-05 06:28:42 +00:00
|
|
|
if len(id) == 0 {
|
2022-05-10 05:25:03 +00:00
|
|
|
return nil
|
2020-07-13 05:25:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 05:25:03 +00:00
|
|
|
return storePersistenceFields(strategy, id, ps)
|
|
|
|
})
|
2020-07-10 13:34:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 07:46:26 +00:00
|
|
|
func (trader *Trader) Shutdown(ctx context.Context) {
|
|
|
|
trader.gracefulShutdown.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
|
2023-04-25 16:37:13 +00:00
|
|
|
func (trader *Trader) injectCommonServices(ctx context.Context, s interface{}) error {
|
|
|
|
isolation := GetIsolationFromContext(ctx)
|
|
|
|
|
|
|
|
ps := isolation.persistenceServiceFacade
|
|
|
|
|
2022-03-14 13:21:04 +00:00
|
|
|
// a special injection for persistence selector:
|
|
|
|
// if user defined the selector, the facade pointer will be nil, hence we need to update the persistence facade pointer
|
|
|
|
sv := reflect.ValueOf(s).Elem()
|
2022-06-29 10:49:42 +00:00
|
|
|
if field, ok := dynamic.HasField(sv, "Persistence"); ok {
|
2022-03-14 13:21:04 +00:00
|
|
|
// the selector is set, but we need to update the facade pointer
|
|
|
|
if !field.IsNil() {
|
|
|
|
elem := field.Elem()
|
|
|
|
if elem.Kind() != reflect.Struct {
|
|
|
|
return fmt.Errorf("field Persistence is not a struct element, %s given", field)
|
|
|
|
}
|
|
|
|
|
2023-07-09 13:23:42 +00:00
|
|
|
if err := dynamic.InjectField(elem.Interface(), "Facade", ps, true); err != nil {
|
2022-03-14 13:21:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-07-01 05:09:30 +00:00
|
|
|
if err := ParseStructAndInject(field.Interface(), persistenceFacade); err != nil {
|
2022-03-14 13:21:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 05:09:30 +00:00
|
|
|
return dynamic.ParseStructAndInject(s,
|
2022-03-05 04:39:39 +00:00
|
|
|
&trader.logger,
|
2022-06-19 04:29:36 +00:00
|
|
|
Notification,
|
2022-03-05 04:39:39 +00:00
|
|
|
trader.environment.TradeService,
|
2022-03-05 05:37:27 +00:00
|
|
|
trader.environment.OrderService,
|
|
|
|
trader.environment.DatabaseService,
|
2022-03-05 04:39:39 +00:00
|
|
|
trader.environment.AccountService,
|
2022-03-06 10:17:42 +00:00
|
|
|
trader.environment,
|
2023-04-25 16:37:13 +00:00
|
|
|
ps, // if the strategy use persistence facade separately
|
2022-03-05 04:39:39 +00:00
|
|
|
)
|
2021-02-16 07:58:21 +00:00
|
|
|
}
|