Ejemplo n.º 1
0
        public void getfromstore()
        {
            var storage = new StorageRegistry()
            {
                DefaultDrive = new LocalMarketDataDrive {
                    Path = @"C:\Personal Soft\StockSharp\Hydra\MyData\"
                }
            };
            var security = new Security
            {
                Id         = "SIH7@FORTS",
                PriceStep  = 1m,
                Decimals   = 1,
                VolumeStep = 1
            };
            var candles       = storage.GetCandleStorage(typeof(TimeFrameCandle), security, TimeSpan.FromMinutes(5), null, StorageFormats.Csv);
            var loadedCandles = candles.Load(new DateTime(2017, 1, 18, 0, 0, 0), DateTime.Today + TimeSpan.FromMinutes(1000));

            Console.WriteLine(loadedCandles.Count());
            foreach (var c in loadedCandles)
            {
                Console.WriteLine("Сделка {0} : {1}", c.ClosePrice, c.OpenPrice);
            }


            Console.ReadLine();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Сохранить свечи по инструменту в хранилище.
 /// </summary>
 /// <typeparam name="TCandle">Тип свечи.</typeparam>
 /// <param name="security">Инструмент.</param>
 /// <param name="candles">Свечи.</param>
 protected void SaveCandles <TCandle>(Security security, IEnumerable <TCandle> candles)
     where TCandle : Candle
 {
     candles
     .GroupBy(c => Tuple.Create(c.GetType(), c.Arg))
     .ForEach(g => SafeSave(security, g.Key.Item1.ToCandleMessageType(), g.Key.Item2, g, c => c.OpenTime, new[]
     {
         CreateErrorCheck <TCandle>(c => c.Security.PriceStep != null && c.Security.PriceStep != 0 && c.OpenPrice % c.Security.PriceStep != 0, LocalizedStrings.Str2203),
         CreateErrorCheck <TCandle>(c => c.Security.PriceStep != null && c.Security.PriceStep != 0 && c.HighPrice % c.Security.PriceStep != 0, LocalizedStrings.Str2204),
         CreateErrorCheck <TCandle>(c => c.Security.PriceStep != null && c.Security.PriceStep != 0 && c.LowPrice % c.Security.PriceStep != 0, LocalizedStrings.Str2205),
         CreateErrorCheck <TCandle>(c => c.Security.PriceStep != null && c.Security.PriceStep != 0 && c.ClosePrice % c.Security.PriceStep != 0, LocalizedStrings.Str2206)
     },
                            (s, d, c) => (IMarketDataStorage <TCandle>)StorageRegistry.GetCandleStorage(g.Key.Item1, security, g.Key.Item2, d, c)));
 }
Ejemplo n.º 3
0
        public void loadCandles()
        {
            //StorageFormats storageFormat = (StorageFormats)Enum.Parse(typeof(StorageFormats), settings.Element("storage-format").Value.ToUpper());

            /*  var security = new Security
             * {
             *    Id = "SIH7@FORTS"
             *    PriceStep = 1m,
             *    Decimals = 1,
             *    VolumeStep = 1
             * };*/
            var dataStorage = storage.GetCandleStorage(typeof(TimeFrameCandle), security, timespan, null, StorageFormats.Csv);

            candles = dataStorage.Load(new DateTime(2017, 1, 20, 0, 0, 0), DateTime.Today + TimeSpan.FromMinutes(1000));
        }
Ejemplo n.º 4
0
        protected override TimeSpan OnProcess()
        {
            var allSecurity = this.GetAllSecurity();

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

            var source = new YahooHistorySource();

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

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

            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;
                }

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

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

                    var storage    = StorageRegistry.GetCandleStorage(series.CandleType, security.Security, series.Arg, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

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

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

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

                        try
                        {
                            this.AddInfoLog(LocalizedStrings.Str2298Params, series.Arg, emptyDate, security.Security.Id);
                            var candles = source.GetCandles(security.Security, (TimeSpan)series.Arg, emptyDate, emptyDate + TimeSpan.FromDays(1).Max((TimeSpan)series.Arg));

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

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

            return(base.OnProcess());
        }
Ejemplo n.º 5
0
        protected override TimeSpan OnProcess()
        {
            // если фильтр по инструментам выключен (выбран инструмент все инструменты)
            if (this.GetAllSecurity() != null)
            {
                //throw new InvalidOperationException("Источник не поддерживает закачку данных по всем инструментам.");
                this.AddWarningLog(LocalizedStrings.Str2292);
                return(TimeSpan.MaxValue);
            }

            var source = new AlorHistorySource();

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

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

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

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

                #region LoadCandles
                if (security.CandleSeries.Any())
                {
                    foreach (var series in security.CandleSeries)
                    {
                        if (!CanProcess())
                        {
                            break;
                        }

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

                        var storage    = StorageRegistry.GetCandleStorage(series.CandleType, security.Security, series.Arg, _settings.Drive, _settings.StorageFormat);
                        var emptyDates = allDates.Except(storage.Dates).ToArray();

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

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

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

                            try
                            {
                                this.AddInfoLog(LocalizedStrings.Str2298Params, series.Arg, emptyDate, security.Security.Id);
                                var candles = source.GetCandles(security.Security, (TimeSpan)series.Arg, emptyDate, emptyDate);

                                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(TimeFrameCandle), series.Arg));
                                }
                            }
                            catch (Exception ex)
                            {
                                HandleError(new InvalidOperationException(LocalizedStrings.Str2299Params
                                                                          .Put(series.Arg, emptyDate, security.Security.Id), ex));
                            }
                        }
                    }
                }
                #endregion
            }

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

            return(base.OnProcess());
        }
Ejemplo n.º 6
0
        private void LoadData(Security security)
        {
            _candle    = null;
            _lastPrice = 0m;
            _allCandles.Clear();

            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 ? 2 : 30 * (int)_timeframe.TotalMinutes;

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

                if (isBuild)
                {
                    foreach (var tick in storage.GetTickMessageStorage(security, new LocalMarketDataDrive(path), format).Load())
                    {
                        AppendTick(security, tick);

                        _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 candle in storage.GetCandleStorage(typeof(TimeFrameCandle), security, _timeframe, new LocalMarketDataDrive(path), format).Load())
                    {
                        lock (_updatedCandles.SyncRoot)
                            _updatedCandles[candle.OpenTime] = _candle = (TimeFrameCandle)candle;

                        _lastTime  = candle.OpenTime + _timeframe;
                        _lastPrice = _candle.ClosePrice;

                        _tradeGenerator.Process(new ExecutionMessage
                        {
                            ExecutionType = ExecutionTypes.Tick,
                            SecurityId    = security.ToSecurityId(),
                            ServerTime    = _lastTime,
                            TradePrice    = _lastPrice,
                        });

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

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

                            maxDays--;

                            if (maxDays == 0)
                            {
                                break;
                            }
                        }
                    }
                }
            })
            .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());
        }
Ejemplo n.º 7
0
        private void Download_OnClick(object sender, RoutedEventArgs e)
        {
            var year     = SelectedYear;
            var from     = From.Value ?? year.Days.First();
            var to       = (To.Value ?? year.Days.Last()).EndOfDay();
            var trader   = SelectedTrader;
            var security = SelectedSecurity;
            var tf       = SelectedTimeFrame;
            var series   = new CandleSeries(typeof(TimeFrameCandle), security, tf);

            BusyIndicator.BusyContent = "Подготовка данных...";
            BusyIndicator.IsBusy      = true;

            Dictionary <DateTimeOffset, Tuple <MyTrade[], MyTrade> > trades = null;

            var worker = new BackgroundWorker {
                WorkerReportsProgress = true
            };

            worker.DoWork += (o, ea) =>
            {
                var candleStorage = _dataRegistry.GetCandleStorage(series, format: StorageFormats.Csv);

                _candles = candleStorage.Load(from, to);

                var candlesDatesCache = _candlesDates.SafeAdd(Tuple.Create(security, tf), k => new DatesCache(Path.Combine(((LocalMarketDataDrive)candleStorage.Drive.Drive).GetSecurityPath(security.ToSecurityId()), "{0}min_date.bin".Put((int)tf.TotalMinutes))));

                var minCandleDate = candlesDatesCache.MinValue;
                var maxCandleDate = candlesDatesCache.MaxValue;

                if (from < minCandleDate || to > maxCandleDate)
                {
                    var finamFrom = from;
                    var finamTo   = to;

                    if (maxCandleDate != default(DateTime) && finamFrom >= minCandleDate && finamFrom <= maxCandleDate)
                    {
                        finamFrom = maxCandleDate + TimeSpan.FromDays(1);
                    }

                    if (minCandleDate != default(DateTime) && finamTo >= minCandleDate && finamTo <= maxCandleDate)
                    {
                        finamTo = minCandleDate - TimeSpan.FromDays(1);
                    }

                    if (finamTo > finamFrom)
                    {
                        worker.ReportProgress(1);

                        var newCandles = (tf.Ticks == 1
                                                        ? finamFrom.Range(finamTo, TimeSpan.FromDays(1)).SelectMany(day => _finamHistorySource.GetTrades(security, day, day)).ToEx().ToCandles <TimeFrameCandle>(tf)
                                                        : _finamHistorySource.GetCandles(security, tf, finamFrom, finamTo)
                                          ).ToArray();

                        candleStorage.Save(newCandles);

                        foreach (var date in newCandles.Select(c => c.OpenTime.Date).Distinct())
                        {
                            candlesDatesCache.Add(date);
                        }

                        candlesDatesCache.Save();

                        _candles = _candles.Concat(newCandles);
                    }
                }

                var traderDrive   = new LocalMarketDataDrive(trader);
                var traderStorage = _traderStorages.SafeAdd(trader, key => new StorageRegistry {
                    DefaultDrive = traderDrive
                });

                var olStorage       = traderStorage.GetOrderLogStorage(security, format: StorageFormats.Csv);
                var tradeDatesCache = _tradesDates.SafeAdd(trader, k => new DatesCache(Path.Combine(traderDrive.Path, "dates.bin")));

                trades = from
                         .Range(to, TimeSpan.FromDays(1))
                         .Intersect(year.Days)
                         .SelectMany(date =>
                {
                    if (olStorage.Dates.Contains(date))
                    {
                        return(olStorage.Load(date));
                    }

                    if (tradeDatesCache.Contains(date))
                    {
                        return(Enumerable.Empty <OrderLogItem>());
                    }

                    worker.ReportProgress(2, date);

                    var loadedTrades = year.GetTrades(_securityStorage, trader, date);

                    var secTrades = Enumerable.Empty <OrderLogItem>();

                    foreach (var group in loadedTrades.GroupBy(t => t.Order.Security))
                    {
                        var sec = group.Key;

                        traderStorage
                        .GetOrderLogStorage(sec, format: StorageFormats.Csv)
                        .Save(group.OrderBy(i => i.Order.Time));

                        if (group.Key == security)
                        {
                            secTrades = group;
                        }
                    }

                    tradeDatesCache.Add(date);
                    tradeDatesCache.Save();

                    return(secTrades);
                })
                         .GroupBy(ol =>
                {
                    var time = ol.Order.Time;

                    var period = security.Board.WorkingTime.GetPeriod(time.DateTime);
                    if (period != null && period.Times.Length > 0)
                    {
                        var last = period.Times.Last().Max;

                        if (time.TimeOfDay >= last)
                        {
                            time = time.AddTicks(-1);
                        }
                    }

                    return(time.Truncate(tf));
                })
                         .ToDictionary(g => g.Key, g =>
                {
                    var candleTrades = g
                                       .Select(order => new MyTrade
                    {
                        Order = order.Order,
                        Trade = order.Trade
                    })
                                       .ToArray();

                    if (candleTrades.Length > 0)
                    {
                        var order  = candleTrades[0].Order;
                        var volume = candleTrades.Sum(t1 => t1.Trade.Volume * (t1.Order.Direction == Sides.Buy ? 1 : -1));

                        if (volume == 0)
                        {
                            return(Tuple.Create(candleTrades, (MyTrade)null));
                        }

                        var side = volume > 0 ? Sides.Buy : Sides.Sell;

                        volume = volume.Abs();

                        var availableVolume = volume;
                        var avgPrice        = 0m;

                        foreach (var trade in candleTrades.Where(t1 => t1.Order.Direction == side))
                        {
                            var tradeVol = trade.Trade.Volume.Min(availableVolume);
                            avgPrice    += trade.Trade.Price * tradeVol;

                            availableVolume -= tradeVol;

                            if (availableVolume <= 0)
                            {
                                break;
                            }
                        }

                        avgPrice = avgPrice / volume;

                        return(Tuple.Create(candleTrades, new MyTrade
                        {
                            Order = new Order
                            {
                                Security = order.Security,
                                Direction = side,
                                Time = g.Key,
                                Portfolio = order.Portfolio,
                                Price = avgPrice,
                                Volume = volume,
                            },
                            Trade = new Trade
                            {
                                Security = order.Security,
                                Time = g.Key,
                                Volume = volume,
                                Price = avgPrice
                            }
                        }));
                    }

                    return(null);
                });
            };

            worker.ProgressChanged += (o, ea) =>
            {
                switch (ea.ProgressPercentage)
                {
                case 1:
                    BusyIndicator.BusyContent = "Скачивание свечей...";
                    break;

                default:
                    BusyIndicator.BusyContent = "Скачивание сделок за {0:yyyy-MM-dd}...".Put(ea.UserState);
                    break;
                }
            };

            worker.RunWorkerCompleted += (o, ea) =>
            {
                BusyIndicator.IsBusy = false;

                if (ea.Error == null)
                {
                    Chart.ClearAreas();
                    _statisticManager.Reset();

                    var area = new ChartArea();
                    area.YAxises.Add(new ChartAxis
                    {
                        Id            = "equity",
                        AutoRange     = true,
                        AxisType      = ChartAxisType.Numeric,
                        AxisAlignment = ChartAxisAlignment.Left,
                    });
                    Chart.AddArea(area);

                    var candlesElem = new ChartCandleElement {
                        ShowAxisMarker = false
                    };
                    Chart.AddElement(area, candlesElem, series);

                    var tradesElem = new ChartTradeElement
                    {
                        BuyStrokeColor  = Colors.Black,
                        SellStrokeColor = Colors.Black,
                        FullTitle       = "trades",
                    };
                    Chart.AddElement(area, tradesElem);

                    var equityElem = new ChartIndicatorElement {
                        YAxisId = "equity", FullTitle = "equity", IndicatorPainter = new PnlPainter()
                    };
                    var equityInd = new SimpleMovingAverage {
                        Length = 1
                    };
                    Chart.AddElement(area, equityElem);

                    var positionArea = new ChartArea {
                        Height = 200
                    };
                    Chart.AddArea(positionArea);

                    var positionElem = new ChartIndicatorElement {
                        FullTitle = "position"
                    };
                    var positionInd = new SimpleMovingAverage {
                        Length = 1
                    };
                    Chart.AddElement(positionArea, positionElem);

                    Chart.IsAutoRange = true;

                    var pnlQueue = new PnLQueue(security.ToSecurityId());
                    //var level1Info = new Level1ChangeMessage
                    //{
                    //	SecurityId = pnlQueue.SecurityId,
                    //}
                    //.TryAdd(Level1Fields.PriceStep, security.PriceStep)
                    //.TryAdd(Level1Fields.StepPrice, security.StepPrice);

                    //pnlQueue.ProcessLevel1(level1Info);

                    var pos = 0m;

                    var chartValues = _candles
                                      .Select(c =>
                    {
                        c.State = CandleStates.Finished;

                        pnlQueue.ProcessLevel1(new Level1ChangeMessage
                        {
                            SecurityId = security.ToSecurityId(),
                        }.TryAdd(Level1Fields.LastTradePrice, c.ClosePrice));

                        var values = new Dictionary <IChartElement, object>
                        {
                            { candlesElem, c },
                        };

                        var candleTrade = trades.TryGetValue(c.OpenTime);

                        if (candleTrade != null)
                        {
                            if (candleTrade.Item2 != null)
                            {
                                values.Add(tradesElem, candleTrade.Item2);
                            }

                            foreach (var myTrade in candleTrade.Item1)
                            {
                                pos    += myTrade.Order.Direction == Sides.Buy ? myTrade.Trade.Volume : -myTrade.Trade.Volume;
                                var pnl = pnlQueue.Process(myTrade.ToMessage());

                                _statisticManager.AddMyTrade(pnl);
                            }

                            _statisticManager.AddPosition(c.OpenTime, pos);
                            _statisticManager.AddPnL(c.OpenTime, pnlQueue.RealizedPnL + pnlQueue.UnrealizedPnL);
                        }

                        values.Add(equityElem, equityInd.Process(pnlQueue.RealizedPnL + pnlQueue.UnrealizedPnL));
                        values.Add(positionElem, positionInd.Process(pos));

                        return(new RefPair <DateTimeOffset, IDictionary <IChartElement, object> >
                        {
                            First = c.OpenTime,
                            Second = values
                        });
                    })
                                      .ToArray();

                    Chart.Draw(chartValues);

                    Chart.IsAutoRange = false;
                }
                else
                {
                    new MessageBoxBuilder()
                    .Error()
                    .Owner(this)
                    .Text(ea.Error.ToString())
                    .Show();
                }
            };

            worker.RunWorkerAsync();
        }
Ejemplo n.º 8
0
        protected override TimeSpan OnProcess()
        {
            var source = CreateSource();

            var allSecurity = this.GetAllSecurity();

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

            var hasNonQuandl = selectedSecurities.Any(s => !IsQuandl(s));

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

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

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

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

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

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

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

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

            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;
                }

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

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

                    var tf = (TimeSpan)series.Arg;

                    var storage    = StorageRegistry.GetCandleStorage(series.CandleType, 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;
                        }

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

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

                            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(TimeFrameCandle), series.Arg));
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleError(new InvalidOperationException(LocalizedStrings.Str2299Params
                                                                      .Put(series.Arg, emptyDate, security.Security.Id), ex));
                        }
                    }
                }
            }

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

            return(base.OnProcess());
        }
Ejemplo n.º 9
0
 private IMarketDataStorage <Candle> GetStorage(CandleSeries series)
 {
     return(StorageRegistry.GetCandleStorage(series, Drive ?? StorageRegistry.DefaultDrive));
 }
Ejemplo n.º 10
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 series in (allSecurity ?? security).CandleSeries)
                {
                    if (!CanProcess())
                    {
                        break;
                    }

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

                    var storage    = StorageRegistry.GetCandleStorage(series.CandleType, security.Security, series.Arg, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

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

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

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

                        try
                        {
                            this.AddInfoLog(LocalizedStrings.Str2298Params, series.Arg, emptyDate, security.Security.Id);
                            var candles = source.GetCandles(security.Security, (TimeSpan)series.Arg, emptyDate, emptyDate + TimeSpan.FromDays(1).Max((TimeSpan)series.Arg));

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

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

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

            return(base.OnProcess());
        }
Ejemplo n.º 11
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).MarketDataTypesSet.Contains(typeof(Level1ChangeMessage)))
                {
                    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 series in (allSecurity ?? security).CandleSeries)
                {
                    if (!CanProcess())
                    {
                        break;
                    }

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

                    var storage    = StorageRegistry.GetCandleStorage(series.CandleType, security.Security, series.Arg, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

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

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

                        try
                        {
                            this.AddInfoLog(LocalizedStrings.Str2298Params, series.Arg, emptyDate, emptyDate, security.Security.Id);
                            var candles = source.LoadCandles(security.Security, (TimeSpan)series.Arg, 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), series.Arg));
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleError(new InvalidOperationException(LocalizedStrings.Str2299Params
                                                                      .Put(series.Arg, emptyDate, security.Security.Id), ex));
                        }
                    }
                }
                #endregion
            }

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

            return(base.OnProcess());
        }
Ejemplo n.º 12
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).MarketDataTypesSet.Contains(typeof(Trade)))
                {
                    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 series in (allSecurity ?? security).CandleSeries)
                {
                    if (!CanProcess())
                    {
                        break;
                    }

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

                    var storage    = StorageRegistry.GetCandleStorage(series.CandleType, security.Security, series.Arg, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

                    if (emptyDates.IsEmpty())
                    {
                        this.AddInfoLog(LocalizedStrings.Str2297Params, series.Arg, 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, series.Arg, currDate, till, security.Security.Id);

                            var candles = source.GetCandles(security.Security, (TimeSpan)series.Arg, 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), series.Arg));
                            }

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

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

                _settings.StartFrom = endDate;
                SaveSettings();
            }

            return(base.OnProcess());
        }
Ejemplo n.º 13
0
        protected override TimeSpan OnProcess()
        {
            var allSecurity = this.GetAllSecurity();

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

            var source = new UxHistorySource();

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

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

                selectedSecurities = this.ToHydraSecurities(EntityRegistry.Securities.Filter(ExchangeBoard.Ux).Concat(EntityRegistry.Securities.Filter(ExchangeBoard.UxStock)));
            }

            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).MarketDataTypesSet.Contains(typeof(Trade)))
                {
                    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);
                                }
                            }
                            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 series in (allSecurity ?? security).CandleSeries)
                {
                    if (!CanProcess())
                    {
                        break;
                    }

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

                    var storage    = StorageRegistry.GetCandleStorage(series.CandleType, security.Security, series.Arg, _settings.Drive, _settings.StorageFormat);
                    var emptyDates = allDates.Except(storage.Dates).ToArray();

                    if (emptyDates.IsEmpty())
                    {
                        this.AddInfoLog(LocalizedStrings.Str2297Params, series.Arg, 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, series.Arg, currDate, till, security.Security.Id);

                            var candles = source.GetCandles(security.Security, (TimeSpan)series.Arg, currDate, till);

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

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

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

            return(base.OnProcess());
        }
Ejemplo n.º 14
0
        private void Download_OnClick(object sender, RoutedEventArgs e)
        {
            var settings = new Settings
            {
                Year      = SelectedYear.Year,
                Trader    = Trader.Text,
                From      = From.Value,
                To        = To.Value,
                Security1 = Security1.Text,
                Security2 = Security2.Text,
                Security3 = Security3.Text,
                Security4 = Security4.Text,
                TimeFrame = SelectedTimeFrame,
                Apart     = Apart.IsChecked == true,
            };

            CultureInfo.InvariantCulture.DoInCulture(() => new XmlSerializer <SettingsStorage>().Serialize(settings.Save(), _settingsFile));

            var year   = SelectedYear;
            var from   = From.Value ?? year.Days.First();
            var to     = (To.Value ?? year.Days.Last()).EndOfDay();
            var trader = SelectedTrader;
            var tf     = SelectedTimeFrame;
            var apart  = Apart.IsChecked == true;

            var seriesSet = _securityCtrls
                            .Where(pair => pair.Key.SelectedSecurity != null)
                            .Select(pair => Tuple.Create(new CandleSeries(typeof(TimeFrameCandle), pair.Key.SelectedSecurity, tf), pair.Value))
                            .ToArray();

            BusyIndicator.BusyContent = "Подготовка данных...";
            BusyIndicator.IsBusy      = true;

            _candles.Clear();

            var trades = new Dictionary <Security, Dictionary <DateTimeOffset, Tuple <MyTrade[], MyTrade> > >();

            var worker = new BackgroundWorker {
                WorkerReportsProgress = true
            };

            worker.DoWork += (o, ea) =>
            {
                foreach (var series in seriesSet)
                {
                    var security      = series.Item1.Security;
                    var candleStorage = _dataRegistry.GetCandleStorage(series.Item1, format: StorageFormats.Csv);
                    var secCandles    = _candles.SafeAdd(security);

                    secCandles.Clear();
                    secCandles.AddRange(candleStorage.Load(from, to));

                    var candlesDatesCache = _candlesDates.SafeAdd(Tuple.Create(security, tf), k => new DatesCache(Path.Combine(((LocalMarketDataDrive)candleStorage.Drive.Drive).GetSecurityPath(security.ToSecurityId()), "{0}min_date.bin".Put((int)tf.TotalMinutes))));

                    var minCandleDate = candlesDatesCache.MinValue;
                    var maxCandleDate = candlesDatesCache.MaxValue;

                    if (from >= minCandleDate && to <= maxCandleDate)
                    {
                        continue;
                    }

                    var finamFrom = from;
                    var finamTo   = to;

                    if (maxCandleDate != null && finamFrom >= minCandleDate && finamFrom <= maxCandleDate)
                    {
                        finamFrom = maxCandleDate.Value + TimeSpan.FromDays(1);
                    }

                    if (minCandleDate != null && finamTo >= minCandleDate && finamTo <= maxCandleDate)
                    {
                        finamTo = minCandleDate.Value - TimeSpan.FromDays(1);
                    }

                    if (finamTo <= finamFrom)
                    {
                        continue;
                    }

                    TimeFrameCandle[] newCandles;

                    if (tf.Ticks == 1)
                    {
                        newCandles = finamFrom.Range(finamTo, TimeSpan.FromDays(1)).SelectMany(day =>
                        {
                            worker.ReportProgress(1, Tuple.Create(security, day));

                            var candles = _finamHistorySource.GetTrades(security, day, day).ToEx().ToCandles <TimeFrameCandle>(tf).ToArray();
                            candleStorage.Save(candles);
                            candlesDatesCache.Add(day);
                            return(candles);
                        }).ToArray();
                    }
                    else
                    {
                        worker.ReportProgress(1, Tuple.Create(security, finamFrom, finamTo));
                        newCandles = _finamHistorySource.GetCandles(security, tf, finamFrom, finamTo).ToArray();

                        candleStorage.Save(newCandles);
                        candlesDatesCache.Add(newCandles.Select(c => c.OpenTime.Date).Distinct().ToArray());
                    }

                    // TODO
                    secCandles.AddRange(newCandles);
                }

                var traderDrive   = new LocalMarketDataDrive(Path.Combine(_settingsDir, trader));
                var traderStorage = _traderStorages.SafeAdd(trader, key => new StorageRegistry {
                    DefaultDrive = traderDrive
                });

                foreach (var series in seriesSet)
                {
                    var security = series.Item1.Security;

                    var olStorage       = traderStorage.GetOrderLogStorage(security, format: StorageFormats.Csv);
                    var tradeDatesCache = _tradesDates.SafeAdd(trader, k => new DatesCache(Path.Combine(traderDrive.Path, "dates.xml")));

                    var secTrades = from
                                    .Range(to, TimeSpan.FromDays(1))
                                    .Intersect(year.Days)
                                    .SelectMany(date =>
                    {
                        if (olStorage.Dates.Contains(date))
                        {
                            return(olStorage.Load(date));
                        }

                        if (tradeDatesCache.Contains(date))
                        {
                            return(Enumerable.Empty <OrderLogItem>());
                        }

                        worker.ReportProgress(2, date);

                        var loadedTrades = year.GetTrades(_securityStorage, trader, date);

                        var dateTrades = Enumerable.Empty <OrderLogItem>();

                        foreach (var group in loadedTrades.GroupBy(t => t.Order.Security))
                        {
                            var sec = group.Key;

                            traderStorage
                            .GetOrderLogStorage(sec, format: StorageFormats.Csv)
                            .Save(group.OrderBy(i => i.Order.Time));

                            if (group.Key == security)
                            {
                                dateTrades = group;
                            }
                        }

                        tradeDatesCache.Add(date);

                        return(dateTrades);
                    })
                                    .GroupBy(ol =>
                    {
                        var time = ol.Order.Time;

                        var period = security.Board.WorkingTime.GetPeriod(time.ToLocalTime(security.Board.Exchange.TimeZoneInfo));
                        if (period != null && period.Times.Length > 0)
                        {
                            var last = period.Times.Last().Max;

                            if (time.TimeOfDay >= last)
                            {
                                time = time.AddTicks(-1);
                            }
                        }

                        if (tf == TimeSpan.FromDays(1) && period != null && period.Times.Length > 0)
                        {
                            return(new DateTimeOffset(time.Date + period.Times[0].Min, time.Offset));
                        }

                        return(time.Truncate(tf));
                    })
                                    .ToDictionary(g => g.Key, g =>
                    {
                        var candleTrades = g.Select(ol => new MyTrade
                        {
                            Order = ol.Order,
                            Trade = ol.Trade
                        })
                                           .ToArray();

                        if (candleTrades.Length == 0)
                        {
                            return(null);
                        }

                        var order  = candleTrades[0].Order;
                        var volume = candleTrades.Sum(t1 => t1.Trade.Volume * (t1.Order.Direction == Sides.Buy ? 1 : -1));

                        if (volume == 0)
                        {
                            return(Tuple.Create(candleTrades, (MyTrade)null));
                        }

                        var side = volume > 0 ? Sides.Buy : Sides.Sell;

                        volume = volume.Abs();

                        var availableVolume = volume;
                        var avgPrice        = 0m;

                        foreach (var trade in candleTrades.Where(t1 => t1.Order.Direction == side))
                        {
                            var tradeVol = trade.Trade.Volume.Min(availableVolume);
                            avgPrice    += trade.Trade.Price * tradeVol;

                            availableVolume -= tradeVol;

                            if (availableVolume <= 0)
                            {
                                break;
                            }
                        }

                        avgPrice = avgPrice / volume;

                        return(Tuple.Create(candleTrades, new MyTrade
                        {
                            Order = new Order
                            {
                                Security = order.Security,
                                Direction = side,
                                Time = g.Key,
                                Portfolio = order.Portfolio,
                                Price = avgPrice,
                                Volume = volume,
                            },
                            Trade = new Trade
                            {
                                Security = order.Security,
                                Time = g.Key,
                                Volume = volume,
                                Price = avgPrice
                            }
                        }));
                    });

                    trades.Add(security, secTrades);
                }
            };

            worker.ProgressChanged += (o, ea) =>
            {
                switch (ea.ProgressPercentage)
                {
                case 1:
                {
                    if (ea.UserState is Tuple <Security, DateTime> )
                    {
                        BusyIndicator.BusyContent = "Скачивание {Item1.Id} тиков за {Item2:yyyy-MM-dd}...".PutEx(ea.UserState);
                    }
                    else
                    {
                        BusyIndicator.BusyContent = "Скачивание {Item1.Id} свечей с {Item2:yyyy-MM-dd} по {Item3:yyyy-MM-dd}...".PutEx(ea.UserState);
                    }

                    break;
                }

                default:
                    BusyIndicator.BusyContent = "Скачивание сделок за {0:yyyy-MM-dd}...".Put(ea.UserState);
                    break;
                }
            };

            worker.RunWorkerCompleted += (o, ea) =>
            {
                BusyIndicator.IsBusy = false;

                if (ea.Error == null)
                {
                    Chart.ClearAreas();

                    _statisticManager.Reset();

                    var equityInd = new SimpleMovingAverage {
                        Length = 1
                    };
                    ChartIndicatorElement equityElem;
                    var candlesAreas = new Dictionary <CandleSeries, ChartArea>();

                    if (apart)
                    {
                        foreach (var series in seriesSet)
                        {
                            var area = new ChartArea {
                                Title = series.Item1.Security.Id
                            };
                            Chart.AddArea(area);
                            area.YAxises.Clear();
                            candlesAreas.Add(series.Item1, area);
                        }

                        var equityArea = new ChartArea {
                            Title = LocalizedStrings.PnL
                        };
                        Chart.AddArea(equityArea);

                        equityElem = new ChartIndicatorElement
                        {
                            FullTitle        = LocalizedStrings.PnL,
                            IndicatorPainter = new PnlPainter()
                        };
                        Chart.AddElement(equityArea, equityElem);
                    }
                    else
                    {
                        var candlesArea = new ChartArea();
                        Chart.AddArea(candlesArea);

                        foreach (var tuple in seriesSet)
                        {
                            candlesAreas.Add(tuple.Item1, candlesArea);
                        }

                        const string equityYAxis = "Equity";

                        candlesArea.YAxises.Clear();
                        candlesArea.YAxises.Add(new ChartAxis
                        {
                            Id            = equityYAxis,
                            AutoRange     = true,
                            AxisType      = ChartAxisType.Numeric,
                            AxisAlignment = ChartAxisAlignment.Left,
                        });
                        equityElem = new ChartIndicatorElement
                        {
                            YAxisId          = equityYAxis,
                            FullTitle        = LocalizedStrings.PnL,
                            IndicatorPainter = new PnlPainter()
                        };
                        Chart.AddElement(candlesArea, equityElem);
                    }

                    var positionArea = new ChartArea {
                        Height = 100
                    };
                    Chart.AddArea(positionArea);
                    positionArea.YAxises.Clear();

                    var chartValues = new SortedDictionary <DateTimeOffset, IDictionary <IChartElement, object> >();
                    var pnlValues   = new Dictionary <DateTimeOffset, decimal>();

                    foreach (var series in seriesSet)
                    {
                        var security = series.Item1.Security;

                        var candleYAxis = "Candles_Y_" + security.Id;

                        var candlesArea = candlesAreas[series.Item1];

                        candlesArea.YAxises.Add(new ChartAxis
                        {
                            Id            = candleYAxis,
                            AutoRange     = true,
                            AxisType      = ChartAxisType.Numeric,
                            AxisAlignment = ChartAxisAlignment.Right,
                        });

                        var candlesElem = new ChartCandleElement
                        {
                            ShowAxisMarker = false,
                            YAxisId        = candleYAxis,
                        };
                        Chart.AddElement(candlesArea, candlesElem, series.Item1);

                        var tradesElem = new ChartTradeElement
                        {
                            BuyStrokeColor  = Colors.Black,
                            SellStrokeColor = Colors.Black,
                            BuyColor        = series.Item2.Buy,
                            SellColor       = series.Item2.Sell,
                            FullTitle       = LocalizedStrings.Str985 + " " + security.Id,
                            YAxisId         = candleYAxis,
                        };
                        Chart.AddElement(candlesArea, tradesElem);

                        var posYAxis = "Pos_Y_" + security.Id;
                        positionArea.YAxises.Add(new ChartAxis
                        {
                            Id            = posYAxis,
                            AutoRange     = true,
                            AxisType      = ChartAxisType.Numeric,
                            AxisAlignment = ChartAxisAlignment.Right,
                        });
                        var positionElem = new ChartIndicatorElement
                        {
                            FullTitle = LocalizedStrings.Str862 + " " + security.Id,
                            YAxisId   = posYAxis,
                            Color     = series.Item2.Position
                        };
                        var positionInd = new SimpleMovingAverage {
                            Length = 1
                        };
                        Chart.AddElement(positionArea, positionElem);

                        var pnlQueue = new PnLQueue(security.ToSecurityId());
                        //var level1Info = new Level1ChangeMessage
                        //{
                        //	SecurityId = pnlQueue.SecurityId,
                        //}
                        //.TryAdd(Level1Fields.PriceStep, security.PriceStep)
                        //.TryAdd(Level1Fields.StepPrice, security.StepPrice);

                        //pnlQueue.ProcessLevel1(level1Info);

                        var pos = 0m;

                        var secTrades = trades[security];

                        var secValues = _candles[security]
                                        .Select(c =>
                        {
                            if (c.State != CandleStates.Finished)
                            {
                                c.State = CandleStates.Finished;
                            }

                            pnlQueue.ProcessLevel1(new Level1ChangeMessage
                            {
                                SecurityId = security.ToSecurityId(),
                            }.TryAdd(Level1Fields.LastTradePrice, c.ClosePrice));

                            var values = new Dictionary <IChartElement, object>
                            {
                                { candlesElem, c },
                            };

                            var candleTrade = secTrades.TryGetValue(c.OpenTime);

                            if (candleTrade != null)
                            {
                                if (candleTrade.Item2 != null)
                                {
                                    values.Add(tradesElem, candleTrade.Item2);
                                }

                                foreach (var myTrade in candleTrade.Item1)
                                {
                                    pos    += myTrade.Order.Direction == Sides.Buy ? myTrade.Trade.Volume : -myTrade.Trade.Volume;
                                    var pnl = pnlQueue.Process(myTrade.ToMessage());

                                    _statisticManager.AddMyTrade(pnl);
                                }

                                _statisticManager.AddPosition(c.OpenTime, pos);
                                _statisticManager.AddPnL(c.OpenTime, pnlQueue.RealizedPnL + pnlQueue.UnrealizedPnL);
                            }

                            pnlValues[c.OpenTime] = pnlValues.TryGetValue(c.OpenTime) + (pnlQueue.RealizedPnL + pnlQueue.UnrealizedPnL);
                            values.Add(positionElem, positionInd.Process(pos));

                            return(new RefPair <DateTimeOffset, IDictionary <IChartElement, object> >
                            {
                                First = c.OpenTime,
                                Second = values
                            });
                        })
                                        .ToArray();

                        foreach (var pair in secValues)
                        {
                            var dict = chartValues.SafeAdd(pair.First, key => new Dictionary <IChartElement, object>());

                            foreach (var pair2 in pair.Second)
                            {
                                dict[pair2.Key] = pair2.Value;
                            }
                        }
                    }

                    foreach (var pair in pnlValues)
                    {
                        chartValues[pair.Key].Add(equityElem, equityInd.Process(pair.Value));
                    }

                    Chart.IsAutoRange = true;

                    try
                    {
                        Chart.Draw(chartValues.Select(p => RefTuple.Create(p.Key, p.Value)));
                    }
                    finally
                    {
                        Chart.IsAutoRange = false;
                    }
                }
                else
                {
                    new MessageBoxBuilder()
                    .Error()
                    .Owner(this)
                    .Text(ea.Error.ToString())
                    .Show();
                }
            };

            worker.RunWorkerAsync();
        }
Ejemplo n.º 15
0
        protected override TimeSpan OnProcess()
        {
            if (_settings.ExportType == ExportTypes.Sql && _settings.Connection == null)
            {
                this.AddErrorLog(LocalizedStrings.Str3768);
                return(TimeSpan.MaxValue);
            }

            var allSecurity        = this.GetAllSecurity();
            var supportedDataTypes = (allSecurity == null
                                        ? Enumerable.Empty <Type>()
                                        : SupportedMarketDataTypes.Intersect(allSecurity.MarketDataTypes)
                                      ).ToArray();

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

            Func <int, bool> isCancelled = count => !CanProcess();

            var hasSecurities = false;

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

                if (!CanProcess())
                {
                    break;
                }

                var path = _settings.ExportFolder;

                if (path.IsEmpty())
                {
                    path = DriveCache.Instance.DefaultDrive.Path;
                }

                foreach (var dataType in (allSecurity == null ? security.MarketDataTypes : supportedDataTypes))
                {
                    if (!CanProcess())
                    {
                        break;
                    }

                    this.AddInfoLog(LocalizedStrings.Str3769Params.Put(security.Security.Id, dataType.Name, _settings.ExportType));

                    var fromStorage = dataType == typeof(Candle)
                                                ? StorageRegistry.GetCandleStorage(_settings.CandleSettings.CandleType, security.Security, _settings.CandleSettings.Arg, _settings.Drive, _settings.StorageFormat)
                                                : StorageRegistry.GetStorage(security.Security, dataType, null, _settings.Drive, _settings.StorageFormat);

                    var arg = dataType == typeof(Candle) ? _settings.CandleSettings.Arg
                                                : (dataType == typeof(OrderLogItem) ? ExecutionTypes.OrderLog :
                                                   (dataType == typeof(Trade) ? (object)ExecutionTypes.Tick : null));

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

                    if (from == null || to == null)
                    {
                        this.AddWarningLog(LocalizedStrings.Str3770);
                        continue;
                    }

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

                    if (from > to)
                    {
                        continue;
                    }

                    BaseExporter exporter;

                    if (_settings.ExportType == ExportTypes.Sql)
                    {
                        exporter = new DatabaseExporter(security.Security, arg, isCancelled, _settings.Connection)
                        {
                            BatchSize   = _settings.BatchSize,
                            CheckUnique = _settings.CheckUnique,
                        };
                    }
                    else
                    {
                        var fileName = Path.Combine(path, security.Security.GetFileName(
                                                        dataType == typeof(Candle) ? _settings.CandleSettings.CandleType : dataType,
                                                        dataType == typeof(Candle) ? _settings.CandleSettings.Arg : null,
                                                        from.Value, to.Value, _settings.ExportType));

                        switch (_settings.ExportType)
                        {
                        case ExportTypes.Excel:
                            exporter = new ExcelExporter(security.Security, arg, isCancelled, fileName, () => this.AddErrorLog(LocalizedStrings.Str3771));
                            break;

                        case ExportTypes.Xml:
                            exporter = new XmlExporter(security.Security, arg, isCancelled, fileName);
                            break;

                        case ExportTypes.Txt:
                            exporter = new TextExporter(security.Security, arg, isCancelled, fileName);
                            break;

                        case ExportTypes.Bin:
                            exporter = new BinExporter(security.Security, arg, isCancelled, DriveCache.Instance.GetDrive(path));
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }

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

                        try
                        {
                            this.AddInfoLog(LocalizedStrings.Str3772Params.Put(security.Security.Id, dataType.Name, _settings.ExportType, date));
                            exporter.Export(dataType, fromStorage.Load(date));
                        }
                        catch (Exception ex)
                        {
                            HandleError(ex);
                        }
                    }
                }
            }

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

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

                _settings.StartFrom = DateTime.Today - TimeSpan.FromDays(_settings.Offset);
                SaveSettings();
            }

            return(base.OnProcess());
        }