2020-07-10 13:34:39 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-07 06:20:03 +00:00
|
|
|
"strings"
|
2020-07-13 05:25:48 +00:00
|
|
|
"time"
|
2020-07-13 04:57:18 +00:00
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-10-14 02:06:15 +00:00
|
|
|
"github.com/pkg/errors"
|
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/service"
|
|
|
|
"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-12 14:46:06 +00:00
|
|
|
// SingleExchangeStrategy represents the single Exchange strategy
|
|
|
|
type SingleExchangeStrategy interface {
|
2020-10-14 02:06:15 +00:00
|
|
|
Run(ctx context.Context, orderExecutor types.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-14 02:06:15 +00:00
|
|
|
Run(ctx context.Context, orderExecutionRouter types.OrderExecutionRouter, sessions map[string]*ExchangeSession) error
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExchangeSession presents the exchange connection session
|
|
|
|
// It also maintains and collects the data returned from the stream.
|
2020-09-28 07:01:10 +00:00
|
|
|
type ExchangeSession struct {
|
2020-10-12 14:46:06 +00:00
|
|
|
// Exchange session name
|
2020-09-28 07:01:10 +00:00
|
|
|
Name string
|
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
// The exchange account states
|
2020-09-28 07:01:10 +00:00
|
|
|
Account *Account
|
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
// Stream is the connection stream of the exchange
|
2020-10-03 12:09:22 +00:00
|
|
|
Stream types.Stream
|
2020-09-28 07:01:10 +00:00
|
|
|
|
|
|
|
Subscriptions []types.Subscription
|
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
Exchange types.Exchange
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
// Markets defines market configuration of a symbol
|
2020-09-28 07:01:10 +00:00
|
|
|
Markets map[string]types.Market
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
LastPrices map[string]float64
|
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
// Trades collects the executed trades from the exchange
|
|
|
|
// map: symbol -> []trade
|
2020-09-28 07:01:10 +00:00
|
|
|
Trades map[string][]types.Trade
|
2020-10-12 14:46:06 +00:00
|
|
|
|
|
|
|
MarketDataStore *MarketDataStore
|
2020-09-28 07:01:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 11:42:17 +00:00
|
|
|
func (session *ExchangeSession) Subscribe(channel types.Channel, symbol string, options types.SubscribeOptions) *ExchangeSession {
|
2020-09-28 07:01:10 +00:00
|
|
|
session.Subscriptions = append(session.Subscriptions, types.Subscription{
|
|
|
|
Channel: channel,
|
|
|
|
Symbol: symbol,
|
|
|
|
Options: options,
|
|
|
|
})
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
// Environment presents the real exchange data layer
|
|
|
|
type Environment struct {
|
|
|
|
TradeService *service.TradeService
|
|
|
|
TradeSync *service.TradeSync
|
2020-10-03 11:42:17 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
sessions map[string]*ExchangeSession
|
2020-10-12 09:33:02 +00:00
|
|
|
}
|
2020-10-03 11:42:17 +00:00
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
func NewEnvironment(db *sqlx.DB) *Environment {
|
|
|
|
tradeService := &service.TradeService{DB: db}
|
|
|
|
return &Environment{
|
|
|
|
TradeService: tradeService,
|
|
|
|
TradeSync: &service.TradeSync{
|
|
|
|
Service: tradeService,
|
|
|
|
},
|
2020-10-12 14:46:06 +00:00
|
|
|
sessions: make(map[string]*ExchangeSession),
|
2020-10-12 09:33:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
func (environ *Environment) AddExchange(name string, exchange types.Exchange) (session *ExchangeSession) {
|
|
|
|
session = &ExchangeSession{
|
2020-10-12 14:46:06 +00:00
|
|
|
Name: name,
|
|
|
|
Exchange: exchange,
|
|
|
|
Markets: make(map[string]types.Market),
|
|
|
|
Trades: make(map[string][]types.Trade),
|
|
|
|
LastPrices: make(map[string]float64),
|
2020-09-28 07:01:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
environ.sessions[name] = session
|
2020-09-28 07:01:10 +00:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
func (environ *Environment) Init(ctx context.Context) (err error) {
|
2020-10-12 09:33:02 +00:00
|
|
|
startTime := time.Now().AddDate(0, 0, -7) // sync from 7 days ago
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
for _, session := range environ.sessions {
|
2020-10-12 09:33:02 +00:00
|
|
|
loadedSymbols := make(map[string]struct{})
|
|
|
|
for _, sub := range session.Subscriptions {
|
|
|
|
loadedSymbols[sub.Symbol] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for symbol := range loadedSymbols {
|
|
|
|
if err := environ.TradeSync.Sync(ctx, session.Exchange, symbol, startTime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var trades []types.Trade
|
|
|
|
|
|
|
|
tradingFeeCurrency := session.Exchange.PlatformFeeCurrency()
|
|
|
|
if strings.HasPrefix(symbol, tradingFeeCurrency) {
|
|
|
|
trades, err = environ.TradeService.QueryForTradingFeeCurrency(symbol, tradingFeeCurrency)
|
|
|
|
} else {
|
|
|
|
trades, err = environ.TradeService.Query(symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("symbol %s: %d trades loaded", symbol, len(trades))
|
|
|
|
session.Trades[symbol] = trades
|
2020-10-12 14:46:06 +00:00
|
|
|
|
|
|
|
currentPrice, err := session.Exchange.QueryAveragePrice(ctx, symbol)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
session.LastPrices[symbol] = currentPrice
|
2020-10-12 09:33:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:49:27 +00:00
|
|
|
balances, err := session.Exchange.QueryAccountBalances(ctx)
|
2020-10-12 09:33:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-13 03:23:12 +00:00
|
|
|
session.Account = &Account{Balances: balances}
|
2020-10-12 14:49:27 +00:00
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
session.Stream = session.Exchange.NewStream()
|
2020-10-12 14:46:06 +00:00
|
|
|
|
|
|
|
session.Account.BindStream(session.Stream)
|
|
|
|
|
|
|
|
marketDataStore := NewMarketDataStore()
|
|
|
|
marketDataStore.BindStream(session.Stream)
|
|
|
|
|
|
|
|
// update last prices
|
|
|
|
session.Stream.OnKLineClosed(func(kline types.KLine) {
|
|
|
|
session.LastPrices[kline.Symbol] = kline.Close
|
|
|
|
})
|
|
|
|
|
2020-10-12 09:33:02 +00:00
|
|
|
session.Stream.OnTrade(func(trade *types.Trade) {
|
|
|
|
// append trades
|
|
|
|
session.Trades[trade.Symbol] = append(session.Trades[trade.Symbol], *trade)
|
|
|
|
|
|
|
|
if err := environ.TradeService.Insert(*trade); err != nil {
|
|
|
|
log.WithError(err).Errorf("trade insert error: %+v", *trade)
|
|
|
|
}
|
|
|
|
})
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-12 09:33:02 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
func (environ *Environment) Connect(ctx context.Context) error {
|
|
|
|
for _, session := range environ.sessions {
|
2020-10-12 09:33:02 +00:00
|
|
|
if err := session.Stream.Connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
return nil
|
2020-09-28 07:01:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 01:28:13 +00:00
|
|
|
|
2020-10-14 02:06:15 +00:00
|
|
|
type Notifiability struct {
|
2020-10-12 14:46:06 +00:00
|
|
|
notifiers []Notifier
|
2020-10-14 02:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Notifiability) AddNotifier(notifier Notifier) {
|
|
|
|
m.notifiers = append(m.notifiers, notifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Notifiability) Notify(msg string, args ...interface{}) {
|
|
|
|
for _, n := range m.notifiers {
|
|
|
|
n.Notify(msg, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Trader struct {
|
|
|
|
Notifiability
|
2020-10-12 14:46:06 +00:00
|
|
|
environment *Environment
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
crossExchangeStrategies []CrossExchangeStrategy
|
|
|
|
exchangeStrategies map[string][]SingleExchangeStrategy
|
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-12 14:46:06 +00:00
|
|
|
// AttachStrategy attaches the single exchange strategy on an exchange session.
|
|
|
|
// Single exchange strategy is the default behavior.
|
2020-10-13 06:50:59 +00:00
|
|
|
func (trader *Trader) AttachStrategy(session string, strategy SingleExchangeStrategy) {
|
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-12 14:46:06 +00:00
|
|
|
trader.exchangeStrategies[session] = append(trader.exchangeStrategies[session], strategy)
|
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
// AttachCrossExchangeStrategy attaches the cross exchange strategy
|
|
|
|
func (trader *Trader) AttachCrossExchangeStrategy(strategy CrossExchangeStrategy) error {
|
|
|
|
trader.crossExchangeStrategies = append(trader.crossExchangeStrategies, strategy)
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
|
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
|
|
|
|
for session, strategies := range trader.exchangeStrategies {
|
2020-10-14 02:06:15 +00:00
|
|
|
// we can move this to the exchange session,
|
|
|
|
// that way we can mount the notification on the exchange with DSL
|
|
|
|
orderExecutor := &ExchangeOrderExecutor{
|
|
|
|
Notifiability: trader.Notifiability,
|
|
|
|
Exchange: nil,
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
for _, strategy := range strategies {
|
2020-10-14 02:06:15 +00:00
|
|
|
err := strategy.Run(ctx, orderExecutor, trader.environment.sessions[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,
|
|
|
|
sessions: trader.environment.sessions,
|
|
|
|
}
|
|
|
|
|
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-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-09-19 03:09:20 +00:00
|
|
|
trader.Notify("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-14 02:06:15 +00:00
|
|
|
/*
|
2020-09-07 06:33:58 +00:00
|
|
|
func (trader *Trader) reportPnL() {
|
2020-07-16 07:36:02 +00:00
|
|
|
report := trader.ProfitAndLossCalculator.Calculate()
|
2020-07-11 03:23:48 +00:00
|
|
|
report.Print()
|
2020-09-19 03:09:20 +00:00
|
|
|
trader.NotifyPnL(report)
|
|
|
|
}
|
2020-10-14 02:06:15 +00:00
|
|
|
*/
|
2020-09-19 03:09:20 +00:00
|
|
|
|
2020-10-14 02:06:15 +00:00
|
|
|
/*
|
2020-09-19 03:09:20 +00:00
|
|
|
func (trader *Trader) NotifyPnL(report *accounting.ProfitAndLossReport) {
|
2020-10-12 14:46:06 +00:00
|
|
|
for _, n := range trader.notifiers {
|
2020-09-19 03:09:20 +00:00
|
|
|
n.NotifyPnL(report)
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 02:06:15 +00:00
|
|
|
*/
|
2020-09-19 03:09:20 +00:00
|
|
|
|
|
|
|
func (trader *Trader) NotifyTrade(trade *types.Trade) {
|
2020-10-12 14:46:06 +00:00
|
|
|
for _, n := range trader.notifiers {
|
2020-09-19 03:09:20 +00:00
|
|
|
n.NotifyTrade(trade)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 10:08:02 +00:00
|
|
|
func (trader *Trader) SubmitOrder(ctx context.Context, order types.SubmitOrder) {
|
2020-09-19 03:09:20 +00:00
|
|
|
trader.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order)
|
2020-07-10 13:34:39 +00:00
|
|
|
|
2020-09-18 10:15:45 +00:00
|
|
|
orderProcessor := &OrderProcessor{
|
|
|
|
MinQuoteBalance: 0,
|
|
|
|
MaxAssetBalance: 0,
|
|
|
|
MinAssetBalance: 0,
|
|
|
|
MinProfitSpread: 0,
|
|
|
|
MaxOrderAmount: 0,
|
2020-10-12 14:46:06 +00:00
|
|
|
// FIXME:
|
|
|
|
// Exchange: trader.Exchange,
|
|
|
|
Trader: trader,
|
2020-09-18 10:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := orderProcessor.Submit(ctx, order)
|
|
|
|
|
2020-07-10 13:34:39 +00:00
|
|
|
if err != nil {
|
2020-09-16 06:05:03 +00:00
|
|
|
log.WithError(err).Errorf("order create error: side %s quantity: %s", order.Side, order.QuantityString)
|
2020-07-10 13:34:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 02:06:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
type ExchangeOrderExecutionRouter struct {
|
|
|
|
Notifiability
|
|
|
|
|
|
|
|
sessions map[string]*ExchangeSession
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExchangeOrderExecutionRouter) SubmitOrderTo(ctx context.Context, session string, order types.SubmitOrder) error {
|
|
|
|
es, ok := e.sessions[session]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("exchange session %s not found", session)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Notify(":memo: Submitting order to %s %s %s %s with quantity: %s", session, order.Symbol, order.Type, order.Side, order.QuantityString, order)
|
|
|
|
|
|
|
|
order.PriceString = order.Market.FormatVolume(order.Price)
|
|
|
|
order.QuantityString = order.Market.FormatVolume(order.Quantity)
|
|
|
|
return es.Exchange.SubmitOrder(ctx, order)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ExchangeOrderExecutor is an order executor wrapper for single exchange instance.
|
|
|
|
type ExchangeOrderExecutor struct {
|
|
|
|
Notifiability
|
|
|
|
|
|
|
|
Exchange types.Exchange
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExchangeOrderExecutor) SubmitOrder(ctx context.Context, order types.SubmitOrder) error {
|
|
|
|
e.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order)
|
|
|
|
|
|
|
|
order.PriceString = order.Market.FormatVolume(order.Price)
|
|
|
|
order.QuantityString = order.Market.FormatVolume(order.Quantity)
|
|
|
|
return e.Exchange.SubmitOrder(ctx, order)
|
|
|
|
}
|