From 9e81fc257f452a77760c2529fea1ab5fee9c3c8b Mon Sep 17 00:00:00 2001 From: Martin Molinero Date: Thu, 1 Feb 2024 11:17:00 -0300 Subject: [PATCH] Filter repeated trade ids --- .../CoinbaseBrokerage.Messaging.cs | 13 +++++++++++++ .../Models/WebSocket/CoinbaseMarketTradesEvent.cs | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.Messaging.cs b/QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.Messaging.cs index 73548b8..44dfc94 100644 --- a/QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.Messaging.cs +++ b/QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.Messaging.cs @@ -53,6 +53,11 @@ public partial class CoinbaseBrokerage /// private readonly ConcurrentDictionary> _orderBooks = new(); + /// + /// Sometimes coinbase likes to duplicate the trades, let's ignore old trade ids + /// + private readonly ConcurrentDictionary> _tradeIds = new(); + /// /// Represents a rate limiter for controlling the frequency of WebSocket operations. /// @@ -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, diff --git a/QuantConnect.CoinbaseBrokerage/Models/WebSocket/CoinbaseMarketTradesEvent.cs b/QuantConnect.CoinbaseBrokerage/Models/WebSocket/CoinbaseMarketTradesEvent.cs index 0953ec0..804ee0a 100644 --- a/QuantConnect.CoinbaseBrokerage/Models/WebSocket/CoinbaseMarketTradesEvent.cs +++ b/QuantConnect.CoinbaseBrokerage/Models/WebSocket/CoinbaseMarketTradesEvent.cs @@ -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; }