move trade struct to types

This commit is contained in:
c9s 2020-07-11 12:42:28 +08:00
parent 9288b6801f
commit db5b203e9d
5 changed files with 15 additions and 14 deletions

View File

@ -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,

View File

@ -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),

View File

@ -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)
}

View File

@ -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"

View File

@ -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
}