Ejemplo n.º 1
0
        public Worker()
        {
            storage = new StorageRegistry();
            securityStorage = storage.GetSecurityStorage();

            connectors = new List<IQFeedTrader>();
        }
Ejemplo n.º 2
0
		public HydraServer(IStorageRegistry storageRegistry, IEntityRegistry entityRegistry, IEnumerable<IHydraTask> tasks)
			: base(storageRegistry, entityRegistry)
		{
			if (tasks == null)
				throw new ArgumentNullException(nameof(tasks));

			_tasks = tasks;
		}
Ejemplo n.º 3
0
        private void ConvertDirectory(string path, IStorageRegistry registry, StorageFormats format, ExchangeBoard board)
        {
            if (!_isStarted)
                return;

            Directory.GetFiles(path, "*.qsh").ForEach(f => ConvertFile(f, registry, format, board));
            Directory.GetDirectories(path).ForEach(d => ConvertDirectory(d, registry, format, board));
        }
Ejemplo n.º 4
0
		/// <summary>
		/// Initializes a new instance of the <see cref="StorageEntityFactory"/>.
		/// </summary>
		/// <param name="entityRegistry">The storage of trade objects.</param>
		/// <param name="storageRegistry">The storage of market data.</param>
		public StorageEntityFactory(IEntityRegistry entityRegistry, IStorageRegistry storageRegistry)
		{
			if (entityRegistry == null)
				throw new ArgumentNullException("entityRegistry");

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

			_entityRegistry = entityRegistry;
			_storageRegistry = storageRegistry;

			_securityStorage = storageRegistry.GetSecurityStorage();

			_securities = _securityStorage.LookupAll()
				.ToDictionary(s => s.Id, s => s, StringComparer.InvariantCultureIgnoreCase);
		}
Ejemplo n.º 5
0
        private void ConvertFile(string fileName, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike, Dictionary <SecurityId, IOrderLogMarketDepthBuilder> orderLog2OrderBookBuilders, int orderBookMaxDepth)
        {
            if (!_isStarted)
            {
                return;
            }

            var fileNameKey = format + "_" + fileName;

            if (_convertedFiles.Contains(fileNameKey))
            {
                return;
            }

            _logManager.Application.AddInfoLog("Начата конвертация файла {0}.", fileName);

            var securitiesStrings = securityLike.Split(",");

            var data = new Dictionary <BusinessEntities.Security, Tuple <List <QuoteChangeMessage>, List <ExecutionMessage>, List <Level1ChangeMessage>, List <ExecutionMessage> > >();

            using (var qr = QshReader.Open(fileName))
            {
                var reader      = qr;
                var currentDate = qr.CurrentDateTime;

                for (var i = 0; i < qr.StreamCount; i++)
                {
                    var stream            = (ISecurityStream)qr[i];
                    var security          = GetSecurity(stream.Security, board);
                    var priceStep         = security.PriceStep ?? 1;
                    var securityId        = security.ToSecurityId();
                    var lastTransactionId = 0L;
                    var builder           = orderLog2OrderBookBuilders?.SafeAdd(securityId, key => new PlazaOrderLogMarketDepthBuilder(key));

                    if (securitiesStrings.Length > 0)
                    {
                        var secCode = securityId.SecurityCode;

                        var streamContainsSecurityFromMask = securitiesStrings.Any(mask =>
                        {
                            var isEndMulti   = mask.EndsWith("*");
                            var isStartMulti = mask.StartsWith("*");

                            if (isStartMulti)
                            {
                                mask = mask.Substring(1);
                            }

                            if (isEndMulti)
                            {
                                mask = mask.Substring(0, mask.Length - 1);
                            }

                            if (isEndMulti)
                            {
                                if (isStartMulti)
                                {
                                    return(secCode.ContainsIgnoreCase(mask));
                                }
                                else
                                {
                                    return(secCode.StartsWith(mask, StringComparison.InvariantCultureIgnoreCase));
                                }
                            }
                            else if (isStartMulti)
                            {
                                return(secCode.EndsWith(mask, StringComparison.InvariantCultureIgnoreCase));
                            }
                            else
                            {
                                return(secCode.CompareIgnoreCase(mask));
                            }
                        });

                        if (!streamContainsSecurityFromMask)
                        {
                            continue;
                        }
                    }

                    var secData = data.SafeAdd(security, key => Tuple.Create(new List <QuoteChangeMessage>(), new List <ExecutionMessage>(), new List <Level1ChangeMessage>(), new List <ExecutionMessage>()));

                    switch (stream.Type)
                    {
                    case StreamType.Quotes:
                    {
                        ((IQuotesStream)stream).Handler += (key, quotes, spread) =>
                        {
                            var quotes2 = quotes.Select(q =>
                                {
                                    Sides side;

                                    switch (q.Type)
                                    {
                                    case QuoteType.Unknown:
                                    case QuoteType.Free:
                                    case QuoteType.Spread:
                                        throw new ArgumentException(q.Type.ToString());

                                    case QuoteType.Ask:
                                    case QuoteType.BestAsk:
                                        side = Sides.Sell;
                                        break;

                                    case QuoteType.Bid:
                                    case QuoteType.BestBid:
                                        side = Sides.Buy;
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException();
                                    }

                                    return(new QuoteChange(side, priceStep * q.Price, q.Volume));
                                }).ToArray();

                            var md = new QuoteChangeMessage
                            {
                                LocalTime  = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
                                SecurityId = securityId,
                                ServerTime = currentDate.ApplyTimeZone(TimeHelper.Moscow),
                                Bids       = quotes2.Where(q => q.Side == Sides.Buy),
                                Asks       = quotes2.Where(q => q.Side == Sides.Sell),
                            };

                            //if (md.Verify())
                            //{
                            secData.Item1.Add(md);

                            TryFlushData(registry, security, format, null, secData.Item1, reader);

                            //}
                            //else
                            //	_logManager.Application.AddErrorLog("Стакан для {0} в момент {1} не прошел валидацию. Лучший бид {2}, Лучший офер {3}.", security, qr.CurrentDateTime, md.BestBid, md.BestAsk);
                        };
                        break;
                    }

                    case StreamType.Deals:
                    {
                        ((IDealsStream)stream).Handler += deal =>
                        {
                            secData.Item2.Add(new ExecutionMessage
                                {
                                    LocalTime     = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
                                    HasTradeInfo  = true,
                                    ExecutionType = ExecutionTypes.Tick,
                                    SecurityId    = securityId,
                                    OpenInterest  = deal.OI == 0 ? (long?)null : deal.OI,
                                    ServerTime    = deal.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                    TradeVolume   = deal.Volume,
                                    TradeId       = deal.Id == 0 ? (long?)null : deal.Id,
                                    TradePrice    = deal.Price,
                                    OriginSide    =
                                        deal.Type == DealType.Buy
                                                                                        ? Sides.Buy
                                                                                        : (deal.Type == DealType.Sell ? Sides.Sell : (Sides?)null)
                                });

                            TryFlushData(registry, security, format, ExecutionTypes.Tick, secData.Item2, reader);
                        };
                        break;
                    }

                    case StreamType.OrdLog:
                    {
                        ((IOrdLogStream)stream).Handler += (key, ol) =>
                        {
                            var currTransactionId = ol.DateTime.Ticks;

                            if (lastTransactionId < currTransactionId)
                            {
                                lastTransactionId = currTransactionId;
                            }
                            else if (lastTransactionId >= currTransactionId)
                            {
                                lastTransactionId++;
                            }

                            var msg = new ExecutionMessage
                            {
                                LocalTime     = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
                                ExecutionType = ExecutionTypes.OrderLog,
                                SecurityId    = securityId,
                                OpenInterest  = ol.OI == 0 ? (long?)null : ol.OI,
                                OrderId       = ol.OrderId,
                                OrderPrice    = priceStep * ol.Price,
                                ServerTime    = ol.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                OrderVolume   = ol.Amount,
                                Balance       = ol.AmountRest,
                                TradeId       = ol.DealId == 0 ? (long?)null : ol.DealId,
                                TradePrice    = ol.DealPrice == 0 ? (decimal?)null : priceStep * ol.DealPrice,
                                TransactionId = lastTransactionId
                            };

                            var status = 0;

                            if (ol.Flags.Contains(OrdLogFlags.Add))
                            {
                                msg.OrderState = OrderStates.Active;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Fill))
                            {
                                msg.OrderState = OrderStates.Done;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Canceled))
                            {
                                msg.OrderState = OrderStates.Done;
                                status        |= 0x200000;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.CanceledGroup))
                            {
                                msg.OrderState = OrderStates.Done;
                                status        |= 0x400000;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Moved))
                            {
                                status |= 0x100000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Buy))
                            {
                                msg.Side = Sides.Buy;
                            }
                            else if (ol.Flags.Contains(OrdLogFlags.Sell))
                            {
                                msg.Side = Sides.Sell;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.FillOrKill))
                            {
                                msg.TimeInForce = TimeInForce.MatchOrCancel;
                                status         |= 0x00080000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Quote))
                            {
                                msg.TimeInForce = TimeInForce.PutInQueue;
                                status         |= 0x01;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.Counter))
                            {
                                status |= 0x02;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.CrossTrade))
                            {
                                status |= 0x20000000;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.NonSystem))
                            {
                                msg.IsSystem = false;
                                status      |= 0x04;
                            }

                            if (ol.Flags.Contains(OrdLogFlags.EndOfTransaction))
                            {
                                status |= 0x1000;
                            }

                            msg.OrderStatus = status;

                            if (builder == null)
                            {
                                secData.Item4.Add(msg);

                                TryFlushData(registry, security, format, ExecutionTypes.OrderLog, secData.Item4, reader);
                            }
                            else
                            {
                                //if (builder.Depth.Bids.Any() || builder.Depth.Asks.Any() || msg.ServerTime.TimeOfDay >= new TimeSpan(0, 18, 45, 00, 1))
                                {
                                    bool updated;

                                    try
                                    {
                                        updated = builder.Update(msg);
                                    }
                                    catch
                                    {
                                        updated = false;
                                    }

                                    if (updated)
                                    {
                                        secData.Item1.Add(new QuoteChangeMessage
                                            {
                                                SecurityId = securityId,
                                                ServerTime = builder.Depth.ServerTime,
                                                Bids       = builder.Depth.Bids.Take(orderBookMaxDepth).ToArray(),
                                                Asks       = builder.Depth.Asks.Take(orderBookMaxDepth).ToArray(),
                                                IsSorted   = builder.Depth.IsSorted,
                                                LocalTime  = builder.Depth.LocalTime,
                                            });

                                        TryFlushData(registry, security, format, null, secData.Item1, reader);
                                    }
                                }
                            }
                        };
                        break;
                    }

                    case StreamType.AuxInfo:
                    {
                        ((IAuxInfoStream)stream).Handler += (key, info) =>
                        {
                            secData.Item3.Add(new Level1ChangeMessage
                                {
                                    LocalTime  = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
                                    SecurityId = securityId,
                                    ServerTime = info.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                }
                                              .TryAdd(Level1Fields.LastTradePrice, priceStep * info.Price)
                                              .TryAdd(Level1Fields.BidsVolume, (decimal)info.BidTotal)
                                              .TryAdd(Level1Fields.AsksVolume, (decimal)info.AskTotal)
                                              .TryAdd(Level1Fields.HighPrice, priceStep * info.HiLimit)
                                              .TryAdd(Level1Fields.LowPrice, priceStep * info.LoLimit)
                                              .TryAdd(Level1Fields.OpenInterest, (decimal)info.OI));

                            TryFlushData(registry, security, format, null, secData.Item3, reader);
                        };
                        break;
                    }

                    case StreamType.OwnOrders:
                    case StreamType.OwnTrades:
                    case StreamType.Messages:
                    case StreamType.None:
                    {
                        continue;
                    }

                    default:
                        throw new ArgumentOutOfRangeException("Неподдерживаемый тип потока {0}.".Put(stream.Type));
                    }
                }

                if (data.Count > 0)
                {
                    while (qr.CurrentDateTime != DateTime.MaxValue && _isStarted)
                    {
                        qr.Read(true);
                    }
                }
            }

            if (!_isStarted)
            {
                return;
            }

            foreach (var pair in data)
            {
                if (pair.Value.Item1.Any())
                {
                    registry.GetQuoteMessageStorage(pair.Key, format: format).Save(pair.Value.Item1);
                }

                if (pair.Value.Item2.Any())
                {
                    registry.GetTickMessageStorage(pair.Key, format: format).Save(pair.Value.Item2);
                }

                if (pair.Value.Item3.Any())
                {
                    registry.GetLevel1MessageStorage(pair.Key, format: format).Save(pair.Value.Item3);
                }

                if (pair.Value.Item4.Any())
                {
                    registry.GetOrderLogMessageStorage(pair.Key, format: format).Save(pair.Value.Item4);
                }
            }

            if (data.Count > 0)
            {
                //File.AppendAllLines(_convertedFilesFile, new[] { fileNameKey });
                _convertedFiles.Add(fileNameKey);
                _convertedPerTaskPoolFiles.Add(fileNameKey);
            }

            _logManager.Application.AddInfoLog("Завершена конвертация файла {0}.", fileName);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BatchEmulation"/>.
        /// </summary>
        /// <param name="securityProvider">The provider of information about instruments.</param>
        /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
        /// <param name="storageRegistry">Market data storage.</param>
        public BatchEmulation(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
        {
            if (securityProvider == null)
            {
                throw new ArgumentNullException(nameof(securityProvider));
            }

            if (portfolios == null)
            {
                throw new ArgumentNullException(nameof(portfolios));
            }

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

            Strategies = Enumerable.Empty <Strategy>();

            EmulationSettings  = new EmulationSettings();
            EmulationConnector = new HistoryEmulationConnector(securityProvider, portfolios, storageRegistry)
            {
                UpdateSecurityLastQuotes = false,
                UpdateSecurityByLevel1   = false
            };

            EmulationConnector.StateChanged      += EmulationConnectorOnStateChanged;
            EmulationConnector.MarketTimeChanged += EmulationConnectorOnMarketTimeChanged;
            EmulationConnector.Disconnected      += EmulationConnectorOnDisconnected;
            EmulationConnector.NewSecurity       += EmulationConnectorOnNewSecurity;
        }
Ejemplo n.º 7
0
		/// <summary>
		/// Initializes a new instance of the <see cref="BatchEmulation"/>.
		/// </summary>
		/// <param name="securityProvider">The provider of information about instruments.</param>
		/// <param name="portfolios">Portfolios, the operation will be performed with.</param>
		/// <param name="storageRegistry">Market data storage.</param>
		public BatchEmulation(ISecurityProvider securityProvider, IEnumerable<Portfolio> portfolios, IStorageRegistry storageRegistry)
		{
			if (securityProvider == null)
				throw new ArgumentNullException(nameof(securityProvider));

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

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

			Strategies = Enumerable.Empty<Strategy>().ToEx();

			EmulationSettings = new EmulationSettings();
			EmulationConnector = new HistoryEmulationConnector(securityProvider, portfolios, storageRegistry)
			{
				UpdateSecurityLastQuotes = false,
				UpdateSecurityByLevel1 = false
			};

			//_basketSessionHolder = new HistoryBasketSessionHolder(EmulationConnector.TransactionIdGenerator);

			EmulationConnector.Adapter.InnerAdapters.Add(new BasketEmulationAdapter(EmulationConnector.TransactionIdGenerator, EmulationSettings));

			EmulationConnector.StateChanged += EmulationConnectorOnStateChanged;
			EmulationConnector.MarketTimeChanged += EmulationConnectorOnMarketTimeChanged;
		}
Ejemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StorageMessageAdapter"/>.
        /// </summary>
        /// <param name="innerAdapter">The adapter, to which messages will be directed.</param>
        /// <param name="entityRegistry">The storage of trade objects.</param>
        /// <param name="storageRegistry">The storage of market data.</param>
        public StorageMessageAdapter(IMessageAdapter innerAdapter, IEntityRegistry entityRegistry, IStorageRegistry storageRegistry)
            : base(innerAdapter)
        {
            if (entityRegistry == null)
            {
                throw new ArgumentNullException(nameof(entityRegistry));
            }

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

            _entityRegistry  = entityRegistry;
            _storageRegistry = storageRegistry;

            Drive = _storageRegistry.DefaultDrive;

            var isProcessing = false;
            var sync         = new SyncObject();

            ThreadingHelper.Timer(() =>
            {
                lock (sync)
                {
                    if (isProcessing)
                    {
                        return;
                    }

                    isProcessing = true;
                }

                try
                {
                    foreach (var pair in GetTicks())
                    {
                        GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.Tick).Save(pair.Value);
                    }

                    foreach (var pair in GetOrderLog())
                    {
                        GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.OrderLog).Save(pair.Value);
                    }

                    foreach (var pair in GetTransactions())
                    {
                        GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.Transaction).Save(pair.Value);
                    }

                    foreach (var pair in GetOrderBooks())
                    {
                        GetStorage(pair.Key, typeof(QuoteChangeMessage), null).Save(pair.Value);
                    }

                    foreach (var pair in GetLevel1())
                    {
                        GetStorage(pair.Key, typeof(Level1ChangeMessage), null).Save(pair.Value);
                    }

                    foreach (var pair in GetCandles())
                    {
                        GetStorage(pair.Key.Item1, pair.Key.Item2, pair.Key.Item3).Save(pair.Value);
                    }

                    var news = GetNews().ToArray();

                    if (news.Length > 0)
                    {
                        _storageRegistry.GetNewsMessageStorage(Drive, Format).Save(news);
                    }
                }
                catch (Exception excp)
                {
                    excp.LogError();
                }
                finally
                {
                    lock (sync)
                        isProcessing = false;
                }
            }).Interval(TimeSpan.FromSeconds(10));
        }
Ejemplo n.º 9
0
		public TerminalConnector(IEntityRegistry entityRegistry, IStorageRegistry storageRegistry) : base(entityRegistry, storageRegistry)
		{
			Connected += OnConnected;
			StorageAdapter.Format = StorageFormats.Csv;
		}
Ejemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
        /// </summary>
        /// <param name="securityProvider">The provider of information about instruments.</param>
        /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
        /// <param name="storageRegistry">Market data storage.</param>
        public HistoryEmulationConnector(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
        {
            if (securityProvider == null)
            {
                throw new ArgumentNullException(nameof(securityProvider));
            }

            if (portfolios == null)
            {
                throw new ArgumentNullException(nameof(portfolios));
            }

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

            // чтобы каждый раз при повторной эмуляции получать одинаковые номера транзакций
            TransactionIdGenerator = new IncrementalIdGenerator();

            _initialMoney = portfolios.ToDictionary(pf => pf, pf => pf.BeginValue);
            EntityFactory = new EmulationEntityFactory(securityProvider, _initialMoney.Keys);

            RiskManager = null;

            SupportSubscriptionTracking = true;
            SupportBasketSecurities     = true;

            HistoryMessageAdapter = new HistoryMessageAdapter(TransactionIdGenerator, securityProvider)
            {
                StorageRegistry = storageRegistry
            };

            InMessageChannel  = new HistoryEmulationMessageChannel(HistoryMessageAdapter, SendOutError);
            OutMessageChannel = new PassThroughMessageChannel();

            Adapter = new HistoryBasketMessageAdapter(this);
            Adapter.InnerAdapters.Add(EmulationAdapter);
            Adapter.InnerAdapters.Add(HistoryMessageAdapter);

            Adapter.LatencyManager    = null;
            Adapter.CommissionManager = null;
            Adapter.PnLManager        = null;
            Adapter.SlippageManager   = null;

            // при тестировании по свечкам, время меняется быстрее и таймаут должен быть больше 30с.
            ReConnectionSettings.TimeOutInterval = TimeSpan.MaxValue;

            //MaxMessageCount = 1000;

            TradesKeepCount = 0;

            RaiseConnectedOnFirstAdapter = false;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StorageMessageAdapter"/>.
        /// </summary>
        /// <param name="innerAdapter">The adapter, to which messages will be directed.</param>
        /// <param name="storageRegistry">The storage of market data.</param>
        /// <param name="snapshotRegistry">Snapshot storage registry.</param>
        /// <param name="candleBuilderProvider">Candle builders provider.</param>
        public StorageMessageAdapter(IMessageAdapter innerAdapter, IStorageRegistry storageRegistry, SnapshotRegistry snapshotRegistry, CandleBuilderProvider candleBuilderProvider)
            : base(innerAdapter)
        {
            _storageRegistry       = storageRegistry ?? throw new ArgumentNullException(nameof(storageRegistry));
            _snapshotRegistry      = snapshotRegistry ?? throw new ArgumentNullException(nameof(snapshotRegistry));
            _candleBuilderProvider = candleBuilderProvider ?? throw new ArgumentNullException(nameof(candleBuilderProvider));

            var isProcessing = false;
            var sync         = new SyncObject();

            var unkByOrderId       = new Dictionary <long, List <ExecutionMessage> >();
            var unkByOrderStringId = new Dictionary <string, List <ExecutionMessage> >(StringComparer.InvariantCultureIgnoreCase);

            ThreadingHelper.Timer(() =>
            {
                lock (sync)
                {
                    if (isProcessing)
                    {
                        return;
                    }

                    isProcessing = true;
                }

                try
                {
                    foreach (var pair in GetTicks())
                    {
                        GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.Tick).Save(pair.Value);
                    }

                    foreach (var pair in GetOrderLog())
                    {
                        GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.OrderLog).Save(pair.Value);
                    }

                    foreach (var pair in GetTransactions())
                    {
                        var secId = pair.Key;

                        if (Mode.Contains(StorageModes.Incremental))
                        {
                            GetStorage <ExecutionMessage>(secId, ExecutionTypes.Transaction).Save(pair.Value);
                        }

                        if (Mode.Contains(StorageModes.Snapshot))
                        {
                            var snapshotStorage = GetSnapshotStorage(typeof(ExecutionMessage), ExecutionTypes.Transaction);

                            foreach (var message in pair.Value)
                            {
                                var originTransId = message.OriginalTransactionId;

                                if (message.TransactionId == 0 && originTransId == 0)
                                {
                                    if (!message.HasTradeInfo)
                                    {
                                        continue;
                                    }

                                    long transId;

                                    if (message.OrderId != null)
                                    {
                                        if (!_orderIds.TryGetValue(message.OrderId.Value, out transId))
                                        {
                                            unkByOrderId.SafeAdd(message.OrderId.Value).Add(message);
                                            continue;
                                        }
                                    }
                                    else if (!message.OrderStringId.IsEmpty())
                                    {
                                        if (!_orderStringIds.TryGetValue(message.OrderStringId, out transId))
                                        {
                                            unkByOrderStringId.SafeAdd(message.OrderStringId).Add(message);
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        continue;
                                    }

                                    originTransId = transId;
                                }
                                else
                                {
                                    // do not store cancellation commands into snapshot
                                    if (message.IsCancellation && message.TransactionId != 0)
                                    {
                                        continue;
                                    }

                                    if (originTransId != 0)
                                    {
                                        if (/*message.TransactionId == 0 && */ _cancellationTransactions.TryGetValue(originTransId, out var temp))
                                        {
                                            // do not store cancellation errors
                                            if (message.Error != null)
                                            {
                                                continue;
                                            }

                                            // override cancel trans id by original order's registration trans id
                                            originTransId = temp;
                                        }

                                        if (_orderStatusIds.Contains(originTransId))
                                        {
                                            // override status request trans id by original order's registration trans id
                                            originTransId = message.TransactionId;
                                        }
                                    }

                                    if (originTransId != 0)
                                    {
                                        if (message.OrderId != null)
                                        {
                                            _orderIds.TryAdd(message.OrderId.Value, originTransId);
                                        }
                                        else if (message.OrderStringId != null)
                                        {
                                            _orderStringIds.TryAdd(message.OrderStringId, originTransId);
                                        }
                                    }
                                }

                                message.SecurityId = secId;

                                if (message.TransactionId == 0)
                                {
                                    message.TransactionId = originTransId;
                                }

                                message.OriginalTransactionId = 0;

                                SaveTransaction(snapshotStorage, message);

                                if (message.OrderId != null)
                                {
                                    if (unkByOrderId.TryGetValue(message.OrderId.Value, out var suspended))
                                    {
                                        unkByOrderId.Remove(message.OrderId.Value);

                                        foreach (var trade in suspended)
                                        {
                                            trade.TransactionId = message.TransactionId;
                                            SaveTransaction(snapshotStorage, trade);
                                        }
                                    }
                                }
                                else if (!message.OrderStringId.IsEmpty())
                                {
                                    if (unkByOrderStringId.TryGetValue(message.OrderStringId, out var suspended))
                                    {
                                        unkByOrderStringId.Remove(message.OrderStringId);

                                        foreach (var trade in suspended)
                                        {
                                            trade.TransactionId = message.TransactionId;
                                            SaveTransaction(snapshotStorage, trade);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    foreach (var pair in GetOrderBooks())
                    {
                        if (Mode.Contains(StorageModes.Incremental))
                        {
                            GetStorage <QuoteChangeMessage>(pair.Key, null).Save(pair.Value);
                        }

                        if (Mode.Contains(StorageModes.Snapshot))
                        {
                            var snapshotStorage = GetSnapshotStorage(typeof(QuoteChangeMessage), null);

                            foreach (var message in pair.Value)
                            {
                                snapshotStorage.Update(message);
                            }
                        }
                    }

                    foreach (var pair in GetLevel1())
                    {
                        var messages = pair.Value.Where(m => m.Changes.Count > 0).ToArray();

                        var dt = DateTime.Today;

                        var historical = messages.Where(m => m.ServerTime < dt).ToArray();
                        var today      = messages.Where(m => m.ServerTime >= dt).ToArray();

                        GetStorage <Level1ChangeMessage>(pair.Key, null).Save(historical);

                        if (Mode.Contains(StorageModes.Incremental))
                        {
                            GetStorage <Level1ChangeMessage>(pair.Key, null).Save(today);
                        }

                        if (Mode.Contains(StorageModes.Snapshot))
                        {
                            var snapshotStorage = GetSnapshotStorage(typeof(Level1ChangeMessage), null);

                            foreach (var message in today)
                            {
                                snapshotStorage.Update(message);
                            }
                        }
                    }

                    foreach (var pair in GetCandles())
                    {
                        GetStorage(pair.Key.Item1, pair.Key.Item2, pair.Key.Item3).Save(pair.Value);
                    }

                    foreach (var pair in GetPositionChanges())
                    {
                        var messages = pair.Value.Where(m => m.Changes.Count > 0).ToArray();

                        if (Mode.Contains(StorageModes.Incremental))
                        {
                            GetStorage <PositionChangeMessage>(pair.Key, null).Save(messages);
                        }

                        if (Mode.Contains(StorageModes.Snapshot))
                        {
                            var snapshotStorage = GetSnapshotStorage(typeof(PositionChangeMessage), null);

                            foreach (var message in messages)
                            {
                                snapshotStorage.Update(message);
                            }
                        }
                    }

                    var news = GetNews().ToArray();

                    if (news.Length > 0)
                    {
                        _storageRegistry.GetNewsMessageStorage(Drive, Format).Save(news);
                    }
                }
                catch (Exception excp)
                {
                    excp.LogError();
                }
                finally
                {
                    lock (sync)
                        isProcessing = false;
                }
            }).Interval(TimeSpan.FromSeconds(10));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BatchEmulation"/>.
        /// </summary>
        /// <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>
        /// <param name="storageRegistry">Market data storage.</param>
        /// <param name="storageFormat">The format of market data. <see cref="StorageFormats.Binary"/> is used by default.</param>
        /// <param name="drive">The storage which is used by default. By default, <see cref="IStorageRegistry.DefaultDrive"/> is used.</param>
        public BatchEmulation(ISecurityProvider securityProvider, IPortfolioProvider portfolioProvider, IExchangeInfoProvider exchangeInfoProvider, IStorageRegistry storageRegistry, StorageFormats storageFormat = StorageFormats.Binary, IMarketDataDrive drive = null)
        {
            _securityProvider     = securityProvider ?? throw new ArgumentNullException(nameof(securityProvider));
            _portfolioProvider    = portfolioProvider ?? throw new ArgumentNullException(nameof(portfolioProvider));
            _exchangeInfoProvider = exchangeInfoProvider ?? throw new ArgumentNullException(nameof(exchangeInfoProvider));

            EmulationSettings = new EmulationSettings();

            StorageSettings = new StorageCoreSettings
            {
                StorageRegistry = storageRegistry,
                Drive           = drive,
                Format          = storageFormat,
            };
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
 /// </summary>
 /// <param name="securities">Instruments, the operation will be performed with.</param>
 /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
 /// <param name="storageRegistry">Market data storage.</param>
 public HistoryEmulationConnector(IEnumerable <Security> securities, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
     : this(new CollectionSecurityProvider(securities), new CollectionPortfolioProvider(portfolios), storageRegistry.CheckOnNull().ExchangeInfoProvider, storageRegistry)
 {
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StorageMessageAdapter"/>.
        /// </summary>
        /// <param name="innerAdapter">The adapter, to which messages will be directed.</param>
        /// <param name="entityRegistry">The storage of trade objects.</param>
        /// <param name="storageRegistry">The storage of market data.</param>
        public StorageMessageAdapter(IMessageAdapter innerAdapter, IEntityRegistry entityRegistry, IStorageRegistry storageRegistry)
            : base(innerAdapter)
        {
            if (entityRegistry == null)
            {
                throw new ArgumentNullException(nameof(entityRegistry));
            }

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

            _entityRegistry  = entityRegistry;
            _storageRegistry = storageRegistry;

            Drive = _storageRegistry.DefaultDrive;

            ThreadingHelper.Timer(() =>
            {
                foreach (var pair in GetTicks())
                {
                    GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.Tick).Save(pair.Value);
                }

                foreach (var pair in GetOrderLog())
                {
                    GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.OrderLog).Save(pair.Value);
                }

                foreach (var pair in GetTransactions())
                {
                    GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.Order).Save(pair.Value);
                }

                foreach (var pair in GetOrderBooks())
                {
                    GetStorage(pair.Key, typeof(QuoteChangeMessage), null).Save(pair.Value);
                }

                foreach (var pair in GetLevel1())
                {
                    GetStorage(pair.Key, typeof(Level1ChangeMessage), null).Save(pair.Value);
                }

                foreach (var pair in GetCandles())
                {
                    GetStorage(pair.Key.Item1, pair.Key.Item2, pair.Key.Item3).Save(pair.Value);
                }

                var news = GetNews().ToArray();

                if (news.Length > 0)
                {
                    _storageRegistry.GetNewsMessageStorage(Drive, Format).Save(news);
                }
            }).Interval(TimeSpan.FromSeconds(10));
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BatchEmulation"/>.
 /// </summary>
 /// <param name="securities">Instruments, the operation will be performed with.</param>
 /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
 /// <param name="storageRegistry">Market data storage.</param>
 public BatchEmulation(IEnumerable <Security> securities, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
     : this(new CollectionSecurityProvider(securities), new CollectionPortfolioProvider(portfolios), new InMemoryExchangeInfoProvider(), storageRegistry, StorageFormats.Binary, storageRegistry.DefaultDrive)
 {
 }
Ejemplo n.º 16
0
        private void Process(IStorageRegistry storageRegistry, Security security, StorageFormats format, IMarketDataDrive drive)
        {
            this.GuiSync(() =>
            {
                _visibleEntries.Clear();

                Columns.RemoveRange(_candleColumns.Values);
                _candleColumns.Values.ForEach(c => FormatRules.Remove(c));
                _candleColumns.Clear();
            });

            if (security == null)
            {
                return;
            }

            var dict = new Dictionary <DateTime, MarketDataEntry>();

            drive = drive ?? storageRegistry.DefaultDrive;

            var candles = new Dictionary <string, Tuple <Type, object> >();

            lock (_syncObject)
            {
                if (_isChanged)
                {
                    return;
                }
            }

            foreach (var tuple in drive.GetCandleTypes(security.ToSecurityId(), format))
            {
                foreach (var arg in tuple.Item2)
                {
                    var key = tuple.Item1.Name.Replace("CandleMessage", string.Empty) + " " + arg;
                    candles.Add(key, Tuple.Create(tuple.Item1, arg));
                }
            }

            var candleNames = candles.Keys.ToArray();

            if (candleNames.Length > 0)
            {
                this.GuiSync(() =>
                {
                    foreach (var candleName in candleNames)
                    {
                        var column = new DataGridTextColumn
                        {
                            Header  = candleName,
                            Binding = new Binding
                            {
                                Path      = new PropertyPath("Candles[{0}]".Put(candleName)),
                                Converter = new BoolToCheckMarkConverter()
                            }
                        };
                        //var cbElement = new FrameworkElementFactory(typeof(CheckBox));

                        //var column = new DataGridTemplateColumn
                        //{
                        //	Header = candle,
                        //	CellStyle = new Style(),
                        //	//SortMemberPath = "Candles[{0}]".Put(key)
                        //	CellTemplate = new DataTemplate(typeof(CheckBox))
                        //	{
                        //		VisualTree = cbElement,
                        //	}
                        //};

                        //var bind = new Binding { Path = new PropertyPath("Candles[{0}]".Put(candle)) };

                        //cbElement.SetBinding(ToggleButton.IsCheckedProperty, bind);
                        //cbElement.SetValue(IsHitTestVisibleProperty, false);
                        //cbElement.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Center);

                        Columns.Add(column);
                        _candleColumns.Add(candleName, column);

                        //ApplyFormatRules(column);
                    }

                    //ApplyFormatRules();
                });
            }

            Add(dict, storageRegistry.GetTickMessageStorage(security, drive, format), candleNames, e => e.IsTick         = true);
            Add(dict, storageRegistry.GetQuoteMessageStorage(security, drive, format), candleNames, e => e.IsDepth       = true);
            Add(dict, storageRegistry.GetLevel1MessageStorage(security, drive, format), candleNames, e => e.IsLevel1     = true);
            Add(dict, storageRegistry.GetOrderLogMessageStorage(security, drive, format), candleNames, e => e.IsOrderLog = true);

            foreach (var c in candleNames)
            {
                var candleName = c;

                lock (_syncObject)
                {
                    if (_isChanged)
                    {
                        return;
                    }
                }

                var tuple = candles[candleName];
                Add(dict, storageRegistry.GetCandleMessageStorage(tuple.Item1, security, tuple.Item2, drive, format), candleNames, e => e.Candles[candleName] = true);
            }

            if (dict.Count > 0)
            {
                // добавляем рабочие дни, которые отсутствуют в данных
                var emptyDays = dict.Keys.Min().Range(dict.Keys.Max(), TimeSpan.FromDays(1))
                                .Where(d => security.Board.WorkingTime.IsTradeDate(d, true) && !dict.ContainsKey(d));

                foreach (var batch in emptyDays.Batch(10))
                {
                    lock (_syncObject)
                    {
                        if (_isChanged)
                        {
                            return;
                        }
                    }

                    TryAddEntries(dict, batch, candleNames, e => { });
                }
            }

            lock (_syncObject)
            {
                if (_isChanged)
                {
                    return;
                }
            }

            this.GuiSync(RefreshSort);
        }
Ejemplo n.º 17
0
        private void InitializeDataSource()
        {
            _storageRegistry = new StorageRegistry();
            ConfigManager.RegisterService(_storageRegistry);

            _entityRegistry = (HydraEntityRegistry)ConfigManager.GetService <IEntityRegistry>();
            _entityRegistry.TasksSettings.Recycle = false;
            ((SecurityList)_entityRegistry.Securities).BulkLoad = true;

            var database = (Database)_entityRegistry.Storage;

            if (database != null)
            {
                var conStr = new DbConnectionStringBuilder
                {
                    ConnectionString = database.ConnectionString
                };

                _dbFile = (string)conStr.Cast <KeyValuePair <string, object> >().ToDictionary(StringComparer.InvariantCultureIgnoreCase).TryGetValue("Data Source");

                if (_dbFile != null)
                {
                    _dbFile = _dbFile.Replace("%Documents%", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

                    conStr["Data Source"]     = _dbFile;
                    database.ConnectionString = conStr.ToString();

                    _dbFile.CreateDirIfNotExists();

                    if (!File.Exists(_dbFile))
                    {
                        Properties.Resources.StockSharp.Save(_dbFile);
                        _entityRegistry.Version = HydraEntityRegistry.LatestVersion;

                        UpdateDatabaseWalMode();
                    }
                }
            }

            CheckDatabase();

            ConfigManager.RegisterService <IExchangeInfoProvider>(new ExchangeInfoProvider(_entityRegistry));

            var allSec = _entityRegistry.Securities.GetAllSecurity();

            if (allSec != null)
            {
                return;
            }

            _entityRegistry.Securities.Add(new Security
            {
                Id   = Core.Extensions.AllSecurityId,
                Code = "ALL",
                //Class = task.GetDisplayName(),
                Name          = LocalizedStrings.Str2835,
                Board         = ExchangeBoard.Associated,
                ExtensionInfo = new Dictionary <object, object>(),
            });
            _entityRegistry.Securities.DelayAction.WaitFlush();
        }
Ejemplo n.º 18
0
            public OptimizationEmulationConnector(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry, StorageFormats format, IMarketDataDrive drive)
                : base(securityProvider, portfolios, storageRegistry)
            {
                Adapter = new BasketEmulationAdapter(this);
                Adapter.InnerAdapters.Add(EmulationAdapter);
                Adapter.InnerAdapters.Add(HistoryMessageAdapterEx);

                Adapter.LatencyManager    = null;
                Adapter.CommissionManager = null;
                Adapter.PnLManager        = null;
                Adapter.SlippageManager   = null;

                HistoryMessageAdapter.StorageFormat = format;
                HistoryMessageAdapter.Drive         = drive;
            }
Ejemplo n.º 19
0
		/// <summary>
		/// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
		/// </summary>
		/// <param name="securityProvider">The provider of information about instruments.</param>
		/// <param name="portfolios">Portfolios, the operation will be performed with.</param>
		/// <param name="storageRegistry">Market data storage.</param>
		public HistoryEmulationConnector(ISecurityProvider securityProvider, IEnumerable<Portfolio> portfolios, IStorageRegistry storageRegistry)
		{
			if (securityProvider == null)
				throw new ArgumentNullException(nameof(securityProvider));

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

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

			// чтобы каждый раз при повторной эмуляции получать одинаковые номера транзакций
			TransactionIdGenerator = new IncrementalIdGenerator();

			_initialMoney = portfolios.ToDictionary(pf => pf, pf => pf.BeginValue);
			EntityFactory = new EmulationEntityFactory(securityProvider, _initialMoney.Keys);

			InMessageChannel = new PassThroughMessageChannel();
			OutMessageChannel = new PassThroughMessageChannel();

			LatencyManager = null;
			RiskManager = null;
			CommissionManager = null;
			PnLManager = null;
			SlippageManager = null;

			HistoryMessageAdapter = new HistoryMessageAdapter(TransactionIdGenerator, securityProvider) { StorageRegistry = storageRegistry };
			_historyChannel = new InMemoryMessageChannel("History Out", SendOutError);

			Adapter = new HistoryBasketMessageAdapter(this);
			Adapter.InnerAdapters.Add(EmulationAdapter);
			Adapter.InnerAdapters.Add(new ChannelMessageAdapter(HistoryMessageAdapter, new InMemoryMessageChannel("History In", SendOutError), _historyChannel));

			// при тестировании по свечкам, время меняется быстрее и таймаут должен быть больше 30с.
			ReConnectionSettings.TimeOutInterval = TimeSpan.MaxValue;

			MaxMessageCount = 1000;

			TradesKeepCount = 0;
		}
Ejemplo n.º 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
 /// </summary>
 /// <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="storageRegistry">Market data storage.</param>
 /// <param name="exchangeInfoProvider">Exchanges and trading boards provider.</param>
 public HistoryEmulationConnector(ISecurityProvider securityProvider, IPortfolioProvider portfolioProvider, IExchangeInfoProvider exchangeInfoProvider, IStorageRegistry storageRegistry)
     : this(new HistoryMessageAdapter(new IncrementalIdGenerator(), securityProvider) { StorageRegistry = storageRegistry }, true, new InMemoryMessageChannel(new MessageByLocalTimeQueue(), "Emulator in", err => err.LogError()), securityProvider, portfolioProvider, exchangeInfoProvider)
 {
 }
Ejemplo n.º 21
0
        /// <summary>
        /// Создать <see cref="BatchEmulation"/>.
        /// </summary>
        /// <param name="securityProvider">Поставщик информации об инструментах.</param>
        /// <param name="portfolios">Портфели, с которыми будет вестись работа.</param>
        /// <param name="storageRegistry">Хранилище данных.</param>
        public BatchEmulation(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
        {
            if (securityProvider == null)
            {
                throw new ArgumentNullException("securityProvider");
            }

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

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

            Strategies = Enumerable.Empty <Strategy>().ToEx();

            EmulationSettings  = new EmulationSettings();
            EmulationConnector = new HistoryEmulationConnector(securityProvider, portfolios, storageRegistry)
            {
                UpdateSecurityLastQuotes = false,
                UpdateSecurityByLevel1   = false
            };

            _basketSessionHolder = new HistoryBasketSessionHolder(EmulationConnector.TransactionIdGenerator);

            EmulationConnector.TransactionAdapter = new BasketEmulationAdapter(_basketSessionHolder, EmulationSettings)
            {
                OutMessageProcessor = new NonThreadMessageProcessor()
            };

            EmulationConnector.StateChanged      += EmulationConnectorOnStateChanged;
            EmulationConnector.MarketTimeChanged += EmulationConnectorOnMarketTimeChanged;
        }
Ejemplo n.º 22
0
		private void Process(IStorageRegistry storageRegistry, Security security, StorageFormats format, IMarketDataDrive drive)
		{
			this.GuiSync(() =>
			{
				_visibleEntries.Clear();
			
				Columns.RemoveRange(_candleColumns.Values);
				_candleColumns.Values.ForEach(c => FormatRules.Remove(c));
				_candleColumns.Clear();
			});

			if (security == null)
				return;

			var dict = new Dictionary<DateTime, MarketDataEntry>();

			drive = drive ?? storageRegistry.DefaultDrive;

			var candles = new Dictionary<string, Tuple<Type, object>>();

			lock (_syncObject)
			{
				if (_isChanged)
					return;
			}

			foreach (var tuple in drive.GetCandleTypes(security.ToSecurityId(), format))
			{
				foreach (var arg in tuple.Item2)
				{
					var key = tuple.Item1.Name.Replace("CandleMessage", string.Empty) + " " + arg;
					candles.Add(key, Tuple.Create(tuple.Item1, arg));
				}
			}

			var candleNames = candles.Keys.ToArray();

			if (candleNames.Length > 0)
			{
				this.GuiSync(() =>
				{
					foreach (var candleName in candleNames)
					{
						var column = new DataGridTextColumn
						{
							Header = candleName,
							Binding = new Binding
							{
								Path = new PropertyPath("Candles[{0}]".Put(candleName)),
								Converter = new BoolToCheckMarkConverter()
							}
						};
						//var cbElement = new FrameworkElementFactory(typeof(CheckBox));

						//var column = new DataGridTemplateColumn
						//{
						//	Header = candle,
						//	CellStyle = new Style(),
						//	//SortMemberPath = "Candles[{0}]".Put(key)
						//	CellTemplate = new DataTemplate(typeof(CheckBox))
						//	{
						//		VisualTree = cbElement,
						//	}
						//};

						//var bind = new Binding { Path = new PropertyPath("Candles[{0}]".Put(candle)) };
						
						//cbElement.SetBinding(ToggleButton.IsCheckedProperty, bind);
						//cbElement.SetValue(IsHitTestVisibleProperty, false);
						//cbElement.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Center);

						Columns.Add(column);
						_candleColumns.Add(candleName, column);

						//ApplyFormatRules(column);
					}

					//ApplyFormatRules();
				});
			}

			Add(dict, storageRegistry.GetTickMessageStorage(security, drive, format), candleNames, e => e.IsTick = true);
			Add(dict, storageRegistry.GetQuoteMessageStorage(security, drive, format), candleNames, e => e.IsDepth = true);
			Add(dict, storageRegistry.GetLevel1MessageStorage(security, drive, format), candleNames, e => e.IsLevel1 = true);
			Add(dict, storageRegistry.GetOrderLogMessageStorage(security, drive, format), candleNames, e => e.IsOrderLog = true);

			foreach (var c in candleNames)
			{
				var candleName = c;

				lock (_syncObject)
				{
					if (_isChanged)
						return;
				}

				var tuple = candles[candleName];
				Add(dict, storageRegistry.GetCandleMessageStorage(tuple.Item1, security, tuple.Item2, drive, format), candleNames, e => e.Candles[candleName] = true);
			}

			if (dict.Count > 0)
			{
				// добавляем рабочие дни, которые отсутствуют в данных
				var emptyDays = dict.Keys.Min().Range(dict.Keys.Max(), TimeSpan.FromDays(1))
					.Where(d => security.Board.WorkingTime.IsTradeDate(d, true) && !dict.ContainsKey(d));

				foreach (var batch in emptyDays.Batch(10))
				{
					lock (_syncObject)
					{
						if (_isChanged)
							return;
					}

					TryAddEntries(dict, batch, candleNames, e => { });
				}
			}

			lock (_syncObject)
			{
				if (_isChanged)
					return;
			}

			this.GuiSync(RefreshSort);
		}
Ejemplo n.º 23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StorageMessageAdapter"/>.
        /// </summary>
        /// <param name="innerAdapter">The adapter, to which messages will be directed.</param>
        /// <param name="entityRegistry">The storage of trade objects.</param>
        /// <param name="storageRegistry">The storage of market data.</param>
        public StorageMessageAdapter(IMessageAdapter innerAdapter, IEntityRegistry entityRegistry, IStorageRegistry storageRegistry)
            : base(innerAdapter)
        {
            if (entityRegistry == null)
            {
                throw new ArgumentNullException(nameof(entityRegistry));
            }

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

            _entityRegistry  = entityRegistry;
            _storageRegistry = storageRegistry;

            var isProcessing = false;
            var sync         = new SyncObject();

            ThreadingHelper.Timer(() =>
            {
                lock (sync)
                {
                    if (isProcessing)
                    {
                        return;
                    }

                    isProcessing = true;
                }

                try
                {
                    foreach (var pair in GetTicks())
                    {
                        GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.Tick).Save(pair.Value);
                    }

                    foreach (var pair in GetOrderLog())
                    {
                        GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.OrderLog).Save(pair.Value);
                    }

                    foreach (var pair in GetTransactions())
                    {
                        // TODO
                        //if (Mode.Contains(StorageModes.Incremental))
                        GetStorage <ExecutionMessage>(pair.Key, ExecutionTypes.Transaction).Save(pair.Value);

                        //if (Mode.Contains(StorageModes.Snapshot))
                        //{
                        //	var snapshotStorage = GetSnapshotStorage(typeof(ExecutionMessage), ExecutionTypes.Transaction);

                        //	foreach (var message in pair.Value)
                        //		snapshotStorage.Update(message);
                        //}
                    }

                    foreach (var pair in GetOrderBooks())
                    {
                        if (Mode.Contains(StorageModes.Incremental))
                        {
                            GetStorage <QuoteChangeMessage>(pair.Key, null).Save(pair.Value);
                        }

                        if (Mode.Contains(StorageModes.Snapshot))
                        {
                            var snapshotStorage = GetSnapshotStorage(typeof(QuoteChangeMessage), null);

                            foreach (var message in pair.Value)
                            {
                                snapshotStorage.Update(message);
                            }
                        }
                    }

                    foreach (var pair in GetLevel1())
                    {
                        var messages = pair.Value.Where(m => m.Changes.Count > 0).ToArray();

                        var dt = DateTime.Today;

                        var historical = messages.Where(m => m.ServerTime < dt).ToArray();
                        var today      = messages.Where(m => m.ServerTime >= dt).ToArray();

                        GetStorage <Level1ChangeMessage>(pair.Key, null).Save(historical);

                        if (Mode.Contains(StorageModes.Incremental))
                        {
                            GetStorage <Level1ChangeMessage>(pair.Key, null).Save(today);
                        }

                        if (Mode.Contains(StorageModes.Snapshot))
                        {
                            var snapshotStorage = GetSnapshotStorage(typeof(Level1ChangeMessage), null);

                            foreach (var message in today)
                            {
                                snapshotStorage.Update(message);
                            }
                        }
                    }

                    foreach (var pair in GetCandles())
                    {
                        GetStorage(pair.Key.Item1, pair.Key.Item2, pair.Key.Item3).Save(pair.Value);
                    }

                    foreach (var pair in GetPositionChanges())
                    {
                        var messages = pair.Value.Where(m => m.Changes.Count > 0).ToArray();

                        if (Mode.Contains(StorageModes.Incremental))
                        {
                            GetStorage <PositionChangeMessage>(pair.Key, null).Save(messages);
                        }

                        if (Mode.Contains(StorageModes.Snapshot))
                        {
                            var snapshotStorage = GetSnapshotStorage(typeof(PositionChangeMessage), null);

                            foreach (var message in messages)
                            {
                                snapshotStorage.Update(message);
                            }
                        }
                    }

                    var news = GetNews().ToArray();

                    if (news.Length > 0)
                    {
                        _storageRegistry.GetNewsMessageStorage(Drive, Format).Save(news);
                    }
                }
                catch (Exception excp)
                {
                    excp.LogError();
                }
                finally
                {
                    lock (sync)
                        isProcessing = false;
                }
            }).Interval(TimeSpan.FromSeconds(10));
        }
Ejemplo n.º 24
0
            public OptimizationEmulationConnector(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
                : base(securityProvider, portfolios, storageRegistry)
            {
                Adapter = new BasketEmulationAdapter(this);
                Adapter.InnerAdapters.Add(EmulationAdapter);
                Adapter.InnerAdapters.Add(HistoryMessageAdapter);

                Adapter.LatencyManager    = null;
                Adapter.CommissionManager = null;
                Adapter.PnLManager        = null;
                Adapter.SlippageManager   = null;
            }
Ejemplo n.º 25
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BatchEmulation"/>.
        /// </summary>
        /// <param name="securityProvider">The provider of information about instruments.</param>
        /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
        /// <param name="storageRegistry">Market data storage.</param>
        public BatchEmulation(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
        {
            if (securityProvider == null)
            {
                throw new ArgumentNullException(nameof(securityProvider));
            }

            if (portfolios == null)
            {
                throw new ArgumentNullException(nameof(portfolios));
            }

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

            Strategies = Enumerable.Empty <Strategy>();

            EmulationSettings  = new EmulationSettings();
            EmulationConnector = new HistoryEmulationConnector(securityProvider, portfolios, storageRegistry)
            {
                UpdateSecurityLastQuotes = false,
                UpdateSecurityByLevel1   = false
            };

            //_basketSessionHolder = new HistoryBasketSessionHolder(EmulationConnector.TransactionIdGenerator);

            EmulationConnector.Adapter.InnerAdapters.Add(new BasketEmulationAdapter(EmulationConnector.TransactionIdGenerator, EmulationSettings));

            EmulationConnector.StateChanged      += EmulationConnectorOnStateChanged;
            EmulationConnector.MarketTimeChanged += EmulationConnectorOnMarketTimeChanged;
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
        /// </summary>
        /// <param name="securityProvider">The provider of information about instruments.</param>
        /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
        /// <param name="storageRegistry">Market data storage.</param>
        public HistoryEmulationConnector(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
        {
            if (securityProvider == null)
            {
                throw new ArgumentNullException(nameof(securityProvider));
            }

            if (portfolios == null)
            {
                throw new ArgumentNullException(nameof(portfolios));
            }

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

            // чтобы каждый раз при повторной эмуляции получать одинаковые номера транзакций
            TransactionIdGenerator = new IncrementalIdGenerator();

            EntityFactory = new EmulationEntityFactory(securityProvider, portfolios);

            RiskManager = null;

            SupportBasketSecurities = true;

            InMessageChannel  = new HistoryEmulationMessageChannel(this);
            OutMessageChannel = new PassThroughMessageChannel();

            Adapter = new HistoryBasketMessageAdapter(this);
            Adapter.InnerAdapters.Add(EmulationAdapter);
            Adapter.InnerAdapters.Add(new HistoryMessageAdapter(TransactionIdGenerator, securityProvider)
            {
                StorageRegistry = storageRegistry
            });

            Adapter.LatencyManager    = null;
            Adapter.CommissionManager = null;
            Adapter.PnLManager        = null;
            Adapter.SlippageManager   = null;

            // при тестировании по свечкам, время меняется быстрее и таймаут должен быть больше 30с.
            //ReConnectionSettings.TimeOutInterval = TimeSpan.MaxValue;

            //MaxMessageCount = 1000;

            TradesKeepCount = 0;

            Adapter.SupportCandlesCompression            = false;
            Adapter.SupportBuildingFromOrderLog          = false;
            Adapter.SupportPartialDownload               = false;
            Adapter.SupportLookupTracking                = false;
            Adapter.SupportOrderBookTruncate             = false;
            Adapter.ConnectDisconnectEventOnFirstAdapter = false;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
 /// </summary>
 /// <param name="securities">Instruments, the operation will be performed with.</param>
 /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
 /// <param name="storageRegistry">Market data storage.</param>
 public HistoryEmulationConnector(IEnumerable <Security> securities, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
     : this((ISecurityProvider) new CollectionSecurityProvider(securities), portfolios, storageRegistry)
 {
 }
Ejemplo n.º 28
0
		private void InitializeDataSource()
		{
			_storageRegistry = new StorageRegistry();
			ConfigManager.RegisterService(_storageRegistry);

			_entityRegistry = (HydraEntityRegistry)ConfigManager.GetService<IEntityRegistry>();
			_entityRegistry.TasksSettings.Recycle = false;
			((SecurityList)_entityRegistry.Securities).BulkLoad = true;

			var database = (Database)_entityRegistry.Storage;

			if (database != null)
			{
				var conStr = new DbConnectionStringBuilder
				{
					ConnectionString = database.ConnectionString
				};

				_dbFile = (string)conStr.Cast<KeyValuePair<string, object>>().ToDictionary(StringComparer.InvariantCultureIgnoreCase).TryGetValue("Data Source");

				if (_dbFile != null)
				{
					_dbFile = _dbFile.Replace("%Documents%", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

					conStr["Data Source"] = _dbFile;
					database.ConnectionString = conStr.ToString();

					_dbFile.CreateDirIfNotExists();

					if (!File.Exists(_dbFile))
					{
						Properties.Resources.StockSharp.Save(_dbFile);
						_entityRegistry.Version = HydraEntityRegistry.LatestVersion;

						UpdateDatabaseWalMode();
					}
				}
			}

			CheckDatabase();

			ConfigManager.RegisterService<IExchangeInfoProvider>(new ExchangeInfoProvider(_entityRegistry));

			var allSec = _entityRegistry.Securities.ReadById(Core.Extensions.AllSecurityId);

			if (allSec != null)
				return;

			_entityRegistry.Securities.Add(new Security
			{
				Id = Core.Extensions.AllSecurityId,
				Code = "ALL",
				//Class = task.GetDisplayName(),
				Name = LocalizedStrings.Str2835,
				Board = ExchangeBoard.Associated,
				ExtensionInfo = new Dictionary<object, object>(),
			});
			_entityRegistry.Securities.DelayAction.WaitFlush();
		}
Ejemplo n.º 29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StockSharpExporter"/>.
        /// </summary>
        /// <param name="security">Security.</param>
        /// <param name="arg">The data parameter.</param>
        /// <param name="isCancelled">The processor, returning export interruption sign.</param>
        /// <param name="storageRegistry">The storage of market data.</param>
        /// <param name="drive">Storage.</param>
        /// <param name="format">Format type.</param>
        public StockSharpExporter(Security security, object arg, Func <int, bool> isCancelled, IStorageRegistry storageRegistry, IMarketDataDrive drive, StorageFormats format)
            : base(security, arg, isCancelled, drive.Path)
        {
            if (storageRegistry == null)
            {
                throw new ArgumentNullException(nameof(storageRegistry));
            }

            if (drive == null)
            {
                throw new ArgumentNullException(nameof(drive));
            }

            _storageRegistry = storageRegistry;
            _drive           = drive;
            _format          = format;
        }
Ejemplo n.º 30
0
        private void ConvertFile(string fileName, IStorageRegistry registry, StorageFormats format, ExchangeBoard board)
        {
            if (!_isStarted)
                return;

            var fileNameKey = format + "_" + fileName;

            if (_convertedFiles.Contains(fileNameKey))
                return;

            _logManager.Application.AddInfoLog("Конвертация файла {0}.", fileName);

            const int maxBufCount = 1000;

            var data = new Dictionary<Security, Tuple<List<QuoteChangeMessage>, List<ExecutionMessage>, List<Level1ChangeMessage>, List<ExecutionMessage>>>();

            using (var qr = QshReader.Open(fileName))
            {
                var currentDate = qr.CurrentDateTime;

                for (var i = 0; i < qr.StreamCount; i++)
                {
                    var stream = (ISecurityStream)qr[i];
                    var security = GetSecurity(stream.Security, board);
                    var priceStep = security.PriceStep ?? 1;
                    var securityId = security.ToSecurityId();
                    var lastTransactionId = 0L;

                    var secData = data.SafeAdd(security, key => Tuple.Create(new List<QuoteChangeMessage>(), new List<ExecutionMessage>(), new List<Level1ChangeMessage>(), new List<ExecutionMessage>()));

                    switch (stream.Type)
                    {
                        case StreamType.Stock:
                        {
                            ((IStockStream)stream).Handler += (key, quotes, spread) =>
                            {
                                var quotes2 = quotes.Select(q =>
                                {
                                    Sides side;

                                    switch (q.Type)
                                    {
                                        case QuoteType.Unknown:
                                        case QuoteType.Free:
                                        case QuoteType.Spread:
                                            throw new ArgumentException(q.Type.ToString());
                                        case QuoteType.Ask:
                                        case QuoteType.BestAsk:
                                            side = Sides.Sell;
                                            break;
                                        case QuoteType.Bid:
                                        case QuoteType.BestBid:
                                            side = Sides.Buy;
                                            break;
                                        default:
                                            throw new ArgumentOutOfRangeException();
                                    }

                                    return new QuoteChange(side, priceStep * q.Price, q.Volume);
                                }).ToArray();

                                var md = new QuoteChangeMessage
                                {
                                    SecurityId = securityId,
                                    ServerTime = currentDate.ApplyTimeZone(TimeHelper.Moscow),
                                    Bids = quotes2.Where(q => q.Side == Sides.Buy),
                                    Asks = quotes2.Where(q => q.Side == Sides.Sell),
                                };

                                //if (md.Verify())
                                //{
                                secData.Item1.Add(md);

                                if (secData.Item1.Count > maxBufCount)
                                {
                                    registry.GetQuoteMessageStorage(security).Save(secData.Item1);
                                    secData.Item1.Clear();
                                }
                                //}
                                //else
                                //	_logManager.Application.AddErrorLog("Стакан для {0} в момент {1} не прошел валидацию. Лучший бид {2}, Лучший офер {3}.", security, qr.CurrentDateTime, md.BestBid, md.BestAsk);
                            };
                            break;
                        }
                        case StreamType.Deals:
                        {
                            ((IDealsStream)stream).Handler += deal =>
                            {
                                secData.Item2.Add(new ExecutionMessage
                                {
                                    ExecutionType = ExecutionTypes.Tick,
                                    SecurityId = securityId,
                                    OpenInterest = deal.OI == 0 ? (long?)null : deal.OI,
                                    ServerTime = deal.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                    Volume = deal.Volume,
                                    TradeId = deal.Id == 0 ? (long?)null : deal.Id,
                                    TradePrice = (decimal)deal.Price,
                                    OriginSide =
                                        deal.Type == DealType.Buy
                                            ? Sides.Buy
                                            : (deal.Type == DealType.Sell ? Sides.Sell : (Sides?)null)
                                });

                                if (secData.Item2.Count > maxBufCount)
                                {
                                    registry.GetTickMessageStorage(security).Save(secData.Item2);
                                    secData.Item2.Clear();
                                }
                            };
                            break;
                        }
                        case StreamType.OrdLog:
                        {
                            ((IOrdLogStream)stream).Handler += (key, ol) =>
                            {
                                var currTransactionId = ol.DateTime.Ticks;

                                if (lastTransactionId < currTransactionId)
                                    lastTransactionId = currTransactionId;
                                else if (lastTransactionId >= currTransactionId)
                                    lastTransactionId++;

                                var msg = new ExecutionMessage
                                {
                                    ExecutionType = ExecutionTypes.OrderLog,
                                    SecurityId = securityId,
                                    OpenInterest = ol.OI == 0 ? (long?)null : ol.OI,
                                    OrderId = ol.OrderId,
                                    Price = priceStep * ol.Price,
                                    ServerTime = ol.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                    Volume = ol.Amount,
                                    Balance = ol.AmountRest,
                                    TradeId = ol.DealId == 0 ? (long?)null : ol.DealId,
                                    TradePrice = ol.DealPrice == 0 ? (decimal?)null : priceStep * ol.DealPrice,
                                    TransactionId = lastTransactionId
                                };

                                var status = 0;

                                if (ol.Flags.Contains(OrdLogFlags.Add))
                                {
                                    msg.OrderState = OrderStates.Active;
                                }
                                else if (ol.Flags.Contains(OrdLogFlags.Fill))
                                {
                                    msg.OrderState = OrderStates.Done;
                                }
                                else if (ol.Flags.Contains(OrdLogFlags.Canceled))
                                {
                                    msg.OrderState = OrderStates.Done;
                                    status |= 0x200000;
                                }
                                else if (ol.Flags.Contains(OrdLogFlags.CanceledGroup))
                                {
                                    msg.OrderState = OrderStates.Done;
                                    status |= 0x400000;
                                }
                                else if (ol.Flags.Contains(OrdLogFlags.Moved))
                                {
                                    status |= 0x100000;
                                }

                                if (ol.Flags.Contains(OrdLogFlags.Buy))
                                {
                                    msg.Side = Sides.Buy;
                                }
                                else if (ol.Flags.Contains(OrdLogFlags.Sell))
                                {
                                    msg.Side = Sides.Sell;
                                }

                                if (ol.Flags.Contains(OrdLogFlags.FillOrKill))
                                {
                                    msg.TimeInForce = TimeInForce.MatchOrCancel;
                                    status |= 0x00080000;
                                }

                                if (ol.Flags.Contains(OrdLogFlags.Quote))
                                {
                                    msg.TimeInForce = TimeInForce.PutInQueue;
                                    status |= 0x01;
                                }

                                if (ol.Flags.Contains(OrdLogFlags.Counter))
                                {
                                    status |= 0x02;
                                }

                                if (ol.Flags.Contains(OrdLogFlags.CrossTrade))
                                {
                                    status |= 0x20000000;
                                }

                                if (ol.Flags.Contains(OrdLogFlags.NonSystem))
                                {
                                    msg.IsSystem = false;
                                    status |= 0x04;
                                }

                                if (ol.Flags.Contains(OrdLogFlags.EndOfTransaction))
                                {
                                    status |= 0x1000;
                                }

                                msg.OrderStatus = (OrderStatus)status;

                                secData.Item4.Add(msg);

                                if (secData.Item4.Count > maxBufCount)
                                {
                                    registry.GetOrderLogMessageStorage(security).Save(secData.Item4);
                                    secData.Item4.Clear();
                                }
                            };
                            break;
                        }
                        case StreamType.AuxInfo:
                        {
                            ((IAuxInfoStream)stream).Handler += (key, info) =>
                            {
                                secData.Item3.Add(new Level1ChangeMessage
                                {
                                    SecurityId = securityId,
                                    ServerTime = info.DateTime.ApplyTimeZone(TimeHelper.Moscow),
                                }
                                .TryAdd(Level1Fields.LastTradePrice, priceStep * info.Price)
                                .TryAdd(Level1Fields.BidsVolume, (decimal)info.BidTotal)
                                .TryAdd(Level1Fields.AsksVolume, (decimal)info.AskTotal)
                                .TryAdd(Level1Fields.HighPrice, priceStep * info.HiLimit)
                                .TryAdd(Level1Fields.LowPrice, priceStep * info.LoLimit)
                                .TryAdd(Level1Fields.OpenInterest, (decimal)info.OI));

                                if (secData.Item3.Count > maxBufCount)
                                {
                                    registry.GetLevel1MessageStorage(security).Save(secData.Item3);
                                    secData.Item3.Clear();
                                }
                            };
                            break;
                        }
                        case StreamType.Orders:
                        case StreamType.Trades:
                        case StreamType.Messages:
                        case StreamType.None:
                        {
                            continue;
                        }
                        default:
                            throw new ArgumentOutOfRangeException("Неподдерживаемый тип потока {0}.".Put(stream.Type));
                    }
                }

                while (qr.CurrentDateTime != DateTime.MaxValue && _isStarted)
                    qr.Read(true);
            }

            if (!_isStarted)
                return;

            foreach (var pair in data)
            {
                if (pair.Value.Item1.Any())
                {
                    registry.GetQuoteMessageStorage(pair.Key, format: format).Save(pair.Value.Item1);
                }

                if (pair.Value.Item2.Any())
                {
                    registry.GetTickMessageStorage(pair.Key, format: format).Save(pair.Value.Item2);
                }

                if (pair.Value.Item3.Any())
                {
                    registry.GetLevel1MessageStorage(pair.Key, format: format).Save(pair.Value.Item3);
                }

                if (pair.Value.Item4.Any())
                {
                    registry.GetOrderLogMessageStorage(pair.Key, format: format).Save(pair.Value.Item4);
                }
            }

            File.AppendAllLines(_convertedFilesFile, new[] { fileNameKey });
            _convertedFiles.Add(fileNameKey);
        }
Ejemplo n.º 31
0
		/// <summary>
		/// Initializes a new instance of the <see cref="BatchEmulation"/>.
		/// </summary>
		/// <param name="securities">Instruments, the operation will be performed with.</param>
		/// <param name="portfolios">Portfolios, the operation will be performed with.</param>
		/// <param name="storageRegistry">Market data storage.</param>
		public BatchEmulation(IEnumerable<Security> securities, IEnumerable<Portfolio> portfolios, IStorageRegistry storageRegistry)
			: this(new CollectionSecurityProvider(securities), portfolios, storageRegistry)
		{
		}
Ejemplo n.º 32
0
		private void Process(IStorageRegistry storageRegistry, Security security, StorageFormats format, IMarketDataDrive drive)
		{
			this.GuiSync(() =>
			{
				_visibleEntries.Clear();
			
				Columns.RemoveRange(_candleColumns.Values);
				_candleColumns.Values.ForEach(c => FormatRules.Remove(c));
				_candleColumns.Clear();
			});

			if (security == null)
				return;

			var dict = new Dictionary<DateTime, MarketDataEntry>();

			drive = drive ?? storageRegistry.DefaultDrive;

			var candles = new Dictionary<string, DataType>();

			lock (_syncObject)
			{
				if (_isChanged)
					return;
			}

			foreach (var tuple in drive.GetAvailableDataTypes(security.ToSecurityId(), format))
			{
				if (tuple.MessageType.IsCandleMessage())
					continue;

				var key = tuple.MessageType.Name.Replace("CandleMessage", string.Empty) + " " + tuple.Arg;
				candles.Add(key, tuple.Clone());
			}

			var candleNames = candles.Keys.ToArray();

			if (candleNames.Length > 0)
			{
				this.GuiSync(() =>
				{
					foreach (var candleName in candleNames)
					{
						var column = new DataGridTextColumn
						{
							Header = candleName,
							Binding = new Binding
							{
								Path = new PropertyPath("Candles[{0}]".Put(candleName)),
								Converter = new BoolToCheckMarkConverter()
							}
						};
						//var cbElement = new FrameworkElementFactory(typeof(CheckBox));

						//var column = new DataGridTemplateColumn
						//{
						//	Header = candle,
						//	CellStyle = new Style(),
						//	//SortMemberPath = "Candles[{0}]".Put(key)
						//	CellTemplate = new DataTemplate(typeof(CheckBox))
						//	{
						//		VisualTree = cbElement,
						//	}
						//};

						//var bind = new Binding { Path = new PropertyPath("Candles[{0}]".Put(candle)) };
						
						//cbElement.SetBinding(ToggleButton.IsCheckedProperty, bind);
						//cbElement.SetValue(IsHitTestVisibleProperty, false);
						//cbElement.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Center);

						Columns.Add(column);
						_candleColumns.Add(candleName, column);

						//ApplyFormatRules(column);
					}

					//ApplyFormatRules();
				});
			}

			Add(dict, storageRegistry.GetTickMessageStorage(security, drive, format).Dates, candleNames, e => e.IsTick = true);
			Add(dict, storageRegistry.GetQuoteMessageStorage(security, drive, format).Dates, candleNames, e => e.IsDepth = true);
			Add(dict, storageRegistry.GetLevel1MessageStorage(security, drive, format).Dates, candleNames, e => e.IsLevel1 = true);
			Add(dict, storageRegistry.GetOrderLogMessageStorage(security, drive, format).Dates, candleNames, e => e.IsOrderLog = true);

			foreach (var c in candleNames)
			{
				var candleName = c;

				lock (_syncObject)
				{
					if (_isChanged)
						return;
				}

				var tuple = candles[candleName];
				Add(dict, storageRegistry.GetCandleMessageStorage(tuple.MessageType, security, tuple.Arg, drive, format).Dates, candleNames, e => e.Candles[candleName] = true);
			}

			if (dict.Count > 0)
			{
				// add gaps by working days
				var emptyDays = dict.Keys.Min().Range(dict.Keys.Max(), TimeSpan.FromDays(1))
					.Where(d => !dict.ContainsKey(d)/* && security.Board.WorkingTime.IsTradeDate(d, true)*/)
					.OrderBy()
					.ToList();

				foreach (var monthGroup in emptyDays.ToArray().GroupBy(d => new DateTime(d.Year, d.Month, 1)))
				{
					var firstDay = monthGroup.First();
					var lastDay = monthGroup.Last();

					// whole month do not have any values
					if (firstDay.Day == 1 && lastDay.Day == monthGroup.Key.DaysInMonth())
						emptyDays.RemoveRange(monthGroup);
				}

				Add(dict, emptyDays, candleNames, e => { });
			}

			lock (_syncObject)
			{
				if (_isChanged)
					return;
			}

			this.GuiSync(RefreshSort);
		}
Ejemplo n.º 33
0
		private void InitializeDataSource()
		{
			_storageRegistry = new StorageRegistry();
			ConfigManager.RegisterService(_storageRegistry);

			_entityRegistry = (HydraEntityRegistry)ConfigManager.GetService<IEntityRegistry>();
			_entityRegistry.TasksSettings.Recycle = false;
			((SecurityList)_entityRegistry.Securities).BulkLoad = true;

			_dbFile = _entityRegistry.FirstTimeInit(Properties.Resources.StockSharp, db => _entityRegistry.Version = HydraEntityRegistry.LatestVersion);

			CheckDatabase();

			ConfigManager.RegisterService<IExchangeInfoProvider>(new ExchangeInfoProvider(_entityRegistry));

			var allSec = _entityRegistry.Securities.GetAllSecurity();

			if (allSec != null)
				return;

			_entityRegistry.Securities.Add(new Security
			{
				Id = Core.Extensions.AllSecurityId,
				Code = "ALL",
				//Class = task.GetDisplayName(),
				Name = LocalizedStrings.Str2835,
				Board = ExchangeBoard.Associated,
				ExtensionInfo = new Dictionary<object, object>(),
			});
			_entityRegistry.Securities.DelayAction.WaitFlush();
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
        /// </summary>
        /// <param name="securityProvider">The provider of information about instruments.</param>
        /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
        /// <param name="storageRegistry">Market data storage.</param>
        public HistoryEmulationConnector(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
        {
            if (securityProvider == null)
            {
                throw new ArgumentNullException("securityProvider");
            }

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

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

            // чтобы каждый раз при повторной эмуляции получать одинаковые номера транзакций
            TransactionIdGenerator = new IncrementalIdGenerator();

            _initialMoney = portfolios.ToDictionary(pf => pf, pf => pf.BeginValue);
            EntityFactory = new EmulationEntityFactory(securityProvider, _initialMoney.Keys);

            InMessageChannel  = new PassThroughMessageChannel();
            OutMessageChannel = new PassThroughMessageChannel();

            LatencyManager    = null;
            RiskManager       = null;
            CommissionManager = null;
            PnLManager        = null;
            SlippageManager   = null;

            _historyAdapter = new HistoryMessageAdapter(TransactionIdGenerator, securityProvider)
            {
                StorageRegistry = storageRegistry
            };
            _historyChannel = new InMemoryMessageChannel("History Out", SendOutError);

            Adapter = new HistoryBasketMessageAdapter(this);
            Adapter.InnerAdapters.Add(EmulationAdapter);
            Adapter.InnerAdapters.Add(new ChannelMessageAdapter(_historyAdapter, new InMemoryMessageChannel("History In", SendOutError), _historyChannel));

            // при тестировании по свечкам, время меняется быстрее и таймаут должен быть больше 30с.
            ReConnectionSettings.TimeOutInterval = TimeSpan.MaxValue;

            MaxMessageCount = 1000;

            TradesKeepCount = 0;
        }
Ejemplo n.º 35
0
		public TerminalConnector(IEntityRegistry entityRegistry, IStorageRegistry storageRegistry) : base(entityRegistry, storageRegistry)
		{
			
		}
Ejemplo n.º 36
0
 /// <summary>
 /// To get the storage of candles.
 /// </summary>
 /// <typeparam name="TCandle">The candle type.</typeparam>
 /// <typeparam name="TArg">The type of candle parameter.</typeparam>
 /// <param name="storageRegistry">The external storage.</param>
 /// <param name="security">Security.</param>
 /// <param name="arg">Candle arg.</param>
 /// <param name="drive">The storage. If a value is <see langword="null" />, <see cref="IStorageRegistry.DefaultDrive"/> will be used.</param>
 /// <param name="format">The format type. By default <see cref="StorageFormats.Binary"/> is passed.</param>
 /// <returns>The candles storage.</returns>
 public static IMarketDataStorage <Candle> GetCandleStorage <TCandle, TArg>(this IStorageRegistry storageRegistry, Security security, TArg arg, IMarketDataDrive drive = null, StorageFormats format = StorageFormats.Binary)
     where TCandle : Candle
 {
     return(storageRegistry.ThrowIfNull().GetCandleStorage(typeof(TCandle), security, arg, drive, format));
 }
Ejemplo n.º 37
0
		/// <summary>
		/// To refresh the table. It is carried out asynchronously.
		/// </summary>
		/// <param name="storageRegistry">Market-data storage.</param>
		/// <param name="security">Security.</param>
		/// <param name="format">Data format.</param>
		/// <param name="drive">Storage.</param>
		public void BeginMakeEntries(IStorageRegistry storageRegistry, Security security, StorageFormats format, IMarketDataDrive drive)
		{
			if (storageRegistry == null)
				throw new ArgumentNullException(nameof(storageRegistry));

			lock (_syncObject)
			{
				_query = new Query(storageRegistry, security, format, drive);
				_isChanged = true;

				if (_isFlushing)
					return;

				_isFlushing = true;

				ThreadingHelper
					.Thread(() => CultureInfo.InvariantCulture.DoInCulture(OnFlush))
					.Launch();
			}
		}
Ejemplo n.º 38
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
        /// </summary>
        /// <param name="securityProvider">The provider of information about instruments.</param>
        /// <param name="portfolios">Portfolios, the operation will be performed with.</param>
        /// <param name="storageRegistry">Market data storage.</param>
        public HistoryEmulationConnector(ISecurityProvider securityProvider, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
            : base(new EmulationMessageAdapter(new HistoryMessageAdapter(new IncrementalIdGenerator(), securityProvider), new MessageByLocalTimeQueue(), true) { OwnInnerAdapter = true }, securityProvider, new CollectionPortfolioProvider(portfolios))
        {
            if (securityProvider == null)
            {
                throw new ArgumentNullException(nameof(securityProvider));
            }

            if (portfolios == null)
            {
                throw new ArgumentNullException(nameof(portfolios));
            }

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

            // чтобы каждый раз при повторной эмуляции получать одинаковые номера транзакций
            TransactionIdGenerator = EmulationAdapter.TransactionIdGenerator;

            Adapter.LatencyManager    = null;
            Adapter.CommissionManager = null;
            Adapter.PnLManager        = null;
            Adapter.SlippageManager   = null;

            InMessageChannel  = new PassThroughMessageChannel();
            OutMessageChannel = new PassThroughMessageChannel();

            // при тестировании по свечкам, время меняется быстрее и таймаут должен быть больше 30с.
            //ReConnectionSettings.TimeOutInterval = TimeSpan.MaxValue;

            //MaxMessageCount = 1000;

            //Adapter.SupportCandlesCompression = false;
            Adapter.SupportBuildingFromOrderLog          = false;
            Adapter.SupportPartialDownload               = false;
            Adapter.SupportLookupTracking                = false;
            Adapter.SupportOrderBookTruncate             = false;
            Adapter.ConnectDisconnectEventOnFirstAdapter = false;
        }
Ejemplo n.º 39
0
		/// <summary>
		/// Initializes a new instance of the <see cref="HistoryEmulationConnector"/>.
		/// </summary>
		/// <param name="securities">Instruments, the operation will be performed with.</param>
		/// <param name="portfolios">Portfolios, the operation will be performed with.</param>
		/// <param name="storageRegistry">Market data storage.</param>
		public HistoryEmulationConnector(IEnumerable<Security> securities, IEnumerable<Portfolio> portfolios, IStorageRegistry storageRegistry)
			: this((ISecurityProvider)new CollectionSecurityProvider(securities), portfolios, storageRegistry)
		{
		}
Ejemplo n.º 40
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);
            }
        }
Ejemplo n.º 41
0
		/// <summary>
		/// The method is called when the <see cref="Strategy.Start"/> method has been called and the <see cref="Strategy.ProcessState"/> state has been taken the <see cref="ProcessStates.Started"/> value.
		/// </summary>
		protected override void OnStarted()
		{
			var storateRegistry = new StorageRegistry();

			var drive = Environment.GetValue<IMarketDataDrive>("Drive");
			if (drive != null)
				storateRegistry.DefaultDrive = drive;

			StorateRegistry = storateRegistry;

			ThreadingHelper
				.Thread(() =>
				{
					try
					{
						OnAnalyze();
					}
					catch (Exception ex)
					{
						this.AddErrorLog(ex);
						ex.LogError();
					}
				})
				.Name("{0} analyze thread.".Put(Name))
				.Culture(CultureInfo.InvariantCulture)
				.Launch();
		}
Ejemplo n.º 42
0
 /// <summary>
 /// Создать <see cref="BatchEmulation"/>.
 /// </summary>
 /// <param name="securities">Инструменты, с которыми будет вестись работа.</param>
 /// <param name="portfolios">Портфели, с которыми будет вестись работа.</param>
 /// <param name="storageRegistry">Хранилище данных.</param>
 public BatchEmulation(IEnumerable <Security> securities, IEnumerable <Portfolio> portfolios, IStorageRegistry storageRegistry)
     : this(new CollectionSecurityProvider(securities), portfolios, storageRegistry)
 {
 }