我有一个股票行情序列,我想获取过去一小时内的所有数据,并对其进行一些处理。我正在尝试使用reactive extensions 2.0来实现这一点。我在另一篇文章中读到了使用Interval,但我认为它已被弃用。
发布于 2012-07-25 16:54:03
这个扩展方法能解决你的问题吗?
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());
});
}发布于 2012-07-25 19:14:37
您正在寻找窗口操作符!这是我写的一篇关于使用重合序列(序列重叠窗口) 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...
发布于 2012-07-19 23:48:30
Buffer很可能就是您要查找的内容:
var hourlyBatch = ticks.Buffer(TimeSpan.FromHours(1));https://stackoverflow.com/questions/11559255
复制相似问题