/// <summary> /// Iterates the tradeable dates enumerator /// </summary> /// <param name="date">The next tradeable date</param> /// <returns>True if we got a new date from the enumerator, false if it's exhausted, or in live mode if we're already at today</returns> private bool TryGetNextDate(out DateTime date) { if (_isLiveMode && _tradeableDates.Current >= DateTime.Today) { // special behavior for live mode, don't advance past today date = _tradeableDates.Current; return(false); } while (_tradeableDates.MoveNext()) { date = _tradeableDates.Current; CheckForDelisting(date); if (_delisted) { return(true); } if (!_mapFile.HasData(date)) { continue; } // don't do other checks if we haven't goten data for this date yet if (_previous != null && _previous.EndTime > _tradeableDates.Current) { continue; } // check for dividends and split for this security CheckForDividend(date); CheckForSplit(date); // if we have factor files check to see if we need to update the scale factors if (_hasScaleFactors) { // update our price scaling factors in light of the normalization mode UpdateScaleFactors(date); } // check to see if the symbol was remapped var newSymbol = _mapFile.GetMappedSymbol(date, _config.MappedSymbol); if (newSymbol != _config.MappedSymbol) { var changed = new SymbolChangedEvent(_config.Symbol, date, _config.MappedSymbol, newSymbol); _auxiliaryData.Enqueue(changed); _config.MappedSymbol = newSymbol; } // we've passed initial checks,now go get data for this date! return(true); } // no more tradeable dates, we've exhausted the enumerator date = DateTime.MaxValue.Date; return(false); }
/// <summary> /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here. /// </summary> /// <param name="data">Slice object keyed by symbol containing the stock data</param> public override void OnData(Slice data) { _dateTime = Time.Date; if (!Portfolio.Invested) { SetHoldings("SPWR", 1); } foreach (var symbolChangedEvent in data.SymbolChangedEvents.Values) { _changedEvent = symbolChangedEvent; Log($"{Time}: {symbolChangedEvent.OldSymbol} -> {symbolChangedEvent.NewSymbol}"); } }
/// <summary> /// Gets the history for the requested securities /// </summary> /// <param name="requests">The historical data requests</param> /// <param name="sliceTimeZone">The time zone used when time stamping the slice instances</param> /// <returns>An enumerable of the slices of data covering the span specified in each request</returns> public override IEnumerable <Slice> GetHistory(IEnumerable <HistoryRequest> requests, DateTimeZone sliceTimeZone) { List <Slice> result = new(); var slice1Date = new DateTime(2008, 01, 03, 5, 0, 0); var slice2Date = new DateTime(2013, 06, 28, 13, 32, 0); TradeBar tradeBar1 = new TradeBar { Symbol = Symbols.SPY, Time = DateTime.Now }; TradeBar tradeBar2 = new TradeBar { Symbol = Symbols.AAPL, Time = DateTime.Now }; var quoteBar1 = new QuoteBar { Symbol = Symbols.SPY, Time = DateTime.Now }; var tick1 = new Tick(DateTime.Now, Symbols.SPY, 1.1m, 2.1m) { TickType = TickType.Trade }; var split1 = new Split(Symbols.SPY, DateTime.Now, 1, 1, SplitType.SplitOccurred); var dividend1 = new Dividend(Symbols.SPY, DateTime.Now, 1, 1); var delisting1 = new Delisting(Symbols.SPY, DateTime.Now, 1, DelistingType.Delisted); var symbolChangedEvent1 = new SymbolChangedEvent(Symbols.SPY, DateTime.Now, "SPY", "SP"); Slice slice1 = new Slice(slice1Date, new BaseData[] { tradeBar1, tradeBar2, quoteBar1, tick1, split1, dividend1, delisting1, symbolChangedEvent1 }, slice1Date); TradeBar tradeBar3 = new TradeBar { Symbol = Symbols.MSFT, Time = DateTime.Now }; TradeBar tradeBar4 = new TradeBar { Symbol = Symbols.SBIN, Time = DateTime.Now }; var quoteBar2 = new QuoteBar { Symbol = Symbols.SBIN, Time = DateTime.Now }; var tick2 = new Tick(DateTime.Now, Symbols.SBIN, 1.1m, 2.1m) { TickType = TickType.Trade }; var split2 = new Split(Symbols.SBIN, DateTime.Now, 1, 1, SplitType.SplitOccurred); var dividend2 = new Dividend(Symbols.SBIN, DateTime.Now, 1, 1); var delisting2 = new Delisting(Symbols.SBIN, DateTime.Now, 1, DelistingType.Delisted); var symbolChangedEvent2 = new SymbolChangedEvent(Symbols.SBIN, DateTime.Now, "SBIN", "BIN"); Slice slice2 = new Slice(slice2Date, new BaseData[] { tradeBar3, tradeBar4, quoteBar2, tick2, split2, dividend2, delisting2, symbolChangedEvent2 }, slice2Date); result.Add(slice1); result.Add(slice2); return(result); }
/// <summary> /// Check for new mappings /// </summary> /// <param name="eventArgs">The new tradable day event arguments</param> /// <returns>New mapping event if any</returns> public IEnumerable <BaseData> GetEvents(NewTradableDateEventArgs eventArgs) { if (_config.Symbol == eventArgs.Symbol && _mapFile.HasData(eventArgs.Date)) { // check to see if the symbol was remapped var newSymbol = _mapFile.GetMappedSymbol(eventArgs.Date, _config.MappedSymbol); if (newSymbol != _config.MappedSymbol) { var changed = new SymbolChangedEvent( _config.Symbol, eventArgs.Date, _config.MappedSymbol, newSymbol); _config.MappedSymbol = newSymbol; yield return(changed); } } }
/// <summary> /// Check for new mappings /// </summary> /// <param name="eventArgs">The new tradable day event arguments</param> /// <returns>New mapping event if any</returns> public virtual IEnumerable <BaseData> GetEvents(NewTradableDateEventArgs eventArgs) { if (Config.Symbol == eventArgs.Symbol && MapFile.HasData(eventArgs.Date)) { var old = Config.MappedSymbol; var newSymbol = MapFile.GetMappedSymbol(eventArgs.Date, Config.MappedSymbol); Config.MappedSymbol = newSymbol; // check to see if the symbol was remapped if (old != Config.MappedSymbol) { var changed = new SymbolChangedEvent( Config.Symbol, eventArgs.Date, old, Config.MappedSymbol); yield return(changed); } } }