Skip to content

Commit

Permalink
Filter repeated trade ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed Feb 1, 2024
1 parent 5914b6b commit 9e81fc2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.Messaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public partial class CoinbaseBrokerage
/// </example>
private readonly ConcurrentDictionary<Symbol, List<DefaultOrderBook>> _orderBooks = new();

/// <summary>
/// Sometimes coinbase likes to duplicate the trades, let's ignore old trade ids
/// </summary>
private readonly ConcurrentDictionary<Symbol, Tuple<long, DateTime>> _tradeIds = new();

/// <summary>
/// Represents a rate limiter for controlling the frequency of WebSocket operations.
/// </summary>
Expand Down Expand Up @@ -337,6 +342,14 @@ private void EmitTradeTick(CoinbaseMarketTradesEvent tradeUpdates)
{
var symbol = _symbolMapper.GetLeanSymbol(trade.ProductId, SecurityType.Crypto, MarketName);

if (_tradeIds.TryGetValue(symbol, out var lastTradeData)
// ignore old trade ids as long as they have an old timestamp too, just in case it restarted
&& lastTradeData.Item1 > trade.TradeId && lastTradeData.Item2 > trade.Time.UtcDateTime)
{
continue;
}
_tradeIds[symbol] = new (trade.TradeId, trade.Time.UtcDateTime);

var tick = new Tick
{
Value = trade.Price.Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CoinbaseMarketTradesEvent : WebSocketEvent
public class Trade
{
[JsonProperty("trade_id")]
public string TradeId { get; set; }
public long TradeId { get; set; }

[JsonProperty("product_id")]
public string ProductId { get; set; }
Expand Down

0 comments on commit 9e81fc2

Please sign in to comment.