bbgo_origin/bbgo/pnl.go

178 lines
4.5 KiB
Go
Raw Normal View History

2020-06-08 02:42:50 +00:00
package bbgo
2020-07-11 03:23:48 +00:00
import (
2020-07-12 14:52:37 +00:00
"github.com/c9s/bbgo/pkg/bbgo/types"
"github.com/c9s/bbgo/pkg/slack/slackstyle"
2020-07-11 03:23:48 +00:00
log "github.com/sirupsen/logrus"
2020-07-12 14:52:37 +00:00
"github.com/slack-go/slack"
"strconv"
2020-07-11 04:37:09 +00:00
"strings"
2020-07-11 03:23:48 +00:00
"time"
)
2020-06-08 02:42:50 +00:00
2020-07-11 03:23:48 +00:00
type ProfitAndLossCalculator struct {
2020-08-03 12:06:33 +00:00
Symbol string
StartTime time.Time
CurrentPrice float64
Trades []types.Trade
TradingFeeCurrency string
CurrencyPrice map[string]float64
2020-07-11 03:23:48 +00:00
}
2020-07-12 14:52:37 +00:00
func (c *ProfitAndLossCalculator) AddTrade(trade types.Trade) {
2020-07-11 03:23:48 +00:00
c.Trades = append(c.Trades, trade)
}
2020-07-11 04:37:09 +00:00
func (c *ProfitAndLossCalculator) SetCurrencyPrice(symbol string, price float64) {
if c.CurrencyPrice == nil {
c.CurrencyPrice = map[string]float64{}
}
c.CurrencyPrice[symbol] = price
}
2020-07-11 03:23:48 +00:00
func (c *ProfitAndLossCalculator) SetCurrentPrice(price float64) {
c.CurrentPrice = price
}
func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
// copy trades, so that we can truncate it.
var trades = c.Trades
2020-06-08 02:42:50 +00:00
var bidVolume = 0.0
var bidAmount = 0.0
var bidFee = 0.0
2020-07-13 05:41:51 +00:00
var askVolume = 0.0
var askFee = 0.0
2020-08-04 01:47:54 +00:00
var feeRate = 0.0015
2020-07-13 05:41:51 +00:00
2020-08-03 12:06:33 +00:00
for _, trade := range trades {
if trade.Symbol == c.Symbol {
if trade.IsBuyer {
bidVolume += trade.Quantity
bidAmount += trade.Price * trade.Quantity
2020-08-04 01:47:54 +00:00
}
2020-08-03 12:06:33 +00:00
2020-08-04 01:47:54 +00:00
// since we use USDT as the quote currency, we simply check if it matches the currency symbol
if strings.HasPrefix(trade.Symbol, trade.FeeCurrency) {
bidVolume -= trade.Fee
bidFee += trade.Price * trade.Fee
} else if trade.FeeCurrency == "USDT" {
bidFee += trade.Fee
2020-06-08 02:42:50 +00:00
}
2020-08-04 01:47:54 +00:00
} else if trade.FeeCurrency == c.TradingFeeCurrency {
2020-08-03 12:06:33 +00:00
bidVolume -= trade.Fee
2020-06-08 02:42:50 +00:00
}
}
log.Infof("average bid price = (total amount %f + total fee %f) / volume %f", bidAmount, bidFee, bidVolume)
2020-07-11 03:23:48 +00:00
profit := 0.0
averageBidPrice := (bidAmount + bidFee) / bidVolume
2020-06-08 02:42:50 +00:00
for _, t := range trades {
2020-08-03 12:06:33 +00:00
if t.Symbol != c.Symbol {
continue
}
2020-08-04 01:47:54 +00:00
if t.IsBuyer {
continue
}
profit += (t.Price - averageBidPrice) * t.Quantity
askVolume += t.Quantity
2020-07-13 05:41:51 +00:00
2020-08-04 01:47:54 +00:00
// since we use USDT as the quote currency, we simply check if it matches the currency symbol
if strings.HasPrefix(t.Symbol, t.FeeCurrency) {
askFee += t.Price * t.Fee
} else if t.FeeCurrency == "USDT" {
askFee += t.Fee
2020-06-08 02:42:50 +00:00
}
}
profit -= askFee
2020-07-11 03:23:48 +00:00
stock := bidVolume - askVolume
2020-06-08 02:42:50 +00:00
futureFee := 0.0
if stock > 0 {
2020-07-20 05:14:20 +00:00
_ = feeRate
// stockFee := c.CurrentPrice * feeRate * stock
// profit += (c.CurrentPrice-averageBidPrice)*stock - stockFee
// futureFee += stockFee
2020-06-08 02:42:50 +00:00
}
2020-07-11 03:23:48 +00:00
fee := bidFee + askFee + futureFee
return &ProfitAndLossReport{
2020-07-11 04:07:24 +00:00
Symbol: c.Symbol,
StartTime: c.StartTime,
CurrentPrice: c.CurrentPrice,
NumTrades: len(trades),
2020-07-11 03:23:48 +00:00
2020-07-16 10:25:40 +00:00
BidVolume: bidVolume,
AskVolume: askVolume,
2020-08-03 10:47:36 +00:00
Stock: stock,
2020-07-11 03:23:48 +00:00
Profit: profit,
AverageBidPrice: averageBidPrice,
Fee: fee,
}
}
type ProfitAndLossReport struct {
CurrentPrice float64
StartTime time.Time
2020-07-11 04:07:24 +00:00
Symbol string
2020-07-11 03:23:48 +00:00
NumTrades int
Profit float64
AverageBidPrice float64
2020-08-03 10:47:36 +00:00
BidVolume float64
AskVolume float64
2020-07-11 03:23:48 +00:00
Fee float64
2020-08-03 10:47:36 +00:00
Stock float64
2020-07-11 03:23:48 +00:00
}
func (report ProfitAndLossReport) Print() {
log.Infof("trades since: %v", report.StartTime)
log.Infof("average bid price: %s", USD.FormatMoneyFloat64(report.AverageBidPrice))
2020-07-16 10:25:40 +00:00
log.Infof("total bid volume: %f", report.BidVolume)
log.Infof("total ask volume: %f", report.AskVolume)
2020-08-04 01:47:54 +00:00
log.Infof("stock: %f", report.Stock)
2020-07-11 03:23:48 +00:00
log.Infof("current price: %s", USD.FormatMoneyFloat64(report.CurrentPrice))
2020-08-04 01:47:54 +00:00
log.Infof("profit: %s", USD.FormatMoneyFloat64(report.Profit))
2020-07-11 03:23:48 +00:00
}
2020-07-12 14:52:37 +00:00
func (report ProfitAndLossReport) SlackAttachment() slack.Attachment {
var color = ""
if report.Profit > 0 {
color = slackstyle.Green
} else {
color = slackstyle.Red
}
market, ok := types.FindMarket(report.Symbol)
if !ok {
return slack.Attachment{}
}
2020-07-18 02:13:11 +00:00
_ = market
2020-07-12 14:52:37 +00:00
return slack.Attachment{
Title: "Profit and Loss report of " + report.Symbol,
Color: color,
// Pretext: "",
// Text: "",
Fields: []slack.AttachmentField{
2020-08-03 10:47:36 +00:00
{Title: "Symbol", Value: report.Symbol, Short: true},
{Title: "Profit", Value: USD.FormatMoney(report.Profit), Short: true},
{Title: "Current Price", Value: USD.FormatMoney(report.CurrentPrice), Short: true},
{Title: "Average Bid Price", Value: USD.FormatMoney(report.AverageBidPrice), Short: true},
{Title: "Number of Trades", Value: strconv.Itoa(report.NumTrades), Short: true},
{Title: "Stock", Value: strconv.FormatFloat(report.Stock, 'f', 8, 64), Short: true},
2020-07-12 14:52:37 +00:00
},
Footer: report.StartTime.Format(time.RFC822),
FooterIcon: "",
}
}