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; }
/// <summary> /// To add the strategy to the store. /// </summary> /// <param name="strategy">The strategy data.</param> public void CreateStrategy(StrategyData strategy) { var id = Invoke(f => f.CreateStrategy(SessionId, strategy)); if (id < 0) { ValidateError((byte)-id, strategy.Id, strategy.Price); } else { strategy.Id = id; } _strategies.Add(id, strategy); StrategyCreated?.Invoke(strategy); }
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; } } }