2020-07-10 13:34:39 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-07 06:20:03 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-07-13 05:25:48 +00:00
|
|
|
"time"
|
2020-07-13 04:57:18 +00:00
|
|
|
|
2020-09-08 06:56:08 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2020-09-07 06:20:03 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-07-13 04:57:18 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-05 06:25:58 +00:00
|
|
|
"github.com/c9s/bbgo/accounting"
|
|
|
|
"github.com/c9s/bbgo/bbgo/config"
|
|
|
|
"github.com/c9s/bbgo/exchange/binance"
|
|
|
|
"github.com/c9s/bbgo/service"
|
|
|
|
"github.com/c9s/bbgo/types"
|
2020-07-10 13:34:39 +00:00
|
|
|
)
|
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
// MarketStrategy represents the single Exchange strategy
|
|
|
|
type MarketStrategy interface {
|
|
|
|
OnLoad(tradingContext *Context, trader types.Trader) error
|
2020-10-03 12:09:22 +00:00
|
|
|
OnNewStream(stream types.Stream) error
|
2020-07-14 06:54:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
type ExchangeSession struct {
|
|
|
|
Name string
|
|
|
|
|
|
|
|
Account *Account
|
|
|
|
|
2020-10-03 12:09:22 +00:00
|
|
|
Stream types.Stream
|
2020-09-28 07:01:10 +00:00
|
|
|
|
|
|
|
Subscriptions []types.Subscription
|
|
|
|
|
|
|
|
Exchange *binance.Exchange
|
|
|
|
|
|
|
|
Strategies []MarketStrategy
|
|
|
|
|
|
|
|
loadedSymbols map[string]struct{}
|
|
|
|
|
|
|
|
Markets map[string]types.Market
|
|
|
|
|
|
|
|
Trades map[string][]types.Trade
|
|
|
|
}
|
|
|
|
|
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.Symbols(symbol)
|
|
|
|
|
|
|
|
session.Subscriptions = append(session.Subscriptions, types.Subscription{
|
|
|
|
Channel: channel,
|
|
|
|
Symbol: symbol,
|
|
|
|
Options: options,
|
|
|
|
})
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *ExchangeSession) Symbols(symbols ...string) *ExchangeSession {
|
2020-10-03 11:42:17 +00:00
|
|
|
if session.loadedSymbols == nil {
|
|
|
|
session.loadedSymbols = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.Markets == nil {
|
|
|
|
session.Markets = make(map[string]types.Market)
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
for _, symbol := range symbols {
|
|
|
|
session.loadedSymbols[symbol] = struct{}{}
|
|
|
|
|
|
|
|
if market, ok := types.FindMarket(symbol); ok {
|
|
|
|
session.Markets[symbol] = market
|
|
|
|
} else {
|
|
|
|
log.Panicf("market of symbol %s not found", symbol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *ExchangeSession) AddStrategy(strategy MarketStrategy) *ExchangeSession {
|
|
|
|
session.Strategies = append(session.Strategies, strategy)
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2020-07-10 13:34:39 +00:00
|
|
|
type Trader struct {
|
2020-09-07 06:20:03 +00:00
|
|
|
Symbol string
|
|
|
|
TradeService *service.TradeService
|
|
|
|
TradeSync *service.TradeSync
|
|
|
|
|
2020-07-10 13:34:39 +00:00
|
|
|
// Context is trading Context
|
2020-09-07 06:20:03 +00:00
|
|
|
Context *Context
|
2020-07-10 13:34:39 +00:00
|
|
|
|
2020-07-11 05:02:53 +00:00
|
|
|
Exchange *binance.Exchange
|
2020-07-13 05:25:48 +00:00
|
|
|
|
|
|
|
reportTimer *time.Timer
|
2020-07-16 07:36:02 +00:00
|
|
|
|
2020-09-19 01:05:06 +00:00
|
|
|
ProfitAndLossCalculator *accounting.ProfitAndLossCalculator
|
2020-08-11 01:28:13 +00:00
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
Account *Account
|
2020-09-19 02:59:43 +00:00
|
|
|
|
2020-09-19 03:09:20 +00:00
|
|
|
Notifiers []Notifier
|
2020-09-28 07:01:10 +00:00
|
|
|
|
|
|
|
ExchangeSessions map[string]*ExchangeSession
|
2020-07-13 05:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
func New(db *sqlx.DB, exchange *binance.Exchange, symbol string) *Trader {
|
2020-09-07 06:20:03 +00:00
|
|
|
tradeService := &service.TradeService{DB: db}
|
|
|
|
return &Trader{
|
|
|
|
Symbol: symbol,
|
2020-09-07 06:33:58 +00:00
|
|
|
Exchange: exchange,
|
2020-09-07 06:20:03 +00:00
|
|
|
TradeService: tradeService,
|
2020-09-07 06:33:58 +00:00
|
|
|
TradeSync: &service.TradeSync{
|
2020-09-28 07:01:10 +00:00
|
|
|
Service: tradeService,
|
2020-09-07 06:33:58 +00:00
|
|
|
},
|
2020-09-07 06:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 03:09:20 +00:00
|
|
|
func (trader *Trader) AddNotifier(notifier Notifier) {
|
|
|
|
trader.Notifiers = append(trader.Notifiers, notifier)
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
func (trader *Trader) AddExchange(name string, exchange *binance.Exchange) (session *ExchangeSession) {
|
|
|
|
session = &ExchangeSession{
|
|
|
|
Name: name,
|
|
|
|
Exchange: exchange,
|
|
|
|
}
|
2020-09-19 02:59:43 +00:00
|
|
|
|
2020-10-03 11:42:17 +00:00
|
|
|
if trader.ExchangeSessions == nil {
|
|
|
|
trader.ExchangeSessions = make(map[string]*ExchangeSession)
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
trader.ExchangeSessions[name] = session
|
|
|
|
return session
|
2020-09-19 03:09:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
func (trader *Trader) Connect(ctx context.Context) (err error) {
|
|
|
|
log.Info("syncing trades from exchange...")
|
|
|
|
startTime := time.Now().AddDate(0, 0, -7) // sync from 7 days ago
|
2020-09-19 03:25:48 +00:00
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
for _, session := range trader.ExchangeSessions {
|
2020-09-19 03:25:48 +00:00
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
for symbol := range session.loadedSymbols {
|
|
|
|
if err := trader.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 = trader.TradeService.QueryForTradingFeeCurrency(symbol, tradingFeeCurrency)
|
|
|
|
} else {
|
|
|
|
trades, err = trader.TradeService.Query(symbol)
|
|
|
|
}
|
2020-09-19 03:25:48 +00:00
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("symbol %s: %d trades loaded", symbol, len(trades))
|
2020-10-03 11:42:17 +00:00
|
|
|
if session.Trades == nil {
|
|
|
|
session.Trades = make(map[string][]types.Trade)
|
|
|
|
}
|
2020-09-28 07:01:10 +00:00
|
|
|
session.Trades[symbol] = trades
|
|
|
|
|
2020-10-10 04:24:50 +00:00
|
|
|
stockManager := &StockDistribution{
|
2020-09-28 07:01:10 +00:00
|
|
|
Symbol: symbol,
|
|
|
|
TradingFeeCurrency: tradingFeeCurrency,
|
|
|
|
}
|
|
|
|
|
|
|
|
checkpoints, err := stockManager.AddTrades(trades)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("symbol %s: found stock checkpoints: %+v", symbol, checkpoints)
|
|
|
|
}
|
|
|
|
|
|
|
|
session.Account, err = LoadAccount(ctx, session.Exchange)
|
2020-09-19 02:59:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:09:22 +00:00
|
|
|
session.Stream = session.Exchange.NewStream()
|
2020-09-28 07:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-19 02:59:43 +00:00
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
if err := session.Stream.Connect(ctx); err != nil {
|
2020-09-19 02:59:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-09-07 06:20:03 +00:00
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trader *Trader) Initialize(ctx context.Context, startTime time.Time) error {
|
2020-09-28 07:01:10 +00:00
|
|
|
// query all trades from database so that we can get the correct pnl
|
2020-09-07 06:20:03 +00:00
|
|
|
var err error
|
|
|
|
var trades []types.Trade
|
2020-09-19 02:59:43 +00:00
|
|
|
tradingFeeCurrency := trader.Exchange.PlatformFeeCurrency()
|
2020-09-07 06:20:03 +00:00
|
|
|
if strings.HasPrefix(trader.Symbol, tradingFeeCurrency) {
|
|
|
|
trades, err = trader.TradeService.QueryForTradingFeeCurrency(trader.Symbol, tradingFeeCurrency)
|
|
|
|
} else {
|
|
|
|
trades, err = trader.TradeService.Query(trader.Symbol)
|
|
|
|
}
|
2020-07-13 05:25:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-09-07 06:20:03 +00:00
|
|
|
return err
|
2020-07-13 05:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
log.Infof("%d trades loaded", len(trades))
|
|
|
|
|
2020-10-10 04:24:50 +00:00
|
|
|
stockManager := &StockDistribution{
|
2020-09-07 06:20:03 +00:00
|
|
|
Symbol: trader.Symbol,
|
|
|
|
TradingFeeCurrency: tradingFeeCurrency,
|
|
|
|
}
|
|
|
|
|
|
|
|
checkpoints, err := stockManager.AddTrades(trades)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("found checkpoints: %+v", checkpoints)
|
|
|
|
|
|
|
|
market, ok := types.FindMarket(trader.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%s market not found", trader.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPrice, err := trader.Exchange.QueryAveragePrice(ctx, trader.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
trader.Context = &Context{
|
|
|
|
CurrentPrice: currentPrice,
|
|
|
|
Symbol: trader.Symbol,
|
|
|
|
Market: market,
|
|
|
|
StockManager: stockManager,
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if len(checkpoints) > 0 {
|
|
|
|
// get the last checkpoint
|
|
|
|
idx := checkpoints[len(checkpoints)-1]
|
|
|
|
if idx < len(trades)-1 {
|
|
|
|
trades = trades[idx:]
|
|
|
|
firstTrade := trades[0]
|
|
|
|
pnlStartTime = firstTrade.Time
|
|
|
|
notifier.Notify("%s Found the latest trade checkpoint %s", firstTrade.Symbol, firstTrade.Time, firstTrade)
|
|
|
|
}
|
2020-07-13 16:38:52 +00:00
|
|
|
}
|
2020-09-07 06:20:03 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-19 01:05:06 +00:00
|
|
|
trader.ProfitAndLossCalculator = &accounting.ProfitAndLossCalculator{
|
2020-09-07 06:20:03 +00:00
|
|
|
TradingFeeCurrency: tradingFeeCurrency,
|
|
|
|
Symbol: trader.Symbol,
|
|
|
|
StartTime: startTime,
|
|
|
|
CurrentPrice: currentPrice,
|
|
|
|
Trades: trades,
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := LoadAccount(ctx, trader.Exchange)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-07-13 16:38:52 +00:00
|
|
|
}
|
2020-07-13 16:20:15 +00:00
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
trader.Account = account
|
|
|
|
trader.Context.Balances = account.Balances
|
|
|
|
account.Print()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
func (trader *Trader) RunStrategyWithHotReload(ctx context.Context, strategy MarketStrategy, 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-09-28 07:01:10 +00:00
|
|
|
func (trader *Trader) RunStrategy(ctx context.Context, strategy MarketStrategy) (chan struct{}, error) {
|
|
|
|
if err := strategy.OnLoad(trader.Context, trader); err != nil {
|
2020-07-13 16:20:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:09:22 +00:00
|
|
|
stream := trader.Exchange.NewStream()
|
2020-07-13 05:25:48 +00:00
|
|
|
|
2020-09-05 08:22:46 +00:00
|
|
|
// bind kline store to the stream
|
2020-09-08 06:56:08 +00:00
|
|
|
klineStore := NewMarketDataStore()
|
2020-09-19 02:59:43 +00:00
|
|
|
klineStore.BindPrivateStream(stream)
|
2020-09-05 08:22:46 +00:00
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
trader.Account.BindPrivateStream(stream)
|
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
if err := strategy.OnNewStream(stream); err != nil {
|
2020-07-13 16:20:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07 06:20:03 +00:00
|
|
|
if trade.Symbol != trader.Symbol {
|
2020-07-13 05:25:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-05 08:22:46 +00:00
|
|
|
if err := trader.TradeService.Insert(*trade); err != nil {
|
2020-08-11 01:28:13 +00:00
|
|
|
log.WithError(err).Error("trade insert error")
|
|
|
|
}
|
|
|
|
|
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-09-16 04:28:15 +00:00
|
|
|
stream.OnKLineClosed(func(kline types.KLine) {
|
|
|
|
trader.ProfitAndLossCalculator.SetCurrentPrice(kline.Close)
|
|
|
|
trader.Context.SetCurrentPrice(kline.Close)
|
2020-07-13 16:20:15 +00:00
|
|
|
})
|
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
if err := stream.Connect(ctx); err != nil {
|
2020-07-13 05:31:40 +00:00
|
|
|
return nil, err
|
2020-07-13 05:25:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 05:31:40 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
|
2020-07-13 05:25:48 +00:00
|
|
|
go func() {
|
2020-07-13 05:31:40 +00:00
|
|
|
defer close(done)
|
2020-07-13 05:25:48 +00:00
|
|
|
defer stream.Close()
|
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
select {
|
2020-07-13 05:25:48 +00:00
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2020-07-13 05:25:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-07-13 05:31:40 +00:00
|
|
|
return done, nil
|
2020-07-10 13:34:39 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trader *Trader) NotifyPnL(report *accounting.ProfitAndLossReport) {
|
|
|
|
for _, n := range trader.Notifiers {
|
|
|
|
n.NotifyPnL(report)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trader *Trader) NotifyTrade(trade *types.Trade) {
|
|
|
|
for _, n := range trader.Notifiers {
|
|
|
|
n.NotifyTrade(trade)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trader *Trader) Notify(msg string, args ...interface{}) {
|
|
|
|
for _, n := range trader.Notifiers {
|
|
|
|
n.Notify(msg, args...)
|
|
|
|
}
|
2020-07-10 13:34:39 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 05:11:34 +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,
|
|
|
|
Exchange: trader.Exchange,
|
|
|
|
Trader: trader,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|