Skip to content

Commit

Permalink
CandleManager. Priority source switch in runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasoukhov committed Sep 8, 2015
1 parent 41760ee commit eefb9d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
6 changes: 5 additions & 1 deletion Algo/Candles/CandleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ public virtual void Start(CandleSeries series, DateTimeOffset from, DateTimeOffs

enumerator = new CandleSourceEnumerator<ICandleManagerSource, Candle>(series, from, to,
series.Security is IndexSecurity ? (IEnumerable<ICandleManagerSource>)new[] { new IndexSecurityCandleManagerSource(this, from, to) } : Sources,
c => Processing.SafeInvoke(series, c),
c =>
{
Processing.SafeInvoke(series, c);
return c.OpenTime;
},
() =>
{
//Stop(series);
Expand Down
14 changes: 11 additions & 3 deletions Algo/Candles/CandleSourceEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ public void ExtendRange(Range<DateTimeOffset> additionalRange)
}

private readonly CandleSeries _series;
private readonly Action<TValue> _processing;
private readonly Func<TValue, DateTimeOffset> _processing;
private readonly Action _stopped;
private readonly SynchronizedQueue<SourceInfo> _sources = new SynchronizedQueue<SourceInfo>();
private bool _manualStopped;
private DateTimeOffset? _nextSourceBegin;

public CandleSourceEnumerator(CandleSeries series, DateTimeOffset from, DateTimeOffset to, IEnumerable<TSource> sources, Action<TValue> processing, Action stopped)
public CandleSourceEnumerator(CandleSeries series, DateTimeOffset from, DateTimeOffset to, IEnumerable<TSource> sources, Func<TValue, DateTimeOffset> processing, Action stopped)
{
if (series == null)
throw new ArgumentNullException("series");
Expand Down Expand Up @@ -136,6 +137,9 @@ public void Start()
CurrentSource.Processing += OnProcessing;
CurrentSource.Stopped += OnStopped;

var next = _sources.Count > 0 ? _sources.Peek() : null;
_nextSourceBegin = next == null ? (DateTimeOffset?)null : next.Range.Min;

CurrentSource.Start(_series, info.Range.Min, info.Range.Max);
}

Expand All @@ -149,7 +153,10 @@ private void OnProcessing(CandleSeries series, TValue value)
if (series != _series)
return;

_processing(value);
var date = _processing(value);

if (_nextSourceBegin != null && date > _nextSourceBegin)
CurrentSource.Stop(series);
}

private void OnStopped(CandleSeries series)
Expand All @@ -164,6 +171,7 @@ private void OnStopped(CandleSeries series)
CurrentSource.Processing -= OnProcessing;
CurrentSource.Stopped -= OnStopped;
CurrentSource = null;
_nextSourceBegin = null;

if (_manualStopped || _sources.IsEmpty())
raiseStop = true;
Expand Down
6 changes: 5 additions & 1 deletion Algo/Candles/Compression/CandleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public CandleSeriesInfo(CandleSeries series, DateTimeOffset from, DateTimeOffset
throw new ArgumentNullException("stopped");

_enumerator = new CandleSourceEnumerator<ICandleBuilderSource, IEnumerable<ICandleBuilderSourceValue>>(series, from, to,
sources, v => handler(series, v), () => stopped(series));
sources, v =>
{
handler(series, v);
return v.Last().Time;
}, () => stopped(series));
}

public Candle CurrentCandle { get; set; }
Expand Down

0 comments on commit eefb9d0

Please sign in to comment.