public void UpdatesAfterCorrectPeriodElapses() { const int periods = 3; var periodSpan = Time.OneMinute; var reference = new DateTime(2016, 04, 06, 12, 0, 0); var referenceUtc = reference.ConvertToUtc(TimeZones.NewYork); var timeKeeper = new TimeKeeper(referenceUtc); var config = new SubscriptionDataConfig(typeof (TradeBar), Symbols.SPY, Resolution.Minute, TimeZones.NewYork, TimeZones.NewYork, true, false, false); var security = new Security(SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), config, new Cash("USD", 0, 0), SymbolProperties.GetDefault("USD")); security.SetLocalTimeKeeper(timeKeeper.GetLocalTimeKeeper(TimeZones.NewYork)); var model = new RelativeStandardDeviationVolatilityModel(periodSpan, periods); security.VolatilityModel = model; var first = new IndicatorDataPoint(reference, 1); security.SetMarketPrice(first); Assert.AreEqual(0m, model.Volatility); const decimal value = 0.471404520791032M; // std of 1,2 is ~0.707 over a mean of 1.5 var second = new IndicatorDataPoint(reference.AddMinutes(1), 2); security.SetMarketPrice(second); Assert.AreEqual(value, model.Volatility); // update should not be applied since not enough time has passed var third = new IndicatorDataPoint(reference.AddMinutes(1.01), 1000); security.SetMarketPrice(third); Assert.AreEqual(value, model.Volatility); var fourth = new IndicatorDataPoint(reference.AddMinutes(2), 3m); security.SetMarketPrice(fourth); Assert.AreEqual(0.5m, model.Volatility); }
private static DateTime InitializeTest(out BasicTemplateAlgorithm algorithm, out Security security, out PartialMarketFillModel model, out MarketOrder order, out OrderTicket ticket) { var referenceTimeNY = new DateTime(2015, 12, 21, 13, 0, 0); var referenceTimeUtc = referenceTimeNY.ConvertToUtc(TimeZones.NewYork); algorithm = new BasicTemplateAlgorithm(); algorithm.SetDateTime(referenceTimeUtc); var transactionHandler = new BacktestingTransactionHandler(); transactionHandler.Initialize(algorithm, new BacktestingBrokerage(algorithm), new TestResultHandler(Console.WriteLine)); Task.Run(() => transactionHandler.Run()); algorithm.Transactions.SetOrderProcessor(transactionHandler); var config = new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Second, TimeZones.NewYork, TimeZones.NewYork, false, false, false); security = new Security(SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), config); model = new PartialMarketFillModel(algorithm.Transactions, 2); algorithm.Securities.Add(security); algorithm.Securities[Symbols.SPY].FillModel = model; security.SetMarketPrice(new Tick { Symbol = Symbols.SPY, Value = 100 }); algorithm.SetFinishedWarmingUp(); order = new MarketOrder(Symbols.SPY, 100, referenceTimeUtc) { Id = 1 }; var request = new SubmitOrderRequest(OrderType.Market, security.Type, security.Symbol, order.Quantity, 0, 0, algorithm.UtcTime, null); ticket = algorithm.Transactions.ProcessRequest(request); return referenceTimeUtc; }
/// <summary> /// Sets the current time interpeting the specified time as a local time /// using the time zone used at instatiation. /// </summary> /// <param name="time">The local time to set the current time time, will be /// converted into UTC</param> public void SetCurrentTime(DateTime time) { _currentTime = time.ConvertToUtc(_setCurrentTimeTimeZone); }
/// <summary> /// Initializes a new instance of the <see cref="ManualTimeProvider"/> class /// </summary> /// <param name="currentTime">The current time in the specified time zone, if the time zone is /// null then the time is interpreted as being in <see cref="TimeZones.Utc"/></param> /// <param name="setCurrentTimeTimeZone">Specify to use this time zone when calling <see cref="SetCurrentTime"/>, /// leave null for the deault of <see cref="TimeZones.Utc"/></param> public ManualTimeProvider(DateTime currentTime, DateTimeZone setCurrentTimeTimeZone = null) { _setCurrentTimeTimeZone = setCurrentTimeTimeZone ?? TimeZones.Utc; _currentTime = currentTime.ConvertToUtc(_setCurrentTimeTimeZone); }
private static Security CreateSecurity(DateTime newLocalTime) { var security = new Security(CreateUsEquitySecurityExchangeHours(), CreateTradeBarConfig(), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); TimeKeeper.SetUtcDateTime(newLocalTime.ConvertToUtc(security.Exchange.TimeZone)); security.Exchange.SetLocalDateTimeFrontier(newLocalTime); security.SetLocalTimeKeeper(TimeKeeper.GetLocalTimeKeeper(TimeZones.NewYork)); security.SetMarketPrice(new IndicatorDataPoint(Symbols.SPY, newLocalTime, 100m)); return security; }