Ejemplo n.º 1
0
        public RealtimeOhlcSource(ExecutionCachedSourceFactory historicalFactory, BfProductCode productCode, TimeSpan frameSpan, IObservable <BfExecution> realtime)
        {
            IDisposable subscribed = null;

            _source = Observable.Create <RealtimeOhlc>(observer =>
            {
                var firstOhlc   = default(RealtimeOhlc);
                var currentOhlc = default(RealtimeOhlc);

                return(realtime.Scan(default(BfExecution), (prev, current) =>
                {
                    if (prev == default(BfExecution))
                    {
                        firstOhlc = currentOhlc = new RealtimeOhlc(current);
                        var refTime = current.ExecutedTime.Round(frameSpan);
                        subscribed = historicalFactory.GetExecutionCachedSource(productCode, current.ExecutionId)
                                     .TakeWhile(tick => tick.ExecutedTime >= refTime)
                                     .Finally(() =>
                        {
                            subscribed.Dispose(); // 本当に必要か?
                        })
                                     .Subscribe(
                            histTick => firstOhlc.Update(histTick),
                            ex => observer.OnError(ex),
                            () => { /* TakeWhile完了後にちゃんと終わるか? */ }
                            );
                        observer.OnNext(firstOhlc);
                    }
                    // Close/Open buffer
                    else if (prev.ExecutedTime.Round(frameSpan) != current.ExecutedTime.Round(frameSpan))
                    {
                        currentOhlc.CommitFrame();
                        currentOhlc = new RealtimeOhlc(current);
                        observer.OnNext(currentOhlc);
                    }
                    else
                    {
                        currentOhlc.Update(current);
                    }
                    return current;
                }).Subscribe().AddTo(_disposables));
            });
        }
 public RealtimeOhlcSourceFactory(RealtimeSourceFactory realtimeFactory, ExecutionCachedSourceFactory historicalFactory)
 {
     _realtimeFactory   = realtimeFactory;
     _historicalFactory = historicalFactory;
 }