首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应式扩展滑动时间窗口

反应式扩展滑动时间窗口
EN

Stack Overflow用户
提问于 2012-07-19 18:52:12
回答 4查看 3.8K关注 0票数 5

我有一个股票行情序列,我想获取过去一小时内的所有数据,并对其进行一些处理。我正在尝试使用reactive extensions 2.0来实现这一点。我在另一篇文章中读到了使用Interval,但我认为它已被弃用。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-07-25 16:54:03

这个扩展方法能解决你的问题吗?

代码语言:javascript
复制
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());
    });
}
票数 9
EN

Stack Overflow用户

发布于 2012-07-25 19:14:37

您正在寻找窗口操作符!这是我写的一篇关于使用重合序列(序列重叠窗口) http://introtorx.com/Content/v1.0.10621.0/17_SequencesOfCoincidence.html的长篇文章

因此,如果你想构建一个滚动平均,你可以使用这种代码

代码语言:javascript
复制
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...

票数 4
EN

Stack Overflow用户

发布于 2012-07-19 23:48:30

Buffer很可能就是您要查找的内容:

代码语言:javascript
复制
var hourlyBatch = ticks.Buffer(TimeSpan.FromHours(1));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11559255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档