Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Feb 28, 2021
2 parents 466bffc + f4183aa commit 3535522
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 21 deletions.
11 changes: 9 additions & 2 deletions binance/Binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
44 changes: 35 additions & 9 deletions binance/FuturesWs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,25 +31,44 @@ 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()
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
}

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
}
Expand Down Expand Up @@ -79,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",
Expand Down
18 changes: 15 additions & 3 deletions binance/SpotWs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"sort"
"strings"
"sync"
"time"
)

Expand All @@ -29,7 +30,9 @@ type depthResp struct {
}

type SpotWs struct {
c *goex.WsConn
c *goex.WsConn
once sync.Once
wsBuilder *goex.WsBuilder

reqId int

Expand All @@ -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
}
Expand All @@ -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{
Expand All @@ -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"},
Expand Down
23 changes: 18 additions & 5 deletions bitmex/SwapWs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
. "github.com/nntaoli-project/goex"
"github.com/nntaoli-project/goex/internal/logger"
"sort"
"sync"
"time"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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{
Expand All @@ -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{
Expand Down
2 changes: 0 additions & 2 deletions huobi/HuobiPro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3535522

Please sign in to comment.