<commit_before>#region S# License
/******************************************************************************************
NOTICE!!!  This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.

Project: StockSharp.Algo.Testing.Algo
File: EmulationMessageAdapter.cs
Created: 2015, 11, 11, 2:32 PM

Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.Algo.Testing
{
	using System;
    using System.Collections.Generic;
    using System.Linq;

    using Ecng.Common;
	using Ecng.Collections;
	using Ecng.Serialization;

    using StockSharp.BusinessEntities;
    using StockSharp.Messages;
	using StockSharp.Algo.Storages;

	/// <summary>
	/// The interface of the real time market data adapter.
	/// </summary>
	public interface IEmulationMessageAdapter : IMessageAdapterWrapper
	{
	}

	/// <summary>
	/// Emulation message adapter.
	/// </summary>
	public class EmulationMessageAdapter : MessageAdapterWrapper, IEmulationMessageAdapter
	{
		private readonly SynchronizedSet<long> _subscriptionIds = new();
		private readonly SynchronizedSet<long> _emuOrderIds = new();

		private readonly IMessageAdapter _inAdapter;
		private readonly bool _isEmulationOnly;

		/// <summary>
		/// Initialize <see cref="EmulationMessageAdapter"/>.
		/// </summary>
		/// <param name="innerAdapter">Underlying adapter.</param>
		/// <param name="inChannel">Incoming messages channel.</param>
		/// <param name="isEmulationOnly">Send <see cref="TimeMessage"/> to emulator.</param>
		/// <param name="securityProvider">The provider of information about instruments.</param>
		/// <param name="portfolioProvider">The portfolio to be used to register orders. If value is not given, the portfolio with default name Simulator will be created.</param>
		/// <param name="exchangeInfoProvider">Exchanges and trading boards provider.</param>
		public EmulationMessageAdapter(IMessageAdapter innerAdapter, IMessageChannel inChannel, bool isEmulationOnly, ISecurityProvider securityProvider, IPortfolioProvider portfolioProvider, IExchangeInfoProvider exchangeInfoProvider)
			: base(innerAdapter)
		{
			Emulator = new MarketEmulator(securityProvider, portfolioProvider, exchangeInfoProvider, TransactionIdGenerator)
			{
				Parent = this,
				Settings =
				{
					ConvertTime = true,
					InitialOrderId = DateTime.Now.Ticks,
					InitialTradeId = DateTime.Now.Ticks,
				}
			};

			InChannel = inChannel;

			_inAdapter = new SubscriptionOnlineMessageAdapter(Emulator);
			_inAdapter = new ChannelMessageAdapter(_inAdapter, inChannel, new PassThroughMessageChannel());
			_inAdapter.NewOutMessage += RaiseNewOutMessage;

			_isEmulationOnly = isEmulationOnly;
		}

		/// <inheritdoc />
		public override void Dispose()
		{
			_inAdapter.NewOutMessage -= RaiseNewOutMessage;
			base.Dispose();
		}

		/// <summary>
		/// Emulator.
		/// </summary>
		public IMarketEmulator Emulator { get; }

		/// <summary>
		/// Settings of exchange emulator.
		/// </summary>
		public MarketEmulatorSettings Settings => Emulator.Settings;

		/// <summary>
		/// Incoming messages channel.
		/// </summary>
		public IMessageChannel InChannel { get; }

		/// <inheritdoc />
		public override IEnumerable<MessageTypes> SupportedInMessages => InnerAdapter.SupportedInMessages.Concat(Emulator.SupportedInMessages).Distinct().ToArray();
		
		/// <inheritdoc />
		public override IEnumerable<MessageTypes> SupportedOutMessages => InnerAdapter.SupportedOutMessages.Concat(Emulator.SupportedOutMessages).Distinct().ToArray();

		/// <inheritdoc />
		public override bool? IsPositionsEmulationRequired => Emulator.IsPositionsEmulationRequired;

		/// <inheritdoc />
		public override bool IsSupportTransactionLog => Emulator.IsSupportTransactionLog;

		private void SendToEmulator(Message message)
		{
			_inAdapter.SendInMessage(message);
		}

		/// <inheritdoc />
		protected override bool OnSendInMessage(Message message)
		{
			switch (message.Type)<commit_msg>EmulationMessageAdapter: add PositionMessageAdapter to chain to process strategy positions<commit_after>#region S# License
/******************************************************************************************
NOTICE!!!  This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.

Project: StockSharp.Algo.Testing.Algo
File: EmulationMessageAdapter.cs
Created: 2015, 11, 11, 2:32 PM

Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License

namespace StockSharp.Algo.Testing
{
	using System;
    using System.Collections.Generic;
    using System.Linq;

    using Ecng.Common;
	using Ecng.Collections;
	using Ecng.Serialization;

	using StockSharp.Algo.Positions;
	using StockSharp.Algo.Strategies;
    using StockSharp.BusinessEntities;
    using StockSharp.Messages;
	using StockSharp.Algo.Storages;

	/// <summary>
	/// The interface of the real time market data adapter.
	/// </summary>
	public interface IEmulationMessageAdapter : IMessageAdapterWrapper
	{
	}

	/// <summary>
	/// Emulation message adapter.
	/// </summary>
	public class EmulationMessageAdapter : MessageAdapterWrapper, IEmulationMessageAdapter
	{
		private readonly SynchronizedSet<long> _subscriptionIds = new();
		private readonly SynchronizedSet<long> _emuOrderIds = new();

		private readonly IMessageAdapter _inAdapter;
		private readonly bool _isEmulationOnly;

		/// <summary>
		/// Initialize <see cref="EmulationMessageAdapter"/>.
		/// </summary>
		/// <param name="innerAdapter">Underlying adapter.</param>
		/// <param name="inChannel">Incoming messages channel.</param>
		/// <param name="isEmulationOnly">Send <see cref="TimeMessage"/> to emulator.</param>
		/// <param name="securityProvider">The provider of information about instruments.</param>
		/// <param name="portfolioProvider">The portfolio to be used to register orders. If value is not given, the portfolio with default name Simulator will be created.</param>
		/// <param name="exchangeInfoProvider">Exchanges and trading boards provider.</param>
		public EmulationMessageAdapter(IMessageAdapter innerAdapter, IMessageChannel inChannel, bool isEmulationOnly, ISecurityProvider securityProvider, IPortfolioProvider portfolioProvider, IExchangeInfoProvider exchangeInfoProvider)
			: base(innerAdapter)
		{
			Emulator = new MarketEmulator(securityProvider, portfolioProvider, exchangeInfoProvider, TransactionIdGenerator)
			{
				Parent = this,
				Settings =
				{
					ConvertTime = true,
					InitialOrderId = DateTime.Now.Ticks,
					InitialTradeId = DateTime.Now.Ticks,
				}
			};

			InChannel = inChannel;

			_inAdapter = new SubscriptionOnlineMessageAdapter(Emulator);
			_inAdapter = new PositionMessageAdapter(_inAdapter, new StrategyPositionManager(Emulator.IsPositionsEmulationRequired ?? true));
			_inAdapter = new ChannelMessageAdapter(_inAdapter, inChannel, new PassThroughMessageChannel());
			_inAdapter.NewOutMessage += RaiseNewOutMessage;

			_isEmulationOnly = isEmulationOnly;
		}

		/// <inheritdoc />
		public override void Dispose()
		{
			_inAdapter.NewOutMessage -= RaiseNewOutMessage;
			base.Dispose();
		}

		/// <summary>
		/// Emulator.
		/// </summary>
		public IMarketEmulator Emulator { get; }

		/// <summary>
		/// Settings of exchange emulator.
		/// </summary>
		public MarketEmulatorSettings Settings => Emulator.Settings;

		/// <summary>
		/// Incoming messages channel.
		/// </summary>
		public IMessageChannel InChannel { get; }

		/// <inheritdoc />
		public override IEnumerable<MessageTypes> SupportedInMessages => InnerAdapter.SupportedInMessages.Concat(Emulator.SupportedInMessages).Distinct().ToArray();

		/// <inheritdoc />
		public override IEnumerable<MessageTypes> SupportedOutMessages => InnerAdapter.SupportedOutMessages.Concat(Emulator.SupportedOutMessages).Distinct().ToArray();

		/// <inheritdoc />
		public override bool? IsPositionsEmulationRequired => Emulator.IsPositionsEmulationRequired;

		/// <inheritdoc />
		public override bool IsSupportTransactionLog => Emulator.IsSupportTransactionLog;

		private void SendToEmulator(Message message)
		{
			_inAdapter.SendInMessage(message);
		}

		/// <inheritdoc />
		protected override bool OnSendInMessage(Message message)
		{
			switch (message.Type)
{'fraction_non_alphanumeric': 0.1390114240470535, 'fraction_numerical': 0.0033932813030200203, 'mean_word_length': 4.646232439335888, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 85, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1674485', 'n_tokens_mistral': 2613, 'n_tokens_neox': 2302, 'n_words': 605}