add binance buyer, maker label

This commit is contained in:
c9s 2020-07-11 12:37:09 +08:00
parent ea33db5aa1
commit 9288b6801f
3 changed files with 39 additions and 4 deletions

View File

@ -251,8 +251,27 @@ func (e *BinanceExchange) QueryTrades(ctx context.Context, symbol string, startT
break break
} }
buyerOrSellerLabel := func(trade *binance.TradeV3) (o string) {
if trade.IsBuyer {
o = "BUYER"
} else {
o = "SELLER"
}
return o
}
makerOrTakerLabel := func(trade *binance.TradeV3) (o string) {
if trade.IsMaker {
o += "MAKER"
} else {
o += "TAKER"
}
return o
}
for _, t := range bnTrades { for _, t := range bnTrades {
// skip trade ID that is the same // skip trade ID that is the same. however this should not happen
if t.ID == lastTradeID { if t.ID == lastTradeID {
continue continue
} }
@ -267,7 +286,7 @@ func (e *BinanceExchange) QueryTrades(ctx context.Context, symbol string, startT
// trade time // trade time
tt := time.Unix(0, t.Time*1000000) tt := time.Unix(0, t.Time*1000000)
log.Infof("trade: %d %4s Price: % 13s Volume: % 13s %s", t.ID, side, t.Price, t.Quantity, tt) log.Infof("[binance] trade: %d %s % 4s price: % 13s volume: % 11s %6s % 5s %s", t.ID, t.Symbol, side, t.Price, t.Quantity, buyerOrSellerLabel(t), makerOrTakerLabel(t), tt)
price, err := strconv.ParseFloat(t.Price, 64) price, err := strconv.ParseFloat(t.Price, 64)
if err != nil { if err != nil {
@ -288,6 +307,7 @@ func (e *BinanceExchange) QueryTrades(ctx context.Context, symbol string, startT
ID: t.ID, ID: t.ID,
Price: price, Price: price,
Volume: quantity, Volume: quantity,
Side: side,
IsBuyer: t.IsBuyer, IsBuyer: t.IsBuyer,
IsMaker: t.IsMaker, IsMaker: t.IsMaker,
Fee: fee, Fee: fee,

View File

@ -2,6 +2,7 @@ package bbgo
import ( import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strings"
"time" "time"
) )
@ -27,12 +28,22 @@ type ProfitAndLossCalculator struct {
StartTime time.Time StartTime time.Time
CurrentPrice float64 CurrentPrice float64
Trades []Trade Trades []Trade
CurrencyPrice map[string]float64
} }
func (c *ProfitAndLossCalculator) AddTrade(trade Trade) { func (c *ProfitAndLossCalculator) AddTrade(trade Trade) {
c.Trades = append(c.Trades, trade) c.Trades = append(c.Trades, trade)
} }
func (c *ProfitAndLossCalculator) SetCurrencyPrice(symbol string, price float64) {
if c.CurrencyPrice == nil {
c.CurrencyPrice = map[string]float64{}
}
c.CurrencyPrice[symbol] = price
}
func (c *ProfitAndLossCalculator) SetCurrentPrice(price float64) { func (c *ProfitAndLossCalculator) SetCurrentPrice(price float64) {
c.CurrentPrice = price c.CurrentPrice = price
} }
@ -61,9 +72,12 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
if t.IsBuyer { if t.IsBuyer {
bidVolume += t.Volume bidVolume += t.Volume
bidAmount += t.Price * t.Volume bidAmount += t.Price * t.Volume
switch t.FeeCurrency {
case "BTC": // since we use USDT as the quote currency, we simply check if it matches the currency symbol
if strings.HasPrefix(t.Symbol, t.FeeCurrency) {
bidFee += t.Price * t.Fee bidFee += t.Price * t.Fee
} else if t.FeeCurrency == "USDT" {
bidFee += t.Fee
} }
} }
} }

View File

@ -8,6 +8,7 @@ type Trade struct {
ID int64 ID int64
Price float64 Price float64
Volume float64 Volume float64
Side string
IsBuyer bool IsBuyer bool
IsMaker bool IsMaker bool
Time time.Time Time time.Time