Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor data streaming fixes #9

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading