From 884a9730f1fce4e99f5c37ded32f8262aca85faf Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 16 Jun 2020 14:22:02 +0800 Subject: [PATCH] refactor kline attachment --- bbgo/kline.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/bbgo/kline.go b/bbgo/kline.go index bad97f8c0..f682ced08 100644 --- a/bbgo/kline.go +++ b/bbgo/kline.go @@ -2,7 +2,9 @@ package bbgo import ( "fmt" + "github.com/slack-go/slack" "math" + "strconv" ) type KLineEvent struct { @@ -124,6 +126,49 @@ func (k KLine) String() string { return fmt.Sprintf("%s %s Open: % 14s Close: % 14s High: % 14s Low: % 14s Volume: % 15s Change: % 11f Max Change: % 11f", k.Symbol, k.Interval, k.Open, k.Close, k.High, k.Low, k.Volume, k.GetChange(), k.GetMaxChange()) } + +func (k KLine) SlackAttachment() slack.Attachment { + return slack.Attachment{ + Text: "KLine", + Fields: []slack.AttachmentField{ + { + Title: "Open", + Value: k.Open, + Short: true, + }, + { + Title: "Close", + Value: k.Close, + Short: true, + }, + { + Title: "High", + Value: k.High, + Short: true, + }, + { + Title: "Low", + Value: k.Low, + Short: true, + }, + { + Title: "Change", + Value: formatVolume(k.GetChange()), + Short: true, + }, + { + Title: "Max Change", + Value: formatVolume(k.GetMaxChange()), + Short: true, + }, + }, + Footer: "", + FooterIcon: "", + } +} + + + type KLineWindow []KLine func (k KLineWindow) Len() int { @@ -245,3 +290,9 @@ func (k *KLineWindow) Truncate(size int) { } *k = (*k)[end-5 : end] } + + +func formatVolume(val float64) string { + return strconv.FormatFloat(val, 'f', 6, 64) +} +