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

BarHistorySynchronizer is a static utility class containing the Synchronize method, which synchronizes a BarHistory to the DateTimes of another TimeSeriesBase. The master can be either a TimeSeries or another BarHistory because both derive from TimeSeriesBase. This is useful when data has been compressed or otherwise exists at a different scale than the Strategy or chart. For example, you can compress daily data to weekly bars and then synchronize the weekly BarHistory back to the original daily BarHistory for plotting. When the source and master use different markets, BarHistorySynchronizer also accounts for market time-zone differences for intraday data.

Static Methods
Synchronize
public static BarHistory Synchronize(BarHistory source, TimeSeriesBase master)

Synchronizes source to the DateTimes of master and returns a new BarHistory whose Scale matches the scale determined from master. The synchronized BarHistory contains one bar for each DateTime in the master series. Values from the source BarHistory are carried forward as appropriate until the next source bar becomes available. If either source or master is null, returns source. If either series contains no data, returns source. The method also synchronizes NamedSeries registered with the source BarHistory. The resulting BarHistory retains the source BarHistory's MetaTag. When synchronizing a Daily or more compressed source BarHistory to an intraday master, WealthLab aligns the source bars with the appropriate market session before making the values available. This prevents a completed Daily, Weekly, or other compressed bar from becoming available prematurely during the intraday session. Markets whose sessions trade through midnight are handled using their configured market open and close times. For intraday synchronization between BarHistory instances associated with different Markets, source DateTimes are converted from the source Market's time zone to the master Market's time zone before synchronization.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using System.Drawing;
namespace WealthLab
{
    public class MyStrategy1 : UserStrategyBase
    {
        public override void Initialize(BarHistory bars)
        {
            //get weekly data
            BarHistory weekly = BarHistoryCompressor.ToWeekly(bars);
            //synchronize it back to the original daily scale
            BarHistory weeklySynched = BarHistorySynchronizer.Synchronize(weekly, bars);
            //plot the weekly bars on top of the daily bars
            PlotBarHistory(weeklySynched, "Price", WLColor.FromArgb(62, 0, 0, 0));
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}