Parent: Object
TimeSeriesSynchronizer is a static utility class used to synchronize a TimeSeries with the DateTimes of another TimeSeriesBase instance. The master series can be either a TimeSeries or a BarHistory. The returned TimeSeries contains the same DateTimes as the master, with values from the source carried forward as appropriate. This is especially useful when working with indicators or TimeSeries calculated at a different scale. For example, you can compress daily data to weekly, calculate an indicator on the weekly series, and then synchronize the result back to the original daily BarHistory for plotting or use in Strategy logic.
Synchronizes the source TimeSeries with the DateTimes of master and returns a new TimeSeries.
The returned TimeSeries has the same DateTimes and Count as master.
For each master DateTime, WealthLab uses the most recent source value that is available as of that point in time.
If either source or master is null or contains no values, an empty TimeSeries is returned.
prefillNans
If the master begins before the first available source value, the synchronized TimeSeries initially contains Double.NaN values.
If prefillNans is supplied, those initial Double.NaN values are replaced with the specified value.
For example:
TimeSeries synced = TimeSeriesSynchronizer.Synchronize(
source,
bars,
0.0);
This fills any leading values before the first available source value with zero. market The optional market parameter supplies MarketDetails used when synchronizing data across scales, particularly daily-or-higher data to intraday data. If master is a BarHistory and market is null, WealthLab automatically uses the BarHistory's Market. If no MarketDetails are available when synchronizing daily-or-higher data to intraday data, WealthLab attempts to determine the market close time from the master series itself. Market information is particularly important for markets whose trading sessions cross midnight. performScaleUpShift Set performScaleUpShift to true when synchronizing from a less granular non-intraday scale to a more granular non-intraday scale and the source value should not become available until the following master period. Examples include:
- Monthly to Daily
- Quarterly to Daily
- Yearly to Monthly When enabled, WealthLab shifts the synchronized values by one master bar where necessary to prevent a completed higher-scale value from becoming available before that period has actually completed. The default value is false. TimeSeriesSynchronizer accounts for differences between the source and master scales. When synchronizing daily-or-higher data to intraday data, WealthLab aligns the source DateTimes with the appropriate market session before performing the synchronization. It also examines the final master bar to determine whether the current source period has actually completed. If the period is still incomplete, the previous completed source value is retained rather than exposing the current incomplete-period value. The returned TimeSeries retains the Description of the source TimeSeries.
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthLab { public class MyStrategy : UserStrategyBase { private TimeSeries _weeklyRSI; public override void Initialize(BarHistory bars) { //compress closing prices to weekly scale TimeSeries weeklyClose = TimeSeriesCompressor.ToWeekly(bars.Close); //calculate RSI using weekly data RSI rsiWeekly = RSI.Series(weeklyClose, 4); //synchronize the weekly indicator back to the chart scale _weeklyRSI = TimeSeriesSynchronizer.Synchronize(rsiWeekly, bars); PlotTimeSeries( _weeklyRSI, "RSI(4-week)", "RSI", WLColor.Blue); } public override void Execute(BarHistory bars, int idx) { } } }
Synchronizes source with master similarly to Synchronize, but does not perform the final-bar scale completion check. Normally, Synchronize protects against exposing a higher-scale value before its period is complete. For example, when synchronizing a weekly series to daily data during the middle of the current week, Synchronize can retain the previous completed weekly value on the latest bar. SynchronizeMidWeek disables this final-period check, allowing the current source value to be synchronized even when the current higher-scale period has not yet completed. This is useful when the source TimeSeries intentionally contains a valid value for an incomplete current period and you want that value available immediately. Unlike Synchronize, SynchronizeMidWeek does not provide parameters for pre-filling NaNs, MarketDetails, or scale-up shifting. The returned TimeSeries retains the Description of the source TimeSeries.