/// <summary>
        /// Запросить получение данных.
        /// </summary>
        /// <param name="series">Серия свечек, для которой необходимо начать получать данные.</param>
        /// <param name="from">Начальная дата, с которой необходимо получать данные.</param>
        /// <param name="to">Конечная дата, до которой необходимо получать данные.</param>
        public override void Start(CandleSeries series, DateTimeOffset from, DateTimeOffset to)
        {
            if (series == null)
            {
                throw new ArgumentNullException("series");
            }

            var values = GetValues(series, from, to);

            if (values == null)
            {
                return;
            }

            lock (_series.SyncRoot)
            {
                if (_series.ContainsKey(series))
                {
                    throw new ArgumentException(LocalizedStrings.Str650Params.Put(series), "series");
                }

                _series.Add(series, new SeriesInfo(series, values.GetEnumerator()));

                if (_series.Count == 1)
                {
                    Monitor.Pulse(_series.SyncRoot);
                }
            }
        }
        /// <summary>
        /// To send data request.
        /// </summary>
        /// <param name="series">The candles series for which data receiving should be started.</param>
        /// <param name="from">The initial date from which you need to get data.</param>
        /// <param name="to">The final date by which you need to get data.</param>
        public override void Start(CandleSeries series, DateTimeOffset from, DateTimeOffset to)
        {
            if (series == null)
            {
                throw new ArgumentNullException("series");
            }

            var storage = GetStorage(series);

            var range = storage.GetRange(from, to);

            if (range == null)
            {
                return;
            }

            lock (_series.SyncRoot)
            {
                if (_series.ContainsKey(series))
                {
                    throw new ArgumentException(LocalizedStrings.Str650Params.Put(series), "series");
                }

                _series.Add(series, new SeriesInfo(series, range.Min, range.Max, storage));

                if (_series.Count == 1)
                {
                    Monitor.Pulse(_series.SyncRoot);
                }
            }
        }
Esempio n. 3
0
        IEnumerable <Range <DateTimeOffset> > IExternalCandleSource.GetSupportedRanges(CandleSeries series)
        {
            if (!UseExternalCandleSource)
            {
                yield break;
            }

            var securityId = series.Security.ToSecurityId();
            var dataType   = series.CandleType.ToCandleMessageType().ToCandleMarketDataType();

            if (_historySourceSubscriptions.ContainsKey(Tuple.Create(securityId, dataType, series.Arg)))
            {
                yield return(new Range <DateTimeOffset>(DateTimeOffset.MinValue, DateTimeOffset.MaxValue));

                yield break;
            }

            var types = _historyAdapter.Drive.GetCandleTypes(securityId, _historyAdapter.StorageFormat);

            foreach (var tuple in types)
            {
                if (tuple.Item1 != series.CandleType.ToCandleMessageType())
                {
                    continue;
                }

                foreach (var arg in tuple.Item2)
                {
                    if (!arg.Equals(series.Arg))
                    {
                        continue;
                    }

                    var dates = _historyAdapter.StorageRegistry.GetCandleMessageStorage(tuple.Item1, series.Security, arg, _historyAdapter.Drive, _historyAdapter.StorageFormat).Dates;

                    if (dates.Any())
                    {
                        yield return(new Range <DateTimeOffset>(dates.First().ApplyTimeZone(TimeZoneInfo.Utc), dates.Last().ApplyTimeZone(TimeZoneInfo.Utc)));
                    }

                    break;
                }

                break;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Получить информацию о пользователях в комнате.
        /// </summary>
        /// <param name="room">Комната.</param>
        /// <returns>Пользователи.</returns>
        public IEnumerable <User> GetAuthors(ChatRoom room)
        {
            if (room == null)
            {
                throw new ArgumentNullException("room");
            }

            var ids    = Invoke(f => f.GetAuthors(SessionId, room.Id));
            var newIds = ids.Where(id => !_users.ContainsKey(id)).ToArray();

            if (!newIds.IsEmpty())
            {
                Invoke(f => f.GetUsers(SessionId, newIds)).ForEach(u => _users.TryAdd(u.Id, u));
            }

            return(ids.Select(GetUser));
        }
Esempio n. 5
0
        /// <inheritdoc />
        public void Save(Portfolio portfolio)
        {
            if (portfolio is null)
            {
                throw new ArgumentNullException(nameof(portfolio));
            }

            var isNew = false;

            lock (_portfolios.SyncRoot)
            {
                if (!_portfolios.ContainsKey(portfolio.Name))
                {
                    isNew = true;
                    _portfolios.Add(portfolio.Name, portfolio);
                }
            }

            (isNew ? NewPortfolio : PortfolioChanged)?.Invoke(portfolio);
        }
Esempio n. 6
0
        /// <inheritdoc />
        public void Save(Position position)
        {
            if (position is null)
            {
                throw new ArgumentNullException(nameof(position));
            }

            var key   = CreateKey(position);
            var isNew = false;

            lock (_positions.SyncRoot)
            {
                if (!_positions.ContainsKey(key))
                {
                    isNew = true;
                    _positions.Add(key, position);
                }
            }

            (isNew ? NewPosition : PositionChanged)?.Invoke(position);
        }
        private void Refresh()
        {
            var ids = Invoke(f => f.GetStrategies(_lastCheckTime)).ToArray();

            foreach (var tuple in ids.Where(t => t.Item2 < 0))
            {
                var strategy = _strategies.TryGetValue(tuple.Item1);

                if (strategy != null)
                {
                    StrategyDeleted?.Invoke(strategy);
                }
            }

            var newIds     = new List <long>();
            var updatedIds = new List <long>();

            foreach (var tuple in ids.Where(t => t.Item2 >= 0))
            {
                var strategy = _strategies.TryGetValue(tuple.Item1);

                if (strategy != null)
                {
                    updatedIds.Add(tuple.Item1);
                }
                else
                {
                    newIds.Add(tuple.Item1);
                }
            }

            var newStrategies = Invoke(f => f.GetDescription(newIds.ToArray()));

            foreach (var newStrategy in newStrategies)
            {
                _strategies.Add(newStrategy.Id, newStrategy);
                StrategyCreated?.Invoke(newStrategy);
            }

            var updatedStrategies = Invoke(f => f.GetDescription(updatedIds.ToArray()));

            foreach (var updatedStrategy in updatedStrategies)
            {
                var strategy = _strategies[updatedStrategy.Id];
                CopyTo(updatedStrategy, strategy);
                StrategyUpdated?.Invoke(strategy);
            }

            _lastCheckTime = DateTime.Now;

            foreach (var backtest in _backtests.CachedValues)
            {
                if (_backtestResults.ContainsKey(backtest))
                {
                    continue;
                }

                var resultId = Invoke(f => f.GetBacktestResult(SessionId, backtest.Id));

                if (resultId == null)
                {
                    continue;
                }

                _backtestResults.Add(backtest, resultId.Value);
                BacktestStopped?.Invoke(backtest);

                _startedBacktests.Remove(backtest);
            }

            foreach (var backtest in _startedBacktests.CachedKeys)
            {
                var count     = Invoke(f => f.GetCompletedIterationCount(SessionId, backtest.Id));
                var prevCount = _startedBacktests[backtest];

                if (count == prevCount)
                {
                    continue;
                }

                BacktestProgressChanged?.Invoke(backtest, count);

                if (count == backtest.Iterations.Length)
                {
                    _startedBacktests.Remove(backtest);
                }
                else
                {
                    _startedBacktests[backtest] = count;
                }
            }
        }
Esempio n. 8
0
 public bool IsFilteredMarketDepthRegistered(Security security)
 {
     return(_registeredFilteredMarketDepths.ContainsKey(security));
 }
Esempio n. 9
0
 internal static bool CanConvert(TimeSpan value)
 {
     return(_values.ContainsKey(value));
 }
Esempio n. 10
0
        private static string Escape(string text, bool useSecurityIds, out IEnumerable <string> identifiers)
        {
            if (text.IsEmptyOrWhiteSpace())
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (useSecurityIds)
            {
                text        = Decode(text.ToUpperInvariant());
                identifiers = GetSecurityIds(text).Distinct().ToArray();

                var i = 0;
                foreach (var id in identifiers)
                {
                    text = text.ReplaceIgnoreCase(id, $"values[{i}]");
                    i++;
                }

                if (i == 0)
                {
                    throw new InvalidOperationException(LocalizedStrings.NoSecIdsFound.Put(text));
                }

                return(ReplaceFuncs(text));
            }
            else
            {
                //var textWithoutFunctions = _funcReplaces
                //	.CachedPairs
                //	.Aggregate(text, (current, pair) => current.ReplaceIgnoreCase(pair.Key, string.Empty));

                const string dotSep = "__DOT__";

                text = text.Replace(".", dotSep);

                var groups = GetVariableNames(text)
                             .Where(g => !g.Value.ContainsIgnoreCase(dotSep) && !long.TryParse(g.Value, out _) && !_funcReplaces.ContainsKey(g.Value))
                             .ToArray();

                var dict = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase);

                foreach (var g in groups.OrderByDescending(g => g.Index))
                {
                    var i = dict.TryGetValue2(g.Value);

                    if (i == null)
                    {
                        i = dict.Count;
                        dict.Add(g.Value, i.Value);
                    }

                    text = text.Remove(g.Index, g.Length).Insert(g.Index, $"values[{i}]");
                }

                identifiers = dict.Keys.ToArray();

                text = text.Replace(dotSep, ".");

                return(ReplaceFuncs(text));
            }
        }