private void ProcessMarketDataMessage(MarketDataMessage message)
        {
            var securityId = message.SecurityId;
            var security   = SecurityProvider.LookupById(securityId);

            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);
        }
Beispiel #2
0
        protected override TimeSpan OnProcess()
        {
            var allSecurity = this.GetAllSecurity();

            // если фильтр по инструментам выключен (выбран инструмент все инструменты)
            IEnumerable <HydraTaskSecurity> selectedSecurities = (allSecurity != null
                                ? this.ToHydraSecurities(EntityRegistry.Securities.Filter(ExchangeBoard.DukasCopy))
                                : Settings.Securities
                                                                  ).ToArray();

            var source = new DukasCopySource();

            if (_settings.UseTemporaryFiles != TempFiles.NotUse)
            {
                source.DumpFolder = GetTempPath();
            }

            if (selectedSecurities.IsEmpty())
            {
                this.AddWarningLog(LocalizedStrings.Str2289);

                source.Refresh(EntityRegistry.Securities, new Security(), SaveSecurity, () => !CanProcess(false));

                selectedSecurities = this.ToHydraSecurities(EntityRegistry.Securities.Filter(ExchangeBoard.DukasCopy));
            }

            if (selectedSecurities.IsEmpty())
            {
                this.AddWarningLog(LocalizedStrings.Str2292);
                return(TimeSpan.MaxValue);
            }

            var startDate = _settings.StartFrom;
            var endDate   = DateTime.Today - TimeSpan.FromDays(_settings.DayOffset);

            var allDates = startDate.Range(endDate, TimeSpan.FromDays(1)).ToArray();

            foreach (var security in selectedSecurities)
            {
                if (!CanProcess())
                {
                    break;
                }

                #region LoadTicks
                if ((allSecurity ?? security).IsLevel1Enabled())
                {
                    var storage    = StorageRegistry.GetLevel1MessageStorage(security.Security, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

                    if (emptyDates.IsEmpty())
                    {
                        this.AddInfoLog(LocalizedStrings.Str2293Params, security.Security.Id);
                    }
                    else
                    {
                        foreach (var emptyDate in emptyDates)
                        {
                            if (!CanProcess())
                            {
                                break;
                            }

                            try
                            {
                                this.AddInfoLog(LocalizedStrings.Str2294Params, emptyDate, security.Security.Id);
                                var ticks = source.LoadTickMessages(security.Security, emptyDate);

                                if (ticks.Any())
                                {
                                    SaveLevel1Changes(security, ticks);
                                }
                                else
                                {
                                    this.AddDebugLog(LocalizedStrings.NoData);
                                }

                                if (_settings.UseTemporaryFiles == TempFiles.UseAndDelete)
                                {
                                    File.Delete(source.GetDumpFile(security.Security, emptyDate, emptyDate, typeof(Level1ChangeMessage), null));
                                }
                            }
                            catch (Exception ex)
                            {
                                HandleError(new InvalidOperationException(LocalizedStrings.Str2295Params
                                                                          .Put(emptyDate, security.Security.Id), ex));
                            }
                        }
                    }
                }
                else
                {
                    this.AddDebugLog(LocalizedStrings.MarketDataNotEnabled, security.Security.Id, typeof(Level1ChangeMessage).Name);
                }

                #endregion

                if (!CanProcess())
                {
                    break;
                }

                #region LoadCandles
                foreach (var pair in (allSecurity ?? security).GetCandleSeries())
                {
                    if (!CanProcess())
                    {
                        break;
                    }

                    if (pair.MessageType != typeof(TimeFrameCandleMessage))
                    {
                        this.AddWarningLog(LocalizedStrings.Str2296Params, pair);
                        continue;
                    }

                    var tf = (TimeSpan)pair.Arg;

                    var storage    = StorageRegistry.GetCandleMessageStorage(pair.MessageType, security.Security, tf, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

                    if (emptyDates.IsEmpty())
                    {
                        this.AddInfoLog(LocalizedStrings.Str2297Params, tf, security.Security.Id);
                        continue;
                    }

                    foreach (var emptyDate in emptyDates)
                    {
                        if (!CanProcess())
                        {
                            break;
                        }

                        try
                        {
                            this.AddInfoLog(LocalizedStrings.Str2298Params, tf, emptyDate, emptyDate, security.Security.Id);
                            var candles = source.LoadCandles(security.Security, tf, emptyDate, _settings.Side);

                            if (candles.Any())
                            {
                                SaveCandles(security, candles);
                            }
                            else
                            {
                                this.AddDebugLog(LocalizedStrings.NoData);
                            }

                            if (_settings.UseTemporaryFiles == TempFiles.UseAndDelete)
                            {
                                File.Delete(source.GetDumpFile(security.Security, emptyDate, emptyDate, typeof(TimeFrameCandleMessage), tf));
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleError(new InvalidOperationException(LocalizedStrings.Str2299Params
                                                                      .Put(tf, emptyDate, security.Security.Id), ex));
                        }
                    }
                }
                #endregion
            }

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

            return(base.OnProcess());
        }
Beispiel #3
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());
        }
Beispiel #4
0
        private void LoadData(CandleSeries series)
        {
            _currCandle    = null;
            _historyLoaded = false;
            _allCandles.Clear();

            _candleTransform.Process(new ResetMessage());
            _candleBuilder = series.CandleType.ToCandleMessageType().ToCandleMarketDataType().CreateCandleBuilder();

            Chart.Reset(new IChartElement[] { _candleElement });

            var storage = new StorageRegistry();

            BusyIndicator.IsBusy = true;

            var path    = HistoryPath.Folder;
            var isBuild = BuildFromTicks.IsChecked == true;
            var format  = Format.SelectedFormat;

            var maxDays = (isBuild || series.CandleType != typeof(TimeFrameCandle))
                                ? 5
                                : 30 * (int)((TimeSpan)series.Arg).TotalMinutes;

            _mdMsg = series.ToMarketDataMessage(true);

            Task.Factory.StartNew(() =>
            {
                var date = DateTime.MinValue;

                if (isBuild)
                {
                    foreach (var tick in storage.GetTickMessageStorage(series.Security, new LocalMarketDataDrive(path), format).Load())
                    {
                        _tradeGenerator.Process(tick);

                        if (_candleTransform.Process(tick))
                        {
                            var candles = _candleBuilder.Process(_mdMsg, _currCandle, _candleTransform);

                            lock (_lock)
                            {
                                foreach (var candle in candles)
                                {
                                    _currCandle = candle;
                                    _updatedCandles[candle.OpenTime] = Tuple.Create(candle, candle.State == CandleStates.Finished);
                                }
                            }
                        }

                        _lastTime = tick.ServerTime;

                        if (date != tick.ServerTime.Date)
                        {
                            date = tick.ServerTime.Date;

                            var str = date.To <string>();
                            this.GuiAsync(() => BusyIndicator.BusyContent = str);

                            maxDays--;

                            if (maxDays == 0)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    foreach (var candleMsg in storage.GetCandleMessageStorage(series.CandleType.ToCandleMessageType(), series.Security, series.Arg, new LocalMarketDataDrive(path), format).Load())
                    {
                        lock (_updatedCandles.SyncRoot)
                        {
                            _currCandle = candleMsg;
                            _updatedCandles[candleMsg.OpenTime] = Tuple.Create(candleMsg, true);
                        }

                        _lastTime = candleMsg.OpenTime;

                        if (candleMsg is TimeFrameCandleMessage)
                        {
                            _lastTime += (TimeSpan)series.Arg;
                        }

                        _tradeGenerator.Process(new ExecutionMessage
                        {
                            ExecutionType = ExecutionTypes.Tick,
                            SecurityId    = series.Security.ToSecurityId(),
                            ServerTime    = _lastTime,
                            TradePrice    = candleMsg.ClosePrice,
                        });

                        if (date != candleMsg.OpenTime.Date)
                        {
                            date = candleMsg.OpenTime.Date;

                            var str = date.To <string>();
                            this.GuiAsync(() => BusyIndicator.BusyContent = str);

                            maxDays--;

                            if (maxDays == 0)
                            {
                                break;
                            }
                        }
                    }
                }

                _historyLoaded = true;
            })
            .ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    Error(t.Exception.Message);
                }

                this.GuiAsync(() =>
                {
                    BusyIndicator.IsBusy = false;
                    Chart.IsAutoRange    = false;
                    //_areaComb.YAxises.First().AutoRange = false;
                });
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
Beispiel #5
0
        private void LoadData(CandleSeries series)
        {
            var msgType = series.CandleType.ToCandleMessageType();

            _transactionId = _transactionIdGenerator.GetNextId();
            _holder.Clear();
            _holder.CreateCandleSeries(_transactionId, series);

            _candleTransform.Process(new ResetMessage());
            _candleBuilder = _builderProvider.Get(msgType);

            var storage = new StorageRegistry();

            //BusyIndicator.IsBusy = true;

            var path    = HistoryPath.Folder;
            var isBuild = BuildFromTicks.IsChecked == true;
            var format  = Format.SelectedFormat;

            var maxDays = (isBuild || series.CandleType != typeof(TimeFrameCandle))
                                ? 2
                                : 30 * (int)((TimeSpan)series.Arg).TotalMinutes;

            _mdMsg = series.ToMarketDataMessage(true);

            Task.Factory.StartNew(() =>
            {
                var date = DateTime.MinValue;

                if (isBuild)
                {
                    foreach (var tick in storage.GetTickMessageStorage(series.Security.ToSecurityId(), new LocalMarketDataDrive(path), format).Load())
                    {
                        _tradeGenerator.Process(tick);

                        if (_candleTransform.Process(tick))
                        {
                            var candles = _candleBuilder.Process(_mdMsg, _currCandle, _candleTransform);

                            foreach (var candle in candles)
                            {
                                _currCandle = candle;
                                _updatedCandles.Add((CandleMessage)candle.Clone());
                            }
                        }

                        _lastTime = tick.ServerTime;

                        if (date != tick.ServerTime.Date)
                        {
                            date = tick.ServerTime.Date;

                            //var str = date.To<string>();
                            //this.GuiAsync(() => BusyIndicator.BusyContent = str);

                            maxDays--;

                            if (maxDays == 0)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    foreach (var candleMsg in storage.GetCandleMessageStorage(msgType, series.Security.ToSecurityId(), series.Arg, new LocalMarketDataDrive(path), format).Load())
                    {
                        if (candleMsg.State != CandleStates.Finished)
                        {
                            candleMsg.State = CandleStates.Finished;
                        }

                        _currCandle = candleMsg;
                        _updatedCandles.Add(candleMsg);

                        _lastTime = candleMsg.OpenTime;

                        if (candleMsg is TimeFrameCandleMessage)
                        {
                            _lastTime += (TimeSpan)series.Arg;
                        }

                        _tradeGenerator.Process(new ExecutionMessage
                        {
                            ExecutionType = ExecutionTypes.Tick,
                            SecurityId    = series.Security.ToSecurityId(),
                            ServerTime    = _lastTime,
                            TradePrice    = candleMsg.ClosePrice,
                        });

                        if (date != candleMsg.OpenTime.Date)
                        {
                            date = candleMsg.OpenTime.Date;

                            //var str = date.To<string>();
                            //this.GuiAsync(() => BusyIndicator.BusyContent = str);

                            maxDays--;

                            if (maxDays == 0)
                            {
                                break;
                            }
                        }
                    }
                }

                _historyLoaded = true;
            })
            .ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    Error(t.Exception.Message);
                }

                //BusyIndicator.IsBusy = false;
                Chart.IsAutoRange             = false;
                ModifyAnnotationBtn.IsEnabled = true;
                NewAnnotationBtn.IsEnabled    = true;
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
Beispiel #6
0
        protected override TimeSpan OnProcess()
        {
            var source = new FinamHistorySource();

            if (_settings.UseTemporaryFiles != TempFiles.NotUse)
            {
                source.DumpFolder = GetTempPath();
            }

            var allSecurity = this.GetAllSecurity();

            // если фильтр по инструментам выключен (выбран инструмент все инструменты)
            var selectedSecurities = (allSecurity != null ? this.ToHydraSecurities(_finamSecurityStorage.Securities) : Settings.Securities).ToArray();

            var hasNonFinam = selectedSecurities.Any(s => !IsFinam(s));

            if (selectedSecurities.IsEmpty() || hasNonFinam)
            {
                this.AddWarningLog(selectedSecurities.IsEmpty()
                                        ? LocalizedStrings.Str2289
                                        : LocalizedStrings.Str2290.Put("Finam"));

                source.Refresh(_finamSecurityStorage, new Security(), SaveSecurity, () => !CanProcess(false));

                selectedSecurities = (allSecurity != null ? this.ToHydraSecurities(_finamSecurityStorage.Securities) : Settings.Securities)
                                     .Where(s =>
                {
                    var retVal = IsFinam(s);

                    if (!retVal)
                    {
                        this.AddWarningLog(LocalizedStrings.Str2291Params, s.Security.Id, "Finam");
                    }

                    return(retVal);
                }).ToArray();
            }

            if (!CanProcess())
            {
                return(base.OnProcess());
            }

            if (selectedSecurities.IsEmpty())
            {
                this.AddWarningLog(LocalizedStrings.Str2292);
                return(TimeSpan.MaxValue);
            }

            var startDate = _settings.StartFrom;
            var endDate   = DateTime.Today - TimeSpan.FromDays(_settings.DayOffset);

            var allDates = startDate.Range(endDate, TimeSpan.FromDays(1)).ToArray();

            foreach (var security in selectedSecurities)
            {
                if (!CanProcess())
                {
                    break;
                }

                #region LoadTrades
                if ((allSecurity ?? security).IsTicksEnabled())
                {
                    var storage    = StorageRegistry.GetTradeStorage(security.Security, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

                    if (emptyDates.IsEmpty())
                    {
                        this.AddInfoLog(LocalizedStrings.Str2293Params, security.Security.Id);
                    }
                    else
                    {
                        foreach (var emptyDate in emptyDates)
                        {
                            if (!CanProcess())
                            {
                                break;
                            }

                            if (_settings.IgnoreWeekends && !security.IsTradeDate(emptyDate))
                            {
                                this.AddDebugLog(LocalizedStrings.WeekEndDate, emptyDate);
                                continue;
                            }

                            try
                            {
                                this.AddInfoLog(LocalizedStrings.Str2294Params, emptyDate, security.Security.Id);
                                var trades = source.GetTicks(security.Security, emptyDate, emptyDate);

                                if (trades.Any())
                                {
                                    SaveTicks(security, trades);
                                }
                                else
                                {
                                    this.AddDebugLog(LocalizedStrings.NoData);
                                }

                                if (_settings.UseTemporaryFiles == TempFiles.UseAndDelete)
                                {
                                    File.Delete(source.GetDumpFile(security.Security, emptyDate, emptyDate, typeof(ExecutionMessage), ExecutionTypes.Tick));
                                }
                            }
                            catch (Exception ex)
                            {
                                HandleError(new InvalidOperationException(LocalizedStrings.Str2295Params
                                                                          .Put(emptyDate, security.Security.Id), ex));
                            }
                        }
                    }
                }
                else
                {
                    this.AddDebugLog(LocalizedStrings.MarketDataNotEnabled, security.Security.Id, typeof(Trade).Name);
                }

                #endregion

                if (!CanProcess())
                {
                    break;
                }

                #region LoadCandles
                foreach (var pair in (allSecurity ?? security).GetCandleSeries())
                {
                    if (!CanProcess())
                    {
                        break;
                    }

                    if (pair.MessageType != typeof(TimeFrameCandleMessage))
                    {
                        this.AddWarningLog(LocalizedStrings.Str2296Params, pair);
                        continue;
                    }

                    var tf = (TimeSpan)pair.Arg;

                    var storage    = StorageRegistry.GetCandleMessageStorage(pair.MessageType, security.Security, tf, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

                    if (emptyDates.IsEmpty())
                    {
                        this.AddInfoLog(LocalizedStrings.Str2297Params, tf, security.Security.Id);
                        continue;
                    }

                    var currDate = emptyDates.First();
                    var lastDate = emptyDates.Last();

                    while (currDate <= lastDate)
                    {
                        if (!CanProcess())
                        {
                            break;
                        }

                        if (_settings.IgnoreWeekends && !security.IsTradeDate(currDate))
                        {
                            this.AddDebugLog(LocalizedStrings.WeekEndDate, currDate);
                            currDate = currDate.AddDays(1);
                            continue;
                        }

                        try
                        {
                            var till = currDate.AddDays(_settings.CandleDayStep - 1);
                            this.AddInfoLog(LocalizedStrings.Str2298Params, tf, currDate, till, security.Security.Id);

                            var candles = source.GetCandles(security.Security, tf, currDate, till);

                            if (candles.Any())
                            {
                                SaveCandles(security, candles);
                            }
                            else
                            {
                                this.AddDebugLog(LocalizedStrings.NoData);
                            }

                            if (_settings.UseTemporaryFiles == TempFiles.UseAndDelete)
                            {
                                File.Delete(source.GetDumpFile(security.Security, currDate, till, typeof(TimeFrameCandleMessage), tf));
                            }

                            currDate = currDate.AddDays(_settings.CandleDayStep);
                        }
                        catch (Exception ex)
                        {
                            HandleError(new InvalidOperationException(LocalizedStrings.Str2299Params
                                                                      .Put(tf, currDate, security.Security.Id), ex));
                        }
                    }
                }
                #endregion
            }

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

                _settings.StartFrom = endDate;
                SaveSettings();
            }

            return(base.OnProcess());
        }
Beispiel #7
0
        protected override TimeSpan OnProcess()
        {
            // если фильтр по инструментам выключен (выбран инструмент все инструменты)
            if (this.GetAllSecurity() != null)
            {
                this.AddWarningLog(LocalizedStrings.Str2549);
                return(TimeSpan.MaxValue);
            }

            if (_settings.IsRealTime)
            {
                return(base.OnProcess());
            }

            var startDate = _settings.StartFrom;
            var endDate   = DateTime.Today - TimeSpan.FromDays(_settings.Offset);

            var allDates = startDate.Range(endDate, TimeSpan.FromDays(1)).ToArray();

            var hasSecurities = false;

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

                if (!CanProcess())
                {
                    break;
                }

                if (security.IsLevel1Enabled())
                {
                    var tradeStorage = StorageRegistry.GetTickMessageStorage(security.Security, _settings.Drive, _settings.StorageFormat);

                    foreach (var date in allDates.Except(tradeStorage.Dates))
                    {
                        if (!CanProcess())
                        {
                            break;
                        }

                        if (_settings.IgnoreWeekends && !security.IsTradeDate(date))
                        {
                            this.AddDebugLog(LocalizedStrings.WeekEndDate, date);
                            continue;
                        }

                        this.AddInfoLog(LocalizedStrings.Str2294Params, date, security.Security.Id);

                        bool isSuccess;
                        var  trades = ((IQFeedTrader)Connector).GetHistoricalLevel1(security.Security.ToSecurityId(), date, date.EndOfDay(), out isSuccess);

                        if (isSuccess)
                        {
                            if (trades.Any())
                            {
                                SaveLevel1Changes(security, trades);
                            }
                            else
                            {
                                this.AddDebugLog(LocalizedStrings.NoData);
                            }
                        }
                        else
                        {
                            this.AddErrorLog(LocalizedStrings.Str2550);
                        }
                    }
                }
                else
                {
                    this.AddDebugLog(LocalizedStrings.MarketDataNotEnabled, security.Security.Id, typeof(Level1ChangeMessage).Name);
                }

                foreach (var series in security.GetCandleSeries())
                {
                    if (!CanProcess())
                    {
                        break;
                    }

                    if (series.MessageType != typeof(TimeFrameCandleMessage))
                    {
                        this.AddWarningLog(LocalizedStrings.Str2296Params, series);
                        continue;
                    }

                    var tf = (TimeSpan)series.Arg;

                    var candleStorage = StorageRegistry.GetCandleMessageStorage(series.MessageType, security.Security, tf, _settings.Drive, _settings.StorageFormat);
                    var emptyDates    = allDates.Except(candleStorage.Dates).ToArray();

                    if (emptyDates.IsEmpty())
                    {
                        this.AddInfoLog(LocalizedStrings.Str2297Params, tf, security.Security.Id);
                        continue;
                    }

                    var currDate = emptyDates.First();
                    var lastDate = emptyDates.Last();

                    while (currDate <= lastDate)
                    {
                        if (!CanProcess())
                        {
                            break;
                        }

                        if (_settings.IgnoreWeekends && !security.IsTradeDate(currDate))
                        {
                            this.AddDebugLog(LocalizedStrings.WeekEndDate, currDate);
                            currDate = currDate.AddDays(1);
                            continue;
                        }

                        var till = currDate.AddDays(_settings.CandleDayStep - 1).EndOfDay();
                        this.AddInfoLog(LocalizedStrings.Str2298Params, series, currDate, till, security.Security.Id);

                        bool isSuccess;
                        var  candles = ((IQFeedTrader)Connector).GetHistoricalCandles(security.Security, series.MessageType, tf, currDate, till, out isSuccess);

                        if (isSuccess)
                        {
                            if (candles.Any())
                            {
                                SaveCandles(security, candles);
                            }
                            else
                            {
                                this.AddDebugLog(LocalizedStrings.NoData);
                            }
                        }
                        else
                        {
                            this.AddErrorLog(LocalizedStrings.Str2550);
                        }

                        currDate = currDate.AddDays(_settings.CandleDayStep);
                    }
                }
            }

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

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

                _settings.StartFrom = endDate;
                SaveSettings();
            }

            return(_settings.Interval);
        }
        private void ProcessMarketDataMessage(MarketDataMessage message)
        {
            var security = SecurityProvider.LookupById(message.SecurityId.SecurityCode + "@" + message.SecurityId.BoardCode);

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

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

            Exception error = null;

            switch (message.DataType)
            {
            case MarketDataTypes.Level1:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add(StorageRegistry.GetLevel1MessageStorage(security, Drive, StorageFormat));

                    BasketStorage.InnerStorages.Add(new InMemoryMarketDataStorage <ClearingMessage>(date => new[]
                        {
                            new ClearingMessage
                            {
                                LocalTime        = date.Date + security.Board.ExpiryTime,
                                SecurityId       = message.SecurityId,
                                ClearMarketDepth = true
                            }
                        }));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <Level1ChangeMessage> >(security, MessageTypes.Level1Change, message.Arg);
                    RemoveStorage <InMemoryMarketDataStorage <ClearingMessage> >(security, ExtendedMessageTypes.Clearing, message.Arg);
                }

                break;
            }

            case MarketDataTypes.MarketDepth:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add((IMarketDataStorage <QuoteChangeMessage>)StorageRegistry.GetMarketDepthStorage(security, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <QuoteChangeMessage> >(security, MessageTypes.QuoteChange, message.Arg);
                }

                break;
            }

            case MarketDataTypes.Trades:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add((IMarketDataStorage <ExecutionMessage>)StorageRegistry.GetTradeStorage(security, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, message.Arg);
                }

                break;
            }

            case MarketDataTypes.OrderLog:
            {
                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add((IMarketDataStorage <ExecutionMessage>)StorageRegistry.GetOrderLogStorage(security, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <ExecutionMessage> >(security, MessageTypes.Execution, message.Arg);
                }

                break;
            }

            case MarketDataTypes.CandleTimeFrame:
            case MarketDataTypes.CandleTick:
            case MarketDataTypes.CandleVolume:
            case MarketDataTypes.CandleRange:
            case MarketDataTypes.CandlePnF:
            case MarketDataTypes.CandleRenko:
            {
                Type         candleMessageType;
                MessageTypes msgType;

                switch (message.DataType)
                {
                case MarketDataTypes.CandleTimeFrame:
                    msgType           = MessageTypes.CandleTimeFrame;
                    candleMessageType = typeof(TimeFrameCandleMessage);
                    break;

                case MarketDataTypes.CandleTick:
                    msgType           = MessageTypes.CandleTick;
                    candleMessageType = typeof(TickCandleMessage);
                    break;

                case MarketDataTypes.CandleVolume:
                    msgType           = MessageTypes.CandleVolume;
                    candleMessageType = typeof(VolumeCandleMessage);
                    break;

                case MarketDataTypes.CandleRange:
                    msgType           = MessageTypes.CandleRange;
                    candleMessageType = typeof(RangeCandleMessage);
                    break;

                case MarketDataTypes.CandlePnF:
                    msgType           = MessageTypes.CandlePnF;
                    candleMessageType = typeof(PnFCandleMessage);
                    break;

                case MarketDataTypes.CandleRenko:
                    msgType           = MessageTypes.CandleRenko;
                    candleMessageType = typeof(RenkoCandleMessage);
                    break;

                default:
                    throw new InvalidOperationException();
                }

                if (message.IsSubscribe)
                {
                    BasketStorage.InnerStorages.Add(StorageRegistry.GetCandleMessageStorage(candleMessageType, security, message.Arg, Drive, StorageFormat));
                }
                else
                {
                    RemoveStorage <IMarketDataStorage <CandleMessage> >(security, msgType, message.Arg);
                }

                break;
            }

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

            RaiseMarketDataMessage(message, error);
        }
Beispiel #9
0
        protected override TimeSpan OnProcess()
        {
            var allSecurity = this.GetAllSecurity();

            var source = new GoogleHistorySource();

            var startDate = _settings.StartFrom;
            var endDate   = DateTime.Today - TimeSpan.FromDays(_settings.DayOffset);

            var allDates = startDate.Range(endDate, TimeSpan.FromDays(1)).ToArray();

            var hasSecurities = false;

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

                if (!CanProcess())
                {
                    break;
                }

                foreach (var pair in (allSecurity ?? security).GetCandleSeries())
                {
                    if (!CanProcess())
                    {
                        break;
                    }

                    if (pair.MessageType != typeof(TimeFrameCandleMessage))
                    {
                        this.AddWarningLog(LocalizedStrings.Str2296Params, pair);
                        continue;
                    }

                    var tf = (TimeSpan)pair.Arg;

                    var storage    = StorageRegistry.GetCandleMessageStorage(pair.MessageType, security.Security, tf, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

                    if (emptyDates.IsEmpty())
                    {
                        this.AddInfoLog(LocalizedStrings.Str2297Params, tf, security.Security.Id);
                        continue;
                    }

                    var currDate = emptyDates.First();
                    var lastDate = emptyDates.Last();

                    while (currDate <= lastDate)
                    {
                        if (!CanProcess())
                        {
                            break;
                        }

                        if (_settings.IgnoreWeekends && !security.IsTradeDate(currDate))
                        {
                            this.AddDebugLog(LocalizedStrings.WeekEndDate, currDate);
                            currDate = currDate.AddDays(1);
                            continue;
                        }

                        try
                        {
                            var till = currDate + TimeSpan.FromDays(_settings.CandleDayStep).Max(tf);
                            this.AddInfoLog(LocalizedStrings.Str2298Params, tf, currDate, till, security.Security.Id);

                            var candles = source.GetCandles(security.Security, tf, currDate, till);

                            if (candles.Any())
                            {
                                SaveCandles(security, candles);
                            }
                            else
                            {
                                this.AddDebugLog(LocalizedStrings.NoData);
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleError(new InvalidOperationException(LocalizedStrings.Str2299Params
                                                                      .Put(tf, currDate, security.Security.Id), ex));
                        }

                        currDate = currDate.AddDays(_settings.CandleDayStep);
                    }
                }
            }

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

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

            return(base.OnProcess());
        }