public void Subscribe(IDataProvider provider, InstrumentList instruments) { if (provider.Status != ProviderStatus.Connected) { provider.Connect(); } var newInstruments = new InstrumentList(); for (int i = 0; i < instruments.Count; i++) { var instrument = instruments.GetByIndex(i); if (!this._submap.ContainsKey(provider.Id)) { this._submap[provider.Id] = new Dictionary <Instrument, int>(); } if (!this._submap[provider.Id].ContainsKey(instrument) || this._submap[provider.Id][instrument] == 0) { this._submap[provider.Id][instrument] = 0; newInstruments.Add(instrument); } this._submap[provider.Id][instrument] += 1; } if (newInstruments.Count > 0) { provider.Subscribe(newInstruments); } }
public void Clear() { foreach (var i in Instruments) { i.Bid = null; i.Ask = null; i.Trade = null; i.Bar = null; } var deleted = new InstrumentList(); foreach (var i in Instruments.Where(i => !i.Loaded)) { deleted.Add(i); } foreach (var i in deleted) { Delete(i); } }
public void Unsubscribe(IDataProvider provider, InstrumentList instruments) { var list = new InstrumentList(); for (int i = 0; i < instruments.Count; i++) { var instrument = instruments.GetByIndex(i); if (this._submap.ContainsKey(provider.Id) && this._submap[provider.Id][instrument] != 0) { this._submap[provider.Id][instrument] -= 1; if (this._submap[provider.Id][instrument] == 0) { list.Add(instrument); } } else { Console.WriteLine($"SubscriptionManager::Unsubscribe Error. Instrument has no subscriptions {instrument.Symbol} on data provider {provider.Name}"); } } provider.Unsubscribe(list); }
public void StartStrategy(Strategy strategy, StrategyMode mode) { lock (this) { this.subscriptions.Clear(); Strategy = strategy; Mode = mode; if (this.framework.Mode == FrameworkMode.Simulation) { this.framework.Clock.DateTime = this.framework.ProviderManager.DataSimulator.DateTime1; this.framework.ExchangeClock.DateTime = DateTime.MinValue; } if (this.framework.EventManager.Status != EventManagerStatus.Running) { this.framework.EventManager.Start(); } SetStatusType(StrategyStatusType.Started); if (Persistence == StrategyPersistence.Full || Persistence == StrategyPersistence.Load) { this.framework.PortfolioManager.Load(strategy.Name); this.framework.OrderManager.Load(strategy.Name, -1); this.framework.OrderServer.SeriesName = strategy.Name; this.framework.OrderManager.IsPersistent = true; } else { this.framework.OrderManager.IsPersistent = false; } strategy.Init(); if ((Persistence == StrategyPersistence.Full || Persistence == StrategyPersistence.Save) && !strategy.Portfolio.IsLoaded) { this.framework.PortfolioManager.Save(strategy.Portfolio); } strategy.EmitStrategyStart(); if (!this.framework.IsExternalDataQueue) { var dictionary = new Dictionary <IDataProvider, InstrumentList>(); while (this.subscriptions.Count != 0) { var dictionary2 = new Dictionary <IDataProvider, InstrumentList>(this.subscriptions); this.subscriptions.Clear(); foreach (var current in dictionary2) { InstrumentList instrumentList = null; if (!dictionary.TryGetValue(current.Key, out instrumentList)) { instrumentList = new InstrumentList(); dictionary[current.Key] = instrumentList; } InstrumentList instrumentList2 = new InstrumentList(); foreach (Instrument current2 in current.Value) { if (!instrumentList.Contains(current2)) { instrumentList.Add(current2); instrumentList2.Add(current2); } } if (current.Key is SellSideStrategy) { this.framework.SubscriptionManager?.Subscribe(current.Key, instrumentList2); } } } SetParametersGroup(); Status = StrategyStatus.Running; this.subscriptions = dictionary; if (this.subscriptions.Count == 0 && mode == StrategyMode.Backtest) { Console.WriteLine($"{DateTime.Now } StrategyManager::StartStrategy {strategy.Name} has no data requests in backtest mode, stopping..."); StopStrategy(); } else { foreach (var current3 in this.subscriptions) { if (!(current3.Key is SellSideStrategy)) { this.framework.SubscriptionManager?.Subscribe(current3.Key, current3.Value); } } if (mode != StrategyMode.Backtest) { strategy.FundamentalProvider?.Connect(); } } } else { SetParametersGroup(); Status = StrategyStatus.Running; } } }