Search Framework:
Synchronizer
Namespace: WealthLab.Core
Parent: Object

The Synchronizer class provides synchronized, iterative access across multiple TimeSeriesBase instances. It is useful when you need to process several TimeSeries or BarHistory objects in chronological order even when they do not all contain data for the same DateTimes. Each call to Next advances the Synchronizer to the earliest remaining DateTime among all of the supplied series and returns the TimeSeriesBase instances that contain data at that DateTime.

Constructors
Synchronizer
public Synchronizer(List<TimeSeriesBase> lst)
public Synchronizer(List<BarHistory> lst)

Creates a Synchronizer for the supplied collection of TimeSeriesBase or BarHistory instances. The Synchronizer maintains an independent current index for each series and initially positions each one at index 0. Both constructors call Reset before iteration begins.



Members
CurrentDateTime
public DateTime CurrentDateTime

Returns the DateTime currently being processed by the Synchronizer. After a successful call to Next, this is the earliest DateTime among the series that still contain unprocessed data. Before iteration begins, or immediately after Reset, the value is DateTime.MinValue.


GetCurrentIndex
public int GetCurrentIndex(TimeSeriesBase tsb)

Returns the current index associated with tsb. The index corresponds to the element in the TimeSeriesBase returned for the current CurrentDateTime. Returns -1 if tsb is not one of the objects managed by the Synchronizer.


IsAtLastBar
public bool IsAtLastBar

Returns true when every TimeSeriesBase that contributed to the current iteration is positioned at its final value. This property is typically checked after calling Next to determine whether the currently returned series have reached their final bars.


public List<TimeSeriesBase> Next()

Advances the Synchronizer to the next available DateTime and returns all TimeSeriesBase instances that contain data at that DateTime. The method works as follows:

  1. Series that contributed to the previous call are advanced by one index.
  2. Series that still contain unprocessed data are examined.
  3. The earliest DateTime among those series becomes CurrentDateTime.
  4. Every series whose current DateTime matches that DateTime is returned. A TimeSeriesBase is therefore returned only when it has data for the current synchronized DateTime. If no series contain any remaining data, an empty List is returned. The current index for a returned series can be obtained using GetCurrentIndex. For example:
List<BarHistory> histories = new List<BarHistory>
{
    bars1,
    bars2,
    bars3
};
Synchronizer sync = new Synchronizer(histories);
while (true)
{
    List<TimeSeriesBase> participants = sync.Next();
    if (participants.Count == 0)
        break;
    DateTime dt = sync.CurrentDateTime;
    foreach (TimeSeriesBase ts in participants)
    {
        int idx = sync.GetCurrentIndex(ts);
        //process ts at idx
    }
}

If two or more series contain data at the same DateTime, they are returned together from the same call to Next. If one series is missing a DateTime that exists in another, it simply does not participate in that iteration.


Reset
public void Reset()

Resets the Synchronizer to the beginning of all managed series. After calling Reset:

  • CurrentDateTime is set to DateTime.MinValue.
  • The current index of every managed TimeSeriesBase is reset to 0. The next call to Next resumes iteration from the earliest DateTime available in the supplied series.