bbgo_origin/bbgo/trader.go

324 lines
7.2 KiB
Go
Raw Normal View History

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
"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"
"github.com/c9s/bbgo/pkg/bbgo/config"
2020-09-07 06:20:03 +00:00
"github.com/c9s/bbgo/pkg/bbgo/service"
2020-07-12 11:44:05 +00:00
"github.com/c9s/bbgo/pkg/bbgo/exchange/binance"
"github.com/c9s/bbgo/pkg/bbgo/types"
2020-07-10 13:34:39 +00:00
)
2020-07-14 06:54:23 +00:00
type Strategy interface {
2020-09-07 06:20:03 +00:00
Load(tradingContext *Context, trader types.Trader) error
OnNewStream(stream *types.StandardPrivateStream) error
2020-07-14 06:54:23 +00:00
}
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-13 05:25:48 +00:00
Notifier *SlackNotifier
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
ProfitAndLossCalculator *ProfitAndLossCalculator
2020-09-07 06:20:03 +00:00
Account *Account
2020-07-13 05:25:48 +00:00
}
2020-09-07 06:20:03 +00:00
func NewTrader(db *sqlx.DB, exchange *binance.Exchange, symbol string) *Trader {
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{
Service: tradeService,
Exchange: exchange,
},
2020-09-07 06:20:03 +00:00
}
}
func (trader *Trader) Initialize(ctx context.Context, startTime time.Time) error {
2020-09-07 06:33:58 +00:00
log.Info("syncing trades from exchange...")
2020-09-07 06:20:03 +00:00
if err := trader.TradeSync.Sync(ctx, trader.Symbol, startTime); err != nil {
return err
}
var err error
var trades []types.Trade
tradingFeeCurrency := trader.Exchange.TradingFeeCurrency()
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))
stockManager := &StockManager{
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
*/
trader.ProfitAndLossCalculator = &ProfitAndLossCalculator{
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
}
func (trader *Trader) RunStrategyWithHotReload(ctx context.Context, strategy Strategy, configFile string) (chan struct{}, error) {
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")
}
trader.Notifier.Notify("config reloaded, restarting trader")
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-07 06:20:03 +00:00
func (trader *Trader) RunStrategy(ctx context.Context, strategy Strategy) (chan struct{}, error) {
if err := strategy.Load(trader.Context, trader); err != nil {
2020-07-13 16:20:15 +00:00
return nil, err
}
stream, err := trader.Exchange.NewPrivateStream()
if err != nil {
2020-07-13 05:31:40 +00:00
return nil, err
2020-07-13 05:25:48 +00:00
}
2020-09-05 08:22:46 +00:00
// bind kline store to the stream
klineStore := NewMarketDataStore()
2020-09-05 08:22:46 +00:00
klineStore.BindPrivateStream(&stream.StandardPrivateStream)
2020-09-07 06:20:03 +00:00
trader.Account.BindPrivateStream(stream)
if err := strategy.OnNewStream(&stream.StandardPrivateStream); 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 {
log.WithError(err).Error("trade insert error")
}
2020-09-07 06:33:58 +00:00
trader.Notifier.ReportTrade(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
})
})
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-07-13 05:25:48 +00:00
var eventC = make(chan interface{}, 20)
if err := stream.Connect(ctx, eventC); 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()
for {
select {
case <-ctx.Done():
return
// drain the event channel
case <-eventC:
}
}
}()
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-07-13 16:20:15 +00:00
trader.Notifier.ReportPnL(report)
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) {
trader.Notifier.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 {
log.WithError(err).Errorf("order create error: side %s quantity: %s", order.Side, order.QuantityString)
2020-07-10 13:34:39 +00:00
return
}
}