diff --git a/bbgo/event.go b/bbgo/event.go deleted file mode 100644 index 28ab3b36b..000000000 --- a/bbgo/event.go +++ /dev/null @@ -1,40 +0,0 @@ -package bbgo - -/* - -kline - -{ - "e": "kline", // KLineEvent type - "E": 123456789, // KLineEvent time - "s": "BNBBTC", // Symbol - "k": { - "t": 123400000, // Kline start time - "T": 123460000, // Kline close time - "s": "BNBBTC", // Symbol - "i": "1m", // Interval - "f": 100, // First trade ID - "L": 200, // Last trade ID - "o": "0.0010", // Open price - "c": "0.0020", // Close price - "h": "0.0025", // High price - "l": "0.0015", // Low price - "v": "1000", // Base asset volume - "n": 100, // Number of trades - "x": false, // Is this kline closed? - "q": "1.0000", // Quote asset volume - "V": "500", // Taker buy base asset volume - "Q": "0.500", // Taker buy quote asset volume - "B": "123456" // Ignore - } -} - - - - -*/ -type EventBase struct { - Event string `json:"e"` // event - Time int64 `json:"E"` -} - diff --git a/bbgo/kline.go b/bbgo/kline.go deleted file mode 100644 index fee984def..000000000 --- a/bbgo/kline.go +++ /dev/null @@ -1,12 +0,0 @@ -package bbgo - -import ( - "github.com/c9s/bbgo/pkg/types" -) - -type KLineEvent struct { - EventBase - Symbol string `json:"s"` - KLine *types.KLine `json:"k,omitempty"` -} - diff --git a/bbgo/kline_detector.go b/bbgo/kline_detector.go index 88cb86b65..610d34f96 100644 --- a/bbgo/kline_detector.go +++ b/bbgo/kline_detector.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/adshao/go-binance" "github.com/c9s/bbgo/pkg/bbgo/types" + binance2 "github.com/c9s/bbgo/pkg/exchange/binance" "github.com/c9s/bbgo/pkg/util" "github.com/slack-go/slack" "math" @@ -124,7 +125,7 @@ func (d *KLineDetector) String() string { return name } -func (d *KLineDetector) NewOrder(e *KLineEvent, tradingCtx *TradingContext) *Order { +func (d *KLineDetector) NewOrder(e *binance2.KLineEvent, tradingCtx *TradingContext) *Order { var kline types.KLineOrWindow = e.KLine if d.EnableLookBack { klineWindow := tradingCtx.KLineWindows[e.KLine.Interval] @@ -151,7 +152,7 @@ func (d *KLineDetector) NewOrder(e *KLineEvent, tradingCtx *TradingContext) *Ord } } -func (d *KLineDetector) Detect(e *KLineEvent, tradingCtx *TradingContext) (reason string, kline types.KLineOrWindow, ok bool) { +func (d *KLineDetector) Detect(e *binance2.KLineEvent, tradingCtx *TradingContext) (reason string, kline types.KLineOrWindow, ok bool) { kline = e.KLine // if the 3m trend is drop, do not buy, let 5m window handle it. diff --git a/bbgo/parse.go b/bbgo/parse.go index a9e080bca..920078f66 100644 --- a/bbgo/parse.go +++ b/bbgo/parse.go @@ -1,12 +1,2 @@ package bbgo -import "strconv" - -func MustParseFloat(s string) float64 { - v, err := strconv.ParseFloat(s, 64) - if err != nil { - panic(err) - } - return v -} - diff --git a/bbgo/parser.go b/bbgo/parser.go deleted file mode 100644 index 920078f66..000000000 --- a/bbgo/parser.go +++ /dev/null @@ -1,2 +0,0 @@ -package bbgo - diff --git a/bbgo/pnl.go b/bbgo/pnl.go index db2631363..3760ac052 100644 --- a/bbgo/pnl.go +++ b/bbgo/pnl.go @@ -7,23 +7,6 @@ import ( "time" ) -func CalculateAverageCost(trades []types.Trade) (averageCost float64) { - var totalCost = 0.0 - var totalQuantity = 0.0 - for _, t := range trades { - if t.IsBuyer { - totalCost += t.Price * t.Volume - totalQuantity += t.Volume - } else { - totalCost -= t.Price * t.Volume - totalQuantity -= t.Volume - } - } - - averageCost = totalCost / totalQuantity - return -} - type ProfitAndLossCalculator struct { Symbol string StartTime time.Time diff --git a/exchange/binance/exchange.go b/exchange/binance/exchange.go index 777dc655c..ddb895c09 100644 --- a/exchange/binance/exchange.go +++ b/exchange/binance/exchange.go @@ -5,6 +5,7 @@ import ( "github.com/adshao/go-binance" "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" "github.com/sirupsen/logrus" "strconv" "time" @@ -14,13 +15,20 @@ type Exchange struct { Client *binance.Client } +func NewExchange(key, secret string) *Exchange { + var client = binance.NewClient(key, secret) + return &Exchange{ + Client: client, + } +} + func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (float64, error) { resp, err := e.Client.NewAveragePriceService().Symbol(symbol).Do(ctx) if err != nil { return 0, err } - return bbgo.MustParseFloat(resp.Price), nil + return util.MustParseFloat(resp.Price), nil } func (e *Exchange) NewPrivateStream(ctx context.Context) (*PrivateStream, error) { diff --git a/exchange/binance/parse.go b/exchange/binance/parse.go index 182ce2261..9208741f9 100644 --- a/exchange/binance/parse.go +++ b/exchange/binance/parse.go @@ -4,8 +4,8 @@ import ( "encoding/json" "errors" "fmt" - "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" "github.com/valyala/fastjson" "time" ) @@ -50,7 +50,7 @@ executionReport } */ type ExecutionReportEvent struct { - bbgo.EventBase + EventBase Symbol string `json:"s"` ClientOrderID string `json:"c"` @@ -92,12 +92,12 @@ func (e *ExecutionReportEvent) Trade() (*types.Trade, error) { return &types.Trade{ ID: e.TradeID, Symbol: e.Symbol, - Price: bbgo.MustParseFloat(e.LastExecutedPrice), - Volume: bbgo.MustParseFloat(e.LastExecutedQuantity), + Price: util.MustParseFloat(e.LastExecutedPrice), + Volume: util.MustParseFloat(e.LastExecutedQuantity), IsBuyer: e.Side == "BUY", IsMaker: e.IsMaker, Time: tt, - Fee: bbgo.MustParseFloat(e.CommissionAmount), + Fee: util.MustParseFloat(e.CommissionAmount), FeeCurrency: e.CommissionAsset, }, nil } @@ -114,7 +114,7 @@ balanceUpdate } */ type BalanceUpdateEvent struct { - bbgo.EventBase + EventBase Asset string `json:"a"` Delta string `json:"d"` @@ -176,7 +176,7 @@ type Balance struct { } type OutboundAccountInfoEvent struct { - bbgo.EventBase + EventBase MakerCommissionRate int `json:"m"` TakerCommissionRate int `json:"t"` @@ -208,7 +208,7 @@ func ParseEvent(message string) (interface{}, error) { switch eventType { case "kline": - var event bbgo.KLineEvent + var event KLineEvent err := json.Unmarshal([]byte(message), &event) return &event, err @@ -236,3 +236,48 @@ func ParseEvent(message string) (interface{}, error) { return nil, fmt.Errorf("unsupported message: %s", message) } + +type KLineEvent struct { + EventBase + Symbol string `json:"s"` + KLine *types.KLine `json:"k,omitempty"` +} + +/* + +kline + +{ + "e": "kline", // KLineEvent type + "E": 123456789, // KLineEvent time + "s": "BNBBTC", // Symbol + "k": { + "t": 123400000, // Kline start time + "T": 123460000, // Kline close time + "s": "BNBBTC", // Symbol + "i": "1m", // Interval + "f": 100, // First trade ID + "L": 200, // Last trade ID + "o": "0.0010", // Open price + "c": "0.0020", // Close price + "h": "0.0025", // High price + "l": "0.0015", // Low price + "v": "1000", // Base asset volume + "n": 100, // Number of trades + "x": false, // Is this kline closed? + "q": "1.0000", // Quote asset volume + "V": "500", // Taker buy base asset volume + "Q": "0.500", // Taker buy quote asset volume + "B": "123456" // Ignore + } +} + + + + +*/ +type EventBase struct { + Event string `json:"e"` // event + Time int64 `json:"E"` +} + diff --git a/types/kline.go b/types/kline.go index c9cd93d43..1a8254947 100644 --- a/types/kline.go +++ b/types/kline.go @@ -59,19 +59,19 @@ func (k KLine) GetTrend() int { } func (k KLine) GetHigh() float64 { - return bbgo.MustParseFloat(k.High) + return util.MustParseFloat(k.High) } func (k KLine) GetLow() float64 { - return bbgo.MustParseFloat(k.Low) + return util.MustParseFloat(k.Low) } func (k KLine) GetOpen() float64 { - return bbgo.MustParseFloat(k.Open) + return util.MustParseFloat(k.Open) } func (k KLine) GetClose() float64 { - return bbgo.MustParseFloat(k.Close) + return util.MustParseFloat(k.Close) } func (k KLine) GetMaxChange() float64 { diff --git a/util/math.go b/util/math.go index a3bd4c51d..6a30bdb4e 100644 --- a/util/math.go +++ b/util/math.go @@ -20,3 +20,12 @@ func Pow10(n int64) int64 { func FormatFloat(val float64, prec int) string { return strconv.FormatFloat(val, 'f', prec, 64) } + +func MustParseFloat(s string) float64 { + v, err := strconv.ParseFloat(s, 64) + if err != nil { + panic(err) + } + return v +} +