Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BufferMessageAdapter"/>.
 /// </summary>
 /// <param name="innerAdapter">Underlying adapter.</param>
 /// <param name="settings">Storage settings.</param>
 /// <param name="buffer">Storage buffer.</param>
 /// <param name="snapshotRegistry">Snapshot storage registry.</param>
 public BufferMessageAdapter(IMessageAdapter innerAdapter, StorageCoreSettings settings, StorageBuffer buffer, SnapshotRegistry snapshotRegistry)
     : base(innerAdapter)
 {
     Settings         = settings ?? throw new ArgumentNullException(nameof(settings));
     Buffer           = buffer ?? throw new ArgumentNullException(nameof(buffer));
     SnapshotRegistry = snapshotRegistry;            // ?? throw new ArgumentNullException(nameof(snapshotRegistry));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StorageMetaInfoMessageAdapter"/>.
 /// </summary>
 /// <param name="innerAdapter">The adapter, to which messages will be directed.</param>
 /// <param name="securityStorage">Securities meta info storage.</param>
 /// <param name="positionStorage">Position storage.</param>
 /// <param name="exchangeInfoProvider">Exchanges and trading boards provider.</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 StorageMetaInfoMessageAdapter(IMessageAdapter innerAdapter, ISecurityStorage securityStorage,
                                      IPositionStorage positionStorage, IExchangeInfoProvider exchangeInfoProvider,
                                      IStorageRegistry storageRegistry, SnapshotRegistry snapshotRegistry, CandleBuilderProvider candleBuilderProvider)
     : base(innerAdapter, storageRegistry, snapshotRegistry, candleBuilderProvider)
 {
     _securityStorage      = securityStorage ?? throw new ArgumentNullException(nameof(securityStorage));
     _positionStorage      = positionStorage ?? throw new ArgumentNullException(nameof(positionStorage));
     _exchangeInfoProvider = exchangeInfoProvider ?? throw new ArgumentNullException(nameof(_exchangeInfoProvider));
 }
        /// <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));

            if (!(this is StorageMetaInfoMessageAdapter))
            {
                StartStorageTimer();
            }
        }
Exemple #4
0
 private ISnapshotStorage GetSnapshotStorage(DataType dataType)
 => SnapshotRegistry.GetSnapshotStorage(dataType);
Exemple #5
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));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StorageProcessor"/>.
 /// </summary>
 /// <param name="storageRegistry">The storage of market data.</param>
 /// <param name="snapshotRegistry">Snapshot storage registry.</param>
 /// <param name="candleBuilderProvider">Candle builders provider.</param>
 public StorageProcessor(IStorageRegistry storageRegistry, SnapshotRegistry snapshotRegistry, CandleBuilderProvider candleBuilderProvider)
 {
     StorageRegistry       = storageRegistry;      // ?? throw new ArgumentNullException(nameof(storageRegistry));
     SnapshotRegistry      = snapshotRegistry;     // ?? throw new ArgumentNullException(nameof(snapshotRegistry));
     CandleBuilderProvider = candleBuilderProvider ?? throw new ArgumentNullException(nameof(candleBuilderProvider));
 }
        /// <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>
        /// <param name="snapshotRegistry">Snapshot storage registry.</param>
        public StorageMessageAdapter(IMessageAdapter innerAdapter, IEntityRegistry entityRegistry, IStorageRegistry storageRegistry, SnapshotRegistry snapshotRegistry)
            : base(innerAdapter)
        {
            _entityRegistry   = entityRegistry ?? throw new ArgumentNullException(nameof(entityRegistry));
            _storageRegistry  = storageRegistry ?? throw new ArgumentNullException(nameof(storageRegistry));
            _snapshotRegistry = snapshotRegistry ?? throw new ArgumentNullException(nameof(snapshotRegistry));

            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())
                    {
                        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)
                            {
                                if (message.TransactionId == 0 && message.OriginalTransactionId == 0)
                                {
                                    continue;
                                }

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

                                if (message.TransactionId == 0 && _cancellationTransactions.TryGetValue(message.OriginalTransactionId, out var newOriginId))
                                {
                                    // override cancel trans id by original order's registration trans id
                                    message.OriginalTransactionId = newOriginId;
                                }

                                message.SecurityId = secId;
                                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));
        }
 private ISnapshotStorage GetSnapshotStorage(DataType dataType)
 => SnapshotRegistry.GetSnapshotStorage(dataType.MessageType, dataType.Arg);