public void FiltersExpiryRange() { var time = new DateTime(2016, 02, 26); var underlying = new Tick { Value = 10m, Time = time }; var filter = new StrikeExpiryOptionFilter(0, 0, TimeSpan.FromDays(3), TimeSpan.FromDays(7)); var symbols = new[] { Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(0)), // 0 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(1)), // 1 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(2)), // 2 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(3)), // 3 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(4)), // 4 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(5)), // 5 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(6)), // 6 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(7)), // 7 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(8)), // 8 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, time.AddDays(9)), // 9 }; var filtered = filter.Filter(symbols, underlying).ToList(); Assert.AreEqual(5, filtered.Count); Assert.AreEqual(symbols[3], filtered[0]); Assert.AreEqual(symbols[4], filtered[1]); Assert.AreEqual(symbols[5], filtered[2]); Assert.AreEqual(symbols[6], filtered[3]); Assert.AreEqual(symbols[7], filtered[4]); }
public void PassesTicksStraightThrough() { var enumerator = new EnqueueableEnumerator<Tick>(); // add some ticks var currentTime = new DateTime(2015, 10, 08); // returns true even if no data present until stop is called Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); var tick1 = new Tick(currentTime, Symbols.SPY, 199.55m, 199, 200) {Quantity = 10}; enumerator.Enqueue(tick1); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(tick1, enumerator.Current); Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); var tick2 = new Tick(currentTime, Symbols.SPY, 199.56m, 199.21m, 200.02m) {Quantity = 5}; enumerator.Enqueue(tick2); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(tick2, enumerator.Current); enumerator.Stop(); Assert.IsFalse(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); }
public void FiltersStrikeRange() { var expiry = new DateTime(2016, 03, 04); var underlying = new Tick { Value = 10m, Time = new DateTime(2016, 02, 26) }; var filter = new StrikeExpiryOptionFilter(-2, 3, TimeSpan.Zero, TimeSpan.MaxValue); var symbols = new[] { Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 2, expiry), // 0 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 5, expiry), // 1 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 7, expiry), // 2 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 8, expiry), // 3 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 9, expiry), // 4 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 10, expiry), // 5 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 11, expiry), // 6 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 12, expiry), // 7 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 15, expiry), // 8 Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 20, expiry), // 9 }; var filtered = filter.Filter(symbols, underlying).ToList(); Assert.AreEqual(5, filtered.Count); Assert.AreEqual(symbols[3], filtered[0]); Assert.AreEqual(symbols[4], filtered[1]); Assert.AreEqual(symbols[5], filtered[2]); Assert.AreEqual(symbols[6], filtered[3]); Assert.AreEqual(symbols[7], filtered[4]); }
private bool Compare(Tick expected, Tick actual) { return expected.Time == actual.Time && expected.BidPrice == actual.BidPrice && expected.AskPrice == actual.AskPrice && expected.Quantity == actual.Quantity; }
public void AggregatesNewTradeBarProperly() { TradeBar newTradeBar = null; var creator = new BaseDataConsolidator(4); creator.DataConsolidated += (sender, tradeBar) => { newTradeBar = tradeBar; }; var reference = DateTime.Today; var bar1 = new Tick { Symbol = "SPY", Time = reference, Value = 5, Quantity = 10 }; creator.Update(bar1); Assert.IsNull(newTradeBar); var bar2 = new Tick { Symbol = "SPY", Time = reference.AddHours(1), Value = 10, Quantity = 20 }; creator.Update(bar2); Assert.IsNull(newTradeBar); var bar3 = new Tick { Symbol = "SPY", Time = reference.AddHours(2), Value = 1, Quantity = 10 }; creator.Update(bar3); Assert.IsNull(newTradeBar); var bar4 = new Tick { Symbol = "SPY", Time = reference.AddHours(3), Value = 9, Quantity = 20 }; creator.Update(bar4); Assert.IsNotNull(newTradeBar); Assert.AreEqual("SPY", newTradeBar.Symbol); Assert.AreEqual(bar1.Time, newTradeBar.Time); Assert.AreEqual(bar1.Value, newTradeBar.Open); Assert.AreEqual(bar2.Value, newTradeBar.High); Assert.AreEqual(bar3.Value, newTradeBar.Low); Assert.AreEqual(bar4.Value, newTradeBar.Close); // base data can't aggregate volume Assert.AreEqual(0, newTradeBar.Volume); }
public void AccessesTicksBySymbol() { Tick tick1 = new Tick(DateTime.Now, Symbols.SPY, 1, 2); Tick tick2 = new Tick(DateTime.Now, Symbols.SPY, 1.1m, 2.1m); Slice slice = new Slice(DateTime.Now, new[] { tick1, tick2 }); List<Tick> data = slice[tick1.Symbol]; Assert.IsInstanceOf(typeof(List<Tick>), data); Assert.AreEqual(2, data.Count); }
public void AccessesTicksCollection() { Tick tick1 = new Tick(DateTime.Now, Symbols.SPY, 1, 2); Tick tick2 = new Tick(DateTime.Now, Symbols.SPY, 1.1m, 2.1m); Tick tick3 = new Tick(DateTime.Now, Symbols.AAPL, 1, 2); Tick tick4 = new Tick(DateTime.Now, Symbols.AAPL, 1.1m, 2.1m); Slice slice = new Slice(DateTime.Now, new[] { tick1, tick2, tick3, tick4 }); Ticks ticks = slice.Ticks; Assert.AreEqual(2, ticks.Count); Assert.AreEqual(2, ticks[Symbols.SPY].Count); Assert.AreEqual(2, ticks[Symbols.AAPL].Count); }
public void ConstructsFromLine() { const string line = "15093000,1456300,100,P,T,0"; var baseDate = new DateTime(2013, 10, 08); var tick = new Tick("SPY", line, baseDate); var ms = (tick.Time - baseDate).TotalMilliseconds; Assert.AreEqual(15093000, ms); Assert.AreEqual(1456300, tick.LastPrice * 10000m); Assert.AreEqual(100, tick.Quantity); Assert.AreEqual("P", tick.Exchange); Assert.AreEqual("T", tick.SaleCondition); Assert.AreEqual(false, tick.Suspicious); }
public void ReturnsTheSameObjectReference() { var identity = new IdentityDataConsolidator<Tick>(); var tick = new Tick(); int count = 0; identity.DataConsolidated += (sender, data) => { Assert.IsTrue(ReferenceEquals(tick, data)); count++; }; identity.Update(tick); Assert.AreEqual(1, count); }
public void RecordsInternalQueueCount() { var enumerator = new EnqueueableEnumerator<Tick>(); var currentTime = new DateTime(2015, 12, 01); var tick = new Tick(currentTime, Symbols.SPY, 100, 101); enumerator.Enqueue(tick); Assert.AreEqual(1, enumerator.Count); tick = new Tick(currentTime, Symbols.SPY, 100, 101); enumerator.Enqueue(tick); Assert.AreEqual(2, enumerator.Count); enumerator.MoveNext(); Assert.AreEqual(1, enumerator.Count); enumerator.MoveNext(); Assert.AreEqual(0, enumerator.Count); }
public void AcceptsTickDataWithSameTimestamps() { var reference = new DateTime(2015, 09, 23); var identity = new IdentityDataConsolidator<Tick>(); int count = 0; identity.DataConsolidated += (sender, data) => { count++; }; var tradeBar = new Tick { EndTime = reference }; identity.Update(tradeBar); tradeBar = (Tick)tradeBar.Clone(); identity.Update(tradeBar); Assert.AreEqual(2, count); }
/// <summary> /// Handle a new price update packet: /// </summary> private void OnLevel1SummaryUpdateEvent(object sender, Level1SummaryUpdateEventArgs e) { // if ticker is not found, unsubscribe if (e.NotFound) Unsubscribe(e.Symbol); // only update if we have a value if (e.Last == 0) return; // only accept trade updates if (e.TypeOfUpdate != Level1SummaryUpdateEventArgs.UpdateType.ExtendedTrade && e.TypeOfUpdate != Level1SummaryUpdateEventArgs.UpdateType.Trade) return; count++; var time = FeedTime; var symbol = Symbol.Create(e.Symbol, SecurityType.Equity, Market.USA); var last = (decimal)(e.TypeOfUpdate == Level1SummaryUpdateEventArgs.UpdateType.ExtendedTrade ? e.ExtendedTradingLast : e.Last); if (e.Symbol.Contains(".FXCM")) { // the feed time is in NYC/EDT, convert it into EST time = FeedTime.ConvertTo(TimeZones.NewYork, TimeZones.EasternStandard); symbol = Symbol.Create(e.Symbol.Replace(".FXCM", string.Empty), SecurityType.Forex, Market.FXCM); } var tick = new Tick(time, symbol, last, (decimal)e.Bid, (decimal)e.Ask) { AskSize = e.AskSize, BidSize = e.BidSize, TickType = TickType.Trade, Quantity = e.IncrementalVolume }; _dataQueue.Add(tick); _prices[e.Symbol] = e.Last; }
/// <summary> /// Updates the current working bar or creates a new working bar if TriggerArchive has been called /// </summary> /// <remarks> /// This method assumes only one thread will be using this method. It is intended to /// be consumed by the live trading data feed data tasks (live/custom) /// </remarks> /// <param name="tick">The new data to aggregate</param> public void Update(Tick tick) { if (!IsMarketOpen(tick)) return; // get the current working bar and update it BaseData working; if (!_queue.TryPeek(out working)) { // the consumer took the bar, create a new one working = CreateNewTradeBar(tick.LastPrice, tick.Quantity); _queue.Enqueue(working); } working.Update(tick.LastPrice, tick.BidPrice, tick.AskPrice, tick.Quantity, tick.BidSize, tick.AskSize); }
static void Main(string[] args) { if (args.Length != 5) { Console.WriteLine("Usage: LeanTicksToBars SYMBOLS TYPE MARKET FROMDATE TODATE"); Console.WriteLine("SYMBOLS = eg EURUSD,USDJPY"); Console.WriteLine("TYPE = Forex/Cfd"); Console.WriteLine("MARKET = eg oanda"); Console.WriteLine("FROMDATE = yyyymmdd"); Console.WriteLine("TODATE = yyyymmdd"); Environment.Exit(1); } // Load settings from command line var tickers = args[0].Split(','); var securityType = (SecurityType)Enum.Parse(typeof(SecurityType), args[1]); var market = args[2].ToLower(); var startDate = DateTime.ParseExact(args[3], "yyyyMMdd", CultureInfo.InvariantCulture); var endDate = DateTime.ParseExact(args[4], "yyyyMMdd", CultureInfo.InvariantCulture); // Load settings from config.json var dataDirectory = Config.Get("data-folder", "../../../Lean/Data/"); var rootPath = Path.Combine(dataDirectory, securityType.ToString().ToLower(), market, "tick"); foreach (var ticker in tickers) { var symbol = Symbol.Create(ticker, securityType, market); var path = Path.Combine(rootPath, ticker.ToLower()); Console.WriteLine(path); var date = startDate; while (date <= endDate) { var fileName = Path.Combine(path, date.ToString("yyyyMMdd") + "_quote.zip"); if (File.Exists(fileName)) { Console.WriteLine(fileName); var ticks = new List<Tick>(); // Read tick data using (var zip = new StreamReader(fileName)) { using (var reader = Compression.UnzipStream(zip.BaseStream)) { string line; while ((line = reader.ReadLine()) != null) { var csv = line.ToCsv(3); var time = date.Date.AddMilliseconds(csv[0].ToInt64()); var bid = csv[1].ToDecimal(); var ask = csv[2].ToDecimal(); var tick = new Tick(time, symbol, bid, ask); ticks.Add(tick); } } } // Save the data (all resolutions) foreach (var res in new[] { Resolution.Second, Resolution.Minute, Resolution.Hour, Resolution.Daily }) { var resData = AggregateTicks(symbol, ticks, res.ToTimeSpan()); var writer = new LeanDataWriter(securityType, res, symbol, dataDirectory, market); writer.Write(resData); } } date = date.AddDays(1); } } }
void HandleTickSize(object sender, IB.TickSizeEventArgs e) { var symbol = default(SymbolCacheKey); if (!_subscribedTickets.TryGetValue(e.TickerId, out symbol)) return; var tick = new Tick(); tick.Symbol = symbol.Item2; tick.Quantity = AdjustQuantity(symbol.Item1, e.Size); tick.Time = GetBrokerTime(); if (tick.Quantity == 0) return; switch (e.TickType) { case IB.TickType.BidSize: tick.TickType = TickType.Quote; _lastBidPrices.TryGetValue(symbol, out tick.BidPrice); _lastBidSizes[symbol] = tick.Quantity; tick.Value = tick.BidPrice; break; case IB.TickType.AskSize: tick.TickType = TickType.Quote; _lastAskPrices.TryGetValue(symbol, out tick.AskPrice); _lastAskSizes[symbol] = tick.Quantity; tick.Value = tick.AskPrice; break; case IB.TickType.LastSize: tick.TickType = TickType.Trade; decimal lastPrice; _lastPrices.TryGetValue(symbol, out lastPrice); _lastVolumes[symbol] = tick.Quantity; tick.Value = lastPrice; break; default: return; } lock (_ticks) if (tick.IsValid()) _ticks.Add(tick); }
void HandleTickPrice(object sender, IB.TickPriceEventArgs e) { var symbol = default(SymbolCacheKey); if (!_subscribedTickets.TryGetValue(e.TickerId, out symbol)) return; var tick = new Tick(); tick.Symbol = symbol.Item2; tick.Time = GetBrokerTime(); tick.Value = e.Price; if (e.Price <= 0 && symbol.Item1 != SecurityType.Future && symbol.Item1 != SecurityType.Option) return; switch (e.TickType) { case IB.TickType.BidPrice: tick.TickType = TickType.Quote; tick.BidPrice = e.Price; _lastBidSizes.TryGetValue(symbol, out tick.Quantity); _lastBidPrices[symbol] = e.Price; break; case IB.TickType.AskPrice: tick.TickType = TickType.Quote; tick.AskPrice = e.Price; _lastAskSizes.TryGetValue(symbol, out tick.Quantity); _lastAskPrices[symbol] = e.Price; break; case IB.TickType.LastPrice: tick.TickType = TickType.Trade; tick.Value = e.Price; _lastPrices[symbol] = e.Price; break; case IB.TickType.HighPrice: case IB.TickType.LowPrice: case IB.TickType.ClosePrice: case IB.TickType.OpenPrice: default: return; } lock (_ticks) if (tick.IsValid()) _ticks.Add(tick); }
public void AggregatesNewTicksInPeriodWithRoundedTime() { TradeBar consolidated = null; var consolidator = new TickConsolidator(TimeSpan.FromMinutes(1)); consolidator.DataConsolidated += (sender, bar) => { consolidated = bar; }; var reference = new DateTime(2015, 06, 02); var tick1 = new Tick { Symbol = "EURUSD", Time = reference.AddSeconds(3), Value = 1.1000m }; consolidator.Update(tick1); Assert.IsNull(consolidated); var tick2 = new Tick { Symbol = "EURUSD", Time = reference.AddSeconds(10), Value = 1.1005m }; consolidator.Update(tick2); Assert.IsNull(consolidated); var tick3 = new Tick { Symbol = "EURUSD", Time = reference.AddSeconds(61), Value = 1.1010m }; consolidator.Update(tick3); Assert.IsNotNull(consolidated); Assert.AreEqual(consolidated.Time, reference); Assert.AreEqual(consolidated.Open, tick1.Value); Assert.AreEqual(consolidated.Close, tick2.Value); var tick4 = new Tick { Symbol = "EURUSD", Time = reference.AddSeconds(70), Value = 1.1015m }; consolidator.Update(tick4); Assert.IsNotNull(consolidated); var tick5 = new Tick { Symbol = "EURUSD", Time = reference.AddSeconds(118), Value = 1.1020m }; consolidator.Update(tick5); Assert.IsNotNull(consolidated); var tick6 = new Tick { Symbol = "EURUSD", Time = reference.AddSeconds(140), Value = 1.1025m }; consolidator.Update(tick6); Assert.IsNotNull(consolidated); Assert.AreEqual(consolidated.Time, reference.AddSeconds(60)); Assert.AreEqual(consolidated.Open, tick3.Value); Assert.AreEqual(consolidated.Close, tick5.Value); }
void HandleTickPrice(object sender, IB.TickPriceEventArgs e) { var symbol = default(SymbolCacheKey); if (!_subscribedTickets.TryGetValue(e.TickerId, out symbol)) return; var tick = new Tick(); // in the event of a symbol change this will break since we'll be assigning the // new symbol to the permtick which won't be known by the algorithm tick.Symbol = new Symbol(symbol.Item2); tick.Time = GetBrokerTime(); if (symbol.Item1 == SecurityType.Forex) { // forex exchange hours are specified in UTC-05 tick.Time = tick.Time.ConvertTo(TimeZones.NewYork, TimeZones.EasternStandard); } tick.Value = e.Price; if (e.Price <= 0 && symbol.Item1 != SecurityType.Future && symbol.Item1 != SecurityType.Option) return; switch (e.TickType) { case IB.TickType.BidPrice: tick.TickType = TickType.Quote; tick.BidPrice = e.Price; _lastBidSizes.TryGetValue(symbol, out tick.Quantity); _lastBidPrices[symbol] = e.Price; break; case IB.TickType.AskPrice: tick.TickType = TickType.Quote; tick.AskPrice = e.Price; _lastAskSizes.TryGetValue(symbol, out tick.Quantity); _lastAskPrices[symbol] = e.Price; break; case IB.TickType.LastPrice: tick.TickType = TickType.Trade; tick.Value = e.Price; _lastPrices[symbol] = e.Price; break; case IB.TickType.HighPrice: case IB.TickType.LowPrice: case IB.TickType.ClosePrice: case IB.TickType.OpenPrice: default: return; } lock (_ticks) if (tick.IsValid()) _ticks.Add(tick); }
/// <summary> /// Trade causing an update to the current tradebar object. /// </summary> /// <param name="tick"></param> /// <remarks>We build bars from the trade data, or if its a tick stream just pass the trade through as a tick.</remarks> public void Update(Tick tick) { //If the second has ticked over, and we have data not processed yet, wait for it to be stored: var barStartTime = ComputeBarStartTime(tick); while (_data != null && _data.Time < barStartTime) { Thread.Sleep(1); } lock (_lock) { switch (_type.Name) { case "TradeBar": if (_data == null) { _data = new TradeBar(barStartTime, _config.Symbol, tick.LastPrice, tick.LastPrice, tick.LastPrice, tick.LastPrice, tick.Quantity); } else { //Update the bar: _data.Update(tick.LastPrice, tick.Quantity, tick.BidPrice, tick.AskPrice); } break; //Each tick is a new data obj. case "Tick": _queue.Enqueue(tick); break; } } // End of Lock }
public void TickRoundTrip() { var tick = new Tick { Symbol = Symbols.EURGBP, AskPrice = 1, Time = DateTime.Now, Exchange = "", Value = 2, EndTime = DateTime.Now, Quantity = 1, BidPrice = 2, SaleCondition = "" }; var json = JsonConvert.SerializeObject(tick, Settings); var actual = JsonConvert.DeserializeObject<Tick>(json, Settings); Assert.AreEqual(tick.Symbol, actual.Symbol); json = JsonConvert.SerializeObject(tick, Settings); actual = JsonConvert.DeserializeObject<Tick>(json); Assert.AreEqual(tick.Symbol, actual.Symbol); }
/// <summary> /// Cloner constructor for fill forward engine implementation. Clone the original tick into this new tick: /// </summary> /// <param name="original">Original tick we're cloning</param> public Tick(Tick original) { Symbol = original.Symbol; Time = new DateTime(original.Time.Ticks); Value = original.Value; BidPrice = original.BidPrice; AskPrice = original.AskPrice; Exchange = original.Exchange; SaleCondition = original.SaleCondition; Quantity = original.Quantity; Suspicious = original.Suspicious; DataType = MarketDataType.Tick; }
private static void UpdateContract(OptionContract contract, Tick tick) { if (tick.TickType == TickType.Trade) { contract.LastPrice = tick.Price; } else if (tick.TickType == TickType.Quote) { if (tick.AskPrice != 0m) { contract.AskPrice = tick.AskPrice; contract.AskSize = tick.AskSize; } if (tick.BidPrice != 0m) { contract.BidPrice = tick.BidPrice; contract.BidSize = tick.BidSize; } } }
/// <summary> /// Trade causing an update to the current tradebar object. /// </summary> /// <param name="tick"></param> /// <remarks>We build bars from the trade data, or if its a tick stream just pass the trade through as a tick.</remarks> public void Update(Tick tick) { // if we're not within the configured market hours don't process the data if (!ExchangeIsOpen(tick.Time)) { return; } //If the second has ticked over, and we have data not processed yet, wait for it to be stored: var barStartTime = ComputeBarStartTime(tick); while (_data != null && _data.Time < barStartTime) { Thread.Sleep(1); } lock (_lock) { switch (_type.Name) { case "TradeBar": if (_data == null) { _data = new TradeBar(barStartTime, _config.Symbol, tick.LastPrice, tick.LastPrice, tick.LastPrice, tick.LastPrice, tick.Quantity, _config.Resolution.ToTimeSpan()); } else { //Update the bar: _data.Update(tick.LastPrice, tick.Quantity, tick.BidPrice, tick.AskPrice); } break; //Each tick is a new data obj. case "Tick": _queue.Enqueue(tick); break; } } }
/// <summary> /// Trade causing an update to the current tradebar object. /// </summary> /// <param name="tick"></param> /// <remarks>We build bars from the trade data, or if its a tick stream just pass the trade through as a tick.</remarks> public void Update(Tick tick) { // if we're not within the configured market hours don't process the data if (!_security.Exchange.IsOpenDuringBar(tick.Time, tick.EndTime, _config.ExtendedMarketHours)) { return; } var barStartTime = ComputeBarStartTime(); try { //If the second has ticked over, and we have data not processed yet, wait for it to be stored: // we're waiting for the trigger archive to enqueue and set _data to null var timeout = DateTime.UtcNow.AddMilliseconds(50); while (_data != null && _data.Time < barStartTime) { if (DateTime.UtcNow > timeout) { Log.Error("StreamStore.Update(Tick): Timeout reached."); break; } Thread.Sleep(1); } } catch (NullReferenceException) { // we were waiting for _data to go null, it just so happened to go null // between the null check and the comparison } lock (_lock) { if (_data == null) { _data = new TradeBar(barStartTime, _config.Symbol, tick.LastPrice, tick.LastPrice, tick.LastPrice, tick.LastPrice, tick.Quantity, _config.Resolution.ToTimeSpan()); } else { //Update the bar: _data.Update(tick.LastPrice, tick.Quantity, tick.BidPrice, tick.AskPrice); } } }
/// <summary> /// Event handler for streaming ticks /// </summary> /// <param name="data">The data object containing the received tick</param> private void OnDataReceived(RateStreamResponse data) { if (data.tick == null) return; var securityType = _symbolMapper.GetBrokerageSecurityType(data.tick.instrument); var symbol = _symbolMapper.GetLeanSymbol(data.tick.instrument, securityType, Market.Oanda); var time = GetDateTimeFromString(data.tick.time); var bidPrice = Convert.ToDecimal(data.tick.bid); var askPrice = Convert.ToDecimal(data.tick.ask); var tick = new Tick(time, symbol, bidPrice, askPrice); lock (_ticks) { _ticks.Add(tick); } }
/******************************************************** * CLASS METHODS *********************************************************/ /// <summary> /// Tick implementation of reader method: read a line of data from the source and convert it to a tick object. /// </summary> /// <param name="datafeed">Source of the datafeed</param> /// <param name="config">Subscription configuration object for algorithm</param> /// <param name="line">Line from the datafeed source</param> /// <param name="date">Date of this reader request</param> /// <returns>New Initialized tick</returns> public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { var _tick = new Tick(); //Select the URL source of the data depending on where the system is trading. switch (datafeed) { //Local File System Storage and Backtesting QC Data Store Feed use same files: case DataFeedEndpoint.FileSystem: case DataFeedEndpoint.Backtesting: //Create a new instance of our tradebar: _tick = new Tick(config, line, date, datafeed); break; case DataFeedEndpoint.LiveTrading: break; } return _tick; }
void HandleTickSize(object sender, IB.TickSizeEventArgs e) { var symbol = default(Symbol); if (!_subscribedTickets.TryGetValue(e.TickerId, out symbol)) return; var tick = new Tick(); // in the event of a symbol change this will break since we'll be assigning the // new symbol to the permtick which won't be known by the algorithm tick.Symbol = symbol; var securityType = symbol.ID.SecurityType; tick.Quantity = AdjustQuantity(securityType, e.Size); tick.Time = GetBrokerTime(); if (securityType == SecurityType.Forex) { // forex exchange hours are specified in UTC-05 tick.Time = tick.Time.ConvertTo(TimeZones.NewYork, TimeZones.EasternStandard); } if (tick.Quantity == 0) return; switch (e.TickType) { case IB.TickType.BidSize: tick.TickType = TickType.Quote; _lastBidPrices.TryGetValue(symbol, out tick.BidPrice); _lastBidSizes[symbol] = tick.Quantity; tick.Value = tick.BidPrice; tick.BidSize = tick.Quantity; break; case IB.TickType.AskSize: tick.TickType = TickType.Quote; _lastAskPrices.TryGetValue(symbol, out tick.AskPrice); _lastAskSizes[symbol] = tick.Quantity; tick.Value = tick.AskPrice; tick.AskSize = tick.Quantity; break; case IB.TickType.LastSize: tick.TickType = TickType.Trade; decimal lastPrice; _lastPrices.TryGetValue(symbol, out lastPrice); _lastVolumes[symbol] = tick.Quantity; tick.Value = lastPrice; break; default: return; } lock (_ticks) if (tick.IsValid()) _ticks.Add(tick); }
/// <summary> /// MarketDataSnapshot message handler /// </summary> private void OnMarketDataSnapshot(MarketDataSnapshot message) { if (message.getRequestID() == _currentRequest) { var securityType = _symbolMapper.GetBrokerageSecurityType(message.getInstrument().getSymbol()); var symbol = _symbolMapper.GetLeanSymbol(message.getInstrument().getSymbol(), securityType, Market.FXCM); var time = FromJavaDateUtc(message.getDate().toDate()); if (message.getFXCMTimingInterval() == FXCMTimingIntervalFactory.TICK) { var bid = Convert.ToDecimal(message.getBidClose()); var ask = Convert.ToDecimal(message.getAskClose()); var tick = new Tick(time, symbol, bid, ask); //Add tick _currentBaseData.Add(tick); } else // it bars { var open = Convert.ToDecimal((message.getBidOpen() + message.getAskOpen()) / 2); var high = Convert.ToDecimal((message.getBidHigh() + message.getAskHigh()) / 2); var low = Convert.ToDecimal((message.getBidLow() + message.getAskLow()) / 2); var close = Convert.ToDecimal((message.getBidClose() + message.getAskClose()) / 2); var bar = new TradeBar(time, symbol, open, high, low, close, 0); // add bar to list _currentBaseData.Add(bar); } if (message.getFXCMContinuousFlag() == IFixValueDefs.__Fields.FXCMCONTINUOUS_END) { _mapRequestsToAutoResetEvents[_currentRequest].Set(); _mapRequestsToAutoResetEvents.Remove(_currentRequest); } } }
/// <summary> /// MarketDataSnapshot message handler /// </summary> private void OnMarketDataSnapshot(MarketDataSnapshot message) { // update the current prices for the instrument var instrument = message.getInstrument(); _rates[instrument.getSymbol()] = message; // if instrument is subscribed, add ticks to list var securityType = _symbolMapper.GetBrokerageSecurityType(instrument.getSymbol()); var symbol = _symbolMapper.GetLeanSymbol(instrument.getSymbol(), securityType, Market.FXCM); if (_subscribedSymbols.Contains(symbol)) { var time = FromJavaDate(message.getDate().toDate()); var bidPrice = Convert.ToDecimal(message.getBidClose()); var askPrice = Convert.ToDecimal(message.getAskClose()); var tick = new Tick(time, symbol, bidPrice, askPrice); lock (_ticks) { _ticks.Add(tick); } } if (message.getRequestID() == _currentRequest) { if (message.getFXCMContinuousFlag() == IFixValueDefs.__Fields.FXCMCONTINUOUS_END) { _mapRequestsToAutoResetEvents[_currentRequest].Set(); _mapRequestsToAutoResetEvents.Remove(_currentRequest); } } }
void HandleTickSize(object sender, IB.TickSizeEventArgs e) { var symbol = default(SymbolCacheKey); if (!_subscribedTickets.TryGetValue(e.TickerId, out symbol)) return; var tick = new Tick(); // in the event of a symbol change this will break since we'll be assigning the // new symbol to the permtick which won't be known by the algorithm tick.Symbol = new Symbol(symbol.Item2); tick.Quantity = AdjustQuantity(symbol.Item1, e.Size); tick.Time = GetBrokerTime(); if (tick.Quantity == 0) return; switch (e.TickType) { case IB.TickType.BidSize: tick.TickType = TickType.Quote; _lastBidPrices.TryGetValue(symbol, out tick.BidPrice); _lastBidSizes[symbol] = tick.Quantity; tick.Value = tick.BidPrice; break; case IB.TickType.AskSize: tick.TickType = TickType.Quote; _lastAskPrices.TryGetValue(symbol, out tick.AskPrice); _lastAskSizes[symbol] = tick.Quantity; tick.Value = tick.AskPrice; break; case IB.TickType.LastSize: tick.TickType = TickType.Trade; decimal lastPrice; _lastPrices.TryGetValue(symbol, out lastPrice); _lastVolumes[symbol] = tick.Quantity; tick.Value = lastPrice; break; default: return; } lock (_ticks) if (tick.IsValid()) _ticks.Add(tick); }