17 using System.Collections.Generic;
18 using Newtonsoft.Json;
34 private readonly IPeriodSpecification _periodSpecification;
39 public Guid
Id {
get;
protected set; }
44 public Guid?
GroupId {
get;
protected set; }
96 public TimeSpan
Period {
get;
internal set; }
111 public double?
Weight {
get;
private set; }
126 public string Tag {
get;
protected set; }
179 : this(symbol, period, type, direction, null, null, null, null, tag)
212 _periodSpecification =
new TimeSpanPeriodSpecification(period);
224 : this(symbol, expiryFunc, type, direction, null, null, null, null, tag)
240 public Insight(
Symbol symbol, Func<DateTime, DateTime> expiryFunc,
InsightType type,
InsightDirection direction,
double? magnitude,
double? confidence,
string sourceModel =
null,
double? weight =
null,
string tag =
"")
241 : this(symbol, new FuncPeriodSpecification(expiryFunc), type, direction, magnitude, confidence, sourceModel, weight, tag)
260 public Insight(DateTime generatedTimeUtc,
Symbol symbol, TimeSpan period,
InsightType type,
InsightDirection direction,
double? magnitude,
double? confidence,
string sourceModel =
null,
double? weight =
null,
string tag =
"")
261 : this(symbol, period, type, direction, magnitude, confidence, sourceModel, weight, tag)
279 private Insight(
Symbol symbol, IPeriodSpecification periodSpec,
InsightType type,
InsightDirection direction,
double? magnitude,
double? confidence,
string sourceModel =
null,
double? weight =
null,
string tag =
"")
295 _periodSpecification = periodSpec;
298 var period = (periodSpec as TimeSpanPeriodSpecification)?.
Period;
316 _periodSpecification.SetPeriodAndCloseTime(
this, exchangeHours);
359 var spec =
new ResolutionBarCountPeriodSpecification(resolution, barCount);
360 return new Insight(symbol, spec,
InsightType.Price, direction, magnitude, confidence, sourceModel, weight, tag);
375 public static Insight Price(
Symbol symbol, DateTime closeTimeLocal,
InsightDirection direction,
double? magnitude =
null,
double? confidence =
null,
string sourceModel =
null,
double? weight =
null,
string tag =
"")
377 var spec = closeTimeLocal ==
Time.
EndOfTime ? (IPeriodSpecification)
378 new EndOfTimeCloseTimePeriodSpecification() :
new CloseTimePeriodSpecification(closeTimeLocal);
379 return new Insight(symbol, spec,
InsightType.Price, direction, magnitude, confidence, sourceModel, weight, tag);
394 public static Insight Price(
Symbol symbol, TimeSpan period,
InsightDirection direction,
double? magnitude =
null,
double? confidence =
null,
string sourceModel =
null,
double? weight =
null,
string tag =
"")
402 new EndOfTimeCloseTimePeriodSpecification() :
new TimeSpanPeriodSpecification(period);
403 return new Insight(symbol, spec,
InsightType.Price, direction, magnitude, confidence, sourceModel, weight, tag);
418 public static Insight Price(
Symbol symbol, Func<DateTime, DateTime> expiryFunc,
InsightDirection direction,
double? magnitude =
null,
double? confidence =
null,
string sourceModel =
null,
double? weight =
null,
string tag =
"")
420 return new Insight(symbol, expiryFunc,
InsightType.Price, direction, magnitude, confidence, sourceModel, weight, tag);
429 if (insights ==
null)
431 throw new ArgumentNullException(nameof(insights));
434 var groupId = Guid.NewGuid();
435 foreach (var insight
in insights)
437 if (insight.GroupId.HasValue)
442 insight.GroupId = groupId;
451 public static IEnumerable<Insight>
Group(
Insight insight) =>
Group(
new[] {insight});
463 new Symbol(sid, serializedInsight.
Ticker ?? sid.Symbol),
464 TimeSpan.FromSeconds(serializedInsight.
Period),
465 serializedInsight.
Type,
471 serializedInsight.
Tag
474 Id = Guid.Parse(serializedInsight.
Id),
479 GroupId =
string.IsNullOrEmpty(serializedInsight.
GroupId) ? (Guid?)
null : Guid.Parse(serializedInsight.
GroupId)
487 insight.Score.Finalize(insight.CloseTimeUtc);
531 var barSize = resolution.ToTimeSpan();
532 var startTimeLocal = generatedTimeUtc.ConvertFromUtc(exchangeHours.
TimeZone);
534 return closeTimeLocal.ConvertToUtc(exchangeHours.
TimeZone);
551 var barSize = period.ToHigherResolutionEquivalent(
false);
556 var barCount = (int)(period.Ticks / barSize.ToTimeSpan().Ticks);
557 var closeTimeUtc =
ComputeCloseTime(exchangeHours, generatedTimeUtc, barSize, barCount);
558 if (closeTimeUtc == generatedTimeUtc)
563 var totalPeriodUsed = barSize.ToTimeSpan().Multiply(barCount);
564 if (totalPeriodUsed != period)
566 var delta = period - totalPeriodUsed;
571 var percentOfDay = delta.Ticks / (double)
Time.
OneDay.Ticks;
575 if (delta != TimeSpan.Zero)
613 private interface IPeriodSpecification
621 private class TimeSpanPeriodSpecification : IPeriodSpecification
623 public readonly TimeSpan
Period;
625 public TimeSpanPeriodSpecification(TimeSpan period)
627 if (period == TimeSpan.Zero)
645 private class ResolutionBarCountPeriodSpecification : IPeriodSpecification
648 public readonly
int BarCount;
650 public ResolutionBarCountPeriodSpecification(
Resolution resolution,
int barCount)
671 insight.Period = insight.CloseTimeUtc - insight.GeneratedTimeUtc;
678 private class CloseTimePeriodSpecification : IPeriodSpecification
680 public readonly DateTime CloseTimeLocal;
682 public CloseTimePeriodSpecification(DateTime closeTimeLocal)
684 CloseTimeLocal = closeTimeLocal;
690 var closeTimeLocal = exchangeHours.
IsOpen(CloseTimeLocal,
false)
694 insight.CloseTimeUtc = closeTimeLocal.ConvertToUtc(exchangeHours.
TimeZone);
696 if (insight.GeneratedTimeUtc > insight.CloseTimeUtc)
698 throw new ArgumentOutOfRangeException(nameof(closeTimeLocal), $
"Insight closeTimeLocal must not be in the past.");
701 insight.Period = insight.CloseTimeUtc - insight.GeneratedTimeUtc;
709 private class FuncPeriodSpecification : IPeriodSpecification
711 public readonly Func<DateTime, DateTime> _expiryFunc;
713 public FuncPeriodSpecification(Func<DateTime, DateTime> expiryFunc)
715 _expiryFunc = expiryFunc;
720 var closeTimeLocal = insight.GeneratedTimeUtc.ConvertFromUtc(exchangeHours.
TimeZone);
721 closeTimeLocal = _expiryFunc(closeTimeLocal);
724 if (!exchangeHours.
IsOpen(closeTimeLocal,
false))
729 insight.CloseTimeUtc = closeTimeLocal.ConvertToUtc(exchangeHours.
TimeZone);
730 insight.Period = insight.CloseTimeUtc - insight.GeneratedTimeUtc;
738 private class EndOfTimeCloseTimePeriodSpecification : IPeriodSpecification
742 insight.Period = Time.EndOfTimeTimeSpan;
743 insight.CloseTimeUtc = Time.EndOfTime;