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

BarHistoryCompressor is a static utility class containing methods that compress a BarHistory from a more granular scale to a less granular scale. For example, you can use ToWeekly to compress a daily BarHistory into weekly bars. Compression preserves the first Open, highest High, lowest Low, last Close, and sums Volume for each resulting bar. Named TimeSeries registered with the source BarHistory are also carried into the compressed BarHistory. If you want to use data calculated from a compressed BarHistory on a chart or Strategy running at the original scale, synchronize the resulting TimeSeries back to the original scale using TimeSeriesSynchronizer.

Static Methods
ToDaily
public static BarHistory ToDaily(BarHistory bh)

Compresses bh to Daily bars and returns the resulting BarHistory. If the source is already Daily, returns the source BarHistory. For intraday markets whose sessions trade through midnight, WealthLab uses the Market session boundaries to determine daily bars. An incomplete final intraday session is excluded from the result.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;
namespace WealthScriptTest
{
    public class BarHistoryToDailyScaleExample : UserStrategyBase
    {
        TimeSeries _dailyAvgInBaseScale;
        TimeSeries _dailyLows;
        public override void Initialize(BarHistory bars)
        {
            if (!bars.IsIntraday)
                throw new InvalidOperationException("Example is intended for an intraday BarHistory");
            BarHistory dayBar = BarHistoryCompressor.ToDaily(bars);
            IndicatorBase daySmaOfLows = SMA.Series(dayBar.Low, 10);
            _dailyAvgInBaseScale = TimeSeriesSynchronizer.Synchronize(daySmaOfLows, bars.Close);
            PlotTimeSeries(_dailyAvgInBaseScale, "SMA(10) of Daily Lows", "Price", WLColor.Blue, PlotStyle.Dots);
            _dailyLows = TimeSeriesSynchronizer.Synchronize(dayBar.Low, bars.Close);
            PlotTimeSeries(_dailyLows, "Daily Low", "Price", WLColor.Red, PlotStyle.Dots);
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

ToHour
public static BarHistory ToHour(BarHistory bhMinute, int interval)

Compresses bhMinute to an interval-hour scale and returns the resulting BarHistory. Internally, WealthLab compresses the data to interval * 60 minute bars and then assigns the resulting HistoryScale to the corresponding Hour scale.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;
namespace WealthScriptTest
{
    public class BarHistoryToHourlyExample : UserStrategyBase
    {
        TimeSeries _rsi2hour;
        public override void Initialize(BarHistory bars)
        {
            if (!bars.IsIntraday)
                throw new InvalidOperationException("Example is intended for an intraday BarHistory");
            BarHistory h2Bars = BarHistoryCompressor.ToHour(bars, 2);
            _rsi2hour = RSI.Series(h2Bars.Low, 14);
            _rsi2hour = TimeSeriesSynchronizer.Synchronize(_rsi2hour, bars.Close);
            PlotTimeSeries(_rsi2hour, "RSI(14) of 2 hour bars", "RSIPane", WLColor.Blue, PlotStyle.Line);
        }
        public override void Execute(BarHistory bars, int idx)
        {
            if (!HasOpenPosition(bars, PositionType.Long))
            {
                if (_rsi2hour.CrossesUnder(30, idx))
                    PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
            }
            else
            {
                if (_rsi2hour.CrossesOver(50, idx))
                    PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
            }
        }
    }
}

ToMinute
public static BarHistory ToMinute(BarHistory bh, int interval, bool includePartial = false, bool avoidPrePostCheck = false)

Compresses an intraday BarHistory to an interval-minute scale and returns the resulting BarHistory. The source can contain Minute, Second, or Tick data. When the source is Minute data, interval must be greater than or equal to the source interval and must be an exact multiple of it. Set includePartial to true to include the final incomplete compressed bar. The avoidPrePostCheck parameter affects how session alignment is handled when pre/post-market data is present. If bh is null, returns null. If the requested scale is the same as the source scale, returns the source BarHistory. If the source frequency cannot be compressed to Minute bars, returns null.


ToMonthly
public static BarHistory ToMonthly(BarHistory bh)

Compresses bh to Monthly bars and returns the resulting BarHistory. If the source is already Monthly, returns the source BarHistory.


ToNDay
public static BarHistory ToNDay(BarHistory bh, int interval)

Compresses bh into interval-day bars using a Frequency.NDays HistoryScale. N-Day intervals reset at the beginning of each calendar month, so a bar will also end when the month changes even if it has not yet accumulated the requested number of trading days. Intraday source data is first compressed to Daily bars. Returns null if the source frequency is already more compressed than Daily.


ToQuarterly
public static BarHistory ToQuarterly(BarHistory bh)

Compresses bh to Quarterly bars and returns the resulting BarHistory. If the source is already Quarterly, returns the source BarHistory.


ToScale
public static BarHistory ToScale(BarHistory bh, HistoryScale scale)

Compresses bh to the supplied HistoryScale. WealthLab dispatches the request to the appropriate compression method based on the HistoryScale's Frequency. Supported frequencies include Daily, Weekly, Monthly, Quarterly, Yearly, Hour, Minute, Second, Tick, NDays, and WeeklyStartDay. If bh already has the requested scale, returns the source BarHistory. If bh contains no bars, returns an empty BarHistory with the requested Scale.


ToSecond
public static BarHistory ToSecond(BarHistory bh, int interval)

Compresses Tick or Second data to an interval-second scale and returns the resulting BarHistory. When compressing Second data, interval must be greater than or equal to the source interval and must be an exact multiple of it. If the requested scale is the same as the source scale, returns the source BarHistory. Returns null when the source cannot be compressed to the requested Second scale.


ToTick
public static BarHistory ToTick(BarHistory bh, int interval)

Compresses Tick data to a less granular Tick interval. The requested interval must be greater than or equal to the source Tick interval and must be an exact multiple of it. If the requested interval is the same as the source interval, returns the source BarHistory. Returns null when the source is not Tick data or cannot be compressed to the requested interval.


ToWeekly
public static BarHistory ToWeekly(BarHistory bh)

Compresses bh to Weekly bars and returns the resulting BarHistory. If the source is already Weekly, returns the source BarHistory. For markets configured to trade all seven days of the week, WealthLab creates weeks that begin on Monday, so the final bar of each week is Sunday's bar.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
namespace WealthLab
{
    public class MyStrategy1 : UserStrategyBase
    {
        public override void Initialize(BarHistory bars)
        {
            BarHistory weekly = BarHistoryCompressor.ToWeekly(bars);
            RSI rsiWeekly = new RSI(weekly.Close, 4);
            TimeSeries rsiWeeklySynched = TimeSeriesSynchronizer.Synchronize(rsiWeekly, bars);
            PlotTimeSeries(rsiWeeklySynched, "RSI(Weekly4)", "RSI", WLColor.Blue);
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

ToWeeklyStartDate
public static BarHistory ToWeeklyStartDate(BarHistory bh, DayOfWeek dow)

Compresses bh into weekly bars whose weeks begin on the specified dow. The resulting BarHistory uses a Frequency.WeeklyStartDay HistoryScale. If the source already uses the requested WeeklyStartDay scale, returns the source BarHistory.


ToYearly
public static BarHistory ToYearly(BarHistory bh)

Compresses bh to Yearly bars and returns the resulting BarHistory. If the source is already Yearly, returns the source BarHistory.