Skip to content

Commit 3535522

Browse files
committed
Merge branch 'dev'
2 parents 466bffc + f4183aa commit 3535522

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

binance/Binance.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,15 +651,22 @@ func (bn *Binance) adaptOrder(currencyPair CurrencyPair, orderMap map[string]int
651651
orderSide = BUY
652652
}
653653

654+
quoteQty := ToFloat64(orderMap["cummulativeQuoteQty"])
655+
qty := ToFloat64(orderMap["executedQty"])
656+
avgPrice := 0.0
657+
if qty > 0 {
658+
avgPrice = FloatToFixed(quoteQty/qty, 8)
659+
}
660+
654661
return Order{
655662
OrderID: ToInt(orderMap["orderId"]),
656-
OrderID2: fmt.Sprintf("%.0f",orderMap["orderId"]),
663+
OrderID2: fmt.Sprintf("%.0f", orderMap["orderId"]),
657664
Cid: orderMap["clientOrderId"].(string),
658665
Currency: currencyPair,
659666
Price: ToFloat64(orderMap["price"]),
660667
Amount: ToFloat64(orderMap["origQty"]),
661668
DealAmount: ToFloat64(orderMap["executedQty"]),
662-
AvgPrice: FloatToFixed(ToFloat64(orderMap["cummulativeQuoteQty"])/ToFloat64(orderMap["executedQty"]), 8),
669+
AvgPrice: avgPrice,
663670
Side: TradeSide(orderSide),
664671
Status: adaptOrderStatus(orderMap["status"].(string)),
665672
OrderTime: ToInt(orderMap["time"]),

binance/FuturesWs.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ import (
1010
"os"
1111
"sort"
1212
"strings"
13+
"sync"
1314
"time"
1415
)
1516

1617
type FuturesWs struct {
17-
base *BinanceFutures
18-
f *goex.WsConn
19-
d *goex.WsConn
18+
base *BinanceFutures
19+
fOnce sync.Once
20+
dOnce sync.Once
21+
22+
wsBuilder *goex.WsBuilder
23+
f *goex.WsConn
24+
d *goex.WsConn
2025

2126
depthCallFn func(depth *goex.Depth)
2227
tickerCallFn func(ticker *goex.FutureTicker)
@@ -26,25 +31,44 @@ type FuturesWs struct {
2631
func NewFuturesWs() *FuturesWs {
2732
futuresWs := new(FuturesWs)
2833

29-
wsBuilder := goex.NewWsBuilder().
34+
futuresWs.wsBuilder = goex.NewWsBuilder().
3035
ProxyUrl(os.Getenv("HTTPS_PROXY")).
3136
ProtoHandleFunc(futuresWs.handle).AutoReconnect()
32-
futuresWs.f = wsBuilder.WsUrl("wss://fstream.binance.com/ws").Build()
33-
futuresWs.d = wsBuilder.WsUrl("wss://dstream.binance.com/ws").Build()
34-
futuresWs.base = NewBinanceFutures(&goex.APIConfig{
35-
HttpClient: &http.Client{
37+
38+
httpCli := &http.Client{
39+
Timeout: 10 * time.Second,
40+
}
41+
42+
if os.Getenv("HTTPS_PROXY") != "" {
43+
httpCli = &http.Client{
3644
Transport: &http.Transport{
3745
Proxy: func(r *http.Request) (*url.URL, error) {
3846
return url.Parse(os.Getenv("HTTPS_PROXY"))
3947
},
4048
},
4149
Timeout: 10 * time.Second,
42-
},
50+
}
51+
}
52+
53+
futuresWs.base = NewBinanceFutures(&goex.APIConfig{
54+
HttpClient: httpCli,
4355
})
4456

4557
return futuresWs
4658
}
4759

60+
func (s *FuturesWs) connectUsdtFutures() {
61+
s.fOnce.Do(func() {
62+
s.f = s.wsBuilder.WsUrl("wss://fstream.binance.com/ws").Build()
63+
})
64+
}
65+
66+
func (s *FuturesWs) connectFutures() {
67+
s.dOnce.Do(func() {
68+
s.d = s.wsBuilder.WsUrl("wss://dstream.binance.com/ws").Build()
69+
})
70+
}
71+
4872
func (s *FuturesWs) DepthCallback(f func(depth *goex.Depth)) {
4973
s.depthCallFn = f
5074
}
@@ -79,12 +103,14 @@ func (s *FuturesWs) SubscribeDepth(pair goex.CurrencyPair, contractType string)
79103
func (s *FuturesWs) SubscribeTicker(pair goex.CurrencyPair, contractType string) error {
80104
switch contractType {
81105
case goex.SWAP_USDT_CONTRACT:
106+
s.connectUsdtFutures()
82107
return s.f.Subscribe(req{
83108
Method: "SUBSCRIBE",
84109
Params: []string{pair.AdaptUsdToUsdt().ToLower().ToSymbol("") + "@ticker"},
85110
Id: 1,
86111
})
87112
default:
113+
s.connectFutures()
88114
sym, _ := s.base.adaptToSymbol(pair.AdaptUsdtToUsd(), contractType)
89115
return s.d.Subscribe(req{
90116
Method: "SUBSCRIBE",

binance/SpotWs.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"sort"
1010
"strings"
11+
"sync"
1112
"time"
1213
)
1314

@@ -29,7 +30,9 @@ type depthResp struct {
2930
}
3031

3132
type SpotWs struct {
32-
c *goex.WsConn
33+
c *goex.WsConn
34+
once sync.Once
35+
wsBuilder *goex.WsBuilder
3336

3437
reqId int
3538

@@ -42,17 +45,22 @@ func NewSpotWs() *SpotWs {
4245
spotWs := &SpotWs{}
4346
logger.Debugf("proxy url: %s", os.Getenv("HTTPS_PROXY"))
4447

45-
wsBuilder := goex.NewWsBuilder().
48+
spotWs.wsBuilder = goex.NewWsBuilder().
4649
WsUrl("wss://stream.binance.com:9443/stream?streams=depth/miniTicker/ticker/trade").
4750
ProxyUrl(os.Getenv("HTTPS_PROXY")).
4851
ProtoHandleFunc(spotWs.handle).AutoReconnect()
4952

50-
spotWs.c = wsBuilder.Build()
5153
spotWs.reqId = 1
5254

5355
return spotWs
5456
}
5557

58+
func (s *SpotWs) connect() {
59+
s.once.Do(func() {
60+
s.c = s.wsBuilder.Build()
61+
})
62+
}
63+
5664
func (s *SpotWs) DepthCallback(f func(depth *goex.Depth)) {
5765
s.depthCallFn = f
5866
}
@@ -70,6 +78,8 @@ func (s *SpotWs) SubscribeDepth(pair goex.CurrencyPair) error {
7078
s.reqId++
7179
}()
7280

81+
s.connect()
82+
7383
return s.c.Subscribe(req{
7484
Method: "SUBSCRIBE",
7585
Params: []string{
@@ -84,6 +94,8 @@ func (s *SpotWs) SubscribeTicker(pair goex.CurrencyPair) error {
8494
s.reqId++
8595
}()
8696

97+
s.connect()
98+
8799
return s.c.Subscribe(req{
88100
Method: "SUBSCRIBE",
89101
Params: []string{pair.ToLower().ToSymbol("") + "@ticker"},

bitmex/SwapWs.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
. "github.com/nntaoli-project/goex"
77
"github.com/nntaoli-project/goex/internal/logger"
88
"sort"
9+
"sync"
910
"time"
1011
)
1112

@@ -42,7 +43,9 @@ type depthData struct {
4243
}
4344

4445
type SwapWs struct {
45-
c *WsConn
46+
c *WsConn
47+
once sync.Once
48+
wsBuilder *WsBuilder
4649

4750
depthCall func(depth *Depth)
4851
tickerCall func(ticker *FutureTicker)
@@ -52,14 +55,20 @@ type SwapWs struct {
5255

5356
func NewSwapWs() *SwapWs {
5457
s := new(SwapWs)
55-
wsBuilder := NewWsBuilder().DisableEnableCompression().WsUrl("wss://www.bitmex.com/realtime")
56-
wsBuilder = wsBuilder.Heartbeat(func() []byte { return []byte("ping") }, 5*time.Second)
57-
wsBuilder = wsBuilder.ProtoHandleFunc(s.handle).AutoReconnect()
58-
s.c = wsBuilder.Build()
58+
s.wsBuilder = NewWsBuilder().DisableEnableCompression().WsUrl("wss://www.bitmex.com/realtime")
59+
s.wsBuilder = s.wsBuilder.Heartbeat(func() []byte { return []byte("ping") }, 5*time.Second)
60+
s.wsBuilder = s.wsBuilder.ProtoHandleFunc(s.handle).AutoReconnect()
61+
//s.c = wsBuilder.Build()
5962
s.tickerCacheMap = make(map[string]FutureTicker, 10)
6063
return s
6164
}
6265

66+
func (s *SwapWs) connect() {
67+
s.once.Do(func() {
68+
s.c = s.wsBuilder.Build()
69+
})
70+
}
71+
6372
func (s *SwapWs) DepthCallback(f func(depth *Depth)) {
6473
s.depthCall = f
6574
}
@@ -74,6 +83,8 @@ func (s *SwapWs) TradeCallback(f func(trade *Trade, contract string)) {
7483

7584
func (s *SwapWs) SubscribeDepth(pair CurrencyPair, contractType string) error {
7685
//{"op": "subscribe", "args": ["orderBook10:XBTUSD"]}
86+
s.connect()
87+
7788
op := SubscribeOp{
7889
Op: "subscribe",
7990
Args: []string{
@@ -84,6 +95,8 @@ func (s *SwapWs) SubscribeDepth(pair CurrencyPair, contractType string) error {
8495
}
8596

8697
func (s *SwapWs) SubscribeTicker(pair CurrencyPair, contractType string) error {
98+
s.connect()
99+
87100
return s.c.Subscribe(SubscribeOp{
88101
Op: "subscribe",
89102
Args: []string{

huobi/HuobiPro.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,6 @@ func (hbpro *HuoBiPro) GetKlineRecords(currency CurrencyPair, period KlinePeriod
593593
return klines, nil
594594
}
595595

596-
//非个人,整个交易所的交易记录
597-
//https://github.com/huobiapi/API_Docs/wiki/REST_api_reference#get-markettrade-获取-trade-detail-数据
598596
func (hbpro *HuoBiPro) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error) {
599597
var (
600598
trades []Trade

0 commit comments

Comments
 (0)