代码拉取完成,页面将自动刷新
package hbdmswap
import (
"fmt"
. "gitee.com/378077287/exchanges"
"github.com/MauriceGit/skiplist"
"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(),
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。