From db5b203e9da45c1f79b4a4a9a2a5b987f2bdccdf Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 11 Jul 2020 12:42:28 +0800 Subject: [PATCH] move trade struct to types --- bbgo/binance_exchange.go | 5 +++-- bbgo/parser.go | 5 +++-- bbgo/pnl.go | 7 ++++--- bbgo/trader.go | 3 ++- {bbgo => types}/trade.go | 9 +++------ 5 files changed, 15 insertions(+), 14 deletions(-) rename {bbgo => types}/trade.go (81%) diff --git a/bbgo/binance_exchange.go b/bbgo/binance_exchange.go index 19f079db9..0ee0ebaee 100644 --- a/bbgo/binance_exchange.go +++ b/bbgo/binance_exchange.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/adshao/go-binance" + "github.com/c9s/bbgo/pkg/types" "github.com/gorilla/websocket" log "github.com/sirupsen/logrus" "strconv" @@ -228,7 +229,7 @@ func (e *BinanceExchange) QueryKLines(ctx context.Context, symbol, interval stri return kLines, nil } -func (e *BinanceExchange) QueryTrades(ctx context.Context, symbol string, startTime time.Time) (trades []Trade, err error) { +func (e *BinanceExchange) QueryTrades(ctx context.Context, symbol string, startTime time.Time) (trades []types.Trade, err error) { log.Infof("[binance] querying %s trades from %s", symbol, startTime) var lastTradeID int64 = 0 @@ -303,7 +304,7 @@ func (e *BinanceExchange) QueryTrades(ctx context.Context, symbol string, startT return nil, err } - trades = append(trades, Trade{ + trades = append(trades, types.Trade{ ID: t.ID, Price: price, Volume: quantity, diff --git a/bbgo/parser.go b/bbgo/parser.go index e70649148..a8b4162ba 100644 --- a/bbgo/parser.go +++ b/bbgo/parser.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/c9s/bbgo/pkg/types" "time" "github.com/valyala/fastjson" @@ -82,13 +83,13 @@ type BinanceExecutionReportEvent struct { OrderCreationTime int `json:"O"` } -func (e *BinanceExecutionReportEvent) Trade() (*Trade, error) { +func (e *BinanceExecutionReportEvent) Trade() (*types.Trade, error) { if e.CurrentExecutionType != "TRADE" { return nil, errors.New("execution report is not a trade") } tt := time.Unix(0, e.TransactionTime/1000000) - return &Trade{ + return &types.Trade{ ID: e.TradeID, Symbol: e.Symbol, Price: MustParseFloat(e.LastExecutedPrice), diff --git a/bbgo/pnl.go b/bbgo/pnl.go index 8d4759003..db2631363 100644 --- a/bbgo/pnl.go +++ b/bbgo/pnl.go @@ -1,12 +1,13 @@ package bbgo import ( + "github.com/c9s/bbgo/pkg/types" log "github.com/sirupsen/logrus" "strings" "time" ) -func CalculateAverageCost(trades []Trade) (averageCost float64) { +func CalculateAverageCost(trades []types.Trade) (averageCost float64) { var totalCost = 0.0 var totalQuantity = 0.0 for _, t := range trades { @@ -27,12 +28,12 @@ type ProfitAndLossCalculator struct { Symbol string StartTime time.Time CurrentPrice float64 - Trades []Trade + Trades []types.Trade CurrencyPrice map[string]float64 } -func (c *ProfitAndLossCalculator) AddTrade(trade Trade) { +func (c *ProfitAndLossCalculator) AddTrade(trade types.Trade) { c.Trades = append(c.Trades, trade) } diff --git a/bbgo/trader.go b/bbgo/trader.go index 6f3452135..0877475cd 100644 --- a/bbgo/trader.go +++ b/bbgo/trader.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/c9s/bbgo/pkg/slack/slackstyle" + "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/util" "github.com/leekchan/accounting" "github.com/sirupsen/logrus" @@ -74,7 +75,7 @@ func (t *Trader) Errorf(err error, format string, args ...interface{}) { } } -func (t *Trader) ReportTrade(e *BinanceExecutionReportEvent, trade *Trade) { +func (t *Trader) ReportTrade(e *BinanceExecutionReportEvent, trade *types.Trade) { var color = "" if trade.IsBuyer { color = "#228B22" diff --git a/bbgo/trade.go b/types/trade.go similarity index 81% rename from bbgo/trade.go rename to types/trade.go index 99aaa3569..9d0fefffb 100644 --- a/bbgo/trade.go +++ b/types/trade.go @@ -1,14 +1,12 @@ -package bbgo +package types -import ( - "time" -) +import "time" type Trade struct { ID int64 Price float64 Volume float64 - Side string + Side string IsBuyer bool IsMaker bool Time time.Time @@ -16,4 +14,3 @@ type Trade struct { Fee float64 FeeCurrency string } -