bybit-api
bybit-api copied to clipboard
Getting "readNumberAsString: invalid number" when getting trades from WebSocket
Hello, I am getting this error when subscribing to trades via websocket:
2022/09/12 19:16:34 BybitWs subscribeHandler
2022/09/12 19:16:35 BybitWs {"topic":"trade.BTCUSDT","data":[{"symbol":"BTCUSDT","tick_direction":"ZeroMinusTick","price":"22230.50","size":0.004,"timestamp":"2022-09-12T17:16:33.000Z","trade_time_ms":"1663002993247","side":"Sell","trade_id":"d6634491-3ffd-537b-8424-e800c5b36f1b","is_block_trade":"false"}]}
2022/09/12 19:16:35 BybitWs []*ws.Trade: ws.Trade.Price: readNumberAsString: invalid number, error found in #10 byte of ...|","price":"22230.50"|..., bigger context ...|BTCUSDT","tick_direction":"ZeroMinusTick","price":"22230.50","size":0.004,"timestamp":"2022-09-12T17|...
This is the code I am using to connect and subscribe to the topic:
cfg := &ws.Configuration{
Addr: "wss://stream.bybit.com/realtime_public",
ApiKey: "",
SecretKey: "",
AutoReconnect: true,
DebugMode: true,
}
b := ws.New(cfg)
b.Subscribe("trade.BTCUSDT")
b.On(ws.WSTrade, handleTrade)
b.Start()
forever := make(chan struct{})
<-forever
I am not sure if I am doing anything wrong here but it looks like the library for some reason is not able to unmarshal the bybit json to the Trade struct.
Hi.... I get into same error, after following the source code, I found that websocket for Linear contract returns some fields (prices, size) in wrong data type as defined in Struct. Check them and adjust as your convinience.
["price":"22230.50"|
Regards