2021-10-08 06:57:44 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
2021-10-09 05:24:28 +00:00
|
|
|
"fmt"
|
2021-10-13 17:03:57 +00:00
|
|
|
"github.com/slack-go/slack"
|
2021-10-09 05:24:28 +00:00
|
|
|
"time"
|
|
|
|
|
2021-10-08 06:57:44 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
|
|
|
)
|
|
|
|
|
2021-10-08 07:09:55 +00:00
|
|
|
// Profit struct stores the PnL information
|
|
|
|
type Profit struct {
|
2021-10-09 05:24:28 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
|
2021-10-08 07:09:55 +00:00
|
|
|
// Profit is the profit of this trade made. negative profit means loss.
|
|
|
|
Profit fixedpoint.Value `json:"profit" db:"profit"`
|
|
|
|
|
|
|
|
// NetProfit is (profit - trading fee)
|
|
|
|
NetProfit fixedpoint.Value `json:"netProfit" db:"net_profit"`
|
|
|
|
AverageCost fixedpoint.Value `json:"averageCost" db:"average_ost"`
|
|
|
|
|
2021-10-13 17:03:57 +00:00
|
|
|
TradeAmount fixedpoint.Value `json:"tradeAmount" db:"trade_amount"`
|
2021-10-08 11:41:39 +00:00
|
|
|
|
2021-10-08 11:47:44 +00:00
|
|
|
// ProfitMargin is a percentage of the profit and the capital amount
|
2021-10-09 05:24:28 +00:00
|
|
|
ProfitMargin fixedpoint.Value `json:"profitMargin" db:"profit_margin"`
|
2021-10-08 11:47:44 +00:00
|
|
|
|
|
|
|
// NetProfitMargin is a percentage of the net profit and the capital amount
|
|
|
|
NetProfitMargin fixedpoint.Value `json:"netProfitMargin" db:"net_profit_margin"`
|
2021-10-08 11:41:39 +00:00
|
|
|
|
2021-10-14 02:14:11 +00:00
|
|
|
QuoteCurrency string `json:"quoteCurrency" db:"quote_currency"`
|
|
|
|
BaseCurrency string `json:"baseCurrency" db:"base_currency"`
|
2021-10-08 11:41:39 +00:00
|
|
|
|
2021-10-08 07:09:55 +00:00
|
|
|
// FeeInUSD is the summed fee of this profit,
|
|
|
|
// you will need to convert the trade fee into USD since the fee currencies can be different.
|
|
|
|
FeeInUSD fixedpoint.Value `json:"feeInUSD" db:"fee_in_usd"`
|
|
|
|
Time time.Time `json:"time" db:"time"`
|
|
|
|
Strategy string `json:"strategy" db:"strategy"`
|
|
|
|
StrategyInstanceID string `json:"strategyInstanceID" db:"strategy_instance_id"`
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:33:15 +00:00
|
|
|
func (p *Profit) SlackAttachment() slack.Attachment {
|
2021-10-14 02:14:11 +00:00
|
|
|
var color = pnlColor(p.Profit)
|
|
|
|
var title = fmt.Sprintf("%s PnL ", p.Symbol)
|
2021-10-14 02:13:21 +00:00
|
|
|
title += pnlEmojiMargin(p.Profit, p.ProfitMargin, defaultPnlLevelResolution) + " "
|
2021-10-14 02:07:27 +00:00
|
|
|
title += pnlSignString(p.Profit) + " " + p.QuoteCurrency
|
2021-10-13 17:03:57 +00:00
|
|
|
|
|
|
|
var fields []slack.AttachmentField
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !p.NetProfit.IsZero() {
|
2021-10-13 17:03:57 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Net Profit",
|
2021-10-14 02:13:21 +00:00
|
|
|
Value: pnlSignString(p.NetProfit) + " " + p.QuoteCurrency,
|
2021-10-13 17:03:57 +00:00
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !p.ProfitMargin.IsZero() {
|
2021-10-13 17:03:57 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Profit Margin",
|
|
|
|
Value: p.ProfitMargin.Percentage(),
|
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !p.NetProfitMargin.IsZero() {
|
2021-10-13 17:03:57 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Net Profit Margin",
|
|
|
|
Value: p.NetProfitMargin.Percentage(),
|
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !p.TradeAmount.IsZero() {
|
2021-10-13 17:03:57 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Trade Amount",
|
|
|
|
Value: p.TradeAmount.String() + " " + p.QuoteCurrency,
|
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !p.FeeInUSD.IsZero() {
|
2021-10-13 17:03:57 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Fee In USD",
|
|
|
|
Value: p.FeeInUSD.String() + " USD",
|
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:33:15 +00:00
|
|
|
if len(p.Strategy) != 0 {
|
2021-10-13 17:03:57 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Strategy",
|
|
|
|
Value: p.Strategy,
|
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return slack.Attachment{
|
|
|
|
Color: color,
|
|
|
|
Title: title,
|
|
|
|
Fields: fields,
|
|
|
|
// Footer: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:33:15 +00:00
|
|
|
func (p *Profit) PlainText() string {
|
2021-10-13 23:47:55 +00:00
|
|
|
var emoji string
|
2022-02-03 11:19:56 +00:00
|
|
|
if !p.ProfitMargin.IsZero() {
|
2021-10-13 23:47:55 +00:00
|
|
|
emoji = pnlEmojiMargin(p.Profit, p.ProfitMargin, defaultPnlLevelResolution)
|
|
|
|
} else {
|
|
|
|
emoji = pnlEmojiSimple(p.Profit)
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
return fmt.Sprintf("%s trade profit %s %s %s (%s), net profit =~ %s %s (%s)",
|
2021-10-09 05:24:28 +00:00
|
|
|
p.Symbol,
|
2021-10-13 23:47:55 +00:00
|
|
|
emoji,
|
2022-02-03 11:19:56 +00:00
|
|
|
p.Profit.String(), p.QuoteCurrency,
|
|
|
|
p.ProfitMargin.Percentage(),
|
|
|
|
p.NetProfit.String(), p.QuoteCurrency,
|
|
|
|
p.NetProfitMargin.Percentage(),
|
2021-10-09 05:24:28 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var lossEmoji = "🔥"
|
|
|
|
var profitEmoji = "💰"
|
2021-10-13 23:47:55 +00:00
|
|
|
var defaultPnlLevelResolution = fixedpoint.NewFromFloat(0.001)
|
2021-10-09 05:24:28 +00:00
|
|
|
|
2021-10-14 02:07:27 +00:00
|
|
|
func pnlColor(pnl fixedpoint.Value) string {
|
2022-02-03 11:19:56 +00:00
|
|
|
if pnl.Sign() > 0 {
|
2021-10-14 02:07:27 +00:00
|
|
|
return types.GreenColor
|
|
|
|
}
|
|
|
|
return types.RedColor
|
|
|
|
}
|
|
|
|
|
|
|
|
func pnlSignString(pnl fixedpoint.Value) string {
|
2022-02-03 11:19:56 +00:00
|
|
|
if pnl.Sign() > 0 {
|
2021-10-14 02:07:27 +00:00
|
|
|
return "+" + pnl.String()
|
|
|
|
}
|
|
|
|
return pnl.String()
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:47:55 +00:00
|
|
|
func pnlEmojiSimple(pnl fixedpoint.Value) string {
|
2022-02-03 11:19:56 +00:00
|
|
|
if pnl.Sign() < 0 {
|
2021-10-09 05:24:28 +00:00
|
|
|
return lossEmoji
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if pnl.IsZero() {
|
2021-10-09 05:24:28 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return profitEmoji
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:47:55 +00:00
|
|
|
func pnlEmojiMargin(pnl, margin, resolution fixedpoint.Value) (out string) {
|
2022-02-03 11:19:56 +00:00
|
|
|
if margin.IsZero() {
|
2021-10-14 02:13:21 +00:00
|
|
|
return pnlEmojiSimple(pnl)
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if pnl.Sign() < 0 {
|
2021-10-13 23:47:55 +00:00
|
|
|
out = lossEmoji
|
2022-02-03 11:19:56 +00:00
|
|
|
level := (margin.Neg()).Div(resolution).Int()
|
|
|
|
for i := 1; i < level; i++ {
|
2021-10-13 23:47:55 +00:00
|
|
|
out += lossEmoji
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if pnl.IsZero() {
|
2021-10-13 23:47:55 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
out = profitEmoji
|
2022-02-03 11:19:56 +00:00
|
|
|
level := margin.Div(resolution).Int()
|
|
|
|
for i := 1; i < level; i++ {
|
2021-10-13 23:47:55 +00:00
|
|
|
out += profitEmoji
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-10-08 06:57:44 +00:00
|
|
|
type ProfitStats struct {
|
2021-10-09 05:30:02 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
QuoteCurrency string `json:"quoteCurrency"`
|
|
|
|
BaseCurrency string `json:"baseCurrency"`
|
|
|
|
|
2021-10-08 07:09:55 +00:00
|
|
|
AccumulatedPnL fixedpoint.Value `json:"accumulatedPnL,omitempty"`
|
|
|
|
AccumulatedNetProfit fixedpoint.Value `json:"accumulatedNetProfit,omitempty"`
|
|
|
|
AccumulatedProfit fixedpoint.Value `json:"accumulatedProfit,omitempty"`
|
|
|
|
AccumulatedLoss fixedpoint.Value `json:"accumulatedLoss,omitempty"`
|
2021-10-08 11:43:53 +00:00
|
|
|
AccumulatedVolume fixedpoint.Value `json:"accumulatedVolume,omitempty"`
|
2021-10-08 07:09:55 +00:00
|
|
|
AccumulatedSince int64 `json:"accumulatedSince,omitempty"`
|
|
|
|
|
|
|
|
TodayPnL fixedpoint.Value `json:"todayPnL,omitempty"`
|
|
|
|
TodayNetProfit fixedpoint.Value `json:"todayNetProfit,omitempty"`
|
|
|
|
TodayProfit fixedpoint.Value `json:"todayProfit,omitempty"`
|
|
|
|
TodayLoss fixedpoint.Value `json:"todayLoss,omitempty"`
|
|
|
|
TodaySince int64 `json:"todaySince,omitempty"`
|
2021-10-08 06:57:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 16:22:44 +00:00
|
|
|
func (s *ProfitStats) Init(market types.Market) {
|
|
|
|
s.Symbol = market.Symbol
|
|
|
|
s.BaseCurrency = market.BaseCurrency
|
|
|
|
s.QuoteCurrency = market.QuoteCurrency
|
|
|
|
if s.AccumulatedSince == 0 {
|
|
|
|
s.AccumulatedSince = time.Now().Unix()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 11:16:40 +00:00
|
|
|
func (s *ProfitStats) AddProfit(profit Profit) {
|
2022-02-03 11:19:56 +00:00
|
|
|
s.AccumulatedPnL = s.AccumulatedPnL.Add(profit.Profit)
|
|
|
|
s.AccumulatedNetProfit = s.AccumulatedNetProfit.Add(profit.NetProfit)
|
|
|
|
s.TodayPnL = s.TodayPnL.Add(profit.Profit)
|
|
|
|
s.TodayNetProfit = s.TodayNetProfit.Add(profit.NetProfit)
|
|
|
|
|
|
|
|
if profit.Profit.Sign() < 0 {
|
|
|
|
s.AccumulatedLoss = s.AccumulatedLoss.Add(profit.Profit)
|
|
|
|
s.TodayLoss = s.TodayLoss.Add(profit.Profit)
|
|
|
|
} else if profit.Profit.Sign() > 0 {
|
|
|
|
s.AccumulatedProfit = s.AccumulatedLoss.Add(profit.Profit)
|
|
|
|
s.TodayProfit = s.TodayProfit.Add(profit.Profit)
|
2021-10-08 06:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProfitStats) AddTrade(trade types.Trade) {
|
|
|
|
if s.IsOver24Hours() {
|
|
|
|
s.ResetToday()
|
|
|
|
}
|
2021-10-08 11:43:53 +00:00
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
s.AccumulatedVolume = s.AccumulatedVolume.Add(trade.Quantity)
|
2021-10-08 06:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProfitStats) IsOver24Hours() bool {
|
|
|
|
return time.Since(time.Unix(s.TodaySince, 0)) > 24*time.Hour
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProfitStats) ResetToday() {
|
2022-02-03 11:19:56 +00:00
|
|
|
s.TodayPnL = fixedpoint.Zero
|
|
|
|
s.TodayNetProfit = fixedpoint.Zero
|
|
|
|
s.TodayProfit = fixedpoint.Zero
|
|
|
|
s.TodayLoss = fixedpoint.Zero
|
2021-10-08 06:57:44 +00:00
|
|
|
|
|
|
|
var beginningOfTheDay = util.BeginningOfTheDay(time.Now().Local())
|
|
|
|
s.TodaySince = beginningOfTheDay.Unix()
|
|
|
|
}
|
2021-10-09 05:30:02 +00:00
|
|
|
|
|
|
|
func (s *ProfitStats) PlainText() string {
|
|
|
|
since := time.Unix(s.AccumulatedSince, 0).Local()
|
2021-10-14 00:55:55 +00:00
|
|
|
return fmt.Sprintf("%s Profit Today\n"+
|
2022-02-03 11:19:56 +00:00
|
|
|
"Profit %s %s\n"+
|
|
|
|
"Net profit %s %s\n"+
|
|
|
|
"Trade Loss %s %s\n"+
|
2021-10-14 00:55:55 +00:00
|
|
|
"Summary:\n"+
|
2022-02-03 11:19:56 +00:00
|
|
|
"Accumulated Profit %s %s\n"+
|
|
|
|
"Accumulated Net Profit %s %s\n"+
|
|
|
|
"Accumulated Trade Loss %s %s\n"+
|
2021-10-14 00:55:55 +00:00
|
|
|
"Since %s",
|
|
|
|
s.Symbol,
|
2022-02-03 11:19:56 +00:00
|
|
|
s.TodayPnL.String(), s.QuoteCurrency,
|
|
|
|
s.TodayNetProfit.String(), s.QuoteCurrency,
|
|
|
|
s.TodayLoss.String(), s.QuoteCurrency,
|
|
|
|
s.AccumulatedPnL.String(), s.QuoteCurrency,
|
|
|
|
s.AccumulatedNetProfit.String(), s.QuoteCurrency,
|
|
|
|
s.AccumulatedLoss.String(), s.QuoteCurrency,
|
2021-10-09 05:30:02 +00:00
|
|
|
since.Format(time.RFC822),
|
|
|
|
)
|
|
|
|
}
|
2021-10-13 17:21:25 +00:00
|
|
|
|
2021-10-13 23:33:15 +00:00
|
|
|
func (s *ProfitStats) SlackAttachment() slack.Attachment {
|
2021-10-14 02:07:27 +00:00
|
|
|
var color = pnlColor(s.AccumulatedPnL)
|
|
|
|
var title = fmt.Sprintf("%s Accumulated PnL %s %s", s.Symbol, pnlSignString(s.AccumulatedPnL), s.QuoteCurrency)
|
2021-10-13 17:21:25 +00:00
|
|
|
|
|
|
|
since := time.Unix(s.AccumulatedSince, 0).Local()
|
|
|
|
title += " Since " + since.Format(time.RFC822)
|
|
|
|
|
|
|
|
var fields []slack.AttachmentField
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !s.TodayPnL.IsZero() {
|
2021-10-18 00:45:27 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "P&L Today",
|
|
|
|
Value: pnlSignString(s.TodayPnL) + " " + s.QuoteCurrency,
|
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !s.TodayProfit.IsZero() {
|
2021-10-13 17:21:25 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Profit Today",
|
2021-10-14 02:13:21 +00:00
|
|
|
Value: pnlSignString(s.TodayProfit) + " " + s.QuoteCurrency,
|
2021-10-13 17:21:25 +00:00
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !s.TodayNetProfit.IsZero() {
|
2021-10-13 17:21:25 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Net Profit Today",
|
2021-10-14 02:13:21 +00:00
|
|
|
Value: pnlSignString(s.TodayNetProfit) + " " + s.QuoteCurrency,
|
2021-10-13 17:21:25 +00:00
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !s.TodayLoss.IsZero() {
|
2021-10-13 17:21:25 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Loss Today",
|
2021-10-14 02:13:21 +00:00
|
|
|
Value: pnlSignString(s.TodayLoss) + " " + s.QuoteCurrency,
|
2021-10-13 17:21:25 +00:00
|
|
|
Short: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !s.AccumulatedPnL.IsZero() {
|
2021-10-18 00:45:27 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Accumulated P&L",
|
|
|
|
Value: pnlSignString(s.AccumulatedPnL) + " " + s.QuoteCurrency,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !s.AccumulatedProfit.IsZero() {
|
2021-10-13 17:21:25 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Accumulated Profit",
|
2021-10-14 02:13:21 +00:00
|
|
|
Value: pnlSignString(s.AccumulatedProfit) + " " + s.QuoteCurrency,
|
2021-10-13 17:21:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !s.AccumulatedNetProfit.IsZero() {
|
2021-10-13 17:21:25 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Accumulated Net Profit",
|
2021-10-14 02:13:21 +00:00
|
|
|
Value: pnlSignString(s.AccumulatedNetProfit) + " " + s.QuoteCurrency,
|
2021-10-13 17:21:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if !s.AccumulatedLoss.IsZero() {
|
2021-10-13 17:21:25 +00:00
|
|
|
fields = append(fields, slack.AttachmentField{
|
|
|
|
Title: "Accumulated Loss",
|
2021-10-14 02:13:21 +00:00
|
|
|
Value: pnlSignString(s.AccumulatedLoss) + " " + s.QuoteCurrency,
|
2021-10-13 17:21:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return slack.Attachment{
|
|
|
|
Color: color,
|
|
|
|
Title: title,
|
|
|
|
Fields: fields,
|
|
|
|
// Footer: "",
|
|
|
|
}
|
|
|
|
}
|