2020-10-16 02:21:37 +00:00
|
|
|
package pnl
|
2020-09-19 01:05:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-12-05 16:47:41 +00:00
|
|
|
"encoding/json"
|
2020-09-19 01:05:06 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-12-04 18:16:48 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2020-10-16 02:21:37 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-09-19 01:05:06 +00:00
|
|
|
"github.com/slack-go/slack"
|
|
|
|
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/slack/slackstyle"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-09-19 01:05:06 +00:00
|
|
|
)
|
|
|
|
|
2020-10-16 02:21:37 +00:00
|
|
|
type AverageCostPnlReport struct {
|
2022-02-02 11:37:18 +00:00
|
|
|
LastPrice fixedpoint.Value `json:"lastPrice"`
|
2021-12-05 16:46:50 +00:00
|
|
|
StartTime time.Time `json:"startTime"`
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Market types.Market `json:"market"`
|
2020-09-19 01:05:06 +00:00
|
|
|
|
2021-12-05 16:46:50 +00:00
|
|
|
NumTrades int `json:"numTrades"`
|
|
|
|
Profit fixedpoint.Value `json:"profit"`
|
|
|
|
NetProfit fixedpoint.Value `json:"netProfit"`
|
|
|
|
UnrealizedProfit fixedpoint.Value `json:"unrealizedProfit"`
|
2022-02-02 11:37:18 +00:00
|
|
|
AverageCost fixedpoint.Value `json:"averageCost"`
|
|
|
|
BuyVolume fixedpoint.Value `json:"buyVolume,omitempty"`
|
|
|
|
SellVolume fixedpoint.Value `json:"sellVolume,omitempty"`
|
|
|
|
FeeInUSD fixedpoint.Value `json:"feeInUSD"`
|
|
|
|
Stock fixedpoint.Value `json:"stock"`
|
|
|
|
CurrencyFees map[string]fixedpoint.Value `json:"currencyFees"`
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 16:47:41 +00:00
|
|
|
func (report *AverageCostPnlReport) JSON() ([]byte, error) {
|
|
|
|
return json.MarshalIndent(report, "", " ")
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:21:37 +00:00
|
|
|
func (report AverageCostPnlReport) Print() {
|
2020-11-10 06:18:27 +00:00
|
|
|
log.Infof("TRADES SINCE: %v", report.StartTime)
|
|
|
|
log.Infof("NUMBER OF TRADES: %d", report.NumTrades)
|
2022-02-02 11:37:18 +00:00
|
|
|
log.Infof("AVERAGE COST: %s", types.USD.FormatMoney(report.AverageCost))
|
2022-02-09 10:23:35 +00:00
|
|
|
log.Infof("TOTAL BUY VOLUME: %v", report.BuyVolume)
|
|
|
|
log.Infof("TOTAL SELL VOLUME: %v", report.SellVolume)
|
2022-02-02 11:37:18 +00:00
|
|
|
log.Infof("STOCK: %s", report.Stock.String())
|
2021-12-04 18:16:48 +00:00
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// log.Infof("FEE (USD): %f", report.FeeInUSD)
|
2022-02-02 11:37:18 +00:00
|
|
|
log.Infof("CURRENT PRICE: %s", types.USD.FormatMoney(report.LastPrice))
|
2020-11-10 06:18:27 +00:00
|
|
|
log.Infof("CURRENCY FEES:")
|
2020-09-19 01:05:06 +00:00
|
|
|
for currency, fee := range report.CurrencyFees {
|
2022-02-02 11:37:18 +00:00
|
|
|
log.Infof(" - %s: %s", currency, fee.String())
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
2022-02-02 11:37:18 +00:00
|
|
|
log.Infof("PROFIT: %s", types.USD.FormatMoney(report.Profit))
|
|
|
|
log.Infof("UNREALIZED PROFIT: %s", types.USD.FormatMoney(report.UnrealizedProfit))
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 02:21:37 +00:00
|
|
|
func (report AverageCostPnlReport) SlackAttachment() slack.Attachment {
|
2020-10-09 07:59:33 +00:00
|
|
|
var color = slackstyle.Red
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
if report.UnrealizedProfit.Sign() > 0 {
|
2020-09-19 01:05:06 +00:00
|
|
|
color = slackstyle.Green
|
|
|
|
}
|
|
|
|
|
|
|
|
return slack.Attachment{
|
|
|
|
Title: report.Symbol + " Profit and Loss report",
|
|
|
|
Text: "Profit " + types.USD.FormatMoney(report.Profit),
|
|
|
|
Color: color,
|
|
|
|
// Pretext: "",
|
|
|
|
// Text: "",
|
|
|
|
Fields: []slack.AttachmentField{
|
|
|
|
{Title: "Profit", Value: types.USD.FormatMoney(report.Profit)},
|
|
|
|
{Title: "Unrealized Profit", Value: types.USD.FormatMoney(report.UnrealizedProfit)},
|
2022-02-03 04:55:25 +00:00
|
|
|
{Title: "Current Price", Value: report.Market.FormatPrice(report.LastPrice), Short: true},
|
|
|
|
{Title: "Average Cost", Value: report.Market.FormatPrice(report.AverageCost), Short: true},
|
2021-12-05 16:46:50 +00:00
|
|
|
|
2021-12-04 18:16:48 +00:00
|
|
|
// FIXME:
|
|
|
|
// {Title: "Fee (USD)", Value: types.USD.FormatMoney(report.FeeInUSD), Short: true},
|
2022-02-02 11:37:18 +00:00
|
|
|
{Title: "Stock", Value: report.Stock.String(), Short: true},
|
2020-09-19 01:05:06 +00:00
|
|
|
{Title: "Number of Trades", Value: strconv.Itoa(report.NumTrades), Short: true},
|
|
|
|
},
|
|
|
|
Footer: report.StartTime.Format(time.RFC822),
|
|
|
|
FooterIcon: "",
|
|
|
|
}
|
|
|
|
}
|