Source/NFX/Financial/Market/SamplingUtils.cs
/// <summary>
/// Aggregates source stream of the normally equidistant samples of the same type by the specified factor
/// </summary>
/// <param name="source">Source stream</param>
/// <param name="times">Factor of aggergation, i.e. 4x means aggregate 4 samples into one</param>
/// <param name="samplingRateVariationPct">
///  The allowed variation in timing between samples, once this variation is exceeded the system emits new aggregate
/// </param>
/// <returns>Aggregated sample stream</returns>
Aggregates source stream of the normally equidistant samples of the same type by the specified factor
Aggregates source stream of the normally equidistant samples of the same type by the specified factor
public static IEnumerable<ITimeSeriesSample> AggregateHomogeneousSamples(this IEnumerable<ITimeSeriesSample> source, uint times, float samplingRateVariationPct = 1f)
    {
      if (source==null) yield break;
      ITimeSeriesSample prior = null;
      ITimeSeriesSample emit = null;
      float prate = 0f;
      var cnt = 0;
      foreach(var sample in source)
      {
        if (prior!=null && prate!=0)
        {
          var rate = (sample.TimeStamp - prior.TimeStamp).Ticks;
          if ( (Math.Abs(rate-prate) / Math.Max(rate, prate)) > samplingRateVariationPct)
          {
              if (emit!=null)
              {
                emit.SummarizeAggregation();
                yield return emit;
                emit = null;
              }
              prate = 0;
          }
          else
           prate = rate;
        }
        if (emit==null)
        {
          emit = sample.MakeAggregateInstance();
          prior = sample;
          continue;
        }
        emit.AggregateSample(sample);
        cnt++;
        if (cnt>=times)
        {
          emit.SummarizeAggregation();
          yield return emit;
          emit = null;
        }
        prior = sample;
      }
      if (emit==null) yield break;
      emit.SummarizeAggregation();
      yield return emit;
    }
public static IEnumerable < ITimeSeriesSample > AggregateHomogeneousSamples ( this IEnumerable < ITimeSeriesSample > source , uint times , float samplingRateVariationPct = 1f ) { if ( source == null ) yield break ; ITimeSeriesSample prior = null ; ITimeSeriesSample emit = null ; float prate = 0f ; var cnt = 0 ; foreach ( var sample in source ) { if ( prior != null && prate != 0 ) { var rate = ( sample . TimeStamp - prior . TimeStamp ) . Ticks ; if ( ( Math . Abs ( rate - prate ) / Math . Max ( rate , prate ) ) > samplingRateVariationPct ) { if ( emit != null ) { emit . SummarizeAggregation ( ) ; yield return emit ; emit = null ; } prate = 0 ; } else prate = rate ; } if ( emit == null ) { emit = sample . MakeAggregateInstance ( ) ; prior = sample ; continue ; } emit . AggregateSample ( sample ) ; cnt ++ ; if ( cnt >= times ) { emit . SummarizeAggregation ( ) ; yield return emit ; emit = null ; } prior = sample ; } if ( emit == null ) yield break ; emit . SummarizeAggregation ( ) ; yield return emit ; }
Aggregates source stream of the normally equidistant samples of the same type by the specified factor
Aggregates source stream of the normally equidistant samples of the same type by the specified factor
Aggregated sample stream
Aggregated sample stream
IEnumerable<ITimeSeriesSample>
this
times
uint
Factor of aggergation, i.e. 4x means aggregate 4 samples into one
Factor of aggergation i . e . 4x means aggregate 4 samples into one
samplingRateVariationPct
float
The allowed variation in timing between samples, once this variation is exceeded the system emits new aggregate
The allowed variation in timing between samples once this variation is exceeded the system emits new aggregate
source
None