make trade sync optional

This commit is contained in:
c9s 2020-10-26 13:48:59 +08:00
parent 931c646fde
commit 332ca7ffe8
2 changed files with 37 additions and 18 deletions

View File

@ -113,11 +113,22 @@ func NewEnvironment(db *sqlx.DB) *Environment {
TradeSync: &service.TradeSync{
Service: tradeService,
},
// default trade scan time
tradeScanTime: time.Now().AddDate(0, 0, -7), // sync from 7 days ago
sessions: make(map[string]*ExchangeSession),
}
}
func (environ *Environment) SyncTrades(db *sqlx.DB) *Environment {
environ.TradeService = &service.TradeService{DB: db}
environ.TradeSync = &service.TradeSync{
Service: environ.TradeService,
}
return environ
}
func (environ *Environment) AddExchange(name string, exchange types.Exchange) (session *ExchangeSession) {
session = NewExchangeSession(name, exchange)
environ.sessions[name] = session
@ -160,9 +171,10 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
return nil
}
// SetTradeScanTime overrides the default trade scan time (-7 days)
func (environ *Environment) SetTradeScanTime(t time.Time) *Environment {
// SyncTradesFrom overrides the default trade scan time (-7 days)
func (environ *Environment) SyncTradesFrom(t time.Time) *Environment {
environ.tradeScanTime = t
return environ
}
@ -185,25 +197,28 @@ func (environ *Environment) Connect(ctx context.Context) error {
// trade sync and market data store depends on subscribed symbols so we have to do this here.
for symbol := range loadedSymbols {
log.Infof("syncing trades from %s for symbol %s...", session.Exchange.Name(), symbol)
if err := environ.TradeSync.Sync(ctx, session.Exchange, symbol, environ.tradeScanTime); 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 environ.TradeSync != nil {
log.Infof("syncing trades from %s for symbol %s...", session.Exchange.Name(), symbol)
if err := environ.TradeSync.Sync(ctx, session.Exchange, symbol, environ.tradeScanTime); err != nil {
return err
}
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))
}
if err != nil {
return err
}
log.Infof("symbol %s: %d trades loaded", symbol, len(trades))
session.Trades[symbol] = trades
currentPrice, err := session.Exchange.QueryAveragePrice(ctx, symbol)

View File

@ -70,11 +70,13 @@ func runConfig(ctx context.Context, config *config.Config) error {
// configure notifiers
slackToken := viper.GetString("slack-token")
if len(slackToken) > 0 {
log.Infof("found slack configured, setting up log hook...")
log.AddHook(slacklog.NewLogHook(slackToken, viper.GetString("slack-error-channel")))
}
notifierSet := &bbgo.Notifiability{}
if len(slackToken) > 0 {
log.Infof("adding slack notifier...")
var notifier = slacknotifier.New(slackToken, viper.GetString("slack-channel"))
notifierSet.AddNotifier(notifier)
}
@ -104,10 +106,12 @@ func runConfig(ctx context.Context, config *config.Config) error {
for _, report := range config.PnLReporters {
if len(report.AverageCostBySymbols) > 0 {
trader.ReportPnL(notifier).
trader.ReportPnL(notifierSet).
AverageCostBySymbols(report.AverageCostBySymbols...).
Of(report.Of...).
When(report.When...)
} else {
return errors.Errorf("unsupported PnL reporter: %+v", report)
}