bbgo_origin/bbgo/trader.go

185 lines
4.7 KiB
Go
Raw Normal View History

2020-07-10 13:34:39 +00:00
package bbgo
import (
"context"
"fmt"
2020-07-12 11:44:05 +00:00
"strconv"
"time"
2020-07-10 13:34:39 +00:00
"github.com/leekchan/accounting"
"github.com/sirupsen/logrus"
"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"
"github.com/c9s/bbgo/pkg/slack/slackstyle"
"github.com/c9s/bbgo/pkg/util"
2020-07-10 13:34:39 +00:00
)
var USD = accounting.Accounting{Symbol: "$ ", Precision: 2}
var BTC = accounting.Accounting{Symbol: "BTC ", Precision: 8}
type Trader struct {
// 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{}) {
var slackAttachments []slack.Attachment = nil
var slackArgsStartIdx = -1
for idx, arg := range args {
switch a := arg.(type) {
// concrete type assert first
case slack.Attachment:
if slackArgsStartIdx == -1 {
slackArgsStartIdx = idx
}
slackAttachments = append(slackAttachments, a)
case slackstyle.SlackAttachmentCreator:
if slackArgsStartIdx == -1 {
slackArgsStartIdx = idx
}
slackAttachments = append(slackAttachments, a.SlackAttachment())
}
}
var nonSlackArgs = []interface{}{}
if slackArgsStartIdx > 0 {
nonSlackArgs = args[:slackArgsStartIdx]
}
logrus.Infof(format, nonSlackArgs...)
_, _, err := t.Slack.PostMessageContext(context.Background(), t.InfoChannel,
slack.MsgOptionText(fmt.Sprintf(format, nonSlackArgs...), true),
slack.MsgOptionAttachments(slackAttachments...))
if err != nil {
logrus.WithError(err).Error("Slack error:", err)
}
}
func (t *Trader) Errorf(err error, format string, args ...interface{}) {
logrus.WithError(err).Errorf(format, args...)
_, _, err2 := t.Slack.PostMessageContext(context.Background(), t.ErrorChannel,
slack.MsgOptionText("ERROR: "+err.Error()+" "+fmt.Sprintf(format, args...), true))
if err2 != nil {
logrus.WithError(err2).Error("Slack error:", err2)
}
}
2020-07-12 11:44:05 +00:00
func (t *Trader) ReportTrade(trade *types.Trade) {
2020-07-10 13:34:39 +00:00
var color = ""
if trade.IsBuyer {
color = "#228B22"
} else {
color = "#DC143C"
}
_, _, err := t.Slack.PostMessageContext(context.Background(), t.TradingChannel,
2020-07-11 08:07:09 +00:00
slack.MsgOptionText(util.Render(`:handshake: trade execution`, trade), true),
2020-07-10 13:34:39 +00:00
slack.MsgOptionAttachments(slack.Attachment{
Title: "New Trade",
Color: color,
// Pretext: "",
// Text: "",
Fields: []slack.AttachmentField{
2020-07-11 04:16:42 +00:00
{Title: "Symbol", Value: trade.Symbol, Short: true,},
2020-07-11 08:07:09 +00:00
{Title: "Side", Value: trade.Side, Short: true,},
2020-07-10 13:34:39 +00:00
{Title: "Price", Value: USD.FormatMoney(trade.Price), Short: true,},
{Title: "Volume", Value: t.Context.Market.FormatVolume(trade.Volume), Short: true,},
},
// Footer: tradingCtx.TradeStartTime.Format(time.RFC822),
// FooterIcon: "",
}))
if err != nil {
t.Errorf(err, "Slack send error")
}
}
func (t *Trader) ReportPnL() {
tradingCtx := t.Context
2020-07-11 03:23:48 +00:00
report := tradingCtx.ProfitAndLossCalculator.Calculate()
report.Print()
2020-07-10 13:34:39 +00:00
var color = ""
2020-07-11 03:23:48 +00:00
if report.Profit > 0 {
2020-07-10 13:34:39 +00:00
color = slackstyle.Green
} else {
color = slackstyle.Red
}
_, _, err := t.Slack.PostMessageContext(context.Background(), t.TradingChannel,
slack.MsgOptionText(util.Render(
2020-07-11 03:23:48 +00:00
`:heavy_dollar_sign: Here is your *{{ .symbol }}* PnL report collected since *{{ .startTime }}*`,
2020-07-10 13:34:39 +00:00
map[string]interface{}{
2020-07-11 03:23:48 +00:00
"symbol": tradingCtx.Symbol,
"startTime": report.StartTime.Format(time.RFC822),
2020-07-10 13:34:39 +00:00
}), true),
slack.MsgOptionAttachments(slack.Attachment{
Title: "Profit and Loss report",
Color: color,
// Pretext: "",
// Text: "",
Fields: []slack.AttachmentField{
{
2020-07-11 04:16:42 +00:00
Title: "Symbol",
2020-07-10 13:34:39 +00:00
Value: tradingCtx.Symbol,
Short: true,
},
{
Title: "Profit",
2020-07-11 03:23:48 +00:00
Value: USD.FormatMoney(report.Profit),
2020-07-10 13:34:39 +00:00
Short: true,
},
{
Title: "Current Price",
2020-07-11 03:23:48 +00:00
Value: USD.FormatMoney(report.CurrentPrice),
2020-07-10 13:34:39 +00:00
Short: true,
},
{
Title: "Average Bid Price",
2020-07-11 03:23:48 +00:00
Value: USD.FormatMoney(report.AverageBidPrice),
2020-07-10 13:34:39 +00:00
Short: true,
},
{
Title: "Current Stock",
2020-07-11 03:23:48 +00:00
Value: tradingCtx.Market.FormatVolume(report.Stock),
2020-07-10 13:34:39 +00:00
Short: true,
},
{
Title: "Number of Trades",
2020-07-11 03:23:48 +00:00
Value: strconv.Itoa(report.NumTrades),
2020-07-10 13:34:39 +00:00
Short: true,
},
},
2020-07-11 03:23:48 +00:00
Footer: report.StartTime.Format(time.RFC822),
2020-07-10 13:34:39 +00:00
FooterIcon: "",
}))
if err != nil {
t.Errorf(err, "Slack send error")
}
}
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 {
t.Errorf(err, "order create error: side %s volume: %s", order.Side, order.VolumeStr)
return
}
}