mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* Initial commit setting up a map orderbook system with a buffer. It will write to the buffer, sort apply to main orderbook and then process. * Moves namespaces again * Updates orderbook to use a sweet new WebsocketOrderbookUpdate type to handle all updates whether its using ID or not. So good. Adds many tests * Starting to implement orderbook update handling per exchange. Updates namespaces again. Hopefuylly will find a way to update via ID not timestamp, too many endpoints dont provide update timestamps * Changes orderbookbuffer to use BufferUpdate type instead of orderbook.Base to achieve more functionality and no need for type conversion functions. Updates tests * Updates all instances of ws.orderbook.Update. Simplifies some orderbook logic * Introduces toggleable buffer. Renames orderbooks. Completes implementation for everywhere but OKGroup due to hash calculation * Implements orderbook update for okgroup, but forgets about the orderbook hash checking * Fixes okgroup checksum calculation. Fixes linting issue. Removes redundant Kraken tests. * Introduces sorting toggle and separates from buffer toggle. Uses benchmarks to highlight performance gains * Fixes Gemini rate limit and parsing. Removes comments and fixes typos * Fixes bitfinex orderbook processing * Inbuilt sorting, minor fixes for websocket implementations. Improves test coverage * Adds surprise LakeBTC websocket support * Fixes data race * Fixes rebasing issues due to namespace movements * Addresses PR nits: moves folder namespace from ws to websocket. Removes line spaces in imports. Fixes lakebtc websocket returns and defer fucntions. Fixes comments * Adds poloniex orderook sorting support * Enables bitstamp and hitbtc orderbook sorting. Fixes poloniex's sorting * Renames namespaces and combines monitor and connection into wshandler. Removes unused SPOT const. Changes how orderbook stuff is loaded. It is done in startup with a setup. Removes exchange name from loadsnapshot as well * Removes the connection.go from rebasing issues. Removes error response from functions used in goroutines * Fixes test with exchange name output change * Fixes issues where copy and paste and replace all were used poorly
219 lines
5.3 KiB
Go
219 lines
5.3 KiB
Go
package btse
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/websocket/wshandler"
|
|
log "github.com/thrasher-corp/gocryptotrader/logger"
|
|
)
|
|
|
|
const (
|
|
btseWebsocket = "wss://ws.btse.com/api/ws-feed"
|
|
)
|
|
|
|
// WsConnect connects the websocket client
|
|
func (b *BTSE) WsConnect() error {
|
|
if !b.Websocket.IsEnabled() || !b.IsEnabled() {
|
|
return errors.New(wshandler.WebsocketNotEnabled)
|
|
}
|
|
var dialer websocket.Dialer
|
|
err := b.WebsocketConn.Dial(&dialer, http.Header{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
go b.WsHandleData()
|
|
b.GenerateDefaultSubscriptions()
|
|
|
|
return nil
|
|
}
|
|
|
|
// WsHandleData handles read data from websocket connection
|
|
func (b *BTSE) WsHandleData() {
|
|
b.Websocket.Wg.Add(1)
|
|
|
|
defer func() {
|
|
b.Websocket.Wg.Done()
|
|
}()
|
|
|
|
for {
|
|
select {
|
|
case <-b.Websocket.ShutdownC:
|
|
return
|
|
|
|
default:
|
|
resp, err := b.WebsocketConn.ReadMessage()
|
|
if err != nil {
|
|
b.Websocket.DataHandler <- err
|
|
return
|
|
}
|
|
b.Websocket.TrafficAlert <- struct{}{}
|
|
|
|
type MsgType struct {
|
|
Type string `json:"type"`
|
|
ProductID string `json:"product_id"`
|
|
}
|
|
|
|
if strings.Contains(string(resp.Raw), "connect success") {
|
|
if b.Verbose {
|
|
log.Debugf("%s websocket client successfully connected to %s",
|
|
b.Name, b.Websocket.GetWebsocketURL())
|
|
}
|
|
continue
|
|
}
|
|
|
|
msgType := MsgType{}
|
|
err = common.JSONDecode(resp.Raw, &msgType)
|
|
if err != nil {
|
|
b.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
switch msgType.Type {
|
|
case "ticker":
|
|
var t wsTicker
|
|
err = common.JSONDecode(resp.Raw, &t)
|
|
if err != nil {
|
|
b.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
p := strings.Replace(t.Price.(string), ",", "", -1)
|
|
price, err := strconv.ParseFloat(p, 64)
|
|
if err != nil {
|
|
b.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
|
|
b.Websocket.DataHandler <- wshandler.TickerData{
|
|
Timestamp: time.Now(),
|
|
Pair: currency.NewPairDelimiter(t.ProductID, "-"),
|
|
AssetType: orderbook.Spot,
|
|
Exchange: b.GetName(),
|
|
OpenPrice: price,
|
|
}
|
|
case "snapshot":
|
|
snapshot := websocketOrderbookSnapshot{}
|
|
err := common.JSONDecode(resp.Raw, &snapshot)
|
|
if err != nil {
|
|
b.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
|
|
err = b.wsProcessSnapshot(&snapshot)
|
|
if err != nil {
|
|
b.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ProcessSnapshot processes the initial orderbook snap shot
|
|
func (b *BTSE) wsProcessSnapshot(snapshot *websocketOrderbookSnapshot) error {
|
|
var base orderbook.Base
|
|
for i := range snapshot.Bids {
|
|
p := strings.Replace(snapshot.Bids[i][0].(string), ",", "", -1)
|
|
price, err := strconv.ParseFloat(p, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
a := strings.Replace(snapshot.Bids[i][1].(string), ",", "", -1)
|
|
amount, err := strconv.ParseFloat(a, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
base.Bids = append(base.Bids,
|
|
orderbook.Item{Price: price, Amount: amount})
|
|
}
|
|
|
|
for i := range snapshot.Asks {
|
|
p := strings.Replace(snapshot.Asks[i][0].(string), ",", "", -1)
|
|
price, err := strconv.ParseFloat(p, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
a := strings.Replace(snapshot.Asks[i][1].(string), ",", "", -1)
|
|
amount, err := strconv.ParseFloat(a, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
base.Asks = append(base.Asks,
|
|
orderbook.Item{Price: price, Amount: amount})
|
|
}
|
|
|
|
p := currency.NewPairDelimiter(snapshot.ProductID, "-")
|
|
base.AssetType = orderbook.Spot
|
|
base.Pair = p
|
|
base.LastUpdated = time.Now()
|
|
base.ExchangeName = b.Name
|
|
|
|
err := b.Websocket.Orderbook.LoadSnapshot(&base, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
b.Websocket.DataHandler <- wshandler.WebsocketOrderbookUpdate{
|
|
Pair: p,
|
|
Asset: orderbook.Spot,
|
|
Exchange: b.GetName(),
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions()
|
|
func (b *BTSE) GenerateDefaultSubscriptions() {
|
|
var channels = []string{"snapshot", "ticker"}
|
|
enabledCurrencies := b.GetEnabledCurrencies()
|
|
var subscriptions []wshandler.WebsocketChannelSubscription
|
|
for i := range channels {
|
|
for j := range enabledCurrencies {
|
|
subscriptions = append(subscriptions, wshandler.WebsocketChannelSubscription{
|
|
Channel: channels[i],
|
|
Currency: enabledCurrencies[j],
|
|
})
|
|
}
|
|
}
|
|
b.Websocket.SubscribeToChannels(subscriptions)
|
|
}
|
|
|
|
// Subscribe sends a websocket message to receive data from the channel
|
|
func (b *BTSE) Subscribe(channelToSubscribe wshandler.WebsocketChannelSubscription) error {
|
|
subscribe := websocketSubscribe{
|
|
Type: "subscribe",
|
|
Channels: []websocketChannel{
|
|
{
|
|
Name: channelToSubscribe.Channel,
|
|
ProductIDs: []string{channelToSubscribe.Currency.String()},
|
|
},
|
|
},
|
|
}
|
|
return b.WebsocketConn.SendMessage(subscribe)
|
|
}
|
|
|
|
// Unsubscribe sends a websocket message to stop receiving data from the channel
|
|
func (b *BTSE) Unsubscribe(channelToSubscribe wshandler.WebsocketChannelSubscription) error {
|
|
subscribe := websocketSubscribe{
|
|
Type: "unsubscribe",
|
|
Channels: []websocketChannel{
|
|
{
|
|
Name: channelToSubscribe.Channel,
|
|
ProductIDs: []string{channelToSubscribe.Currency.String()},
|
|
},
|
|
},
|
|
}
|
|
return b.WebsocketConn.SendMessage(subscribe)
|
|
}
|