2020-10-16 02:14:36 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-20 04:11:44 +00:00
|
|
|
"fmt"
|
2020-12-06 11:36:04 +00:00
|
|
|
"os"
|
2020-10-16 02:14:36 +00:00
|
|
|
"time"
|
|
|
|
|
2020-12-07 03:43:17 +00:00
|
|
|
"github.com/codingconcepts/env"
|
2020-10-16 02:14:36 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-10-17 16:06:08 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-12-29 08:00:03 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-10-16 02:14:36 +00:00
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/accounting/pnl"
|
2020-12-29 08:00:03 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
2020-10-16 02:14:36 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-10-30 21:21:17 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2020-10-16 02:14:36 +00:00
|
|
|
)
|
|
|
|
|
2020-10-20 06:21:46 +00:00
|
|
|
var LoadedExchangeStrategies = make(map[string]SingleExchangeStrategy)
|
|
|
|
var LoadedCrossExchangeStrategies = make(map[string]CrossExchangeStrategy)
|
|
|
|
|
2020-10-28 23:54:59 +00:00
|
|
|
func RegisterStrategy(key string, s interface{}) {
|
2020-12-31 09:13:55 +00:00
|
|
|
loaded := 0
|
2021-01-14 07:10:11 +00:00
|
|
|
if d, ok := s.(SingleExchangeStrategy); ok {
|
2020-10-28 23:54:59 +00:00
|
|
|
LoadedExchangeStrategies[key] = d
|
2020-12-31 09:13:55 +00:00
|
|
|
loaded++
|
|
|
|
}
|
2020-10-28 23:54:59 +00:00
|
|
|
|
2021-01-14 07:10:11 +00:00
|
|
|
if d, ok := s.(CrossExchangeStrategy); ok {
|
2020-10-28 23:54:59 +00:00
|
|
|
LoadedCrossExchangeStrategies[key] = d
|
2020-12-31 09:13:55 +00:00
|
|
|
loaded++
|
|
|
|
}
|
2020-11-15 05:23:26 +00:00
|
|
|
|
2020-12-31 09:13:55 +00:00
|
|
|
if loaded == 0 {
|
|
|
|
panic(fmt.Errorf("%T does not implement SingleExchangeStrategy or CrossExchangeStrategy", s))
|
2020-10-28 23:54:59 +00:00
|
|
|
}
|
2020-10-20 06:21:46 +00:00
|
|
|
}
|
2020-10-20 05:52:25 +00:00
|
|
|
|
2020-11-06 18:57:50 +00:00
|
|
|
var emptyTime time.Time
|
|
|
|
|
2020-10-16 02:14:36 +00:00
|
|
|
// Environment presents the real exchange data layer
|
|
|
|
type Environment struct {
|
2020-10-30 20:36:45 +00:00
|
|
|
// Notifiability here for environment is for the streaming data notification
|
|
|
|
// note that, for back tests, we don't need notification.
|
|
|
|
Notifiability
|
|
|
|
|
2020-12-07 03:43:17 +00:00
|
|
|
PersistenceServiceFacade *PersistenceServiceFacade
|
|
|
|
|
2021-01-14 07:10:11 +00:00
|
|
|
OrderService *service.OrderService
|
2020-10-16 02:14:36 +00:00
|
|
|
TradeService *service.TradeService
|
2020-11-05 03:00:51 +00:00
|
|
|
TradeSync *service.SyncService
|
2020-10-16 02:14:36 +00:00
|
|
|
|
2020-11-06 18:57:50 +00:00
|
|
|
// startTime is the time of start point (which is used in the backtest)
|
|
|
|
startTime time.Time
|
2020-10-20 05:11:04 +00:00
|
|
|
tradeScanTime time.Time
|
|
|
|
sessions map[string]*ExchangeSession
|
2021-02-03 09:27:18 +00:00
|
|
|
|
|
|
|
MysqlURL string
|
2020-10-16 02:14:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:06:39 +00:00
|
|
|
func NewEnvironment() *Environment {
|
2020-10-16 02:14:36 +00:00
|
|
|
return &Environment{
|
2020-10-26 05:48:59 +00:00
|
|
|
// default trade scan time
|
2020-10-20 05:11:04 +00:00
|
|
|
tradeScanTime: time.Now().AddDate(0, 0, -7), // sync from 7 days ago
|
|
|
|
sessions: make(map[string]*ExchangeSession),
|
2021-02-02 18:26:41 +00:00
|
|
|
startTime: time.Now(),
|
2020-10-16 02:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:46:17 +00:00
|
|
|
func (environ *Environment) Session(name string) (*ExchangeSession, bool) {
|
|
|
|
s, ok := environ.sessions[name]
|
|
|
|
return s, ok
|
|
|
|
}
|
|
|
|
|
2020-11-07 12:34:34 +00:00
|
|
|
func (environ *Environment) Sessions() map[string]*ExchangeSession {
|
|
|
|
return environ.sessions
|
|
|
|
}
|
|
|
|
|
2021-02-02 09:26:35 +00:00
|
|
|
func (environ *Environment) ConfigureDatabase(ctx context.Context, dsn string) error {
|
|
|
|
db, err := ConnectMySQL(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-14 07:10:11 +00:00
|
|
|
|
2021-02-03 09:27:18 +00:00
|
|
|
environ.MysqlURL = dsn
|
|
|
|
|
2021-02-02 09:26:35 +00:00
|
|
|
if err := upgradeDB(ctx, "mysql", db.DB); err != nil {
|
|
|
|
return err
|
2021-01-14 07:10:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 09:26:35 +00:00
|
|
|
environ.SetDB(db)
|
2021-01-14 07:10:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-29 08:00:03 +00:00
|
|
|
func (environ *Environment) SetDB(db *sqlx.DB) *Environment {
|
2021-01-14 07:10:11 +00:00
|
|
|
environ.OrderService = &service.OrderService{DB: db}
|
2020-10-26 05:48:59 +00:00
|
|
|
environ.TradeService = &service.TradeService{DB: db}
|
2020-11-05 03:00:51 +00:00
|
|
|
environ.TradeSync = &service.SyncService{
|
|
|
|
TradeService: environ.TradeService,
|
2021-01-14 07:10:11 +00:00
|
|
|
OrderService: environ.OrderService,
|
2020-10-26 05:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return environ
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:46:17 +00:00
|
|
|
// AddExchangeSession adds the existing exchange session or pre-created exchange session
|
|
|
|
func (environ *Environment) AddExchangeSession(name string, session *ExchangeSession) *ExchangeSession {
|
2021-01-30 12:03:59 +00:00
|
|
|
// update Notifiability from the environment
|
|
|
|
session.Notifiability = environ.Notifiability
|
|
|
|
|
2020-10-16 02:14:36 +00:00
|
|
|
environ.sessions[name] = session
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:46:17 +00:00
|
|
|
// AddExchange adds the given exchange with the session name, this is the default
|
|
|
|
func (environ *Environment) AddExchange(name string, exchange types.Exchange) (session *ExchangeSession) {
|
|
|
|
session = NewExchangeSession(name, exchange)
|
|
|
|
return environ.AddExchangeSession(name, session)
|
|
|
|
}
|
|
|
|
|
2020-12-29 08:00:03 +00:00
|
|
|
func (environ *Environment) AddExchangesFromConfig(userConfig *Config) error {
|
|
|
|
if len(userConfig.Sessions) == 0 {
|
|
|
|
return environ.AddExchangesByViperKeys()
|
|
|
|
}
|
|
|
|
|
|
|
|
return environ.AddExchangesFromSessionConfig(userConfig.Sessions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (environ *Environment) AddExchangesByViperKeys() error {
|
|
|
|
for _, n := range SupportedExchanges {
|
|
|
|
if viper.IsSet(string(n) + "-api-key") {
|
|
|
|
exchange, err := cmdutil.NewExchangeWithEnvVarPrefix(n, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
environ.AddExchange(n.String(), exchange)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 09:26:35 +00:00
|
|
|
func NewExchangeSessionFromConfig(name string, sessionConfig *ExchangeSession) (*ExchangeSession, error) {
|
2021-02-02 03:44:07 +00:00
|
|
|
exchangeName, err := types.ValidExchangeName(sessionConfig.ExchangeName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var exchange types.Exchange
|
|
|
|
|
|
|
|
if sessionConfig.Key != "" && sessionConfig.Secret != "" {
|
|
|
|
exchange, err = cmdutil.NewExchangeStandard(exchangeName, sessionConfig.Key, sessionConfig.Secret)
|
|
|
|
} else {
|
|
|
|
exchange, err = cmdutil.NewExchangeWithEnvVarPrefix(exchangeName, sessionConfig.EnvVarPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure exchange
|
|
|
|
if sessionConfig.Margin {
|
|
|
|
marginExchange, ok := exchange.(types.MarginExchange)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("exchange %s does not support margin", exchangeName)
|
2020-12-29 08:00:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 03:44:07 +00:00
|
|
|
if sessionConfig.IsolatedMargin {
|
|
|
|
marginExchange.UseIsolatedMargin(sessionConfig.IsolatedMarginSymbol)
|
|
|
|
} else {
|
|
|
|
marginExchange.UseMargin()
|
2020-12-29 08:00:03 +00:00
|
|
|
}
|
2021-02-02 03:44:07 +00:00
|
|
|
}
|
2020-12-29 08:00:03 +00:00
|
|
|
|
2021-02-02 03:44:07 +00:00
|
|
|
session := NewExchangeSession(name, exchange)
|
2021-02-02 18:26:41 +00:00
|
|
|
session.ExchangeName = sessionConfig.ExchangeName
|
|
|
|
session.EnvVarPrefix = sessionConfig.EnvVarPrefix
|
2021-02-03 09:27:18 +00:00
|
|
|
session.Key = sessionConfig.Key
|
|
|
|
session.Secret = sessionConfig.Secret
|
2021-02-02 18:26:41 +00:00
|
|
|
session.PublicOnly = sessionConfig.PublicOnly
|
2021-02-02 09:26:35 +00:00
|
|
|
session.Margin = sessionConfig.Margin
|
|
|
|
session.IsolatedMargin = sessionConfig.IsolatedMargin
|
2021-02-02 03:44:07 +00:00
|
|
|
session.IsolatedMarginSymbol = sessionConfig.IsolatedMarginSymbol
|
|
|
|
return session, nil
|
|
|
|
}
|
2020-12-21 08:38:41 +00:00
|
|
|
|
2021-02-02 09:26:35 +00:00
|
|
|
func (environ *Environment) AddExchangesFromSessionConfig(sessions map[string]*ExchangeSession) error {
|
2021-02-02 03:44:07 +00:00
|
|
|
for sessionName, sessionConfig := range sessions {
|
|
|
|
session, err := NewExchangeSessionFromConfig(sessionName, sessionConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-21 08:38:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 17:46:17 +00:00
|
|
|
environ.AddExchangeSession(sessionName, session)
|
2020-12-29 08:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
// Init prepares the data that will be used by the strategies
|
2020-10-16 02:14:36 +00:00
|
|
|
func (environ *Environment) Init(ctx context.Context) (err error) {
|
2020-10-28 08:27:25 +00:00
|
|
|
for n := range environ.sessions {
|
|
|
|
var session = environ.sessions[n]
|
2021-01-20 15:08:57 +00:00
|
|
|
|
2021-01-30 02:51:01 +00:00
|
|
|
if err := session.Init(ctx, environ); err != nil {
|
2021-02-02 18:26:41 +00:00
|
|
|
// we can skip initialized sessions
|
|
|
|
if err != ErrSessionAlreadyInitialized {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 02:51:01 +00:00
|
|
|
if err := session.InitSymbols(ctx, environ); err != nil {
|
|
|
|
return err
|
2020-11-02 14:22:17 +00:00
|
|
|
}
|
2020-10-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-07 03:43:17 +00:00
|
|
|
func (environ *Environment) ConfigurePersistence(conf *PersistenceConfig) error {
|
2020-12-07 04:03:56 +00:00
|
|
|
var facade = &PersistenceServiceFacade{
|
|
|
|
Memory: NewMemoryService(),
|
|
|
|
}
|
|
|
|
|
2020-12-06 11:36:04 +00:00
|
|
|
if conf.Redis != nil {
|
2020-12-07 04:03:56 +00:00
|
|
|
if err := env.Set(conf.Redis); err != nil {
|
2020-12-07 03:43:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-06 11:36:04 +00:00
|
|
|
facade.Redis = NewRedisPersistenceService(conf.Redis)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Json != nil {
|
2020-12-07 03:43:17 +00:00
|
|
|
if _, err := os.Stat(conf.Json.Directory); os.IsNotExist(err) {
|
|
|
|
if err2 := os.MkdirAll(conf.Json.Directory, 0777); err2 != nil {
|
2020-12-06 11:36:04 +00:00
|
|
|
log.WithError(err2).Errorf("can not create directory: %s", conf.Json.Directory)
|
2020-12-07 03:43:17 +00:00
|
|
|
return err2
|
2020-12-06 11:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
facade.Json = &JsonPersistenceService{Directory: conf.Json.Directory}
|
|
|
|
}
|
2020-12-07 03:43:17 +00:00
|
|
|
|
|
|
|
environ.PersistenceServiceFacade = facade
|
|
|
|
return nil
|
2020-12-06 11:36:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
// configure notification rules
|
|
|
|
// for symbol-based routes, we should register the same symbol rules for each session.
|
|
|
|
// for session-based routes, we should set the fixed callbacks for each session
|
2020-12-07 03:43:17 +00:00
|
|
|
func (environ *Environment) ConfigureNotification(conf *NotificationConfig) error {
|
2020-10-30 21:21:17 +00:00
|
|
|
// configure routing here
|
|
|
|
if conf.SymbolChannels != nil {
|
|
|
|
environ.SymbolChannelRouter.AddRoute(conf.SymbolChannels)
|
|
|
|
}
|
|
|
|
if conf.SessionChannels != nil {
|
|
|
|
environ.SessionChannelRouter.AddRoute(conf.SessionChannels)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Routing != nil {
|
|
|
|
// configure passive object notification routing
|
|
|
|
switch conf.Routing.Trade {
|
2020-11-17 00:19:22 +00:00
|
|
|
case "$silent": // silent, do not setup notification
|
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
case "$session":
|
|
|
|
defaultTradeUpdateHandler := func(trade types.Trade) {
|
|
|
|
text := util.Render(TemplateTradeReport, trade)
|
2020-10-30 21:31:26 +00:00
|
|
|
environ.Notify(text, &trade)
|
2020-10-30 21:21:17 +00:00
|
|
|
}
|
|
|
|
for name := range environ.sessions {
|
|
|
|
session := environ.sessions[name]
|
|
|
|
|
|
|
|
// if we can route session name to channel successfully...
|
|
|
|
channel, ok := environ.SessionChannelRouter.Route(name)
|
|
|
|
if ok {
|
|
|
|
session.Stream.OnTradeUpdate(func(trade types.Trade) {
|
|
|
|
text := util.Render(TemplateTradeReport, trade)
|
2020-10-30 21:31:26 +00:00
|
|
|
environ.NotifyTo(channel, text, &trade)
|
2020-10-30 21:21:17 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
session.Stream.OnTradeUpdate(defaultTradeUpdateHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$symbol":
|
|
|
|
// configure object routes for Trade
|
|
|
|
environ.ObjectChannelRouter.Route(func(obj interface{}) (channel string, ok bool) {
|
|
|
|
trade, matched := obj.(*types.Trade)
|
|
|
|
if !matched {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
channel, ok = environ.SymbolChannelRouter.Route(trade.Symbol)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
// use same handler for each session
|
|
|
|
handler := func(trade types.Trade) {
|
|
|
|
text := util.Render(TemplateTradeReport, trade)
|
2020-10-30 21:31:26 +00:00
|
|
|
channel, ok := environ.RouteObject(&trade)
|
2020-10-30 21:21:17 +00:00
|
|
|
if ok {
|
2020-10-30 21:31:26 +00:00
|
|
|
environ.NotifyTo(channel, text, &trade)
|
2020-10-30 21:21:17 +00:00
|
|
|
} else {
|
2020-10-30 21:31:26 +00:00
|
|
|
environ.Notify(text, &trade)
|
2020-10-30 21:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, session := range environ.sessions {
|
|
|
|
session.Stream.OnTradeUpdate(handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch conf.Routing.Order {
|
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
case "$silent": // silent, do not setup notification
|
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
case "$session":
|
|
|
|
defaultOrderUpdateHandler := func(order types.Order) {
|
|
|
|
text := util.Render(TemplateOrderReport, order)
|
2020-10-30 21:31:26 +00:00
|
|
|
environ.Notify(text, &order)
|
2020-10-30 21:21:17 +00:00
|
|
|
}
|
|
|
|
for name := range environ.sessions {
|
|
|
|
session := environ.sessions[name]
|
|
|
|
|
|
|
|
// if we can route session name to channel successfully...
|
|
|
|
channel, ok := environ.SessionChannelRouter.Route(name)
|
|
|
|
if ok {
|
|
|
|
session.Stream.OnOrderUpdate(func(order types.Order) {
|
|
|
|
text := util.Render(TemplateOrderReport, order)
|
2020-10-30 21:31:26 +00:00
|
|
|
environ.NotifyTo(channel, text, &order)
|
2020-10-30 21:21:17 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
session.Stream.OnOrderUpdate(defaultOrderUpdateHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$symbol":
|
|
|
|
// add object route
|
|
|
|
environ.ObjectChannelRouter.Route(func(obj interface{}) (channel string, ok bool) {
|
|
|
|
order, matched := obj.(*types.Order)
|
|
|
|
if !matched {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
channel, ok = environ.SymbolChannelRouter.Route(order.Symbol)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
// use same handler for each session
|
|
|
|
handler := func(order types.Order) {
|
|
|
|
text := util.Render(TemplateOrderReport, order)
|
2020-10-30 21:31:26 +00:00
|
|
|
channel, ok := environ.RouteObject(&order)
|
2020-10-30 21:21:17 +00:00
|
|
|
if ok {
|
2020-10-30 21:31:26 +00:00
|
|
|
environ.NotifyTo(channel, text, &order)
|
2020-10-30 21:21:17 +00:00
|
|
|
} else {
|
2020-10-30 21:31:26 +00:00
|
|
|
environ.Notify(text, &order)
|
2020-10-30 21:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, session := range environ.sessions {
|
|
|
|
session.Stream.OnOrderUpdate(handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch conf.Routing.SubmitOrder {
|
2020-11-17 00:19:22 +00:00
|
|
|
|
|
|
|
case "$silent": // silent, do not setup notification
|
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
case "$symbol":
|
|
|
|
// add object route
|
|
|
|
environ.ObjectChannelRouter.Route(func(obj interface{}) (channel string, ok bool) {
|
|
|
|
order, matched := obj.(*types.SubmitOrder)
|
|
|
|
if !matched {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channel, ok = environ.SymbolChannelRouter.Route(order.Symbol)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// currently not used
|
|
|
|
switch conf.Routing.PnL {
|
|
|
|
case "$symbol":
|
|
|
|
environ.ObjectChannelRouter.Route(func(obj interface{}) (channel string, ok bool) {
|
|
|
|
report, matched := obj.(*pnl.AverageCostPnlReport)
|
|
|
|
if !matched {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
channel, ok = environ.SymbolChannelRouter.Route(report.Symbol)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-12-07 03:43:17 +00:00
|
|
|
return nil
|
2020-10-30 21:21:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 06:19:33 +00:00
|
|
|
func (environ *Environment) SetStartTime(t time.Time) *Environment {
|
|
|
|
environ.startTime = t
|
|
|
|
return environ
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
// SyncTradesFrom overrides the default trade scan time (-7 days)
|
|
|
|
func (environ *Environment) SyncTradesFrom(t time.Time) *Environment {
|
|
|
|
environ.tradeScanTime = t
|
|
|
|
return environ
|
|
|
|
}
|
|
|
|
|
|
|
|
func (environ *Environment) Connect(ctx context.Context) error {
|
|
|
|
for n := range environ.sessions {
|
|
|
|
// avoid using the placeholder variable for the session because we use that in the callbacks
|
|
|
|
var session = environ.sessions[n]
|
|
|
|
var logger = log.WithField("session", n)
|
|
|
|
|
2020-10-19 14:26:43 +00:00
|
|
|
if len(session.Subscriptions) == 0 {
|
2020-11-15 05:27:33 +00:00
|
|
|
logger.Warnf("exchange session %s has no subscriptions", session.Name)
|
|
|
|
} else {
|
|
|
|
// add the subscribe requests to the stream
|
|
|
|
for _, s := range session.Subscriptions {
|
|
|
|
logger.Infof("subscribing %s %s %v", s.Symbol, s.Channel, s.Options)
|
|
|
|
session.Stream.Subscribe(s.Channel, s.Symbol, s.Options)
|
|
|
|
}
|
2020-10-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof("connecting session %s...", session.Name)
|
2020-10-16 02:14:36 +00:00
|
|
|
if err := session.Stream.Connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-08 13:52:44 +00:00
|
|
|
|
|
|
|
func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (markets types.MarketMap, err error) {
|
|
|
|
err = WithCache(fmt.Sprintf("%s-markets", ex.Name()), &markets, func() (interface{}, error) {
|
|
|
|
return ex.QueryMarkets(ctx)
|
|
|
|
})
|
|
|
|
return markets, err
|
|
|
|
}
|