public override void OnSecuritiesChanged(SecurityChanges changes) { foreach (var tmp in UniverseManager.ActiveSecurities) { Symbol _symbol = tmp.Key; if (!allSymbols.ContainsKey(_symbol)) { IndicatorSet iSet = new IndicatorSet(this, _symbol, strategySignals); iSet.AddIndicators(); allSymbols.Add(_symbol, iSet); Streaks.Add(_symbol, new Streak()); if (DoDebug) { Debug("Added instrument: " + _symbol.Value.ToString()); } } } _universeChanges = changes; foreach (var tmp in changes.RemovedSecurities) { Liquidate(tmp.Symbol, "Removed"); } }
public void OnData(TradeBars data) { int ntpCount = 0; //Need to purchase count foreach (var tmp in data) { string name = tmp.Key.ToString(); Symbol symbol = tmp.Key; if (symbol.Value.ToString() == "SPY") { continue; } decimal price = tmp.Value.Close; int[] status = allSymbols[symbol].Status(); if (DoDebug) { IndicatorSet iSet = allSymbols[symbol]; iSet.Plot(); } if (symbol.Value.ToString() == "AAPL") { allSymbols[symbol].Plot(); } int count = 0; for (int i = 0; i < strategySignals.Length; i++) { if (strategySignals[i]) { count += status[i]; Log(status[i]); } //If indicator applied to strategy } //Check if count exceed minimal signals count int ntp = 0; if ((count >= StrategyMinimalSignalsCount) && !Portfolio[symbol].Invested) { ntp = 1; ntpCount++; } else if ((count < 0) && Portfolio[symbol].Invested) { ntp = -1; } needToPurchase[symbol] = ntp; } int investedCount = 0; if (Portfolio.HoldStock) { foreach (var val in Portfolio) { Symbol nm = val.Key; if ((Portfolio[nm].Invested) && (needToPurchase[nm] == 0)) { investedCount++; } if ((Portfolio[nm].Invested) && (needToPurchase[nm] < 0)) { investedCount--; Liquidate(nm, allSymbols[nm].ToString()); } } } int totalCount = investedCount + ntpCount; foreach (var tmp in needToPurchase) { if (tmp.Value > 0) { SetHoldings(tmp.Key, 1.0 / (totalCount)); } } needToPurchase.Clear(); }