From 3980a482367453531485e0547543b64906f50f95 Mon Sep 17 00:00:00 2001 From: nntaoli <2767415655@qq.com> Date: Sat, 27 Feb 2021 17:16:11 +0800 Subject: [PATCH 1/5] [binance] fix ws set proxy bug Signed-off-by: nntaoli <2767415655@qq.com> --- binance/FuturesWs.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/binance/FuturesWs.go b/binance/FuturesWs.go index 41eef8ea..31eb9da2 100644 --- a/binance/FuturesWs.go +++ b/binance/FuturesWs.go @@ -31,15 +31,24 @@ func NewFuturesWs() *FuturesWs { ProtoHandleFunc(futuresWs.handle).AutoReconnect() futuresWs.f = wsBuilder.WsUrl("wss://fstream.binance.com/ws").Build() futuresWs.d = wsBuilder.WsUrl("wss://dstream.binance.com/ws").Build() - futuresWs.base = NewBinanceFutures(&goex.APIConfig{ - HttpClient: &http.Client{ + + httpCli := &http.Client{ + Timeout: 10 * time.Second, + } + + if os.Getenv("HTTPS_PROXY") != "" { + httpCli = &http.Client{ Transport: &http.Transport{ Proxy: func(r *http.Request) (*url.URL, error) { return url.Parse(os.Getenv("HTTPS_PROXY")) }, }, Timeout: 10 * time.Second, - }, + } + } + + futuresWs.base = NewBinanceFutures(&goex.APIConfig{ + HttpClient: httpCli, }) return futuresWs From f41219e9b4225f75c66c1cc7131472ba126b9a88 Mon Sep 17 00:00:00 2001 From: nntaoli <2767415655@qq.com> Date: Sat, 27 Feb 2021 21:21:38 +0800 Subject: [PATCH 2/5] [bitmex] delayed connection --- bitmex/SwapWs.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/bitmex/SwapWs.go b/bitmex/SwapWs.go index 00a42ac2..9ec5fa56 100644 --- a/bitmex/SwapWs.go +++ b/bitmex/SwapWs.go @@ -6,6 +6,7 @@ import ( . "github.com/nntaoli-project/goex" "github.com/nntaoli-project/goex/internal/logger" "sort" + "sync" "time" ) @@ -42,7 +43,9 @@ type depthData struct { } type SwapWs struct { - c *WsConn + c *WsConn + once sync.Once + wsBuilder *WsBuilder depthCall func(depth *Depth) tickerCall func(ticker *FutureTicker) @@ -52,14 +55,20 @@ type SwapWs struct { func NewSwapWs() *SwapWs { s := new(SwapWs) - wsBuilder := NewWsBuilder().DisableEnableCompression().WsUrl("wss://www.bitmex.com/realtime") - wsBuilder = wsBuilder.Heartbeat(func() []byte { return []byte("ping") }, 5*time.Second) - wsBuilder = wsBuilder.ProtoHandleFunc(s.handle).AutoReconnect() - s.c = wsBuilder.Build() + s.wsBuilder = NewWsBuilder().DisableEnableCompression().WsUrl("wss://www.bitmex.com/realtime") + s.wsBuilder = s.wsBuilder.Heartbeat(func() []byte { return []byte("ping") }, 5*time.Second) + s.wsBuilder = s.wsBuilder.ProtoHandleFunc(s.handle).AutoReconnect() + //s.c = wsBuilder.Build() s.tickerCacheMap = make(map[string]FutureTicker, 10) return s } +func (s *SwapWs) connect() { + s.once.Do(func() { + s.c = s.wsBuilder.Build() + }) +} + func (s *SwapWs) DepthCallback(f func(depth *Depth)) { s.depthCall = f } @@ -74,6 +83,8 @@ func (s *SwapWs) TradeCallback(f func(trade *Trade, contract string)) { func (s *SwapWs) SubscribeDepth(pair CurrencyPair, contractType string) error { //{"op": "subscribe", "args": ["orderBook10:XBTUSD"]} + s.connect() + op := SubscribeOp{ Op: "subscribe", Args: []string{ @@ -84,6 +95,8 @@ func (s *SwapWs) SubscribeDepth(pair CurrencyPair, contractType string) error { } func (s *SwapWs) SubscribeTicker(pair CurrencyPair, contractType string) error { + s.connect() + return s.c.Subscribe(SubscribeOp{ Op: "subscribe", Args: []string{ From ae6b8292a9c10e0fd5c101cd44adabc8f22ee754 Mon Sep 17 00:00:00 2001 From: nntaoli <2767415655@qq.com> Date: Sat, 27 Feb 2021 21:29:57 +0800 Subject: [PATCH 3/5] [binance] ws delayed connect --- binance/FuturesWs.go | 29 +++++++++++++++++++++++------ binance/SpotWs.go | 18 +++++++++++++++--- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/binance/FuturesWs.go b/binance/FuturesWs.go index 31eb9da2..d5128cbe 100644 --- a/binance/FuturesWs.go +++ b/binance/FuturesWs.go @@ -10,13 +10,18 @@ import ( "os" "sort" "strings" + "sync" "time" ) type FuturesWs struct { - base *BinanceFutures - f *goex.WsConn - d *goex.WsConn + base *BinanceFutures + fOnce sync.Once + dOnce sync.Once + + wsBuilder *goex.WsBuilder + f *goex.WsConn + d *goex.WsConn depthCallFn func(depth *goex.Depth) tickerCallFn func(ticker *goex.FutureTicker) @@ -26,11 +31,9 @@ type FuturesWs struct { func NewFuturesWs() *FuturesWs { futuresWs := new(FuturesWs) - wsBuilder := goex.NewWsBuilder(). + futuresWs.wsBuilder = goex.NewWsBuilder(). ProxyUrl(os.Getenv("HTTPS_PROXY")). ProtoHandleFunc(futuresWs.handle).AutoReconnect() - futuresWs.f = wsBuilder.WsUrl("wss://fstream.binance.com/ws").Build() - futuresWs.d = wsBuilder.WsUrl("wss://dstream.binance.com/ws").Build() httpCli := &http.Client{ Timeout: 10 * time.Second, @@ -54,6 +57,18 @@ func NewFuturesWs() *FuturesWs { return futuresWs } +func (s *FuturesWs) connectUsdtFutures() { + s.fOnce.Do(func() { + s.f = s.wsBuilder.WsUrl("wss://fstream.binance.com/ws").Build() + }) +} + +func (s *FuturesWs) connectFutures() { + s.dOnce.Do(func() { + s.d = s.wsBuilder.WsUrl("wss://dstream.binance.com/ws").Build() + }) +} + func (s *FuturesWs) DepthCallback(f func(depth *goex.Depth)) { s.depthCallFn = f } @@ -88,12 +103,14 @@ func (s *FuturesWs) SubscribeDepth(pair goex.CurrencyPair, contractType string) func (s *FuturesWs) SubscribeTicker(pair goex.CurrencyPair, contractType string) error { switch contractType { case goex.SWAP_USDT_CONTRACT: + s.connectUsdtFutures() return s.f.Subscribe(req{ Method: "SUBSCRIBE", Params: []string{pair.AdaptUsdToUsdt().ToLower().ToSymbol("") + "@ticker"}, Id: 1, }) default: + s.connectFutures() sym, _ := s.base.adaptToSymbol(pair.AdaptUsdtToUsd(), contractType) return s.d.Subscribe(req{ Method: "SUBSCRIBE", diff --git a/binance/SpotWs.go b/binance/SpotWs.go index 47169cb2..ad33efe8 100644 --- a/binance/SpotWs.go +++ b/binance/SpotWs.go @@ -8,6 +8,7 @@ import ( "os" "sort" "strings" + "sync" "time" ) @@ -29,7 +30,9 @@ type depthResp struct { } type SpotWs struct { - c *goex.WsConn + c *goex.WsConn + once sync.Once + wsBuilder *goex.WsBuilder reqId int @@ -42,17 +45,22 @@ func NewSpotWs() *SpotWs { spotWs := &SpotWs{} logger.Debugf("proxy url: %s", os.Getenv("HTTPS_PROXY")) - wsBuilder := goex.NewWsBuilder(). + spotWs.wsBuilder = goex.NewWsBuilder(). WsUrl("wss://stream.binance.com:9443/stream?streams=depth/miniTicker/ticker/trade"). ProxyUrl(os.Getenv("HTTPS_PROXY")). ProtoHandleFunc(spotWs.handle).AutoReconnect() - spotWs.c = wsBuilder.Build() spotWs.reqId = 1 return spotWs } +func (s *SpotWs) connect() { + s.once.Do(func() { + s.c = s.wsBuilder.Build() + }) +} + func (s *SpotWs) DepthCallback(f func(depth *goex.Depth)) { s.depthCallFn = f } @@ -70,6 +78,8 @@ func (s *SpotWs) SubscribeDepth(pair goex.CurrencyPair) error { s.reqId++ }() + s.connect() + return s.c.Subscribe(req{ Method: "SUBSCRIBE", Params: []string{ @@ -84,6 +94,8 @@ func (s *SpotWs) SubscribeTicker(pair goex.CurrencyPair) error { s.reqId++ }() + s.connect() + return s.c.Subscribe(req{ Method: "SUBSCRIBE", Params: []string{pair.ToLower().ToSymbol("") + "@ticker"}, From 02204a50fdcfc76d136f17aa119d946cb198f4c2 Mon Sep 17 00:00:00 2001 From: nntaoli <2767415655@qq.com> Date: Sat, 27 Feb 2021 22:36:29 +0800 Subject: [PATCH 4/5] [huobi pro] delete annotation --- huobi/HuobiPro.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/huobi/HuobiPro.go b/huobi/HuobiPro.go index 5c450656..9c64c752 100644 --- a/huobi/HuobiPro.go +++ b/huobi/HuobiPro.go @@ -593,8 +593,6 @@ func (hbpro *HuoBiPro) GetKlineRecords(currency CurrencyPair, period KlinePeriod return klines, nil } -//非个人,整个交易所的交易记录 -//https://github.com/huobiapi/API_Docs/wiki/REST_api_reference#get-markettrade-获取-trade-detail-数据 func (hbpro *HuoBiPro) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error) { var ( trades []Trade From f4183aaa646d6f0f208d3dc8c22323cfff1f518f Mon Sep 17 00:00:00 2001 From: nntaoli <2767415655@qq.com> Date: Sun, 28 Feb 2021 19:51:45 +0800 Subject: [PATCH 5/5] [binance] calculate the order average price --- binance/Binance.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/binance/Binance.go b/binance/Binance.go index 01bbcdf8..e0c82615 100644 --- a/binance/Binance.go +++ b/binance/Binance.go @@ -651,15 +651,22 @@ func (bn *Binance) adaptOrder(currencyPair CurrencyPair, orderMap map[string]int orderSide = BUY } + quoteQty := ToFloat64(orderMap["cummulativeQuoteQty"]) + qty := ToFloat64(orderMap["executedQty"]) + avgPrice := 0.0 + if qty > 0 { + avgPrice = FloatToFixed(quoteQty/qty, 8) + } + return Order{ OrderID: ToInt(orderMap["orderId"]), - OrderID2: fmt.Sprintf("%.0f",orderMap["orderId"]), + OrderID2: fmt.Sprintf("%.0f", orderMap["orderId"]), Cid: orderMap["clientOrderId"].(string), Currency: currencyPair, Price: ToFloat64(orderMap["price"]), Amount: ToFloat64(orderMap["origQty"]), DealAmount: ToFloat64(orderMap["executedQty"]), - AvgPrice: FloatToFixed(ToFloat64(orderMap["cummulativeQuoteQty"])/ToFloat64(orderMap["executedQty"]), 8), + AvgPrice: avgPrice, Side: TradeSide(orderSide), Status: adaptOrderStatus(orderMap["status"].(string)), OrderTime: ToInt(orderMap["time"]),