Search Framework:
BarHistoryCompressor
Namespace: WealthLab.Core
Parent:

BarHistoryCompressor is a static utility class containing methods that let you compress a BarHistory instance from one scale down to a more compressed scale. For example, you can call BarHistoryCompressor.ToWeekly to compress a daily BarHistory into a weekly one.

If you want to be able to plot a compressed BarHistory onto the chart, you would need to first expand it back to the original scale. You can use the BarHistorySynchronizer utility class to accomplish this.

Static Methods
ToDaily
public static BarHistory ToDaily(BarHistory source)

Compresses the source BarHistory to a daily scale, returning a new BarHistory instance.

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");
			}

			//create a 10-day average of the daily low prices
			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);

			//also plot the daily low prices
			_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 source, int interval)

Compresses the source BarHistory to an interval-hour hourly scale, returning a new BarHistory instance.

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");
			}

			//Compress to 2-hour bars
			BarHistory h2Bars = BarHistoryCompressor.ToHour(bars, 2);

			//Get a RSI(14) of the 2-hour low series
			_rsi2hour = RSI.Series(h2Bars.Low, 14);

			//synchronize the rsi
			_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))
			{
				//buy when the 2-hr rsi of the lows drops below 30
				if (_rsi2hour.CrossesUnder(30, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				//sell when the 2-hr rsi of the lows rises above 50
				if (_rsi2hour.CrossesOver(50, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}

		}
	}
}

ToMinute
public static BarHistory ToMinute(BarHistory source, int interval)

Compresses the source BarHistory to an interval-minute scale, returning a new BarHistory instance. The source should be an intraday BarHistory having higher granularity scale than the requested interval. For example, it makes sense to compress a source of 1 minute data to a 5 minute interval, but not the other way around.


ToMonthly
public static BarHistory ToMonthly(BarHistory source)

Compresses the source BarHistory to a monthly scale, returning a new BarHistory instance.


ToQuarterly
public static BarHistory ToQuarterly(BarHistory source)

Compresses the source BarHistory to a quarterly scale, returning a new BarHistory instance.


ToScale
public static BarHistory ToScale(BarHistory source, Frequency scale)

Compresses the source BarHistory to the specified scale, returning a new BarHistory instance.


ToWeekly
public static BarHistory ToWeekly(BarHistory source)

Compresses the source BarHistory to a weekly scale, returning a new BarHistory instance.
Remarks

  • For markets that trade 7 days of the week (e.g. Cryptos), the week closes on Sunday's bar and opens with Monday's bar.
Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthLab
{
	public class MyStrategy1 : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//get weekly data
			BarHistory weekly = BarHistoryCompressor.ToWeekly(bars);

			//get 4 week RSI
			RSI rsiWeekly = new RSI(weekly.Close, 4);

			//synchronize it back to the original (daily) scale
			TimeSeries rsiWeeklySynched = TimeSeriesSynchronizer.Synchronize(rsiWeekly, bars);

			//Plot the weekly RSI on the daily chart
			PlotTimeSeries(rsiWeeklySynched, "RSI(Weekly4)", "RSI", WLColor.Blue);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}

		//declare private variables below

	}
}

ToYearly
public static BarHistory ToYearly(BarHistory source)

Compresses the source BarHistory to a yearly scale, returning a new BarHistory instance.