public void ChangeDataNormalizationMode() { var tb = new TradeBar { Time = new DateTime(2020, 5, 21, 8, 9, 0), Period = TimeSpan.FromHours(1), Symbol = Symbols.SPY, Open = 100, High = 200, Low = 300, Close = 400 }; var factor = 0.5m; var sumOfDividends = 100m; var adjustedTb = tb.Clone(tb.IsFillForward).Adjust(factor); var exchangeHours = SecurityExchangeHours.AlwaysOpen(TimeZones.Utc); var offsetProvider = new TimeZoneOffsetProvider(TimeZones.Utc, new DateTime(2020, 5, 21), new DateTime(2020, 5, 22)); var emitTimeUtc = offsetProvider.ConvertToUtc(tb.EndTime); _config.SumOfDividends = sumOfDividends; var subscriptionData = new PrecalculatedSubscriptionData( _config, tb, adjustedTb, DataNormalizationMode.Adjusted, emitTimeUtc); _config.DataNormalizationMode = DataNormalizationMode.Raw; Assert.AreEqual(tb.Open, (subscriptionData.Data as TradeBar).Open); Assert.AreEqual(tb.High, (subscriptionData.Data as TradeBar).High); Assert.AreEqual(tb.Low, (subscriptionData.Data as TradeBar).Low); Assert.AreEqual(tb.Close, (subscriptionData.Data as TradeBar).Close); _config.DataNormalizationMode = DataNormalizationMode.Adjusted; Assert.AreEqual(tb.Open * factor, (subscriptionData.Data as TradeBar).Open); Assert.AreEqual(tb.High * factor, (subscriptionData.Data as TradeBar).High); Assert.AreEqual(tb.Low * factor, (subscriptionData.Data as TradeBar).Low); Assert.AreEqual(tb.Close * factor, (subscriptionData.Data as TradeBar).Close); _config.DataNormalizationMode = DataNormalizationMode.TotalReturn; Assert.Throws <ArgumentException>(() => { var data = subscriptionData.Data; } ); _config.DataNormalizationMode = DataNormalizationMode.SplitAdjusted; Assert.Throws <ArgumentException>(() => { var data = subscriptionData.Data; } ); }
public void StopLimitOrderDoesNotFillUsingDataBeforeSubmitTime(decimal orderQuantity, decimal stopPrice, decimal limitPrice) { var time = new DateTime(2018, 9, 24, 9, 30, 0); var timeKeeper = new TimeKeeper(time.ConvertToUtc(TimeZones.NewYork), TimeZones.NewYork); var symbol = Symbol.Create("SPY", SecurityType.Equity, Market.USA); var config = new SubscriptionDataConfig(typeof(TradeBar), symbol, Resolution.Minute, TimeZones.NewYork, TimeZones.NewYork, true, true, false); var security = new Security( SecurityExchangeHoursTests.CreateUsEquitySecurityExchangeHours(), config, new Cash(Currencies.USD, 0, 1m), SymbolProperties.GetDefault(Currencies.USD), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null, new SecurityCache() ); security.SetLocalTimeKeeper(timeKeeper.GetLocalTimeKeeper(TimeZones.NewYork)); var tradeBar = new TradeBar(time, symbol, 290m, 292m, 289m, 291m, 12345); security.SetMarketPrice(tradeBar); time += TimeSpan.FromMinutes(1); timeKeeper.SetUtcDateTime(time.ConvertToUtc(TimeZones.NewYork)); var fillForwardBar = (TradeBar)tradeBar.Clone(true); security.SetMarketPrice(fillForwardBar); var fillModel = new ImmediateFillModel(); var order = new StopLimitOrder(symbol, orderQuantity, stopPrice, limitPrice, time.ConvertToUtc(TimeZones.NewYork)); var fill = fillModel.Fill(new FillModelParameters( security, order, new MockSubscriptionDataConfigProvider(config), Time.OneHour)).OrderEvent; Assert.AreEqual(0, fill.FillQuantity); Assert.AreEqual(0, fill.FillPrice); Assert.AreEqual(OrderStatus.None, fill.Status); time += TimeSpan.FromMinutes(1); timeKeeper.SetUtcDateTime(time.ConvertToUtc(TimeZones.NewYork)); tradeBar = new TradeBar(time, symbol, 290m, 292m, 289m, 291m, 12345); security.SetMarketPrice(tradeBar); fill = fillModel.StopLimitFill(security, order); Assert.AreEqual(orderQuantity, fill.FillQuantity); Assert.AreEqual(limitPrice, fill.FillPrice); Assert.AreEqual(OrderStatus.Filled, fill.Status); Assert.AreEqual(0, fill.OrderFee.Value.Amount); }
public void StopMarketOrderDoesNotFillUsingDataBeforeSubmitTime(decimal orderQuantity, decimal stopPrice) { var time = new DateTime(2018, 9, 24, 9, 30, 0); var timeKeeper = new TimeKeeper(time.ConvertToUtc(TimeZones.NewYork), TimeZones.NewYork); var symbol = Symbol.Create("SPY", SecurityType.Equity, Market.USA); var config = new SubscriptionDataConfig(typeof(TradeBar), symbol, Resolution.Minute, TimeZones.NewYork, TimeZones.NewYork, true, true, false); var security = new Security(SecurityExchangeHoursTests.CreateUsEquitySecurityExchangeHours(), config, new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); security.SetLocalTimeKeeper(timeKeeper.GetLocalTimeKeeper(TimeZones.NewYork)); var tradeBar = new TradeBar(time, symbol, 290m, 292m, 289m, 291m, 12345); security.SetMarketPrice(tradeBar); time += TimeSpan.FromMinutes(1); timeKeeper.SetUtcDateTime(time.ConvertToUtc(TimeZones.NewYork)); var fillForwardBar = (TradeBar)tradeBar.Clone(true); security.SetMarketPrice(fillForwardBar); var fillModel = new ImmediateFillModel(); var order = new StopMarketOrder(symbol, orderQuantity, stopPrice, time.ConvertToUtc(TimeZones.NewYork)); var fill = fillModel.StopMarketFill(security, order); Assert.AreEqual(0, fill.FillQuantity); Assert.AreEqual(0, fill.FillPrice); Assert.AreEqual(OrderStatus.None, fill.Status); time += TimeSpan.FromMinutes(1); timeKeeper.SetUtcDateTime(time.ConvertToUtc(TimeZones.NewYork)); tradeBar = new TradeBar(time, symbol, 290m, 292m, 289m, 291m, 12345); security.SetMarketPrice(tradeBar); fill = fillModel.StopMarketFill(security, order); Assert.AreEqual(orderQuantity, fill.FillQuantity); Assert.AreEqual(stopPrice, fill.FillPrice); Assert.AreEqual(OrderStatus.Filled, fill.Status); Assert.AreEqual(0, fill.OrderFee); }
public void AdjustTradeBar() { var tb = new TradeBar { Time = new DateTime(2020, 5, 21, 8, 9, 0), Period = TimeSpan.FromHours(1), Symbol = Symbols.SPY, Open = 100, High = 200, Low = 300, Close = 400 }; var adjustedTb = tb.Clone(tb.IsFillForward).Adjust(_factor); Assert.AreEqual(tb.Open * _factor, (adjustedTb as TradeBar).Open); Assert.AreEqual(tb.High * _factor, (adjustedTb as TradeBar).High); Assert.AreEqual(tb.Low * _factor, (adjustedTb as TradeBar).Low); Assert.AreEqual(tb.Close * _factor, (adjustedTb as TradeBar).Close); }
public void AdjustTradeBarUsingConfig() { var tb = new TradeBar { Time = new DateTime(2020, 5, 21, 8, 9, 0), Period = TimeSpan.FromHours(1), Symbol = Symbols.SPY, Open = 100, High = 200, Low = 300, Close = 400, Volume = 1000 }; var adjustedTb = tb.Clone(tb.IsFillForward).Normalize(_config.PriceScaleFactor, _config.DataNormalizationMode, _config.SumOfDividends); Assert.AreEqual(tb.Open * _factor, (adjustedTb as TradeBar).Open); Assert.AreEqual(tb.High * _factor, (adjustedTb as TradeBar).High); Assert.AreEqual(tb.Low * _factor, (adjustedTb as TradeBar).Low); Assert.AreEqual(tb.Close * _factor, (adjustedTb as TradeBar).Close); Assert.AreEqual(tb.Volume / _factor, (adjustedTb as TradeBar).Volume); }
public void IgnoresNonTickDataWithSameTimestamps() { var reference = new DateTime(2015, 09, 23); var identity = new IdentityDataConsolidator <TradeBar>(); int count = 0; identity.DataConsolidated += (sender, data) => { count++; }; var tradeBar = new TradeBar { EndTime = reference }; identity.Update(tradeBar); tradeBar = (TradeBar)tradeBar.Clone(); identity.Update(tradeBar); Assert.AreEqual(1, count); }