diff --git a/Algo/OrderLogHelper.cs b/Algo/OrderLogHelper.cs index dba946e5c..1a4682c2b 100644 --- a/Algo/OrderLogHelper.cs +++ b/Algo/OrderLogHelper.cs @@ -58,19 +58,6 @@ namespace StockSharp.Algo /// public static class OrderLogHelper { - /// - /// To check, does the string contain the order registration. - /// - /// Order log item. - /// , if the string contains the order registration, otherwise, . - public static bool IsOrderLogRegistered(this ExecutionMessage item) - { - if (item == null) - throw new ArgumentNullException(nameof(item)); - - return item.OrderState == OrderStates.Active && item.TradePrice == null; - } - /// /// To check, does the string contain the order registration. /// @@ -81,19 +68,6 @@ namespace StockSharp.Algo return item.ToMessage().IsOrderLogRegistered(); } - /// - /// To check, does the string contain the cancelled order. - /// - /// Order log item. - /// , if the string contain the cancelled order, otherwise, . - public static bool IsOrderLogCanceled(this ExecutionMessage item) - { - if (item == null) - throw new ArgumentNullException(nameof(item)); - - return item.OrderState == OrderStates.Done && item.TradePrice == null; - } - /// /// To check, does the string contain the cancelled order. /// @@ -104,19 +78,6 @@ namespace StockSharp.Algo return item.ToMessage().IsOrderLogCanceled(); } - /// - /// To check, does the string contain the order matching. - /// - /// Order log item. - /// , if the string contains order matching, otherwise, . - public static bool IsOrderLogMatched(this ExecutionMessage item) - { - if (item == null) - throw new ArgumentNullException(nameof(item)); - - return item.TradeId != null; - } - /// /// To check, does the string contain the order matching. /// diff --git a/Messages/Extensions.cs b/Messages/Extensions.cs index 6955bd8f9..1a0e2e6f3 100644 --- a/Messages/Extensions.cs +++ b/Messages/Extensions.cs @@ -804,5 +804,44 @@ namespace StockSharp.Messages { return categories?.Contains(MessageAdapterCategories.Russia) == true ? Languages.Russian : Languages.English; } + + /// + /// To check, does the string contain the order registration. + /// + /// Order log item. + /// , if the string contains the order registration, otherwise, . + public static bool IsOrderLogRegistered(this ExecutionMessage item) + { + if (item == null) + throw new ArgumentNullException(nameof(item)); + + return item.OrderState == OrderStates.Active && item.TradePrice == null; + } + + /// + /// To check, does the string contain the cancelled order. + /// + /// Order log item. + /// , if the string contain the cancelled order, otherwise, . + public static bool IsOrderLogCanceled(this ExecutionMessage item) + { + if (item == null) + throw new ArgumentNullException(nameof(item)); + + return item.OrderState == OrderStates.Done && item.TradeVolume == null; + } + + /// + /// To check, does the string contain the order matching. + /// + /// Order log item. + /// , if the string contains order matching, otherwise, . + public static bool IsOrderLogMatched(this ExecutionMessage item) + { + if (item == null) + throw new ArgumentNullException(nameof(item)); + + return item.TradeVolume != null; + } } } \ No newline at end of file diff --git a/Messages/IOrderLogMarketDepthBuilder.cs b/Messages/IOrderLogMarketDepthBuilder.cs index e35f46168..9418bd333 100644 --- a/Messages/IOrderLogMarketDepthBuilder.cs +++ b/Messages/IOrderLogMarketDepthBuilder.cs @@ -15,6 +15,13 @@ Copyright 2010 by StockSharp, LLC #endregion S# License namespace StockSharp.Messages { + using System; + using System.Collections.Generic; + + using Ecng.Collections; + + using StockSharp.Localization; + /// /// Base interface for order book builder. /// @@ -32,4 +39,154 @@ namespace StockSharp.Messages /// Order book was changed. bool Update(ExecutionMessage item); } + + /// + /// Default implementation of . + /// + public class OrderLogMarketDepthBuilder : IOrderLogMarketDepthBuilder + { + private readonly Dictionary _orders = new Dictionary(); + private readonly SortedDictionary _bids = new SortedDictionary(new BackwardComparer()); + private readonly SortedDictionary _asks = new SortedDictionary(); + + /// + /// Initializes a new instance of the . + /// + /// Security ID. + public OrderLogMarketDepthBuilder(SecurityId securityId) + : this(new QuoteChangeMessage { SecurityId = securityId, IsSorted = true }) + { + } + + /// + /// Initializes a new instance of the . + /// + /// Messages containing quotes. + public OrderLogMarketDepthBuilder(QuoteChangeMessage depth) + { + if (depth == null) + throw new ArgumentNullException(nameof(depth)); + + if (!depth.IsSorted) + throw new ArgumentException(LocalizedStrings.Str942, nameof(depth)); + + _depth = depth; + + foreach (var bid in depth.Bids) + _bids.Add(bid.Price, bid); + + foreach (var ask in depth.Asks) + _asks.Add(ask.Price, ask); + + _depth.Bids = _bids.Values; + _depth.Asks = _asks.Values; + } + + private readonly QuoteChangeMessage _depth; + + QuoteChangeMessage IOrderLogMarketDepthBuilder.Depth => _depth; + + bool IOrderLogMarketDepthBuilder.Update(ExecutionMessage item) + { + if (item == null) + throw new ArgumentNullException(nameof(item)); + + if (item.ExecutionType != ExecutionTypes.OrderLog) + throw new ArgumentException(nameof(item)); + + if (item.OrderPrice == 0) + return false; + + var changed = false; + + var quotes = item.Side == Sides.Buy ? _bids : _asks; + + try + { + if (item.IsOrderLogRegistered()) + { + if (item.OrderId != null) + { + var quote = quotes.SafeAdd(item.OrderPrice, key => new QuoteChange(item.Side, key, 0)); + var id = item.OrderId.Value; + + if (item.OrderVolume != null) + { + var volume = item.OrderVolume.Value; + + if (_orders.TryGetValue(id, out var prevVolume)) + { + quote.Volume += (volume - prevVolume); + _orders[id] = volume; + } + else + { + quote.Volume += volume; + _orders.Add(id, volume); + } + + changed = true; + } + } + } + else if (item.IsOrderLogMatched()) + { + if (item.OrderId != null) + { + var id = item.OrderId.Value; + var volume = item.TradeVolume ?? item.OrderVolume; + + if (volume != null) + { + if (_orders.TryGetValue(id, out var prevVolume)) + { + var quote = quotes.TryGetValue(item.OrderPrice); + + if (quote != null) + { + quote.Volume -= volume.Value; + + if (quote.Volume <= 0) + quotes.Remove(item.OrderPrice); + } + + _orders[id] = prevVolume - volume.Value; + changed = true; + } + } + } + } + else if (item.IsOrderLogCanceled()) + { + if (item.OrderId != null) + { + var id = item.OrderId.Value; + + if (_orders.TryGetValue(id, out var prevVolume)) + { + var quote = quotes.TryGetValue(item.OrderPrice); + + if (quote != null) + { + quote.Volume -= prevVolume; + + if (quote.Volume <= 0) + quotes.Remove(item.OrderPrice); + } + + _orders.Remove(id); + changed = true; + } + } + } + } + finally + { + if (changed) + _depth.ServerTime = item.ServerTime; + } + + return changed; + } + } } \ No newline at end of file diff --git a/Messages/MessageAdapter.cs b/Messages/MessageAdapter.cs index 47c32302a..9a2ab82be 100644 --- a/Messages/MessageAdapter.cs +++ b/Messages/MessageAdapter.cs @@ -565,23 +565,16 @@ namespace StockSharp.Messages SendOutMessage(new MarketDataMessage { OriginalTransactionId = originalTransactionId, IsNotSupported = true }); } - /// - /// Check the connection is alive. Uses only for connected states. - /// - /// , is the connection still alive, , if the connection was rejected. + /// public virtual bool IsConnectionAlive() { return true; } - /// - /// Create market depth builder. - /// - /// Security ID. - /// Order log to market depth builder. + /// public virtual IOrderLogMarketDepthBuilder CreateOrderLogMarketDepthBuilder(SecurityId securityId) { - throw new NotSupportedException(); + return new OrderLogMarketDepthBuilder(securityId); } /// diff --git a/Samples/Testing/SampleHistoryTesting/MainWindow.xaml.cs b/Samples/Testing/SampleHistoryTesting/MainWindow.xaml.cs index 6d5638539..33d0fb255 100644 --- a/Samples/Testing/SampleHistoryTesting/MainWindow.xaml.cs +++ b/Samples/Testing/SampleHistoryTesting/MainWindow.xaml.cs @@ -37,12 +37,10 @@ namespace SampleHistoryTesting using StockSharp.Algo.Testing; using StockSharp.Algo.Indicators; using StockSharp.BusinessEntities; - using StockSharp.ITCH; using StockSharp.Logging; using StockSharp.Messages; using StockSharp.Xaml.Charting; using StockSharp.Localization; - using StockSharp.Plaza; public partial class MainWindow { @@ -420,9 +418,7 @@ namespace SampleHistoryTesting { { secId, - LocalizedStrings.ActiveLanguage == Languages.Russian - ? (IOrderLogMarketDepthBuilder)new PlazaOrderLogMarketDepthBuilder(secId) - : new ItchOrderLogMarketDepthBuilder(secId) + new OrderLogMarketDepthBuilder(secId) } } }, diff --git a/Samples/Testing/SampleHistoryTesting/SampleHistoryTesting.csproj b/Samples/Testing/SampleHistoryTesting/SampleHistoryTesting.csproj index 07bd614d6..1c46c8556 100644 --- a/Samples/Testing/SampleHistoryTesting/SampleHistoryTesting.csproj +++ b/Samples/Testing/SampleHistoryTesting/SampleHistoryTesting.csproj @@ -172,14 +172,6 @@ {FCB47C52-E1EA-4020-B88C-E750924F5716} Algo.Strategies - - {5E66A118-9D37-4F93-9598-A8B56A6BE1CA} - ITCH - - - {7EABA894-FC98-4F5E-A212-C40550260886} - Plaza - {2a5c6fad-106c-4a4a-b602-77a6b1034ca2} Algo diff --git a/Samples/Testing/SampleHistoryTesting/SampleHistoryTestingPublic.csproj b/Samples/Testing/SampleHistoryTesting/SampleHistoryTestingPublic.csproj index 980bc9f3f..ad4ddaa29 100644 --- a/Samples/Testing/SampleHistoryTesting/SampleHistoryTestingPublic.csproj +++ b/Samples/Testing/SampleHistoryTesting/SampleHistoryTestingPublic.csproj @@ -61,12 +61,6 @@ ..\..\..\References\stocksharp.ico - - ..\..\..\References\StockSharp.ITCH.dll - - - ..\..\..\References\StockSharp.Plaza.dll - diff --git a/_ReleaseNotes/CHANGE_LOG_API.md b/_ReleaseNotes/CHANGE_LOG_API.md index 1cc0843a5..0ee746a88 100644 --- a/_ReleaseNotes/CHANGE_LOG_API.md +++ b/_ReleaseNotes/CHANGE_LOG_API.md @@ -83,6 +83,7 @@ StockSharp API Change log * (bug) Charting. Fix Equity legend color. * (feature) Diagram. MarketDepthTruncateDiagramElement. * (bug) TimeSpanEditor. Fix DevExpress themes. +* (feature) Default implementation of IOrderLogMarketDepthBuilder. ## v4.4.8: * (feature) ImportSettingsPanel control.