Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Apr 26, 2020
1 parent 7fa3148 commit eda8235
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 8 deletions.
154 changes: 154 additions & 0 deletions exchanges/hbdm/orderbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package hbdm

import (
"fmt"
"github.com/MauriceGit/skiplist"
. "github.com/coinrust/crex"
"github.com/frankrap/huobi-api/hbdm"
)

type DobItem struct {
Price float64
Amount float64
}

func (e DobItem) ExtractKey() float64 {
return e.Price
}

func (e DobItem) String() string {
return fmt.Sprintf("%.2f", e.Price)
}

type DepthOrderBook struct {
symbol string
asks skiplist.SkipList
bids skiplist.SkipList
}

func (d *DepthOrderBook) GetSymbol() string {
return d.symbol
}

func (d *DepthOrderBook) Update(data *hbdm.WSDepthHF) {
if data.Tick.Event == "snapshot" {
d.asks = skiplist.New()
d.bids = skiplist.New()
for _, ask := range data.Tick.Asks {
d.asks.Insert(DobItem{
Price: ask[0],
Amount: ask[1],
})
}
for _, bid := range data.Tick.Bids {
d.bids.Insert(DobItem{
Price: bid[0],
Amount: bid[1],
})
}
return
}

if data.Tick.Event == "update" {
for _, ask := range data.Tick.Asks {
price := ask[0]
amount := ask[1]
if amount == 0 {
d.asks.Delete(DobItem{
Price: price,
Amount: amount,
})
} else {
item := DobItem{
Price: price,
Amount: amount,
}
elem, ok := d.asks.Find(item)
if ok {
d.asks.ChangeValue(elem, item)
} else {
d.asks.Insert(item)
}
}
}
for _, bid := range data.Tick.Bids {
price := bid[0]
amount := bid[1]
if amount == 0 {
d.bids.Delete(DobItem{
Price: price,
Amount: amount,
})
} else {
item := DobItem{
Price: price,
Amount: amount,
}
elem, ok := d.bids.Find(item)
if ok {
d.bids.ChangeValue(elem, item)
} else {
d.bids.Insert(item)
}
}
}
}
}

func (d *DepthOrderBook) GetOrderBook(depth int) (result OrderBook) {
result.Symbol = d.symbol
smallest := d.asks.GetSmallestNode()
if smallest != nil {
item := smallest.GetValue().(DobItem)
result.Asks = append(result.Asks, Item{
Price: item.Price,
Amount: item.Amount,
})
count := 1
node := smallest
for count < depth {
node = d.asks.Next(node)
if node == nil {
break
}
item := node.GetValue().(DobItem)
result.Asks = append(result.Asks, Item{
Price: item.Price,
Amount: item.Amount,
})
count++
}
}

largest := d.bids.GetLargestNode()
if largest != nil {
item := largest.GetValue().(DobItem)
result.Bids = append(result.Bids, Item{
Price: item.Price,
Amount: item.Amount,
})
count := 1
node := largest
for count < depth {
node = d.bids.Prev(node)
if node == nil {
break
}
item := node.GetValue().(DobItem)
result.Bids = append(result.Bids, Item{
Price: item.Price,
Amount: item.Amount,
})
count++
}
}
return
}

func NewDepthOrderBook(symbol string) *DepthOrderBook {
return &DepthOrderBook{
symbol: symbol,
asks: skiplist.New(),
bids: skiplist.New(),
}
}
29 changes: 26 additions & 3 deletions exchanges/hbdm/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type HbdmWebSocket struct {
ws *hbdm.WS
nws *hbdm.NWS
dobMap map[string]*DepthOrderBook
emitter *emission.Emitter
}

Expand All @@ -24,8 +25,9 @@ func (s *HbdmWebSocket) SubscribeTrades(symbol string, contractType string, call

func (s *HbdmWebSocket) SubscribeLevel2Snapshots(symbol string, contractType string, callback func(ob *OrderBook)) error {
s.emitter.On(WSEventL2Snapshot, callback)
s.ws.SubscribeDepth("depth_1",
s.convertToSymbol(symbol, contractType))
//s.ws.SubscribeDepth("depth_1",
// s.convertToSymbol(symbol, contractType))
s.ws.SubscribeDepthHF("depth_1", s.convertToSymbol(symbol, contractType), 20, "incremental")
return nil
}

Expand Down Expand Up @@ -84,6 +86,22 @@ func (s *HbdmWebSocket) depthCallback(depth *hbdm.WSDepth) {
s.emitter.Emit(WSEventL2Snapshot, ob)
}

func (s *HbdmWebSocket) depthHFCallback(depth *hbdm.WSDepthHF) {
// ch: market.BTC_CQ.depth.size_20.high_freq
symbol := depth.Ch
if v, ok := s.dobMap[symbol]; ok {
v.Update(depth)
ob := v.GetOrderBook(20)
s.emitter.Emit(WSEventL2Snapshot, &ob)
} else {
dob := NewDepthOrderBook(symbol)
dob.Update(depth)
s.dobMap[symbol] = dob
ob := dob.GetOrderBook(20)
s.emitter.Emit(WSEventL2Snapshot, &ob)
}
}

func (s *HbdmWebSocket) tradeCallback(trade *hbdm.WSTrade) {
// log.Printf("tradeCallback")
var trades []Trade
Expand Down Expand Up @@ -184,14 +202,19 @@ func (s *HbdmWebSocket) positionsCallback(positions *hbdm.WSPositions) {

func NewHbdmWebSocket(params *Parameters) *HbdmWebSocket {
wsURL := "wss://api.hbdm.com/ws"
if params.WsURL != "" {
wsURL = params.WsURL
}
s := &HbdmWebSocket{
dobMap: make(map[string]*DepthOrderBook),
emitter: emission.NewEmitter(),
}
ws := hbdm.NewWS(wsURL, "", "")
ws := hbdm.NewWS(wsURL, "", "", params.DebugMode)
if params.ProxyURL != "" {
ws.SetProxy(params.ProxyURL)
}
ws.SetDepthCallback(s.depthCallback)
ws.SetDepthHFCallback(s.depthHFCallback)
ws.SetTradeCallback(s.tradeCallback)
ws.Start()
s.ws = ws
Expand Down
1 change: 1 addition & 0 deletions exchanges/hbdm/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func testWebSocket() *HbdmWebSocket {
params.SecretKey = testConfig.SecretKey
params.ProxyURL = testConfig.ProxyURL
params.Testnet = testConfig.Testnet
params.WsURL = "wss://api.btcgateway.pro/ws"
ws := NewHbdmWebSocket(params)
return ws
}
Expand Down
154 changes: 154 additions & 0 deletions exchanges/hbdmswap/orderbook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package hbdmswap

import (
"fmt"
"github.com/MauriceGit/skiplist"
. "github.com/coinrust/crex"
"github.com/frankrap/huobi-api/hbdmswap"
)

type DobItem struct {
Price float64
Amount float64
}

func (e DobItem) ExtractKey() float64 {
return e.Price
}

func (e DobItem) String() string {
return fmt.Sprintf("%.2f", e.Price)
}

type DepthOrderBook struct {
symbol string
asks skiplist.SkipList
bids skiplist.SkipList
}

func (d *DepthOrderBook) GetSymbol() string {
return d.symbol
}

func (d *DepthOrderBook) Update(data *hbdmswap.WSDepthHF) {
if data.Tick.Event == "snapshot" {
d.asks = skiplist.New()
d.bids = skiplist.New()
for _, ask := range data.Tick.Asks {
d.asks.Insert(DobItem{
Price: ask[0],
Amount: ask[1],
})
}
for _, bid := range data.Tick.Bids {
d.bids.Insert(DobItem{
Price: bid[0],
Amount: bid[1],
})
}
return
}

if data.Tick.Event == "update" {
for _, ask := range data.Tick.Asks {
price := ask[0]
amount := ask[1]
if amount == 0 {
d.asks.Delete(DobItem{
Price: price,
Amount: amount,
})
} else {
item := DobItem{
Price: price,
Amount: amount,
}
elem, ok := d.asks.Find(item)
if ok {
d.asks.ChangeValue(elem, item)
} else {
d.asks.Insert(item)
}
}
}
for _, bid := range data.Tick.Bids {
price := bid[0]
amount := bid[1]
if amount == 0 {
d.bids.Delete(DobItem{
Price: price,
Amount: amount,
})
} else {
item := DobItem{
Price: price,
Amount: amount,
}
elem, ok := d.bids.Find(item)
if ok {
d.bids.ChangeValue(elem, item)
} else {
d.bids.Insert(item)
}
}
}
}
}

func (d *DepthOrderBook) GetOrderBook(depth int) (result OrderBook) {
result.Symbol = d.symbol
smallest := d.asks.GetSmallestNode()
if smallest != nil {
item := smallest.GetValue().(DobItem)
result.Asks = append(result.Asks, Item{
Price: item.Price,
Amount: item.Amount,
})
count := 1
node := smallest
for count < depth {
node = d.asks.Next(node)
if node == nil {
break
}
item := node.GetValue().(DobItem)
result.Asks = append(result.Asks, Item{
Price: item.Price,
Amount: item.Amount,
})
count++
}
}

largest := d.bids.GetLargestNode()
if largest != nil {
item := largest.GetValue().(DobItem)
result.Bids = append(result.Bids, Item{
Price: item.Price,
Amount: item.Amount,
})
count := 1
node := largest
for count < depth {
node = d.bids.Prev(node)
if node == nil {
break
}
item := node.GetValue().(DobItem)
result.Bids = append(result.Bids, Item{
Price: item.Price,
Amount: item.Amount,
})
count++
}
}
return
}

func NewDepthOrderBook(symbol string) *DepthOrderBook {
return &DepthOrderBook{
symbol: symbol,
asks: skiplist.New(),
bids: skiplist.New(),
}
}
Loading

0 comments on commit eda8235

Please sign in to comment.