反应性扩展滑动时间窗口

时间:2012-07-19 10:52:12

标签: c# system.reactive

我有一系列的股票报价,我希望在过去一小时内获取所有数据并对其进行一些处理。我试图用反应式扩展2.0实现这一点。我在另一篇文章中读到使用Interval,但我认为这已被弃用。

4 个答案:

答案 0 :(得分:9)

这种扩展方法会解决您的问题吗?

public static IObservable<T[]> RollingBuffer<T>(
    this IObservable<T> @this,
    TimeSpan buffering)
{
    return Observable.Create<T[]>(o =>
    {
        var list = new LinkedList<Timestamped<T>>();
        return @this.Timestamp().Subscribe(tx =>
        {
            list.AddLast(tx);
            while (list.First.Value.Timestamp < DateTime.Now.Subtract(buffering))
            {
                list.RemoveFirst();
            }
            o.OnNext(list.Select(tx2 => tx2.Value).ToArray());
        }, ex => o.OnError(ex), () => o.OnCompleted());
    });
}

答案 1 :(得分:4)

您正在寻找Window运营商! 这是我写的一篇冗长的文章,用于处理重合序列(序列的重叠窗口) http://introtorx.com/Content/v1.0.10621.0/17_SequencesOfCoincidence.html

因此,如果您想构建滚动平均值,则可以使用此类代码

var scheduler = new TestScheduler();
var notifications = new Recorded<Notification<double>>[30];
for (int i = 0; i < notifications.Length; i++)
{
  notifications[i] = new Recorded<Notification<double>>(i*1000000, Notification.CreateOnNext<double>(i));
}
//Push values into an observable sequence 0.1 seconds apart with values from 0 to 30
var source = scheduler.CreateHotObservable(notifications);

source.GroupJoin(
      source,   //Take values from myself
      _=>Observable.Return(0, scheduler), //Just the first value
      _=>Observable.Timer(TimeSpan.FromSeconds(1), scheduler),//Window period, change to 1hour
      (lhs, rhs)=>rhs.Sum())    //Aggregation you want to do.
    .Subscribe(i=>Console.WriteLine (i));
scheduler.Start();

我们可以看到它在收到值时输出滚动总和。

0,1,3,6,10,15,21,28 ......

答案 2 :(得分:1)

很有可能Buffer正是您所寻找的:

var hourlyBatch = ticks.Buffer(TimeSpan.FromHours(1));

答案 3 :(得分:1)

或者假设数据已经Timestamp,只需使用Scan

    public static IObservable<IReadOnlyList<Timestamped<T>>> SlidingWindow<T>(this IObservable<Timestamped<T>> self, TimeSpan length)
    {
        return self.Scan(new LinkedList<Timestamped<T>>(),
                         (ll, newSample) =>
                         {
                             ll.AddLast(newSample);
                             var oldest = newSample.Timestamp - length;
                             while (ll.Count > 0 && list.First.Value.Timestamp < oldest)
                                 list.RemoveFirst();

                             return list;
                         }).Select(l => l.ToList().AsReadOnly());
    }