示例#1
0
		/// <summary>
		/// To emulate orders on history.
		/// </summary>
		/// <param name="orders">Orders to be emulated on history.</param>
		/// <param name="storageRegistry">The external storage for access to history data.</param>
		/// <param name="openedPositions">Trades, describing initial open positions.</param>
		/// <returns>The virtual strategy, containing progress of paper trades.</returns>
		public static Strategy EmulateOrders(this IEnumerable<Order> orders, IStorageRegistry storageRegistry, IDictionary<Security, decimal> openedPositions)
		{
			if (openedPositions == null)
				throw new ArgumentNullException(nameof(openedPositions));

			if (storageRegistry == null)
				throw new ArgumentNullException(nameof(storageRegistry));

			if (orders == null)
				throw new ArgumentNullException(nameof(orders));

			var array = orders.ToArray();

			if (array.IsEmpty())
				throw new ArgumentOutOfRangeException(nameof(orders));

			using (var connector = new RealTimeEmulationTrader<HistoryMessageAdapter>(new HistoryMessageAdapter(new IncrementalIdGenerator(), new CollectionSecurityProvider(array.Select(o => o.Security).Distinct()))
			{
				StorageRegistry = storageRegistry
			}))
			{
				var from = array.Min(o => o.Time);
				var to = from.EndOfDay();

				var strategy = new EquityStrategy(array, openedPositions) { Connector = connector };

				var waitHandle = new SyncObject();

				//connector.UnderlyngMarketDataAdapter.StateChanged += () =>
				//{
				//	if (connector.UnderlyngMarketDataAdapter.State == EmulationStates.Started)
				//		strategy.Start();

				//	if (connector.UnderlyngMarketDataAdapter.State == EmulationStates.Stopped)
				//	{
				//		strategy.Stop();

				//		waitHandle.Pulse();
				//	}
				//};

				connector.UnderlyngMarketDataAdapter.StartDate = from;
				connector.UnderlyngMarketDataAdapter.StopDate = to;

				connector.Connect();

				//lock (waitHandle)
				//{
				//	if (connector.UnderlyngMarketDataAdapter.State != EmulationStates.Stopped)
				//		waitHandle.Wait();
				//}

				return strategy;
			}
		}
示例#2
0
        /// <summary>
        /// Сэмулировать заявки на истории.
        /// </summary>
        /// <param name="orders">Заявки, которые необходимо сэмулировать на истории.</param>
        /// <param name="storageRegistry">Внешнеее хранилище для доступа к исторических данным.</param>
        /// <param name="openedPositions">Сделки, описывающие начальные открытые позиции.</param>
        /// <returns>Виртуальная стратегии, содержащая в себе ход эмуляционных торгов.</returns>
        public static Strategy EmulateOrders(this IEnumerable <Order> orders, IStorageRegistry storageRegistry, IDictionary <Security, decimal> openedPositions)
        {
            if (openedPositions == null)
            {
                throw new ArgumentNullException("openedPositions");
            }

            if (storageRegistry == null)
            {
                throw new ArgumentNullException("storageRegistry");
            }

            if (orders == null)
            {
                throw new ArgumentNullException("orders");
            }

            if (orders.IsEmpty())
            {
                throw new ArgumentOutOfRangeException("orders");
            }

            using (var connector = new RealTimeEmulationTrader <HistoryEmulationConnector>(new HistoryEmulationConnector(orders.Select(o => o.Security).Distinct(), orders.Select(o => o.Portfolio).Distinct())
            {
                StorageRegistry = storageRegistry
            }))
            {
                var from = orders.Min(o => o.Time).Date;
                var to   = from.EndOfDay();

                var strategy = new EquityStrategy(orders, openedPositions)
                {
                    Connector = connector
                };

                var waitHandle = new SyncObject();

                connector.UnderlyingConnector.StateChanged += () =>
                {
                    if (connector.UnderlyingConnector.State == EmulationStates.Started)
                    {
                        strategy.Start();
                    }

                    if (connector.UnderlyingConnector.State == EmulationStates.Stopped)
                    {
                        strategy.Stop();

                        waitHandle.Pulse();
                    }
                };

                connector.Connect();
                connector.StartExport();

                connector.UnderlyingConnector.Start(from, to);

                lock (waitHandle)
                {
                    if (connector.UnderlyingConnector.State != EmulationStates.Stopped)
                    {
                        waitHandle.Wait();
                    }
                }

                return(strategy);
            }
        }