public void FirstMonthIsSunday() { RelativeDate actual = new RelativeDate(2018, 7); DateTime firstWeekDay = actual.First(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 7, 2), firstWeekDay); DateTime secondWeekDay = actual.Second(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 7, 3), secondWeekDay); DateTime thirdWeekDay = actual.Third(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 7, 4), thirdWeekDay); DateTime fourthWeekDay = actual.Fourth(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 7, 5), fourthWeekDay); DateTime lastWeekDay = actual.Last(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 7, 31), lastWeekDay); DateTime secondWeekendDay = actual.Second(RelativeDate.Interval.WeekendDay); Assert.AreEqual(new DateTime(2018, 7, 7), secondWeekendDay); DateTime fourthWeekendDay = actual.Fourth(RelativeDate.Interval.WeekendDay); Assert.AreEqual(new DateTime(2018, 7, 14), fourthWeekendDay); }
public void ReadMeTests() { DateTime firstMonday = new RelativeDate(2018, 6).First(RelativeDate.Interval.Monday); Assert.AreEqual(new DateTime(2018, 6, 4), firstMonday); // first Monday in June 2018 is the 4th DateTime secondThursday = new RelativeDate(2018, 6).Second(RelativeDate.Interval.Thursday); Assert.AreEqual(new DateTime(2018, 6, 14), secondThursday); // second Thursday in June 2018 is the 14th DateTime thirdDay = new RelativeDate(2018, 6).Third(RelativeDate.Interval.Day); Assert.AreEqual(new DateTime(2018, 6, 3), thirdDay); // third day in June 2018 is the 3rd (Duh) DateTime fourthWeekDay = new RelativeDate(2018, 6).Fourth(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 6, 6), fourthWeekDay); // fourth week day in June 2018 is the 6th (It's a Wednesday btw) DateTime lastWeekEndDay = new RelativeDate(2018, 6).Last(RelativeDate.Interval.WeekendDay); Assert.AreEqual(new DateTime(2018, 6, 30), lastWeekEndDay); }
public static BlogViewModel form(BlogViewModel model) { if (model == null) { return(null); } BlogViewModel result = new BlogViewModel { Id = model.Id, Name = model.Name, BlogTags = model.BlogTags, Content = model.Content, DateCreated = model.DateCreated, DateModified = model.DateModified, Description = model.Description, HomeFlag = model.HomeFlag, HotFlag = model.HotFlag, Image = model.Image, SeoAlias = model.SeoAlias, SeoDescription = model.SeoDescription, SeoKeywords = model.SeoKeywords, SeoPageTitle = model.SeoPageTitle, Status = model.Status, Tags = model.Tags, TimeAgo = RelativeDate.TimeAgo(model.DateCreated) }; return(result); }
// should only be called within write lock private void populateConstructWithDayAtIndex(Day day_, RollGeneration gen_) { if (day_ == null || day_.Lines == null) return; #if DEBUG var sw = new Stopwatch(); sw.Start(); #endif foreach (var line in day_.Lines) { var relativeDate = new RelativeDate(day_.Dates, line.GmtDateAsDate().ToTZfromGMT(day_.DispTimeZone)); if (relativeDate.Index == -1) // this shouldn't happen continue; m_data.SetValue(relativeDate, (int)gen_, line); } #if DEBUG sw.Stop(); Logger.Info(string.Format("Backfilling {0} took {1}ms (Lines={2})", day_, sw.ElapsedMilliseconds, day_.Lines.Count), typeof (LiveDayComparison)); #endif }
/// <summary> /// Gets the actual report start date. /// </summary> public static DateTime GetStartDate(RelativeDate startDate, DateTime currentDate) { return(startDate switch { RelativeDate.Yesterday => currentDate.AddDays(-1.0).Date, _ => currentDate.Date, });
public void FirstMonthIsFriday() { RelativeDate actual = new RelativeDate(2018, 6); DateTime fourthWeekDay = actual.Fourth(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 6, 6), fourthWeekDay); }
public void LastMonthIsSunday() { RelativeDate actual = new RelativeDate(2017, 12); DateTime lastWeekDay = actual.Last(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2017, 12, 29), lastWeekDay); }
/// <summary> /// Initializes a new instance of the <see cref="RelativeDateDefinition"/> class. /// </summary> /// <param name="relativeDate">The <see cref="relativeDate"/>.</param> /// <param name="yearDelta">The year delta.</param> /// <param name="monthDelta">The month delta.</param> /// <param name="dayDelta">The day delta.</param> public RelativeDateDefinition(RelativeDate relativeDate, int yearDelta, int monthDelta, int dayDelta) { this.RelativeDate = relativeDate; this.YearDelta = yearDelta; this.MonthDelta = monthDelta; this.DayDelta = dayDelta; this.MatchGroup = relativeDate.ToString(); }
public void WrongTimeReturnsUnsetValue() { // Arrange var conv = new RelativeDate(); // Act var v = conv.Convert(string.Empty, null, null, null); // Assert Assert.AreEqual(DependencyProperty.UnsetValue, v); }
public void GenerateValue(bool force_=false) { var val = new RelativeDate(m_dateIndexes, DateTime.Now, m_zone); if (CurrentValue == val && force_==false) return; CurrentValue = val; OnValueChanged(new RelativeDateChangedArgs(CurrentValue)); }
public void ConvertBackThrowsException() { // Arrange var conv = new RelativeDate(); // Act var ex = ExceptionAssert.Catch <NotSupportedException>(() => conv.ConvertBack(null, null, null, null)); // Assert Assert.IsNotNull(ex); }
public void FirstMonthIsThursday() { RelativeDate actual = new RelativeDate(2018, 3); DateTime thirdWeekDay = actual.Third(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 3, 5), thirdWeekDay); DateTime fourthWeekDay = actual.Fourth(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2018, 3, 6), fourthWeekDay); }
public static async Task PopulateHistoricMinutes(Day rollDay_, CarbonClient cc_) { var dictionary = new SortedDictionary<RelativeDate, Line>(); //FileHelper.GetFront(rollDay_, dictionary); //FileHelper.GetBack(rollDay_, dictionary); //FileHelper.GetSpread(rollDay_, dictionary); await CarbonHelper.Get(rollDay_, dictionary, cc_); rollDay_.Lines = dictionary.Keys.Select(x => dictionary[x]).ToList(); // quick check to see if the sorting on rds is working as expected { RelativeDate prior=default(RelativeDate); int count =0; foreach (var rd in dictionary.Keys) { try { if (count==0) continue; if (prior.UnderlyingDate > rd.UnderlyingDate) { System.Diagnostics.Debugger.Break(); var a = new RelativeDate(prior.IndexDates, prior.UnderlyingDate, prior.DispTimeZone); var b = new RelativeDate(rd.IndexDates, rd.UnderlyingDate, rd.DispTimeZone); } } finally { prior = rd; ++count; } } } // post process for (int i = 0; i < rollDay_.Lines.Count; ++i) { if (i > 0) { if (rollDay_.Lines[i].BackContractValue.IsZero()) rollDay_.Lines[i].BackContractValue = rollDay_.Lines[i - 1].BackContractValue; if (rollDay_.Lines[i].FrontContractValue.IsZero()) rollDay_.Lines[i].FrontContractValue = rollDay_.Lines[i - 1].FrontContractValue; if (rollDay_.Lines[i].SpreadValue.IsZero()) rollDay_.Lines[i].SpreadValue = rollDay_.Lines[i - 1].SpreadValue; } rollDay_.Lines[i].CalcSeriesValue(rollDay_.DefaultTail); } }
public void KnownMonth_Interval_WeekendDay() { //Arrange int targetYear = 2018; int targetMonth = 5; RelativeDate actual; //Act actual = new RelativeDate(targetYear, targetMonth); //Assert Assert.AreEqual(new DateTime(targetYear, targetMonth, 5), actual.First(RelativeDate.Interval.WeekendDay)); Assert.AreEqual(new DateTime(targetYear, targetMonth, 6), actual.Second(RelativeDate.Interval.WeekendDay)); Assert.AreEqual(new DateTime(targetYear, targetMonth, 12), actual.Third(RelativeDate.Interval.WeekendDay)); Assert.AreEqual(new DateTime(targetYear, targetMonth, 13), actual.Fourth(RelativeDate.Interval.WeekendDay)); Assert.AreEqual(new DateTime(targetYear, targetMonth, 27), actual.Last(RelativeDate.Interval.WeekendDay)); }
public void KnownMonth_Interval_Thursday() { //Arrange int targetYear = 2018; int targetMonth = 5; RelativeDate actual; //Act actual = new RelativeDate(targetYear, targetMonth); //Assert Assert.AreEqual(new DateTime(targetYear, targetMonth, 3), actual.First(RelativeDate.Interval.Thursday)); Assert.AreEqual(new DateTime(targetYear, targetMonth, 10), actual.Second(RelativeDate.Interval.Thursday)); Assert.AreEqual(new DateTime(targetYear, targetMonth, 17), actual.Third(RelativeDate.Interval.Thursday)); Assert.AreEqual(new DateTime(targetYear, targetMonth, 24), actual.Fourth(RelativeDate.Interval.Thursday)); Assert.AreEqual(new DateTime(targetYear, targetMonth, 31), actual.Last(RelativeDate.Interval.Thursday)); }
public void MinuteDifferenceIsCorrectlyFormatted() { // Arrange var date = new Mock <IDateProvider>(); date.SetupGet(d => d.Now).Returns(new DateTime(1234, 5, 6, 7, 8, 9).ToLocalTime()); var conv = new RelativeDate { DateProvider = date.Object }; // Act var result = conv.Convert(new DateTime(1234, 5, 6, 7, 7, 0), null, null, null); // Assert Assert.AreEqual($"1{Strings.MinutesShort}", result); }
public void RecentPastIsConvertedToNow() { // Arrange var date = new Mock <IDateProvider>(); date.SetupGet(d => d.Now).Returns(new DateTime(1234, 5, 6, 7, 8, 9).ToLocalTime()); var conv = new RelativeDate { DateProvider = date.Object }; // Act var result = conv.Convert(new DateTime(1234, 5, 6, 7, 8, 0), null, null, null); // Assert Assert.AreEqual(Strings.Now, result); }
public static ClassifiedsViewModel form(ClassifiedsViewModel model) { if (model == null) { return(null); } ClassifiedsViewModel result = new ClassifiedsViewModel { Id = model.Id, Area = model.Area, BalconyDirection = model.BalconyDirection, BedRoom = model.BedRoom, MainDirection = model.MainDirection, ClassifiedsAddressId = model.ClassifiedsAddressId, Floor = model.Floor, Front = model.Front, Furniture = model.Furniture, ImageList = model.ImageList, Latitude = model.Latitude, Longtitude = model.Longtitude, Price = model.Price, RestRoom = model.RestRoom, Title = model.Title, WayIn = model.WayIn, ClassifiedsTag = model.ClassifiedsTag, Content = model.Content, DateCreated = model.DateCreated, DateModified = model.DateModified, Description = model.Description, HomeFlag = model.HomeFlag, HotFlag = model.HotFlag, Image = model.Image, SeoAlias = model.SeoAlias, SeoDescription = model.SeoDescription, SeoKeywords = model.SeoKeywords, SeoPageTitle = model.SeoPageTitle, Status = model.Status, Tags = model.Tags, TypeName = model.Type?.Name, Type = model.Type, TimeAgo = RelativeDate.TimeAgo(model.DateCreated) }; return(result); }
public void DOWSunday() { DateTime firstSunday = new RelativeDate(2018, 6).First(RelativeDate.Interval.Sunday); Assert.AreEqual(new DateTime(2018, 6, 3), firstSunday); DateTime secondSunday = new RelativeDate(2018, 6).Second(RelativeDate.Interval.Sunday); Assert.AreEqual(new DateTime(2018, 6, 10), secondSunday); DateTime thirdSunday = new RelativeDate(2018, 6).Third(RelativeDate.Interval.Sunday); Assert.AreEqual(new DateTime(2018, 6, 17), thirdSunday); DateTime fourthSunday = new RelativeDate(2018, 6).Fourth(RelativeDate.Interval.Sunday); Assert.AreEqual(new DateTime(2018, 6, 24), fourthSunday); DateTime lastSunday = new RelativeDate(2018, 6).Last(RelativeDate.Interval.Sunday); Assert.AreEqual(new DateTime(2018, 6, 24), lastSunday); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return(""); } var date = new DateTime(); if (!DateTime.TryParse(value.ToString(), out date)) { return(""); } if (parameter != null && parameter.ToString() == "relative") { return(RelativeDate.ToRelative(date)); } else { return(date.ToShortDateString() + " at " + date.ToShortTimeString()); } }
public void DOWSaturday() { DateTime firstSaturday = new RelativeDate(2018, 6).First(RelativeDate.Interval.Saturday); Assert.AreEqual(new DateTime(2018, 6, 2), firstSaturday); DateTime secondSaturday = new RelativeDate(2018, 6).Second(RelativeDate.Interval.Saturday); Assert.AreEqual(new DateTime(2018, 6, 9), secondSaturday); DateTime thirdSaturday = new RelativeDate(2018, 6).Third(RelativeDate.Interval.Saturday); Assert.AreEqual(new DateTime(2018, 6, 16), thirdSaturday); DateTime fourthSaturday = new RelativeDate(2018, 6).Fourth(RelativeDate.Interval.Saturday); Assert.AreEqual(new DateTime(2018, 6, 23), fourthSaturday); DateTime lastSaturday = new RelativeDate(2018, 6).Last(RelativeDate.Interval.Saturday); Assert.AreEqual(new DateTime(2018, 6, 30), lastSaturday); }
public void FirstMonthIsSaturday() { RelativeDate actual = new RelativeDate(2017, 7); DateTime first = actual.First(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2017, 7, 3), first); DateTime second = actual.Second(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2017, 7, 4), second); DateTime third = actual.Third(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2017, 7, 5), third); DateTime fourth = actual.Fourth(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2017, 7, 6), fourth); DateTime last = actual.Last(RelativeDate.Interval.WeekDay); Assert.AreEqual(new DateTime(2017, 7, 31), last); }
// shoule be called within the write lock private void setValue(RelativeDate date_, Line line_, ValueUpdatedEventArgs<DateTime, double> val_, DayField valueField_, DayField volumeField_) { BarDataField field = (BarDataField)val_.Index; switch (field) { case BarDataField.Volume: if(volumeField_==DayField.SpreadVolume) Logger.Debug(string.Format("Setting {2} for {0} for minute to {1} [date={3}]", Set, val_.Value, volumeField_,date_), typeof (Day)); line_.SetValue(volumeField_, val_.Value); break; case BarDataField.Close: { line_.SetValue(valueField_, val_.Value); line_.CalcSeriesValue(DefaultTail); } break; } if (m_lastUpdated == null || !object.ReferenceEquals(m_lastUpdated, line_)) { m_lastUpdated = line_; OnNewLineBeingUpdated(new NewLineUpdatedEventArgs( date_, line_)); } }
private void handleFrontContractCandleUpdated(object sender, ValueUpdatedEventArgs<DateTime, double> val_) { var rd = new RelativeDate(IndexedDates, val_.Key.ToTZfromGMT(DispTimeZone),DispTimeZone); if (rd.Index == RelativeDate.INVALID_INDEX) return; try { //Logger.Debug(string.Format("Updating key [{0}] for {1}", rd, Set), typeof(Day)); m_lock.EnterWriteLock(); var v = m_data.GetValue(rd, 0); if (v == null) { Logger.Error("Got an update and the object hadn't been created. This should not happen", typeof(Day)); return; } setValue(rd, v, val_, DayField.FrontValue, DayField.FrontVolume); } finally { m_lock.ExitWriteLock(); } }
private void backFill(ConstructGen<double> data_, DayField valueField_, DayField volumeField_) { if (data_ == null || data_.Dates == null || data_.Dates.Count == 0) { Logger.Error( string.Format("Today's backfill data was empty. Unable to set intraday backfill data for {0} or {1}", valueField_, volumeField_), typeof(Day)); return; } foreach (var key in data_.Keys) { var relativeKey = new RelativeDate(IndexedDates, key.ToTZfromGMT(DispTimeZone),DispTimeZone); if (relativeKey.Index == RelativeDate.INVALID_INDEX) continue; m_data.EnsureKey(relativeKey); var line = m_data.GetValue(relativeKey, 0); if (line == null) { line = new Line {GMTDateTime = key.ToString(Line.MongoDateFormat)}; m_data.SetValue(relativeKey, 0, line); } line.SetValue(valueField_, data_.GetValue(key, (int)BarDataField.Close)); line.SetValue(volumeField_, data_.GetValue(key, (int)BarDataField.Volume)); line.CalcSeriesValue(DefaultTail); } }
private void determineStatus() { var nowInDispTimeZone = DateTime.Now.ToTZfromLocal(DispTimeZone); var rollDate = RollDate.AddHours(DispTimeZone.GetMarketCloseHour()); var lastTrade = LastTrade.AddHours(DispTimeZone.GetMarketCloseHour()); if (nowInDispTimeZone > lastTrade) { m_rollStatus = RollStatus.Finished; return; } if (nowInDispTimeZone > rollDate) { m_rollStatus = RollStatus.Delivery; return; } var minDate = IndexedDates.Select(x => x.Date).Min(); if (nowInDispTimeZone < minDate) { m_rollStatus = RollStatus.Approaching; return; } var rd = new RelativeDate(IndexedDates, nowInDispTimeZone, DispTimeZone); m_rollStatus = rd.Index == RelativeDate.INVALID_INDEX ? RollStatus.OnHoliday : RollStatus.Rolling; }
private static bool LoadFromFile(IEnumerable<string> dirs_, IEnumerable<string> filenames_, DayField valueField_, DayField volumeField_, IDictionary<RelativeDate, Line> values_, Day day_) { bool foundAFile = false; foreach (var dir in dirs_) { foreach (var path in filenames_) { var files = Directory.GetFiles(dir, path); if (files.Length == 0) { continue; } string file = files[0]; Logger.Info(string.Format("Processing file [{0}]", file), typeof(FileHelper)); if (file.EndsWith(".gz")) file = getUnzippedPath(file); try { foundAFile = true; foreach (var p in CsvFile.Read<IntradayFileLineItem>(file)) { if (!p.GmtDate.HasValue) continue; var rd = new RelativeDate(day_.IndexedDates, p.GmtDate.Value.ToTZfromGMT(day_.DispTimeZone), day_.DispTimeZone); if (rd.Index == RelativeDate.INVALID_INDEX) continue; if (!values_.ContainsKey(rd)) values_[rd] = new Line { GMTDateTime = p.GmtDate.Value.ToString(Line.MongoDateFormat) }; values_[rd].SetValue(valueField_, p.CloseMid); values_[rd].SetValue(volumeField_, p.Volume); } } catch (Exception ex_) { Logger.Error("Error", typeof (FileHelper), ex_); } } } return foundAFile; }
protected override RelativeDate calcEndTime(RelativeDate livePoint_) { return livePoint_.GetDayEnd(); }
public ConstructGenGen<RelativeDate, double> GetOIFrontBackOpenInterestValues(RollGeneration generation_) { if (m_oiInterestValues != null) return m_oiInterestValues; var day = GetDay(generation_); var hour = day.DispTimeZone == DisplayTimeZone.NYK ? 15 : 16; var ret = new ConstructGenGen<RelativeDate, double>(new[] { "Front", "Back" }); foreach (var date in day.Dates) { var relDate = new RelativeDate(day.Dates, date.AddHours(hour)); ret.SetValue(relDate, 0, day.OI_From.ValueOnDate(date)); ret.SetValue(relDate, 1, day.OI_To.ValueOnDate(date)); } return m_oiInterestValues = ret; }
public void GetDateInFirstWeek() { Assert.That(RelativeDate.GetDate(2017, 2, EWeeksInMonth.First, EDaysOfWeek.Monday), Is.EqualTo(new DateTime(2017, 2, 6))); }
public void GetDateInSecondWeek() { Assert.That(RelativeDate.GetDate(2017, EMonth.February, EWeeksInMonth.Second, EDaysOfWeek.Monday), Is.EqualTo(new DateTime(2017, 2, 13))); }
public void GetDateInThirdWeek() { Assert.That(RelativeDate.GetDate(2017, 2, EWeeksInMonth.Third, EDaysOfWeekExt.Monday), Is.EqualTo(new DateTime(2017, 2, 20))); }
public void GetDateInAnInvalidWeek() { Assert.That(() => RelativeDate.GetDate(2017, EMonth.February, (EWeeksInMonth)int.MaxValue, EDaysOfWeekExt.Monday), Throws.TypeOf <ArgumentException>()); }
public RelativeDateValidatorCAttribute(RelativeDate relDate, string propertyName) : base(relDate) { this.PropertyName = propertyName; }
public RelativeDateValidatorAttribute(RelativeDate relDate) { this.rdate = relDate; }
protected override RelativeDate calcStartTime(RelativeDate livePoint_) { return livePoint_.GetDayBegin(); }
private void handleLiveKeyCreated(object sender, KeyCreatedEventArgs<DateTime> e_) { var relativeKey = new RelativeDate(m_currentRoll.Dates, e_.NewKey.ToTZfromGMT(m_currentRoll.DispTimeZone)); try { m_lock.EnterWriteLock(); m_data.EnsureKey(relativeKey); if (m_data.GetValue(relativeKey, currentIndex) == null) m_data.SetValue(relativeKey, currentIndex, new Line()); } finally { m_lock.ExitWriteLock(); } }
private void backFill(ConstructGen<double> data_, DayField valueField_, DayField volumeField_) { if (data_ == null || data_.Dates==null || data_.Dates.Count==0) { Logger.Error( string.Format("Today's backfill data was empty. Unable to set intraday backfil data for {0} or {1}", valueField_, volumeField_), typeof (LiveDayComparison)); return; } foreach (var key in data_.Keys) { var relativeKey = new RelativeDate(m_currentRoll.Dates, key); m_data.EnsureKey(relativeKey); var line = m_data.GetValue(relativeKey, currentIndex); if(line==null) { line = new Line(); m_data.SetValue(relativeKey, (int)RollGeneration.Current, line); } line.SetValue(valueField_, data_.GetValue(key, (int)BarDataField.Close)); line.SetValue(volumeField_, data_.GetValue(key, (int) BarDataField.Volume)); line.CalcSeriesValue(m_currentRoll.DefaultTail); } }
public IDoubleMonitor GetLiveSpreadOnHourVolumeMonitor(RollGeneration generation_, bool forceRegeneration_=false) { var key = getMonitorKey("LiveDaysSpreadVolume", generation_); return getorCreateMonitor(key, () => { var startKey = new RelativeDate(GetDay(generation_).Dates, DateTime.Today); return new CumulativeSinceTimeMonitor(startKey, this, generation_, DayField.SpreadVolume); }, forceRegeneration_); }
public ConstructGenGen<RelativeDate, OIValue> BackOIAsRelativeDatesConstruct() { if (m_con_OI_to != null) return m_con_OI_to; m_con_OI_to = new ConstructGenGen<RelativeDate, OIValue>(1); foreach (var dateIndex in IndexedDates) { // create a RelativeDate for the IndexDates var rd = new RelativeDate(IndexedDates, dateIndex.Date, DispTimeZone); // shift it to the following morning (which is where we want to stamp the closing OI for the day) rd = rd.GetNextDayStart(); m_con_OI_to.SetValue( key_: rd, index_: 0, value_: new OIValue(OI_Back==null ? double.NaN : OI_Back.ValueOnExactDate_Else(dateIndex.Date, double.NaN), OIStatus.Actual)); } m_con_OI_to.SortKeys(); return m_con_OI_to; }
public NewLineUpdatedEventArgs(RelativeDate date_, Line updated_) { Date = date_; UpdatedLine = updated_; }
private void backFillFromBloombergToNow() { var startTimeInGMT = TimeZoneHelper.ConvertDateTime(DateTime.Now, TimeZoneInfo.Local, TimeZoneInfo.Utc).StartOfDay(); var endTimeInGMT = TimeZoneHelper.ConvertDateTime(DateTime.Now.StartOfMinute(), TimeZoneInfo.Local, TimeZoneInfo.Utc); { var rd = new RelativeDate(IndexedDates, DateTime.Now.ToTZfromLocal(DispTimeZone), DispTimeZone); var start = rd.GetDayBegin(); if (start.Index != RelativeDate.INVALID_INDEX) startTimeInGMT = start.UnderlyingDate.ToGMTFromTZ(DispTimeZone); } { var histBack = BbgTalk.HistoryRequester.GetIntradayBars( startTimeInGMT, endTimeInGMT, BackContractBloombergTicker(), BBG_LIVE_FIELD, 1); backFill(histBack, DayField.BackValue, DayField.BackVolume); } { var histFront = BbgTalk.HistoryRequester.GetIntradayBars( startTimeInGMT, endTimeInGMT, FrontContractBloombergTicker(), BBG_LIVE_FIELD, 1); backFill(histFront, DayField.FrontValue, DayField.FrontVolume); } { var spread = BbgTalk.HistoryRequester.GetIntradayBars( startTimeInGMT, endTimeInGMT, SpreadContractBbgTicker(m_spreadTickerFormation), BBG_LIVE_FIELD, 1); backFill(spread, DayField.SpreadValue, DayField.SpreadVolume); } m_data.Keys.Sort((a, b) => a.CompareTo(b)); // right, bloomberg doesn't give us every minute when we ask for historical candles, so we need to backfill the data so that we have full data ( for every minute ) Line prior = null; while (startTimeInGMT <= endTimeInGMT) { try { var dispDate = startTimeInGMT.ToTZfromGMT(DispTimeZone); var rd = new RelativeDate(IndexedDates, dispDate, DispTimeZone); if (rd.Index == RelativeDate.INVALID_INDEX) continue; if (m_data.HasKey(rd)) { var line = m_data.GetValue(rd, 0); if (prior == null) prior = line; else { if (line.SpreadValue.IsZero()) line.SpreadValue = prior.SpreadValue; if (line.BackContractValue.IsZero()) line.BackContractValue = prior.BackContractValue; if (line.FrontContractValue.IsZero()) line.FrontContractValue = prior.FrontContractValue; line.CalcSeriesValue(DefaultTail); prior = line; } } else { if (prior == null) continue; var newLine = new Line { GMTDateTime = startTimeInGMT.ToString(Line.MongoDateFormat), SpreadValue = prior.SpreadValue, BackContractValue = prior.BackContractValue, FrontContractValue = prior.FrontContractValue }; newLine.CalcSeriesValue(DefaultTail); m_data.SetValue(rd, 0, newLine); prior = newLine; } } finally { startTimeInGMT = startTimeInGMT.AddMinutes(1d); } } m_data.Keys.Sort((a, b) => a.CompareTo(b)); }
/// <summary> /// Asserts that a <see cref="DateToken"/> is an instance of the <see cref="RelativeDateToken"/> class and that /// its value is equal to the specified parameters. /// </summary> /// <param name="actual">The actual <see cref="DateToken"/>.</param> /// <param name="expectedRelativeDate">The expected <see cref="RelativeDate"/>.</param> private static void AssertAreEqual(DateToken actual, RelativeDate expectedRelativeDate) { Assert.AreEqual(typeof(RelativeDateToken), actual.GetType()); Assert.AreEqual(expectedRelativeDate, ((RelativeDateToken)actual).RelativeDate); }
private void ensureKey(DateTime timeInGMT_, bool forcePublish_=false) { var rd = new RelativeDate(IndexedDates, timeInGMT_.ToTZfromGMT(DispTimeZone),DispTimeZone); if (rd.Index == RelativeDate.INVALID_INDEX) return; try { m_lock.EnterWriteLock(); //Logger.Debug(string.Format("Ensuring key for {1} {0}", rd, Set), typeof (Day)); m_data.EnsureKey(rd); if (m_data.GetValue(rd, 0) == null) { var newLine = new Line { GMTDateTime = timeInGMT_.ToString(Line.MongoDateFormat) }; // found that the subscriptions to live bar data only ticks if volume goes through as is a subscription to trades // so we want to initialise the level values to the live values from the straight price subscriptions if (m_bbgLive_Price_BackContract != null && m_bbgLive_Price_BackContract.HasValue(BBG_LIVE_FIELD)) newLine.BackContractValue = m_bbgLive_Price_BackContract.GetValue<double>(BBG_LIVE_FIELD); if (m_bbgLive_Price_FrontContract != null && m_bbgLive_Price_FrontContract.HasValue(BBG_LIVE_FIELD)) newLine.FrontContractValue = m_bbgLive_Price_FrontContract.GetValue<double>(BBG_LIVE_FIELD); if (m_bbgLive_Price_Spread != null && m_bbgLive_Price_Spread.HasValue(BBG_LIVE_FIELD)) newLine.SpreadValue = m_bbgLive_Price_Spread.GetValue<double>(BBG_LIVE_FIELD); m_data.SetValue(rd, 0, newLine); m_lastUpdated = newLine; OnNewLineBeingUpdated(new NewLineUpdatedEventArgs(rd, newLine)); } else if (forcePublish_) { OnNewLineBeingUpdated(new NewLineUpdatedEventArgs(rd, m_data.GetValue(rd, 0))); } } finally { m_lock.ExitWriteLock(); } }
public static RelativeDate GetPriorDayStart(this RelativeDate date_) { // what's the start of the date that has been passed in? var dayBegin = date_.GetDayBegin(); // if we haven't managed to find the start of the day for the date passed in, then can't go any further if (dayBegin.Index == RelativeDate.INVALID_INDEX) return dayBegin; var priorRD = date_.IndexDates.FirstOrDefault(x => x.Index == dayBegin.Index - 1); if (priorRD == null) return dayBegin; var rd = new RelativeDate(date_.IndexDates, priorRD.Date, date_.DispTimeZone); return rd.GetDayBegin(); }
private void handleSpreadContractCandleUpdated(object sender, ValueUpdatedEventArgs<DateTime, double> val_) { var rd = new RelativeDate(IndexedDates, val_.Key.ToTZfromGMT(DispTimeZone),DispTimeZone); Logger.Debug(string.Format("Received spread contract update for {2} {0}={1}", (BarDataField) val_.Index, val_.Value, Set), typeof (Day)); if (rd.Index == RelativeDate.INVALID_INDEX) { Logger.Info(string.Format("Update was for time outside bounds of roll dates [={0}]", rd.UnderlyingDate), typeof (Day)); return; } try { m_lock.EnterWriteLock(); var v = m_data.GetValue(rd, 0); if (v == null) { Logger.Error("Got an update and the object hadn't been created. This should not happen", typeof(Day)); return; } setValue(rd, v, val_, DayField.SpreadValue, DayField.SpreadVolume); } finally { m_lock.ExitWriteLock(); } }
private void handleSpreadContractUpdated(object sender, ValueUpdatedEventArgs<DateTime, double> val_) { var rd = new RelativeDate(m_currentRoll.Dates, val_.Key.ToTZfromGMT(m_currentRoll.DispTimeZone)); try { m_lock.EnterWriteLock(); var v = m_data.GetValue(rd, currentIndex); if (v == null) { Logger.Error("Got an update and the object hadn't been created. This should not happen", typeof(LiveDayComparison)); return; } setValue(rd, v, val_, DayField.SpreadValue, DayField.SpreadVolume); } finally { m_lock.ExitWriteLock(); } }
public ConstructGenGen<RelativeDate, double> GenerateHourlySums(DayField field_) { Func<RelativeDate, RelativeDate, bool> areSameHour = (a, b) => a.UnderlyingDate.IsSameHourAs(b.UnderlyingDate); try { var data = LinesAsRelativeDates(); m_lock.EnterReadLock(); var con = new ConstructGenGen<RelativeDate, double>(1); if (data == null) return null; var hour = data.Keys[0]; var sum = 0d; foreach (var rd in data.Keys) { if (areSameHour(hour, rd)) sum += data.GetValue(rd, 0).GetValue(field_); else { con.SetValue(new RelativeDate(hour.IndexDates, hour.UnderlyingDate.StartOfHour(),DispTimeZone), 0, sum); sum = 0; hour = rd; } } // do we need to commit last value var lastHour = new RelativeDate(hour.IndexDates, hour.UnderlyingDate.StartOfHour(),DispTimeZone); if (con.LastKey() != lastHour) con.SetValue(lastHour, 0, sum); return con; } finally { m_lock.ExitReadLock(); } }
public void GetDateInLastWeek() { Assert.That(RelativeDate.GetDate(2017, EMonth.February, EWeeksInMonth.Last, EDaysOfWeekExt.Monday), Is.EqualTo(new DateTime(2017, 2, 27))); }
// shoule be called within the write lock private void setValue(RelativeDate date_, Line line_, ValueUpdatedEventArgs<DateTime, double> val_, DayField valueField_, DayField volumeField_) { BarDataField field = (BarDataField)val_.Index; switch (field) { case BarDataField.Volume: line_.SetValue(volumeField_, val_.Value); break; case BarDataField.Close: { line_.SetValue(valueField_, val_.Value); line_.CalcSeriesValue(m_currentRoll.DefaultTail); } break; } if (m_lastUpdated == null || !object.ReferenceEquals(m_lastUpdated, line_)) { m_lastUpdated = line_; OnNewLineBeingUpdated(new NewLineUpdatedEventArgs(RollGeneration.Current, date_, line_)); // keep the previous in sync... var priorEntry = m_data.GetValue(date_, priorIndex); OnNewLineBeingUpdated(new NewLineUpdatedEventArgs(RollGeneration.Prior, date_, priorEntry)); } }
public RelativeDateChangedArgs(RelativeDate value_) { Value = value_; }
public static RelativeDate GetDayBegin(this RelativeDate date_) { var hour = date_.DispTimeZone.GetMarketCloseHour(); // have we passed the market close today? if (date_.UnderlyingDate.Hour >= hour) return new RelativeDate(date_.IndexDates, date_.UnderlyingDate.Date.AddHours(hour),date_.DispTimeZone); // needs to be at the market close 'yesterday'... var priorRD = date_.GetPriorDayRD(); // if we're at the last index in the date list, then just take from midnight if (priorRD==null) return new RelativeDate(date_.IndexDates, date_.UnderlyingDate.Date,date_.DispTimeZone); // now, 'normally' it will be the marketClose time of the prior day var rd = new RelativeDate(date_.IndexDates, priorRD.Date.Date.AddHours(hour), date_.DispTimeZone); if (rd.Index != RelativeDate.INVALID_INDEX) return rd; // however, sometimes, the marketclose time of the prior day gets us to the beginning of a non-trading day (rd.Index will be -1) // try setting to the calendar day before close rd = new RelativeDate(date_.IndexDates, date_.UnderlyingDate.Date.AddDays(-1d).AddHours(hour), date_.DispTimeZone); if (rd.Index==RelativeDate.INVALID_INDEX || date_.UnderlyingDate.DayOfWeek == DayOfWeek.Monday) return new RelativeDate(date_.IndexDates, date_.UnderlyingDate.AddDays(-1d).Date.AddHours(hour), date_.DispTimeZone); if (rd.Index == RelativeDate.INVALID_INDEX) { Logger.Error( string.Format("{0} day begin calculated as {1} which isn't valid", date_.UnderlyingDate, rd.UnderlyingDate), typeof (DateExtensionsMethods)); } return rd; }
public IDoubleMonitor GetLiveSpreadOnDayVolumeMonitor(RollGeneration generation_, bool forceRegeneration_ = false) { var key = getMonitorKey("LiveDaysSpreadVolume", generation_); return getorCreateMonitor(key, () => { // index of current day in current set var index = RelativeDate.FindIndex(GetDay(RollGeneration.Current).Dates, DateTime.Today); if (index < 0) return null; var startKey = new RelativeDate(GetDay(generation_).Dates, GetDay(generation_).Dates[index]); return new CumulativeSinceTimeMonitor(startKey, this, generation_, DayField.SpreadVolume); }, forceRegeneration_); }
public RelativeDateValidatorCAttribute(RelativeDate relDate) : base(relDate) { }
private static void BackfillEveryMinute(Day rollDay_) { if (rollDay_.Lines == null || rollDay_.Lines.Count < 2) return; var toAdd = new List<Line>(); var dates = rollDay_.Lines.Select(x => x.GmtDateAsDate()).OrderBy(x=>x).ToArray(); var date = dates[0]; var lastDate = dates[dates.Length - 1]; int index = 0; while (date <= lastDate) { date = date.AddMinutes(1d); var rd = new RelativeDate(rollDay_.IndexedDates, date.ToTZfromGMT(rollDay_.DispTimeZone), rollDay_.DispTimeZone); if (rd.Index == RelativeDate.INVALID_INDEX) continue; while (index < rollDay_.Lines.Count-1 && rollDay_.Lines[index].GmtDateAsDate() < date) ++index; // if we already have a line for this minute? if (rollDay_.Lines[index].GmtDateAsDate() == date) continue; if (rollDay_.Lines[index].GmtDateAsDate() > date) { --index; var copyThis = rollDay_.Lines[index]; toAdd.Add(new Line { SpreadValue=copyThis.SpreadValue, BackContractValue = copyThis.BackContractValue, FrontContractValue=copyThis.FrontContractValue, GMTDateTime=date.ToString(Line.MongoDateFormat) }); } } if (toAdd.Count > 0) { rollDay_.Lines.AddRange(toAdd); rollDay_.Lines.Sort((a, b) => a.GmtDateAsDate().CompareTo(b.GmtDateAsDate())); } }
private static async Task getFromCarbon(IEnumerable<string> possibleIdentifiers_, IDictionary<RelativeDate, Line> values_, RDDateIndex[] dateIndexes_, DisplayTimeZone zone_, DayField valueField_, DayField volumeField_, CarbonClient cc_) { var startDate = dateIndexes_.Select(x => x.Date).Min().AddDays(-3d); var endDate = dateIndexes_.Select(x => x.Date).Max().AddDays(1d); var result = await cc_.GetTimeSeriesAsync(possibleIdentifiers_, "intra", startDate, endDate); if (result == null) { Logger.Info( string.Format("Could not get results back from carbon for tickers=[{0}], with startdate={1} and enddate={2}", string.Join(",", possibleIdentifiers_), startDate.ToString("dd-MMM-yyyy"), endDate.ToString("dd-MMM-yyyy")), typeof (CarbonHelper)); return; } foreach (var id in possibleIdentifiers_) { var set = result.FirstOrDefault(x => x.Identifier.Equals(id)); if (set == null) continue; foreach (var row in set.Series) { // need to have timestamp and price in order to be a value row... if ( !row.ContainsKey(CarbonFields.TimeStamp) || !row.ContainsKey(CarbonFields.CloseAsk) || !row.ContainsKey(CarbonFields.CloseBid)) continue; var tsGMTstr = row.GetString(CarbonFields.TimeStamp); DateTime tsGMT; if (!DateTime.TryParseExact(tsGMTstr, "yyyyMMdd HHmmss.fff", CultureInfo.InvariantCulture,DateTimeStyles.None, out tsGMT)) continue; // convert gmt to the timezone that we're interested in var tsTZ = tsGMT.ToTZfromGMT(zone_); // create a relative date object so we can check if we need this line var rd = new RelativeDate(dateIndexes_, tsTZ, zone_); if (rd.Index == RelativeDate.INVALID_INDEX) continue; var closeBid = row.GetDouble(CarbonFields.CloseBid); var closeAsk = row.GetDouble(CarbonFields.CloseAsk); if (!closeBid.HasValue || !closeAsk.HasValue || closeBid.Value.IsZero() || closeAsk.Value.IsZero()) continue; var closeMid = (closeBid.Value + closeAsk.Value)/2d; if (!values_.ContainsKey(rd)) values_[rd] = new Line { GMTDateTime = tsGMT.ToString(Line.MongoDateFormat) }; double volume = 0d; if (row.ContainsKey(CarbonFields.Volume)) { var vol = row.GetDouble(CarbonFields.Volume); volume = vol ?? 0d; } values_[rd].SetValue(valueField_, closeMid); values_[rd].SetValue(volumeField_, volume); } // if one of the tickers returned values for the given start/end date, then we don't need to consider the next one break; } }