Skip to content

Commit

Permalink
fixup! Bybit: Fix WS ticker processing
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed May 15, 2024
1 parent 7ffea29 commit 720061c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 64 deletions.
169 changes: 121 additions & 48 deletions exchanges/bybit/bybit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3112,71 +3112,144 @@ func TestWsTicker(t *testing.T) {
t.Parallel()
n := new(Bybit)
testexch.UpdatePairsOnce(t, b)
assetRouting := []asset.Item{asset.Spot, asset.USDTMarginedFutures, asset.Options, asset.USDTMarginedFutures}
assetRouting := []asset.Item{
asset.Spot, asset.Options, asset.USDTMarginedFutures, asset.USDTMarginedFutures,
asset.USDCMarginedFutures, asset.USDCMarginedFutures, asset.CoinMarginedFutures, asset.CoinMarginedFutures,
}
sharedtestvalues.TestFixtureToDataHandler(t, b, n, "testdata/wsTicker.json", func(r []byte) error {
defer slices.Delete(assetRouting, 0, 1)
return n.wsHandleData(assetRouting[0], r)
})
close(n.Websocket.DataHandler)
require.Len(t, n.Websocket.DataHandler, 4, "Should see 4 tickers distributed")
expected := 8
require.Len(t, n.Websocket.DataHandler, expected, "Should see correct number of tickers")
for resp := range n.Websocket.DataHandler {
switch v := resp.(type) {
case *ticker.Price:
assert.Equal(t, n.Name, v.ExchangeName, "ExchangeName should be correct")
switch len(n.Websocket.DataHandler) {
case 3:
switch expected - len(n.Websocket.DataHandler) {
case 1: // Spot
assert.Equal(t, currency.BTC, v.Pair.Base, "Pair base should be correct")
assert.Equal(t, currency.USDT, v.Pair.Quote, "Pair quote should be correct")
assert.Equal(t, 21109.77, v.Last, "Last should be correct")
assert.Equal(t, 21426.99, v.High, "High should be correct")
assert.Equal(t, 20575.00, v.Low, "Low should be correct")
assert.Equal(t, 6780.866843, v.Volume, "Volume should be correct")
assert.Equal(t, "BTC_USDT", v.Pair.String(), "Pair should be correct")
assert.Equal(t, asset.Spot, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 233366401, v.LastUpdated.Unix(), "LastUpdated should be correct")
case 2:
assert.Equal(t, 17216.00, v.Last, "Last should be correct")
assert.Equal(t, 17281.50, v.High, "High should be correct")
assert.Equal(t, 16915.00, v.Low, "Low should be correct")
assert.Equal(t, 91705.276, v.Volume, "Volume should be correct")
assert.Equal(t, 17215.50, v.Bid, "Bid should be correct")
assert.Equal(t, 84.489, v.BidSize, "BidSize should be correct")
assert.Equal(t, 17216.00, v.Ask, "Ask should be correct")
assert.Equal(t, 83.020, v.AskSize, "AskSize should be correct")
assert.Equal(t, 17217.33, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 17227.36, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 68744.761, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, "BTC_USDT", v.Pair.String(), "Pair should be correct")
assert.Equal(t, asset.USDTMarginedFutures, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 233366402, v.LastUpdated.Unix(), "LastUpdated should be correct")
case 1:
assert.Equal(t, 10.00, v.Last, "Last should be correct")
assert.Equal(t, 25.0, v.High, "High should be correct")
assert.Equal(t, 5.00, v.Low, "Low should be correct")
assert.Equal(t, 26.55, v.Volume, "Volume should be correct")
assert.Equal(t, 7.00, v.Bid, "Bid should be correct")
assert.Equal(t, 4.0, v.BidSize, "BidSize should be correct")
assert.Equal(t, 11.00, v.Ask, "Ask should be correct")
assert.Equal(t, 5.10, v.AskSize, "AskSize should be correct")
assert.Equal(t, 7.86976724, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 16823.73, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 49.85, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, "BTC-USD-220930-28000-P", v.Pair.String(), "Pair should be correct")
assert.EqualValues(t, 233366401000, v.LastUpdated.UnixMilli(), "LastUpdated should be correct")

Check failure on line 3140 in exchanges/bybit/bybit_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

cannot use 233366401000 (untyped int constant) as int value in argument to assert.EqualValues (overflows)
case 2: // Option
assert.Equal(t, currency.BTC, v.Pair.Base, "Pair base should be correct")
assert.Equal(t, 3565.00, v.Last, "Last should be correct")
assert.Equal(t, 3715.00, v.High, "High should be correct")
assert.Equal(t, 3555.00, v.Low, "Low should be correct")
assert.Equal(t, 1.62, v.Volume, "Volume should be correct")
assert.Equal(t, 3475.00, v.Bid, "Bid should be correct")
assert.Equal(t, 10.14, v.BidSize, "BidSize should be correct")
assert.Equal(t, 3520.00, v.Ask, "Ask should be correct")
assert.Equal(t, 2.5, v.AskSize, "AskSize should be correct")
assert.Equal(t, 3502.0715721, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 61912.8, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 29.35, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, "BTC-28JUN24-60000-P", v.Pair.String(), "Pair should be correct")
assert.Equal(t, asset.Options, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 233366403, v.LastUpdated.Unix(), "LastUpdated should be correct")
case 0:
assert.Equal(t, 17220.00, v.Last, "Last should be correct")
assert.Equal(t, 17304.99, v.High, "High should be correct")
assert.Equal(t, 16910.01, v.Low, "Low should be correct")
assert.Equal(t, 92705.276, v.Volume, "Volume should be correct")
assert.Equal(t, 17216.50, v.Bid, "Bid should be correct")
assert.Equal(t, 80.289, v.BidSize, "BidSize should be correct")
assert.Equal(t, 17220.04, v.Ask, "Ask should be correct")
assert.Equal(t, 94.200, v.AskSize, "AskSize should be correct")
assert.Equal(t, 17218.12, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 17218.14, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 68750.881, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, "BTC_USDT", v.Pair.String(), "Pair should be correct")
assert.EqualValues(t, 1715742949283, v.LastUpdated.UnixMilli(), "LastUpdated should be correct")

Check failure on line 3156 in exchanges/bybit/bybit_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

cannot use 1715742949283 (untyped int constant) as int value in argument to assert.EqualValues (overflows)
case 3: // USDTMargined snapshot
assert.Equal(t, currency.BTC, v.Pair.Base, "Pair base should be correct")
assert.Equal(t, currency.USDT, v.Pair.Quote, "Pair quote should be correct")
assert.Equal(t, 61874.00, v.Last, "Last should be correct")
assert.Equal(t, 62752.90, v.High, "High should be correct")
assert.Equal(t, 61000.10, v.Low, "Low should be correct")
assert.Equal(t, 98430.1050, v.Volume, "Volume should be correct")
assert.Equal(t, 61873.9, v.Bid, "Bid should be correct")
assert.Equal(t, 3.783, v.BidSize, "BidSize should be correct")
assert.Equal(t, 61874.00, v.Ask, "Ask should be correct")
assert.Equal(t, 16.278, v.AskSize, "AskSize should be correct")
assert.Equal(t, 61875.25, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 61903.73, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 58117.022, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, asset.USDTMarginedFutures, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 1715748762463, v.LastUpdated.UnixMilli(), "LastUpdated should be correct")

Check failure on line 3172 in exchanges/bybit/bybit_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

cannot use 1715748762463 (untyped int constant) as int value in argument to assert.EqualValues (overflows)
case 4: // USDTMargined partial
assert.Equal(t, currency.BTC, v.Pair.Base, "Pair base should be correct")
assert.Equal(t, currency.USDT, v.Pair.Quote, "Pair quote should be correct")
assert.Equal(t, 61874.00, v.Last, "Last should be correct")
assert.Equal(t, 62752.90, v.High, "High should be correct")
assert.Equal(t, 61000.10, v.Low, "Low should be correct")
assert.Equal(t, 98430.1050, v.Volume, "Volume should be correct")
assert.Equal(t, 61873.90, v.Bid, "Bid should be correct")
assert.Equal(t, 3.543, v.BidSize, "BidSize should be correct")
assert.Equal(t, 61874.00, v.Ask, "Ask should be correct")
assert.Equal(t, 16.278, v.AskSize, "AskSize should be correct")
assert.Equal(t, 61875.06, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 61903.59, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 58117.022, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, asset.USDTMarginedFutures, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 233366404, v.LastUpdated.Unix(), "LastUpdated should be correct")
assert.EqualValues(t, 1715748763063, v.LastUpdated.UnixMilli(), "LastUpdated should be correct")

Check failure on line 3188 in exchanges/bybit/bybit_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

cannot use 1715748763063 (untyped int constant) as int value in argument to assert.EqualValues (overflows)
case 5: // USDCMargined snapshot
assert.Equal(t, currency.BTC, v.Pair.Base, "Pair base should be correct")
assert.Equal(t, currency.PERP, v.Pair.Quote, "Pair quote should be correct")
assert.Equal(t, 61945.70, v.Last, "Last should be correct")
assert.Equal(t, 62242.2, v.High, "High should be correct")
assert.Equal(t, 61059.1, v.Low, "Low should be correct")
assert.Equal(t, 427.375, v.Volume, "Volume should be correct")
assert.Equal(t, 61909.2, v.Bid, "Bid should be correct")
assert.Equal(t, 0.035, v.BidSize, "BidSize should be correct")
assert.Equal(t, 61909.60, v.Ask, "Ask should be correct")
assert.Equal(t, 0.082, v.AskSize, "AskSize should be correct")
assert.Equal(t, 61943.58, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 61942.85, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 526.806, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, asset.USDCMarginedFutures, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 1715756612118, v.LastUpdated.UnixMilli(), "LastUpdated should be correct")

Check failure on line 3204 in exchanges/bybit/bybit_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

cannot use 1715756612118 (untyped int constant) as int value in argument to assert.EqualValues (overflows)
case 6: // USDCMargined partial
assert.Equal(t, currency.BTC, v.Pair.Base, "Pair base should be correct")
assert.Equal(t, currency.PERP, v.Pair.Quote, "Pair quote should be correct")
assert.Equal(t, 61945.70, v.Last, "Last should be correct")
assert.Equal(t, 62242.2, v.High, "High should be correct")
assert.Equal(t, 61059.1, v.Low, "Low should be correct")
assert.Equal(t, 427.375, v.Volume, "Volume should be correct")
assert.Equal(t, 61909.5, v.Bid, "Bid should be correct")
assert.Equal(t, 0.035, v.BidSize, "BidSize should be correct")
assert.Equal(t, 61909.60, v.Ask, "Ask should be correct")
assert.Equal(t, 0.082, v.AskSize, "AskSize should be correct")
assert.Equal(t, 61943.58, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 61942.85, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 526.806, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, asset.USDCMarginedFutures, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 171575661221, v.LastUpdated.UnixMilli(), "LastUpdated should be correct")

Check failure on line 3220 in exchanges/bybit/bybit_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

cannot use 171575661221 (untyped int constant) as int value in argument to assert.EqualValues (overflows)
case 7: // CoinMargined snapshot
assert.Equal(t, currency.BTC, v.Pair.Base, "Pair base should be correct")
assert.Equal(t, currency.USD, v.Pair.Quote, "Pair quote should be correct")
assert.Equal(t, 61894.0, v.Last, "Last should be correct")
assert.Equal(t, 62265.5, v.High, "High should be correct")
assert.Equal(t, 61029.5, v.Low, "Low should be correct")
assert.Equal(t, 391976479.0, v.Volume, "Volume should be correct")
assert.Equal(t, 61891.5, v.Bid, "Bid should be correct")
assert.Equal(t, 12667.0, v.BidSize, "BidSize should be correct")
assert.Equal(t, 61892.0, v.Ask, "Ask should be correct")
assert.Equal(t, 60953.0, v.AskSize, "AskSize should be correct")
assert.Equal(t, 61894.0, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 61923.36, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 931760496.0, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, asset.CoinMarginedFutures, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 1715757637952, v.LastUpdated.UnixMilli(), "LastUpdated should be correct")

Check failure on line 3236 in exchanges/bybit/bybit_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

cannot use 1715757637952 (untyped int constant) as int value in argument to assert.EqualValues (overflows)
case 8: // CoinMargined partial
assert.Equal(t, currency.BTC, v.Pair.Base, "Pair base should be correct")
assert.Equal(t, currency.USD, v.Pair.Quote, "Pair quote should be correct")
assert.Equal(t, 61894.0, v.Last, "Last should be correct")
assert.Equal(t, 62265.5, v.High, "High should be correct")
assert.Equal(t, 61029.5, v.Low, "Low should be correct")
assert.Equal(t, 391976479.0, v.Volume, "Volume should be correct")
assert.Equal(t, 61891.5, v.Bid, "Bid should be correct")
assert.Equal(t, 27634.0, v.BidSize, "BidSize should be correct")
assert.Equal(t, 61892.0, v.Ask, "Ask should be correct")
assert.Equal(t, 60953.0, v.AskSize, "AskSize should be correct")
assert.Equal(t, 61894.0, v.MarkPrice, "MarkPrice should be correct")
assert.Equal(t, 61923.36, v.IndexPrice, "IndexPrice should be correct")
assert.Equal(t, 931760496.0, v.OpenInterest, "OpenInterest should be correct")
assert.Equal(t, asset.CoinMarginedFutures, v.AssetType, "AssetType should be correct")
assert.EqualValues(t, 1715757638152, v.LastUpdated.UnixMilli(), "LastUpdated should be correct")

Check failure on line 3252 in exchanges/bybit/bybit_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

cannot use 1715757638152 (untyped int constant) as int value in argument to assert.EqualValues (overflows)
}
case error:
t.Error(v)
Expand Down
10 changes: 7 additions & 3 deletions exchanges/bybit/testdata/wsTicker.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{"topic":"tickers.BTC-USDT","type":"snapshot","ts":233366401000,"cs":2588407389,"data":{"symbol":"BTCUSDT","lastPrice":"21109.77","highPrice24h":"21426.99","lowPrice24h":"20575","prevPrice24h":"20704.93","volume24h":"6780.866843","turnover24h":"141946527.22907118","price24hPcnt":"0.0196","usdIndexPrice":"21120.2400136"}}
{"topic":"tickers.BTC_USDT","type":"snapshot","cs":24987956059,"ts":233366402000,"data":{"symbol":"BTCUSDT","tickDirection":"PlusTick","price24hPcnt":"0.017103","lastPrice":"17216.00","prevPrice24h":"16926.50","highPrice24h":"17281.50","lowPrice24h":"16915.00","prevPrice1h":"17238.00","markPrice":"17217.33","indexPrice":"17227.36","openInterest":"68744.761","openInterestValue":"1183601235.91","turnover24h":"1570383121.943499","volume24h":"91705.276","nextFundingTime":"1673280000000","fundingRate":"-0.000212","bid1Price":"17215.50","bid1Size":"84.489","ask1Price":"17216.00","ask1Size":"83.020"}}
{"topic":"tickers.BTC-6JAN23-17500-C","type":"snapshot","ts":233366403000,"data":{"symbol":"BTC-USD-220930-28000-P","bidPrice":"7","bidSize":"4","bidIv":"0","askPrice":"11","askSize":"5.1","askIv":"0.514","lastPrice":"10","highPrice24h":"25","lowPrice24h":"5","markPrice":"7.86976724","indexPrice":"16823.73","markPriceIv":"0.4896","underlyingPrice":"16815.1","openInterest":"49.85","turnover24h":"446802.8473","volume24h":"26.55","totalVolume":"86","totalTurnover":"1437431","delta":"0.047831","gamma":"0.00021453","vega":"0.81351067","theta":"-19.9115368","predictedDeliveryPrice":"0","change24h":"-0.33333334"},"id":"tickers.BTC-6JAN23-17500-C-2480334983-1672917511074"}
{"topic":"tickers.BTC_USDT","type":"delta","cs":24987956060,"ts":233366404000,"data":{"symbol":"BTC_USDT","tickDirection":"PlusTick","price24hPcnt":"0.017105","lastPrice":"17220.00","prevPrice24h":"17215.00","highPrice24h":"17304.99","lowPrice24h":"16910.01","prevPrice1h":"17244.00","markPrice":"17218.12","indexPrice":"17218.14","openInterest":"68750.881","openInterestValue":"1199601235.91","turnover24h":"1680383121.943499","volume24h":"92705.276","nextFundingTime":"1673290000000","fundingRate":"-0.000214","bid1Price":"17216.50","bid1Size":"80.289","ask1Price":"17220.04","ask1Size":"94.200"}}
{"topic":"tickers.BTC-28JUN24-60000-P","ts":1715742949283,"type":"snapshot","id":"tickers.BTC-28JUN24-60000-P-6222585724-1715742949283","data":{"symbol":"BTC-28JUN24-60000-P","bidPrice":"3475","bidSize":"10.14","bidIv":"0.5479","askPrice":"3520","askSize":"2.5","askIv":"0.5534","lastPrice":"3565","highPrice24h":"3715","lowPrice24h":"3555","markPrice":"3502.0715721","indexPrice":"61912.8","markPriceIv":"0.5513","underlyingPrice":"62588.50709846","openInterest":"29.35","turnover24h":"101005.6875725","volume24h":"1.62","totalVolume":"45","totalTurnover":"2911557","delta":"-0.37596534","gamma":"0.00003161","vega":"82.65324199","theta":"-51.54651685","predictedDeliveryPrice":"0","change24h":"0.02148998"}}
{"topic":"tickers.BTCUSDT","type":"snapshot","data":{"symbol":"BTCUSDT","tickDirection":"ZeroPlusTick","price24hPcnt":"-0.009772","lastPrice":"61874.00","prevPrice24h":"62484.60","highPrice24h":"62752.90","lowPrice24h":"61000.10","prevPrice1h":"61901.80","markPrice":"61875.25","indexPrice":"61903.73","openInterest":"58117.022","openInterestValue":"3596005265.51","turnover24h":"6073739017.8331","volume24h":"98430.1050","nextFundingTime":"1715760000000","fundingRate":"0.00008055","bid1Price":"61873.90","bid1Size":"3.783","ask1Price":"61874.00","ask1Size":"16.278"},"cs":176530003895,"ts":1715748762463}
{"topic":"tickers.BTCUSDT","type":"delta","data":{"symbol":"BTCUSDT","markPrice":"61875.06","indexPrice":"61903.59","openInterestValue":"3595994223.27","bid1Price":"61873.90","bid1Size":"3.543"},"cs":176530004279,"ts":1715748763063}
{"topic":"tickers.BTCPERP","type":"snapshot","data":{"symbol":"BTCPERP","tickDirection":"ZeroMinusTick","price24hPcnt":"-0.000733","lastPrice":"61945.70","prevPrice24h":"61991.20","highPrice24h":"62242.20","lowPrice24h":"61059.10","prevPrice1h":"61868.30","markPrice":"61943.58","indexPrice":"61942.85","openInterest":"526.806","openInterestValue":"32632249.61","turnover24h":"26364945.7684","volume24h":"427.3750","nextFundingTime":"1715760000000","fundingRate":"0.00004979","bid1Price":"61909.20","bid1Size":"0.035","ask1Price":"61909.60","ask1Size":"0.082"},"cs":17493207242,"ts":1715756612118}
{"topic":"tickers.BTCPERP","type":"delta","data":{"symbol":"BTCPERP","bid1Price":"61909.50","bid1Size":"0.035"},"cs":17493207267,"ts":171575661221}
{"topic":"tickers.BTCUSD","type":"snapshot","data":{"symbol":"BTCUSD","tickDirection":"ZeroMinusTick","price24hPcnt":"-0.000096","lastPrice":"61894.00","prevPrice24h":"61900.00","highPrice24h":"62265.50","lowPrice24h":"61029.50","prevPrice1h":"61917.50","markPrice":"61894.00","indexPrice":"61923.36","openInterest":"931760496","openInterestValue":"15054.13","turnover24h":"6363.5775","volume24h":"391976479.0000","nextFundingTime":"1715760000000","fundingRate":"0.0001","bid1Price":"61891.50","bid1Size":"12667","ask1Price":"61892.00","ask1Size":"60953"},"cs":42159594212,"ts":1715757637952}
{"topic":"tickers.BTCUSD","type":"delta","data":{"symbol":"BTCUSD","bid1Price":"61891.50","bid1Size":"27634"},"cs":42159594251,"ts":1715757638152}

0 comments on commit 720061c

Please sign in to comment.