public AllSecurityMarketDataStorage(Security security, object arg, Func <T, DateTimeOffset> getTime, Func <T, Security> getSecurity, Func <Security, IMarketDataDrive, IMarketDataStorage <T> > getStorage, IMarketDataStorageDrive drive)
        {
            if (security == null)
            {
                throw new ArgumentNullException("security");
            }

            if (getTime == null)
            {
                throw new ArgumentNullException("getTime");
            }

            if (getSecurity == null)
            {
                throw new ArgumentNullException("getSecurity");
            }

            if (getStorage == null)
            {
                throw new ArgumentNullException("getStorage");
            }

            if (drive == null)
            {
                throw new ArgumentNullException("drive");
            }

            _security = security;
            _getTime  = getTime;

            _arg  = arg;
            Drive = drive;

            _basket = new BasketMarketDataStorage <T>();

            var idGenerator = new SecurityIdGenerator();

            var parts = idGenerator.Split(security.Id);
            var code  = parts.Item1;

            var securities = InteropHelper
                             .GetDirectories(Path.Combine(Drive.Drive.Path, code.Substring(0, 1)), code + "*")
                             .Select(p => Path.GetFileName(p).FolderNameToSecurityId())
                             .Select(s =>
            {
                var id = idGenerator.Split(s);

                var clone   = security.Clone();
                clone.Id    = s;
                clone.Board = ExchangeBoard.GetOrCreateBoard(id.Item2);
                return(clone);
            });

            foreach (var sec in securities)
            {
                _basket.InnerStorages.Add(getStorage(sec, Drive.Drive));
            }
        }
                public LocalMessageStorage(LocalSecurityMarketDataStorage parent, string fileName, IMessageSerializer <TMessage> serializer)
                {
                    if (parent == null)
                    {
                        throw new ArgumentNullException("parent");
                    }

                    if (serializer == null)
                    {
                        throw new ArgumentNullException("serializer");
                    }

                    if (fileName.IsEmpty())
                    {
                        throw new ArgumentNullException("fileName");
                    }

                    _parent                = parent;
                    _fileName              = fileName;
                    _serializer            = serializer;
                    _fileNameWithExtension = _fileName + GetExtension(_serializer);

                    _datesDict = new Lazy <CachedSynchronizedOrderedDictionary <DateTime, DateTime> >(() =>
                    {
                        var retVal = new CachedSynchronizedOrderedDictionary <DateTime, DateTime>();

                        var datesPath = GetDatesCachePath();

                        if (File.Exists(datesPath))
                        {
                            foreach (var date in LoadDates())
                            {
                                retVal.Add(date, date);
                            }
                        }
                        else
                        {
                            var rootDir = _parent.Path;

                            var dates = InteropHelper
                                        .GetDirectories(rootDir)
                                        .Where(dir => File.Exists(IOPath.Combine(dir, _fileNameWithExtension)))
                                        .Select(dir => IOPath.GetFileName(dir).ToDateTime(_dateFormat));

                            foreach (var date in dates)
                            {
                                retVal.Add(date, date);
                            }

                            SaveDates(retVal.CachedValues);
                        }

                        return(retVal);
                    }).Track();
                }
            public LocalMarketDataStorageDrive(string fileName, string path, StorageFormats format, IMarketDataDrive drive)
            {
                if (drive == null)
                {
                    throw new ArgumentNullException("drive");
                }

                if (fileName.IsEmpty())
                {
                    throw new ArgumentNullException("fileName");
                }

                if (path.IsEmpty())
                {
                    throw new ArgumentNullException("path");
                }

                _fileName = fileName;
                _path     = path;
                _format   = format;
                _drive    = drive;
                _fileNameWithExtension = _fileName + GetExtension(_format);

                _datesDict = new Lazy <CachedSynchronizedOrderedDictionary <DateTime, DateTime> >(() =>
                {
                    var retVal = new CachedSynchronizedOrderedDictionary <DateTime, DateTime>();

                    var datesPath = GetDatesCachePath();

                    if (File.Exists(datesPath))
                    {
                        foreach (var date in LoadDates())
                        {
                            retVal.Add(date, date);
                        }
                    }
                    else
                    {
                        var dates = InteropHelper
                                    .GetDirectories(_path)
                                    .Where(dir => File.Exists(IOPath.Combine(dir, _fileNameWithExtension)))
                                    .Select(dir => IOPath.GetFileName(dir).ToDateTime(_dateFormat));

                        foreach (var date in dates)
                        {
                            retVal.Add(date, date);
                        }

                        SaveDates(retVal.CachedValues);
                    }

                    return(retVal);
                }).Track();
            }
Exemplo n.º 4
0
            private IEnumerable <IMarketDataStorage <ExecutionMessage> > GetExecutionStorages()
            {
                var executionRegistry = _sessionStrategy.GetExecutionStorage();

                var securities = InteropHelper
                                 .GetDirectories(executionRegistry.DefaultDrive.Path)
                                 .Select(letterDir => new { letterDir, name = Path.GetFileName(letterDir) })
                                 .Where(t => t.name != null && t.name.Length == 1)
                                 .SelectMany(t => InteropHelper.GetDirectories(t.letterDir))
                                 .Select(p => Path.GetFileName(p).FolderNameToSecurityId())
                                 .Select(this.LookupById)
                                 .Where(s => s != null)
                                 .ToArray();

                return(securities.Select(s => executionRegistry.GetExecutionStorage(s, ExecutionTypes.Order)));
            }
Exemplo n.º 5
0
            //private static readonly Version _dateVersion = new Version(1, 0);

            public LocalMarketDataStorageDrive(Type dataType, object arg, string path, StorageFormats format, LocalMarketDataDrive drive)
            {
                var fileName = GetFileName(dataType, arg);

                if (path.IsEmpty())
                {
                    throw new ArgumentNullException(nameof(path));
                }

                _path  = path;
                _drive = drive ?? throw new ArgumentNullException(nameof(drive));
                _fileNameWithExtension = fileName + GetExtension(format);
                _datesPath             = IOPath.Combine(_path, fileName + format + "Dates.txt");

                _dataType = DataType.Create(dataType, arg);

                _datesDict = new Lazy <CachedSynchronizedOrderedDictionary <DateTime, DateTime> >(() =>
                {
                    var retVal = new CachedSynchronizedOrderedDictionary <DateTime, DateTime>();

                    if (File.Exists(_datesPath))
                    {
                        foreach (var date in LoadDates())
                        {
                            retVal.Add(date, date);
                        }
                    }
                    else
                    {
                        var dates = InteropHelper
                                    .GetDirectories(_path)
                                    .Where(dir => File.Exists(IOPath.Combine(dir, _fileNameWithExtension)))
                                    .Select(dir => GetDate(IOPath.GetFileName(dir)));

                        foreach (var date in dates)
                        {
                            retVal.Add(date, date);
                        }

                        SaveDates(retVal.CachedValues);
                    }

                    return(retVal);
                }).Track();
            }
Exemplo n.º 6
0
        /// <summary>
        /// Get all available data types.
        /// </summary>
        /// <param name="securityId">Instrument identifier.</param>
        /// <param name="format">Format type.</param>
        /// <returns>Data types.</returns>
        public override IEnumerable <Tuple <Type, object> > GetAvailableDataTypes(SecurityId securityId, StorageFormats format)
        {
            var secPath = GetSecurityPath(securityId);

            if (!Directory.Exists(secPath))
            {
                return(Enumerable.Empty <Tuple <Type, object> >());
            }

            var ext = GetExtension(format);

            return(InteropHelper
                   .GetDirectories(secPath)
                   .SelectMany(dir => Directory.GetFiles(dir, "candles_*" + ext).Select(IOPath.GetFileNameWithoutExtension))
                   .Distinct()
                   .Select(GetDataType)
                   .Where(t => t != null));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Получить для инструмента доступные типы свечек с параметрами.
        /// </summary>
        /// <param name="securityId">Идентификатор инструмента.</param>
        /// <param name="format">Тип формата.</param>
        /// <returns>Доступные типы свечек с параметрами.</returns>
        public override IEnumerable <Tuple <Type, object[]> > GetCandleTypes(SecurityId securityId, StorageFormats format)
        {
            var secPath = GetSecurityPath(securityId);

            var ext = GetExtension(format);

            return(InteropHelper
                   .GetDirectories(secPath)
                   .SelectMany(dir => Directory.GetFiles(dir, "candles_*" + ext).Select(IOPath.GetFileNameWithoutExtension))
                   .Distinct()
                   .Select(fileName =>
            {
                var parts = fileName.Split('_');
                var type = "{0}.{1}Message, {2}".Put(typeof(CandleMessage).Namespace, parts[1], typeof(CandleMessage).Assembly.FullName).To <Type>();
                var value = type.ToCandleArg(parts[2]);

                return Tuple.Create(type, value);
            })
                   .GroupBy(t => t.Item1)
                   .Select(g => Tuple.Create(g.Key, g.Select(t => t.Item2).ToArray()))
                   .ToArray());
        }
            IEnumerable <Tuple <Type, object[]> > ISecurityMarketDataStorage.GetCandleTypes(IMessageSerializer <CandleMessage> serializer)
            {
                var secPath = _parent.GetSecurityPath(_securityId);

                var ext = GetExtension(serializer);

                return(InteropHelper
                       .GetDirectories(secPath)
                       .SelectMany(dir => Directory.GetFiles(dir, "candles_*" + ext).Select(IOPath.GetFileNameWithoutExtension))
                       .Distinct()
                       .Select(fileName =>
                {
                    var parts = fileName.Split('_');
                    var type = "{0}.{1}, {2}".Put(typeof(Candle).Namespace, parts[1], typeof(Candle).Assembly.FullName).To <Type>();
                    var value = type.ToCandleMessageType().ToCandleArg(parts[2]);

                    return Tuple.Create(type, value);
                })
                       .GroupBy(t => t.Item1)
                       .Select(g => Tuple.Create(g.Key, g.Select(t => t.Item2).ToArray()))
                       .ToArray());
            }
Exemplo n.º 9
0
        private void Sync_Click(object sender, RoutedEventArgs e)
        {
            if (_token != null)
            {
                StopSync();
                return;
            }

            Sync.Content = LocalizedStrings.Str2890;

            _token = new CancellationTokenSource();

            Task.Factory.StartNew(() =>
            {
                var securityPaths = new List <string>();

                foreach (var dir in DriveCache.Instance.AllDrives
                         .OfType <LocalMarketDataDrive>()
                         .Select(drive => drive.Path)
                         .Distinct())
                {
                    foreach (var letterDir in InteropHelper.GetDirectories(dir))
                    {
                        if (_token.IsCancellationRequested)
                        {
                            break;
                        }

                        var name = Path.GetFileName(letterDir);

                        if (name == null || name.Length != 1)
                        {
                            continue;
                        }

                        securityPaths.AddRange(InteropHelper.GetDirectories(letterDir));
                    }

                    if (_token.IsCancellationRequested)
                    {
                        break;
                    }
                }

                if (_token.IsCancellationRequested)
                {
                    return;
                }

                var iterCount =
                    securityPaths.Count +                                                                            // кол-во проходов по директории для создания инструмента
                    DriveCache.Instance.AllDrives.Count() * (EntityRegistry.Securities.Count + securityPaths.Count); // кол-во сбросов кэша дат

                this.GuiSync(() => Progress.Maximum = iterCount);

                var logSource = ConfigManager.GetService <LogManager>().Application;

                var securityIdGenerator = new SecurityIdGenerator();

                var securities = EntityRegistry.Securities.ToDictionary(s => s.Id, s => new KeyValuePair <Security, bool>(s, true), StringComparer.InvariantCultureIgnoreCase);

                foreach (var securityPath in securityPaths)
                {
                    if (_token.IsCancellationRequested)
                    {
                        break;
                    }

                    var securityId = Path.GetFileName(securityPath).FolderNameToSecurityId();

                    var isNew = false;

                    var security = securities.TryGetValue(securityId).Key;
                    if (security == null)
                    {
                        var firstDataFile =
                            Directory.EnumerateDirectories(securityPath)
                            .SelectMany(d => Directory.EnumerateFiles(d, "*.bin")
                                        .Concat(Directory.EnumerateFiles(d, "*.csv"))
                                        .OrderBy(f => Path.GetExtension(f).CompareIgnoreCase(".bin") ? 0 : 1))
                            .FirstOrDefault();

                        if (firstDataFile != null)
                        {
                            var info = securityIdGenerator.Split(securityId);

                            decimal priceStep;

                            if (Path.GetExtension(firstDataFile).CompareIgnoreCase(".bin"))
                            {
                                try
                                {
                                    priceStep = File.ReadAllBytes(firstDataFile).Range(6, 16).To <decimal>();
                                }
                                catch (Exception ex)
                                {
                                    throw new InvalidOperationException(LocalizedStrings.Str2929Params.Put(firstDataFile), ex);
                                }
                            }
                            else
                            {
                                priceStep = 0.01m;
                            }

                            security = new Security
                            {
                                Id            = securityId,
                                PriceStep     = priceStep,
                                Name          = info.Item1,
                                Code          = info.Item1,
                                Board         = ExchangeBoard.GetOrCreateBoard(info.Item2),
                                ExtensionInfo = new Dictionary <object, object>()
                            };

                            securities.Add(securityId, new KeyValuePair <Security, bool>(security, false));

                            isNew = true;
                        }
                    }

                    this.GuiSync(() =>
                    {
                        Progress.Value++;

                        if (isNew)
                        {
                            Logs.Messages.Add(new LogMessage(logSource, TimeHelper.Now, LogLevels.Info, LocalizedStrings.Str2930Params.Put(security)));
                        }
                    });
                }

                EntityRegistry.Securities.AddRange(securities.Values.Where(p => !p.Value).Select(p => p.Key));

                if (_token.IsCancellationRequested)
                {
                    return;
                }

                var dataTypes = new Dictionary <Type, object>
                {
                    { typeof(ExecutionMessage), ExecutionTypes.Tick },
                    { typeof(ExecutionMessage), ExecutionTypes.OrderLog },
                    { typeof(ExecutionMessage), ExecutionTypes.Order },
                    { typeof(ExecutionMessage), ExecutionTypes.Trade },
                    { typeof(QuoteChangeMessage), null },
                    { typeof(Level1ChangeMessage), null },
                    { typeof(NewsMessage), null }
                };

                var formats = Enumerator.GetValues <StorageFormats>();

                foreach (var drive in DriveCache.Instance.AllDrives)
                {
                    foreach (var security in EntityRegistry.Securities)
                    {
                        foreach (var dataType in dataTypes)
                        {
                            foreach (var format in formats)
                            {
                                if (_token.IsCancellationRequested)
                                {
                                    break;
                                }

                                drive.GetStorageDrive(security.ToSecurityId(), dataType.Key, dataType.Value, format).ClearDatesCache();
                            }
                        }

                        if (_token.IsCancellationRequested)
                        {
                            break;
                        }

                        this.GuiSync(() =>
                        {
                            Progress.Value++;
                            Logs.Messages.Add(new LogMessage(logSource, TimeHelper.Now, LogLevels.Info,
                                                             LocalizedStrings.Str2931Params.Put(security, drive.Path)));
                        });
                    }

                    if (_token.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }, _token.Token)
            .ContinueWithExceptionHandling(this, res =>
            {
                Sync.Content   = LocalizedStrings.Str2932;
                Sync.IsEnabled = true;

                Progress.Value = 0;

                _token = null;
            });
        }
Exemplo n.º 10
0
        public AllSecurityMarketDataStorage(Security security,
                                            object arg,
                                            Func <T, DateTimeOffset> getTime,
                                            Func <T, Security> getSecurity,
                                            Func <Security, IMarketDataDrive, IMarketDataStorage <T> > getStorage,
                                            IMarketDataStorageDrive drive,
                                            IExchangeInfoProvider exchangeInfoProvider)
        {
            if (security == null)
            {
                throw new ArgumentNullException(nameof(security));
            }

            if (getTime == null)
            {
                throw new ArgumentNullException(nameof(getTime));
            }

            if (getSecurity == null)
            {
                throw new ArgumentNullException(nameof(getSecurity));
            }

            if (getStorage == null)
            {
                throw new ArgumentNullException(nameof(getStorage));
            }

            if (drive == null)
            {
                throw new ArgumentNullException(nameof(drive));
            }

            if (exchangeInfoProvider == null)
            {
                throw new ArgumentNullException(nameof(exchangeInfoProvider));
            }

            _security             = security;
            _exchangeInfoProvider = exchangeInfoProvider;
            _getTime = getTime;

            _arg  = arg;
            Drive = drive;

            _basket = new BasketMarketDataStorage <T>();

            var id   = security.Id.ToSecurityId();
            var code = id.SecurityCode;

            var securities = InteropHelper
                             .GetDirectories(Path.Combine(Drive.Drive.Path, code.Substring(0, 1)), code + "*")
                             .Select(p => Path.GetFileName(p).FolderNameToSecurityId())
                             .Select(s =>
            {
                var idInfo = s.ToSecurityId();

                var clone   = security.Clone();
                clone.Id    = s;
                clone.Board = _exchangeInfoProvider.GetOrCreateBoard(idInfo.BoardCode);
                return(clone);
            });

            foreach (var sec in securities)
            {
                _basket.InnerStorages.Add(getStorage(sec, Drive.Drive));
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Synchronize securities with storage.
        /// </summary>
        /// <param name="drives">Storage drives.</param>
        /// <param name="securityStorage">Securities meta info storage.</param>
        /// <param name="exchangeInfoProvider">Exchanges and trading boards provider.</param>
        /// <param name="newSecurity">The handler through which a new instrument will be passed.</param>
        /// <param name="updateProgress">The handler through which a progress change will be passed.</param>
        /// <param name="logsReceiver">Logs receiver.</param>
        /// <param name="isCancelled">The handler which returns an attribute of search cancel.</param>
        public static void SynchronizeSecurities(this IEnumerable <IMarketDataDrive> drives,
                                                 ISecurityStorage securityStorage, IExchangeInfoProvider exchangeInfoProvider,
                                                 Action <Security> newSecurity, Action <int, int> updateProgress,
                                                 Func <bool> isCancelled, ILogReceiver logsReceiver)
        {
            if (drives == null)
            {
                throw new ArgumentNullException(nameof(drives));
            }

            if (securityStorage == null)
            {
                throw new ArgumentNullException(nameof(securityStorage));
            }

            if (exchangeInfoProvider == null)
            {
                throw new ArgumentNullException(nameof(exchangeInfoProvider));
            }

            if (newSecurity == null)
            {
                throw new ArgumentNullException(nameof(newSecurity));
            }

            if (updateProgress == null)
            {
                throw new ArgumentNullException(nameof(updateProgress));
            }

            if (isCancelled == null)
            {
                throw new ArgumentNullException(nameof(isCancelled));
            }

            if (logsReceiver == null)
            {
                throw new ArgumentNullException(nameof(logsReceiver));
            }

            var securityPaths = new List <string>();
            var progress      = 0;

            foreach (var dir in drives.Select(drive => drive.Path).Distinct())
            {
                foreach (var letterDir in InteropHelper.GetDirectories(dir))
                {
                    if (isCancelled())
                    {
                        break;
                    }

                    var name = Path.GetFileName(letterDir);

                    if (name == null || name.Length != 1)
                    {
                        continue;
                    }

                    securityPaths.AddRange(InteropHelper.GetDirectories(letterDir));
                }

                if (isCancelled())
                {
                    break;
                }
            }

            if (isCancelled())
            {
                return;
            }

            // кол-во проходов по директории для создания инструмента
            var iterCount = securityPaths.Count;

            updateProgress(0, iterCount);

            var securities = securityStorage.LookupAll().ToDictionary(s => s.Id, s => s, StringComparer.InvariantCultureIgnoreCase);

            foreach (var securityPath in securityPaths)
            {
                if (isCancelled())
                {
                    break;
                }

                var securityId = Path.GetFileName(securityPath).FolderNameToSecurityId();

                var isNew = false;

                var security = securities.TryGetValue(securityId);

                if (security == null)
                {
                    var firstDataFile =
                        Directory.EnumerateDirectories(securityPath)
                        .SelectMany(d => Directory.EnumerateFiles(d, "*.bin")
                                    .Concat(Directory.EnumerateFiles(d, "*.csv"))
                                    .OrderBy(f => Path.GetExtension(f).CompareIgnoreCase(".bin") ? 0 : 1))
                        .FirstOrDefault();

                    if (firstDataFile != null)
                    {
                        var id = securityId.ToSecurityId();

                        decimal priceStep;

                        if (Path.GetExtension(firstDataFile).CompareIgnoreCase(".bin"))
                        {
                            try
                            {
                                priceStep = File.ReadAllBytes(firstDataFile).Range(6, 16).To <decimal>();
                            }
                            catch (Exception ex)
                            {
                                throw new InvalidOperationException(LocalizedStrings.Str2929Params.Put(firstDataFile), ex);
                            }
                        }
                        else
                        {
                            priceStep = 0.01m;
                        }

                        security = new Security
                        {
                            Id        = securityId,
                            PriceStep = priceStep,
                            Name      = id.SecurityCode,
                            Code      = id.SecurityCode,
                            Board     = exchangeInfoProvider.GetOrCreateBoard(id.BoardCode),
                        };

                        securities.Add(securityId, security);

                        securityStorage.Save(security, false);
                        newSecurity(security);

                        isNew = true;
                    }
                }

                updateProgress(progress++, iterCount);

                if (isNew)
                {
                    logsReceiver.AddInfoLog(LocalizedStrings.Str2930Params, security);
                }
            }
        }