Skip to content

Commit

Permalink
fix: subscription on trade channel in different Binance (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Romazes authored Jan 24, 2024
1 parent abc706b commit c13cde6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private void OnDataMessage(WebSocketMessage webSocketMessage)
switch (eventType)
{
case "trade":
case "aggTrade":
var trade = objData.ToObject<Trade>();
// futures feed send upper and lower case T confusing json
trade.Time = objData["T"].ToObject<long>();
Expand Down
18 changes: 16 additions & 2 deletions QuantConnect.BinanceBrokerage/BinanceBrokerage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using QuantConnect.Brokerages;
using RestSharp;
using Timer = System.Timers.Timer;
using QuantConnect.BinanceBrokerage.Constants;

namespace QuantConnect.BinanceBrokerage
{
Expand Down Expand Up @@ -68,6 +69,19 @@ public partial class BinanceBrokerage : BaseWebsocketsBrokerage, IDataQueueHandl
protected BinanceBaseRestApiClient ApiClient => _apiClientLazy?.Value;
protected string MarketName { get; set; }

/// <summary>
/// Gets or sets the trade channel used for streaming trade information.
/// </summary>
/// <remarks>
/// The <see cref="TradeChannelName"/> property represents the specific trade channel utilized for streaming trade data.
/// It is initialized with the constant value <see cref="TradeChannels.SpotTradeChannelName"/>, indicating the use of
/// Spot Trade Streams by default.
/// </remarks>
/// <value>
/// The default value is the channel name for Spot Trade Streams: <c>trade</c>.
/// </value>
protected virtual string TradeChannelName { get; } = TradeChannels.SpotTradeChannelName;

/// <summary>
/// Parameterless constructor for brokerage
/// </summary>
Expand Down Expand Up @@ -595,7 +609,7 @@ private bool Subscribe(IWebSocket webSocket, Symbol symbol)
method = "SUBSCRIBE",
@params = new[]
{
$"{brokerageSymbol.ToLowerInvariant()}@trade",
$"{brokerageSymbol.ToLowerInvariant()}@{TradeChannelName}",
$"{brokerageSymbol.ToLowerInvariant()}@bookTicker"
},
id = GetNextRequestId()
Expand All @@ -619,7 +633,7 @@ private bool Unsubscribe(IWebSocket webSocket, Symbol symbol)
method = "UNSUBSCRIBE",
@params = new[]
{
$"{brokerageSymbol.ToLowerInvariant()}@trade",
$"{brokerageSymbol.ToLowerInvariant()}@{TradeChannelName}",
$"{brokerageSymbol.ToLowerInvariant()}@bookTicker"
},
id = GetNextRequestId()
Expand Down
14 changes: 14 additions & 0 deletions QuantConnect.BinanceBrokerage/BinanceCoinFuturesBrokerage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using QuantConnect.Brokerages;
using QuantConnect.Interfaces;
using QuantConnect.Configuration;
using QuantConnect.BinanceBrokerage.Constants;

namespace QuantConnect.BinanceBrokerage
{
Expand All @@ -29,6 +30,19 @@ namespace QuantConnect.BinanceBrokerage
[BrokerageFactory(typeof(BinanceCoinFuturesBrokerageFactory))]
public class BinanceCoinFuturesBrokerage : BinanceBrokerage
{
/// <summary>
/// Gets the trade channel name used for streaming trade information in the overridden context.
/// </summary>
/// <remarks>
/// The <see cref="TradeChannelName"/> property represents the specific trade channel name utilized for streaming
/// trade data in the overridden context. In this specific implementation, it returns the constant value
/// <see cref="TradeChannels.FutureTradeChannelName"/>, indicating the use of Future Trade Streams.
/// </remarks>
/// <value>
/// The value is the channel name for Future Trade Streams: <c>aggTrade</c>.
/// </value>
protected override string TradeChannelName => TradeChannels.FutureTradeChannelName;

public BinanceCoinFuturesBrokerage() : base(Market.Binance)
{
}
Expand Down
14 changes: 14 additions & 0 deletions QuantConnect.BinanceBrokerage/BinanceFuturesBrokerage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using QuantConnect.Brokerages;
using QuantConnect.Interfaces;
using QuantConnect.Configuration;
using QuantConnect.BinanceBrokerage.Constants;

namespace QuantConnect.BinanceBrokerage
{
Expand All @@ -29,6 +30,19 @@ namespace QuantConnect.BinanceBrokerage
[BrokerageFactory(typeof(BinanceFuturesBrokerageFactory))]
public class BinanceFuturesBrokerage : BinanceBrokerage
{
/// <summary>
/// Gets the trade channel name used for streaming trade information in the overridden context.
/// </summary>
/// <remarks>
/// The <see cref="TradeChannelName"/> property represents the specific trade channel name utilized for streaming
/// trade data in the overridden context. In this specific implementation, it returns the constant value
/// <see cref="TradeChannels.FutureTradeChannelName"/>, indicating the use of Future Trade Streams.
/// </remarks>
/// <value>
/// The value is the channel name for Future Trade Streams: <c>aggTrade</c>.
/// </value>
protected override string TradeChannelName => TradeChannels.FutureTradeChannelName;

public BinanceFuturesBrokerage() : base(Market.Binance)
{
}
Expand Down
43 changes: 43 additions & 0 deletions QuantConnect.BinanceBrokerage/Constants/TradeChannels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace QuantConnect.BinanceBrokerage.Constants
{
/// <summary>
/// The <see cref="TradeChannels"/> class contains constant values representing trade channels in different markets.
/// </summary>
public sealed class TradeChannels
{
/// <summary>
/// The channel name for Spot Trade Streams. Spot Trade Streams push raw trade information, where each trade
/// involves a unique buyer and seller.
/// </summary>
/// <remarks>
/// Stream Name Format: <c>{symbol}@trade</c>
/// Update Speed: Real-time
/// </remarks>
public const string SpotTradeChannelName = "trade";

/// <summary>
/// The channel name for Aggregate Trade Streams. Aggregate Trade Streams push market trade information that is
/// aggregated for fills with the same price, updating every 100 milliseconds.
/// </summary>
/// <remarks>
/// Stream Name Format: <c>{symbol}@aggTrade</c>
/// Update Speed: 100ms
/// </remarks>
public const string FutureTradeChannelName = "aggTrade";
}
}

0 comments on commit c13cde6

Please sign in to comment.