Ejemplo n.º 1
0
        private IEnumerableEx <QuoteChangeMessage> GetDepths()
        {
            int maxDepth;

            if (!int.TryParse(Depth.Text, out maxDepth))
            {
                maxDepth = int.MaxValue;
            }

            if (maxDepth <= 0)
            {
                maxDepth = 1;
            }

            var interval = TimeSpan.FromMilliseconds(DepthGenerationInterval.Value ?? 0);

            switch (BuildFrom.SelectedIndex)
            {
            case 0:
            {
                var retVal = StorageRegistry
                             .GetQuoteMessageStorage(SelectedSecurity, Drive, StorageFormat)
                             .Load(From, To + TimeHelper.LessOneDay);

                return(retVal
                       .Select(md =>
                    {
                        md.Bids = md.Bids.Take(maxDepth).ToArray();
                        md.Asks = md.Asks.Take(maxDepth).ToArray();
                        return md;
                    })
                       .WhereWithPrevious((prev, curr) => (curr.ServerTime - prev.ServerTime) >= interval)
                       .ToEx(retVal.Count));
            }

            case 1:
            {
                return(StorageRegistry
                       .GetOrderLogMessageStorage(SelectedSecurity, Drive, StorageFormat)
                       .Load(From + new TimeSpan(18, 45, 0), To + TimeHelper.LessOneDay + new TimeSpan(18, 45, 0))
                       // TODO
                       .ToMarketDepths(OrderLogBuilders.Plaza2.CreateBuilder(SelectedSecurity.ToSecurityId()), interval, maxDepth));
            }

            case 2:
            {
                var level1 = StorageRegistry
                             .GetLevel1MessageStorage(SelectedSecurity, Drive, StorageFormat)
                             .Load(From, To + TimeHelper.LessOneDay);

                return(level1.ToOrderBooks());
            }

            default:
                throw new InvalidOperationException();
            }
        }
Ejemplo n.º 2
0
        private IEnumerableEx <CandleMessage> GetCandles()
        {
            var from = From.Value;
            var to   = To.Value.EndOfDay();

            switch (BuildFrom.SelectedIndex)
            {
            case 0:
                return(StorageRegistry
                       .GetCandleMessageStorage(CandleSeries.CandleType.ToCandleMessageType(), CandleSeries.Security, CandleSeries.Arg, Drive, StorageFormat)
                       .Load(from, to));

            case 1:
                return(StorageRegistry
                       .GetExecutionStorage(SelectedSecurity, ExecutionTypes.Tick, Drive, StorageFormat)
                       .Load(from, to)
                       .ToCandles(CandleSeries));

            case 2:
                return(StorageRegistry
                       .GetExecutionStorage(SelectedSecurity, ExecutionTypes.OrderLog, Drive, StorageFormat)
                       .Load(from, to)
                       .ToTicks()
                       .ToCandles(CandleSeries));

            case 3:
                return(StorageRegistry
                       .GetQuoteMessageStorage(SelectedSecurity, Drive, StorageFormat)
                       .Load(from, to)
                       .ToCandles(CandleSeries));

            case 4:
                return(StorageRegistry
                       .GetExecutionStorage(SelectedSecurity, ExecutionTypes.OrderLog, Drive, StorageFormat)
                       .Load(from, to)
                       .ToMarketDepths(OrderLogBuilders.Plaza2.CreateBuilder(SelectedSecurity.ToSecurityId()))
                       .ToCandles(CandleSeries));

            case 5:
                return(StorageRegistry
                       .GetCandleMessageStorage(typeof(TimeFrameCandleMessage), SelectedSecurity, TimeSpan.FromMinutes(1), Drive, StorageFormat)
                       .Load(from, to)
                       .ToTrades(SelectedSecurity.VolumeStep ?? 1m)
                       .ToCandles(CandleSeries));

            case 6:
                return(StorageRegistry
                       .GetLevel1MessageStorage(SelectedSecurity, Drive, StorageFormat)
                       .Load(from, to)
                       .ToTicks()
                       .ToCandles(CandleSeries));

            default:
                throw new InvalidOperationException(LocalizedStrings.Str2874Params.Put(BuildFrom.SelectedIndex));
            }
        }
Ejemplo n.º 3
0
        private IEnumerableEx <QuoteChangeMessage> GetDepths()
        {
            int maxDepth;

            if (!int.TryParse(Depth.Text, out maxDepth))
            {
                maxDepth = int.MaxValue;
            }

            if (maxDepth <= 0)
            {
                maxDepth = 1;
            }

            var interval = TimeSpan.FromMilliseconds(DepthGenerationInterval.Value ?? 0);

            if (IsBuildFromOrderLog.IsChecked == true)
            {
                return(StorageRegistry
                       .GetExecutionStorage(SelectedSecurity, ExecutionTypes.OrderLog, Drive, StorageFormat)
                       .Load(From + new TimeSpan(18, 45, 0), To + TimeHelper.LessOneDay + new TimeSpan(18, 45, 0))
                       // TODO
                       .ToMarketDepths(OrderLogBuilders.Plaza2.CreateBuilder(SelectedSecurity.ToSecurityId()), interval, maxDepth));
            }

            var retVal = StorageRegistry
                         .GetQuoteMessageStorage(SelectedSecurity, Drive, StorageFormat)
                         .Load(From, To + TimeHelper.LessOneDay);

            return(retVal
                   .Select(md =>
            {
                md.Bids = md.Bids.Take(maxDepth).ToArray();
                md.Asks = md.Asks.Take(maxDepth).ToArray();
                return md;
            })
                   .WhereWithPrevious((prev, curr) => (curr.ServerTime - prev.ServerTime) >= interval)
                   .ToEx(retVal.Count));
        }
Ejemplo n.º 4
0
        private void ProcessMarketDataMessage(MarketDataMessage message)
        {
            var securityId = message.SecurityId;
            var security   = SecurityProvider.LookupById(securityId.ToStringId());

            if (security == null)
            {
                RaiseMarketDataMessage(message, new InvalidOperationException(LocalizedStrings.Str704Params.Put(securityId)));
                return;
            }

            if (StorageRegistry == null)
            {
                RaiseMarketDataMessage(message, new InvalidOperationException(LocalizedStrings.Str1117Params.Put(message.DataType, securityId)));
                return;
            }

            var history = message as HistorySourceMessage;

            Exception error = null;

            switch (message.DataType)
            {
            case MarketDataTypes.Level1:
            {
                if (message.IsSubscribe)
                {
                    if (history == null)
                    {
                        BasketStorage.AddStorage(StorageRegistry.GetLevel1MessageStorage(security, Drive, StorageFormat));

                        BasketStorage.AddStorage(new InMemoryMarketDataStorage <ClearingMessage>(security, null, date => new[]
                            {
                                new ClearingMessage
                                {
                                    LocalTime        = date.Date + security.Board.ExpiryTime,
                                    SecurityId       = securityId,
                                    ClearMarketDepth = true
                                }
                            }));
                    }
                    else
                    {
                        BasketStorage.AddStorage(new InMemoryMarketDataStorage <Level1ChangeMessage>(security, null, history.GetMessages));
                    }
                }
                else
                {
                    BasketStorage.RemoveStorage <IMarketDataStorage <Level1ChangeMessage> >(security, MessageTypes.Level1Change, null);
                    BasketStorage.RemoveStorage <InMemoryMarketDataStorage <ClearingMessage> >(security, ExtendedMessageTypes.Clearing, null);
                }

                break;
            }

            case MarketDataTypes.MarketDepth:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.AddStorage(history == null
                                                        ? StorageRegistry.GetQuoteMessageStorage(security, Drive, StorageFormat)
                                                        : new InMemoryMarketDataStorage <QuoteChangeMessage>(security, null, history.GetMessages));
                }
                else
                {
                    BasketStorage.RemoveStorage <IMarketDataStorage <QuoteChangeMessage> >(security, MessageTypes.QuoteChange, null);
                }

                break;
            }

            case MarketDataTypes.Trades:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.AddStorage(history == null
                                                        ? StorageRegistry.GetTickMessageStorage(security, Drive, StorageFormat)
                                                        : new InMemoryMarketDataStorage <ExecutionMessage>(security, null, history.GetMessages));
                }
                else
                {
                    BasketStorage.RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, ExecutionTypes.Tick);
                }

                break;
            }

            case MarketDataTypes.OrderLog:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.AddStorage(history == null
                                                        ? StorageRegistry.GetOrderLogMessageStorage(security, Drive, StorageFormat)
                                                        : new InMemoryMarketDataStorage <ExecutionMessage>(security, null, history.GetMessages));
                }
                else
                {
                    BasketStorage.RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, ExecutionTypes.OrderLog);
                }

                break;
            }

            case MarketDataTypes.CandleTimeFrame:
            case MarketDataTypes.CandleTick:
            case MarketDataTypes.CandleVolume:
            case MarketDataTypes.CandleRange:
            case MarketDataTypes.CandlePnF:
            case MarketDataTypes.CandleRenko:
            {
                var msgType = message.DataType.ToCandleMessageType();

                if (message.IsSubscribe)
                {
                    var candleType = message.DataType.ToCandleMessage();

                    BasketStorage.AddStorage(history == null
                                                        ? StorageRegistry.GetCandleMessageStorage(candleType, security, message.Arg, Drive, StorageFormat)
                                                        : new InMemoryMarketDataStorage <CandleMessage>(security, message.Arg, history.GetMessages, candleType));
                }
                else
                {
                    BasketStorage.RemoveStorage <IMarketDataStorage <CandleMessage> >(security, msgType, message.Arg);
                }

                break;
            }

            default:
                error = new InvalidOperationException(LocalizedStrings.Str1118Params.Put(message.DataType));
                break;
            }

            RaiseMarketDataMessage(message, error);
        }
Ejemplo n.º 5
0
        protected override TimeSpan OnProcess()
        {
            var hasSecurities = false;

            this.AddInfoLog(LocalizedStrings.Str2306Params.Put(_settings.StartFrom));

            foreach (var security in GetWorkingSecurities())
            {
                hasSecurities = true;

                if (!CanProcess())
                {
                    break;
                }

                //this.AddInfoLog("Обработка инструмента {0}. Конвертация {1}.".Put(security.Security.Id, mode));

                IMarketDataStorage fromStorage;
                IMarketDataStorage toStorage;

                switch (_settings.ConvertMode)
                {
                case ConvertModes.OrderLogToTicks:
                    fromStorage = StorageRegistry.GetOrderLogMessageStorage(security.Security, _settings.Drive, _settings.StorageFormat);
                    toStorage   = StorageRegistry.GetTickMessageStorage(security.Security, _settings.DestinationDrive, _settings.DestinationStorageFormat);
                    break;

                case ConvertModes.OrderLogToDepths:
                    fromStorage = StorageRegistry.GetOrderLogMessageStorage(security.Security, _settings.Drive, _settings.StorageFormat);
                    toStorage   = StorageRegistry.GetQuoteMessageStorage(security.Security, _settings.DestinationDrive, _settings.DestinationStorageFormat);
                    break;

                case ConvertModes.OrderLogToCandles:
                    fromStorage = StorageRegistry.GetOrderLogMessageStorage(security.Security, _settings.Drive, _settings.StorageFormat);
                    toStorage   = StorageRegistry.GetCandleMessageStorage(_settings.CandleSettings.CandleType.ToCandleMessageType(), security.Security, _settings.CandleSettings.Arg, _settings.DestinationDrive, _settings.DestinationStorageFormat);
                    break;

                case ConvertModes.TicksToCandles:
                    fromStorage = StorageRegistry.GetTickMessageStorage(security.Security, _settings.Drive, _settings.StorageFormat);
                    toStorage   = StorageRegistry.GetCandleMessageStorage(_settings.CandleSettings.CandleType.ToCandleMessageType(), security.Security, _settings.CandleSettings.Arg, _settings.DestinationDrive, _settings.DestinationStorageFormat);
                    break;

                case ConvertModes.DepthsToCandles:
                    fromStorage = StorageRegistry.GetQuoteMessageStorage(security.Security, _settings.Drive, _settings.StorageFormat);
                    toStorage   = StorageRegistry.GetCandleMessageStorage(_settings.CandleSettings.CandleType.ToCandleMessageType(), security.Security, _settings.CandleSettings.Arg, _settings.DestinationDrive, _settings.DestinationStorageFormat);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                var from = fromStorage.GetFromDate();
                var to   = fromStorage.GetToDate();

                if (from == null || to == null)
                {
                    //this.AddInfoLog("Нет данных для конвертации.");
                    continue;
                }

                from = _settings.StartFrom.Max(from.Value);
                to   = (DateTime.Today - TimeSpan.FromDays(_settings.Offset)).Min(to.Value);

                foreach (var date in from.Value.Range(to.Value, TimeSpan.FromDays(1)).Except(toStorage.Dates))
                {
                    if (!CanProcess())
                    {
                        break;
                    }

                    this.AddInfoLog(LocalizedStrings.Str3786Params.Put(security.Security.Id, _settings.ConvertMode, date));

                    try
                    {
                        switch (_settings.ConvertMode)
                        {
                        case ConvertModes.OrderLogToTicks:
                        {
                            var ticks = ((IMarketDataStorage <ExecutionMessage>)fromStorage)
                                        .Load(date)
                                        .ToTicks();

                            toStorage.Save(ticks);
                            RaiseDataLoaded(security.Security, typeof(ExecutionMessage), ExecutionTypes.Tick, date, ticks.Count);
                            break;
                        }

                        case ConvertModes.OrderLogToDepths:
                        {
                            var depths = ((IMarketDataStorage <ExecutionMessage>)fromStorage)
                                         .Load(date)
                                         .ToMarketDepths(_settings.MarketDepthInterval, _settings.MarketDepthMaxDepth);

                            toStorage.Save(depths);
                            RaiseDataLoaded(security.Security, typeof(QuoteChangeMessage), null, date, depths.Count);
                            break;
                        }

                        case ConvertModes.OrderLogToCandles:
                        {
                            var candles = ((IMarketDataStorage <ExecutionMessage>)fromStorage)
                                          .Load(date)
                                          .ToTicks()
                                          .ToCandles(new CandleSeries(_settings.CandleSettings.CandleType, security.Security, _settings.CandleSettings.Arg));

                            toStorage.Save(candles);
                            RaiseDataLoaded(security.Security, _settings.CandleSettings.CandleType, _settings.CandleSettings.Arg, date, candles.Count);
                            break;
                        }

                        case ConvertModes.TicksToCandles:
                        {
                            var candles = ((IMarketDataStorage <ExecutionMessage>)fromStorage)
                                          .Load(date)
                                          .ToCandles(new CandleSeries(_settings.CandleSettings.CandleType, security.Security, _settings.CandleSettings.Arg));

                            toStorage.Save(candles);
                            RaiseDataLoaded(security.Security, _settings.CandleSettings.CandleType, _settings.CandleSettings.Arg, date, candles.Count);
                            break;
                        }

                        case ConvertModes.DepthsToCandles:
                        {
                            var candles = ((IMarketDataStorage <QuoteChangeMessage>)fromStorage)
                                          .Load(date)
                                          .ToCandles(new CandleSeries(_settings.CandleSettings.CandleType, security.Security, _settings.CandleSettings.Arg));

                            toStorage.Save(candles);
                            RaiseDataLoaded(security.Security, _settings.CandleSettings.CandleType, _settings.CandleSettings.Arg, date, candles.Count);
                            break;
                        }

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                    catch (Exception ex)
                    {
                        HandleError(ex);
                    }
                }
            }

            if (!hasSecurities)
            {
                this.AddWarningLog(LocalizedStrings.Str2292);
                return(TimeSpan.MaxValue);
            }

            if (CanProcess())
            {
                this.AddInfoLog(LocalizedStrings.Str2300);
            }

            return(base.OnProcess());
        }