From 1f449eca7f15ebcf967e6f0a984883a2736e1f4a Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 15 May 2021 23:50:03 +0800 Subject: [PATCH] implement SlackAttachment interface on Position --- pkg/bbgo/position.go | 40 +++++++++++++++++++++++++++++++++++++++- pkg/types/trade.go | 8 ++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/pkg/bbgo/position.go b/pkg/bbgo/position.go index f34f70841..2bb5b258e 100644 --- a/pkg/bbgo/position.go +++ b/pkg/bbgo/position.go @@ -3,9 +3,12 @@ package bbgo import ( "fmt" "sync" + "time" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" + "github.com/slack-go/slack" ) type Position struct { @@ -20,8 +23,43 @@ type Position struct { sync.Mutex } +func (p *Position) SlackAttachment() slack.Attachment { + var posType = "" + var color = "" + + if p.Base == 0 { + color = "#cccccc" + posType = "Closed" + } else if p.Base > 0 { + posType = "Long" + color = "#228B22" + } else if p.Base < 0 { + posType = "Short" + color = "#DC143C" + } + + title := util.Render(posType+` Position {{ .Symbol }} `, p) + return slack.Attachment{ + // Pretext: "", + // Text: text, + Title: title, + Color: color, + Fields: []slack.AttachmentField{ + {Title: "Average Cost", Value: util.FormatFloat(p.AverageCost.Float64(), 2), Short: true}, + {Title: p.BaseCurrency, Value: util.FormatFloat(p.Base.Float64(), 4), Short: true}, + {Title: p.QuoteCurrency, Value: util.FormatFloat(p.Quote.Float64(), 2)}, + }, + Footer: util.Render("update time {{ . }}", time.Now().Format(time.RFC822)), + // FooterIcon: "", + } +} + +func (p *Position) PlainText() string { + return p.String() +} + func (p *Position) String() string { - return fmt.Sprintf("%s: average cost = %f, base = %f, quote = %f", + return fmt.Sprintf("POSITION %s: average cost = %f, base = %f, quote = %f", p.Symbol, p.AverageCost.Float64(), p.Base.Float64(), diff --git a/pkg/types/trade.go b/pkg/types/trade.go index ebc2ec8c7..cc33d664e 100644 --- a/pkg/types/trade.go +++ b/pkg/types/trade.go @@ -95,11 +95,11 @@ func (trade Trade) SlackAttachment() slack.Attachment { } liquidity := trade.Liquidity() - text := util.Render(slackTradeTextTemplate, trade) + pretext := util.Render(slackTradeTextTemplate, trade) return slack.Attachment{ - Text: text, - // Pretext: "", - Color: color, + // Text: text, + Pretext: pretext, + Color: color, Fields: []slack.AttachmentField{ {Title: "Exchange", Value: trade.Exchange, Short: true}, {Title: "Price", Value: util.FormatFloat(trade.Price, 2), Short: true},