Skip to content

Commit

Permalink
Minor data streaming fixes (#9)
Browse files Browse the repository at this point in the history
* Cleanup timer when there is a WS error

* Fix no bid/ask price
  • Loading branch information
Martin-Molinero authored Nov 17, 2023
1 parent dbfb055 commit 1fe0d9a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private static TestCaseData[] TestParameters
return new[]
{
// valid parameters, for example
new TestCaseData(MCUSDT, Resolution.Second, false),
new TestCaseData(BTCUSDT, Resolution.Tick, false),
new TestCaseData(BTCUSDT, Resolution.Minute, false),
new TestCaseData(BTCUSDT, Resolution.Second, false),
Expand Down
1 change: 1 addition & 0 deletions QuantConnect.BybitBrokerage.Tests/BybitBrokerageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace QuantConnect.BybitBrokerage.Tests
[TestFixture, Explicit("Requires valid credentials to be setup and run outside USA")]
public partial class BybitBrokerageTests : BrokerageTests
{
private static Symbol MCUSDT = Symbol.Create("MCUSDT", SecurityType.CryptoFuture, "bybit");
private static Symbol BTCUSDT = Symbol.Create("BTCUSDT", SecurityType.Crypto, "bybit");
private BybitApi _client;
protected override Symbol Symbol { get; } = BTCUSDT;
Expand Down
5 changes: 5 additions & 0 deletions QuantConnect.BybitBrokerage/BybitBrokerage.Messaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ private void HandleOrderBookSnapshot(BybitOrderBookUpdate orderBookUpdate, Bybit
}

orderBook.BestBidAskUpdated += OnBestBidAskUpdated;
if(orderBook.BestBidPrice == 0 && orderBook.BestAskPrice == 0)
{
// nothing to emit, can happen with illiquid assets
return;
}
EmitQuoteTick(symbol, orderBook.BestBidPrice, orderBook.BestBidSize, orderBook.BestAskPrice,
orderBook.BestAskSize);
}
Expand Down
24 changes: 20 additions & 4 deletions QuantConnect.BybitBrokerage/BybitWebSocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class BybitWebSocketWrapper : WebSocketClientWrapper
/// </summary>
protected override void OnOpen()
{
CleanUpTimer();
_pingTimer = new Timer(TimeSpan.FromSeconds(20).TotalMilliseconds);
_pingTimer.Elapsed += PingTimerElapsed;
_pingTimer.Start();
Expand All @@ -41,6 +42,24 @@ protected override void OnOpen()
/// Event invocator for the <see cref="WebSocketClientWrapper.Close"/> event
/// </summary>
protected override void OnClose(WebSocketCloseData e)
{
CleanUpTimer();
base.OnClose(e);
}

/// <summary>
/// Event invocator for the <see cref="WebSocketClientWrapper.OnError"/> event
/// </summary>
protected override void OnError(WebSocketError e)
{
CleanUpTimer();
base.OnError(e);
}

/// <summary>
/// Helper method to clean up timer if required
/// </summary>
private void CleanUpTimer()
{
if (_pingTimer != null)
{
Expand All @@ -49,11 +68,8 @@ protected override void OnClose(WebSocketCloseData e)
_pingTimer.Dispose();
_pingTimer = null;
}

base.OnClose(e);
}



private void PingTimerElapsed(object sender, ElapsedEventArgs e)
{
Send("{\"op\":\"ping\"}");
Expand Down

0 comments on commit 1fe0d9a

Please sign in to comment.