/// <summary> /// Calculates average price by specified dimensions and datetime of the timeslot. /// </summary> /// <returns> /// <see cref="AveragePriceResult"/> in case Price records found by specified params. /// Otherwise <code>null<code/>. /// </returns> public async Task <AveragePriceResult> CalculateAveragePrice( DateTime dateTime, string portfolioName = null, string instrumentOwnerName = null, string instrumentName = null) { if (dateTime < TimeSlot.Origin) { return(null); } var timeslot = TimeSlot.DateTimeToTimeSlot(dateTime); var filter = BuildFilter(portfolioName, instrumentOwnerName, instrumentName); var dimensions = SelectDimensions(portfolioName, instrumentOwnerName, instrumentName); var response = await _druidClient.GroupByAsync <AveragePriceAggregationResult>( aggr => ConfigureAggregationSpec(aggr, timeslot, filter, dimensions)); if (response.Data.Any()) { var aggregationResult = response.Data.Single(); return(new AveragePriceResult(aggregationResult.Timestamp, aggregationResult.Event.AveragePrice)); } return(null); }
public void DateTimeToTimeSlot_ReturnsZeroTimeSlot_ForOrigin() { var dt = new DateTime(2018, 1, 1); var timeslot = TimeSlot.DateTimeToTimeSlot(dt); timeslot.Index.Should().Be(0); timeslot.Start.Should().Be(dt); timeslot.End.Should().Be(dt.AddSeconds(10_000)); }
public void DateTimeToTimeSlot_ReturnsCorrectTimeSlot(int year, int month, int day, int hour, int minute, int second) { var dt = new DateTime(year, month, day, hour, minute, second); var expectedTimeslotStart = new DateTime(2018, 3, 15, 17, 26, 40); var timeslot = TimeSlot.DateTimeToTimeSlot(dt); timeslot.Index.Should().Be(637); timeslot.Start.Should().Be(expectedTimeslotStart); timeslot.End.Should().Be(expectedTimeslotStart.AddSeconds(10_000)); }
public async Task GetAverageBySingleRecord_ReturnsExactPrice() { var testRecord = _testDataRows.Single(r => r.Portfolio == "Barclays" && r.Owner == "Ford" && r.Instrument == "Swap"); var expectedDate = TimeSlot.DateTimeToTimeSlot(testRecord.Date).Start; var expectedPrice = 712.54M; var result = await _httpClient.GetAveragePrice(testRecord); result.Should().NotBeNull(); result.Date.Should().Be(expectedDate); result.Price.Should().Be(expectedPrice); }
public async Task GetAveragePrice_CanCalculateBy_DateOnly() { var expectedDate = new DateTime(2018, 1, 1); var expectedTimeSlot = TimeSlot.DateTimeToTimeSlot(expectedDate); var testRecords = _testDataRows.Where(r => r.TimeSlot == expectedTimeSlot).ToList(); var expectedPrice = testRecords.Select(r => r.Price).Average().RoundTo(decimals: 3); var result = await _httpClient.GetAveragePrice(new PriceRecordDimensions { Date = expectedDate }); result.Should().NotBeNull(); result.Date.Should().Be(expectedDate); result.Price.RoundTo(decimals: 3).Should().Be(expectedPrice); }
public async Task <AveragePriceDto> ExecuteAsync(DateTime time, string instrument, string owner, string portfolio) { int timeSlot = TimeSlot.DateTimeToTimeSlot(time); IList <InstrumentPrice> pricesList = await _dbService.InstrumentPrices.Where(x => x.TimeSlot == timeSlot && x.Portfolio == portfolio && x.Instrument == instrument && x.InstrumentOwner == owner).ToListAsync(); if (!pricesList.Any()) { return(null); } return(new AveragePriceDto() { Price = pricesList.Sum(x => x.Price) / pricesList.Count, Date = TimeSlot.GetTimeSlotStartDate(timeSlot) }); }
public void DateTimeToTimeSlot_ThrowsError_ForDateSmallerThanOrigin() { var dt = new DateTime(2017, 1, 1); Assert.Throws <ValidationException>(() => TimeSlot.DateTimeToTimeSlot(dt)); }
public void DateTimeToTimeSlotWhenEpochStartTimePassedNumberShouldBeZero() { TimeSlot.DateTimeToTimeSlot(TimeSlot.EpochStart).ShouldBe(0); }
public void DateTimeToTimeSlotValidUTCTimePassedNumberReturned() { TimeSlot.DateTimeToTimeSlot(new DateTime(2018, 03, 15, 17, 34, 50, DateTimeKind.Utc)).ShouldBe(637); }
public void DateTimeToTimeSlotWhenTimeBeforeEpochStartPassedToCtorExceptionShouldBeThrown() { Should.Throw <ArgumentOutOfRangeException>(() => TimeSlot.DateTimeToTimeSlot(new DateTime(2000, 1, 1))); }
private List <InstrumentPrice> ReadFromFile(string filePath) { List <InstrumentPrice> dataList = new List <InstrumentPrice>(); var fileStream = new StreamReader(filePath); string line; fileStream.ReadLine(); // Skip first line InstrumentPrice instrumentPrice; decimal price; DateTime date; while ((line = fileStream.ReadLine()) != null) { var properties = line.Split(','); if (Decimal.TryParse(properties[4], out price)) { if (DateTime.TryParseExact(properties[3], _format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { try { instrumentPrice = new InstrumentPrice(properties[0], properties[1], properties[2], date, TimeSlot.DateTimeToTimeSlot(date), price); dataList.Add(instrumentPrice); } catch (Exception e) { _logger.LogInformation(e, ""); continue; } } else { _logger.LogInformation($"Error parsing Date - {properties[3]}"); } } else { _logger.LogInformation($"Error parsing Price - {properties[4]}"); } } return(dataList); }