bbgo_origin/bbgo/trader.go

51 lines
1.1 KiB
Go
Raw Normal View History

2020-07-10 13:34:39 +00:00
package bbgo
import (
"context"
slack2 "github.com/c9s/bbgo/pkg/slack"
2020-07-12 11:45:03 +00:00
log "github.com/sirupsen/logrus"
2020-07-10 13:34:39 +00:00
"github.com/slack-go/slack"
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
)
type Trader struct {
Notifier *slack2.SlackNotifier
2020-07-10 13:34:39 +00:00
// Context is trading Context
Context *TradingContext
2020-07-11 05:02:53 +00:00
Exchange *binance.Exchange
2020-07-10 13:34:39 +00:00
Slack *slack.Client
TradingChannel string
ErrorChannel string
InfoChannel string
}
func (t *Trader) Infof(format string, args ...interface{}) {
2020-07-12 17:01:33 +00:00
t.Notifier.Infof(format, args...)
2020-07-10 13:34:39 +00:00
}
2020-07-12 11:44:05 +00:00
func (t *Trader) ReportTrade(trade *types.Trade) {
2020-07-12 17:01:33 +00:00
t.Notifier.ReportTrade(trade)
2020-07-10 13:34:39 +00:00
}
func (t *Trader) ReportPnL() {
2020-07-12 14:52:37 +00:00
report := t.Context.ProfitAndLossCalculator.Calculate()
2020-07-11 03:23:48 +00:00
report.Print()
2020-07-12 17:01:33 +00:00
t.Notifier.ReportPnL(report)
2020-07-10 13:34:39 +00:00
}
2020-07-12 11:44:05 +00:00
func (t *Trader) SubmitOrder(ctx context.Context, order *types.Order) {
2020-07-10 13:34:39 +00:00
t.Infof(":memo: Submitting %s order on side %s with volume: %s", order.Type, order.Side, order.VolumeStr, order.SlackAttachment())
err := t.Exchange.SubmitOrder(ctx, order)
if err != nil {
2020-07-13 04:30:35 +00:00
log.WithError(err).Errorf("order create error: side %s volume: %s", order.Side, order.VolumeStr)
2020-07-10 13:34:39 +00:00
return
}
}