Exemplo n.º 1
0
            public CandleMessageBuildableStorage(CandleBuilderProvider provider, IStorageRegistry registry, SecurityId securityId, TimeSpan timeFrame, IMarketDataDrive drive, StorageFormats format)
            {
                if (registry == null)
                {
                    throw new ArgumentNullException(nameof(registry));
                }

                _getStorage = tf => registry.GetCandleMessageStorage(typeof(TimeFrameCandleMessage), securityId, tf, drive, format);
                _original   = _getStorage(timeFrame);

                _timeFrame = timeFrame;

                _compressors = GetSmallerTimeFrames().ToDictionary(tf => tf, tf => new BiggerTimeFrameCandleCompressor(new MarketDataMessage
                {
                    SecurityId  = securityId,
                    DataType    = MarketDataTypes.CandleTimeFrame,
                    Arg         = timeFrame,
                    IsSubscribe = true,
                }, provider.Get(typeof(TimeFrameCandleMessage))));
            }
Exemplo n.º 2
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.ToCandleMarketDataType());

            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, 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, 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());
        }