Consolidators are used to combine data together from finer resolutions into larger ones. This can be useful for indicators with specific data requirements or to perform long term analysis in conjunction with short term signals.

Consolidators should be constructed and setup in your Initialize() method; this ensures they are only initialized once. There are three key steps to create and register a consolidator:

  1. Create the consolidator object.
  2. Bind an event handler to handle the new bars.
  3. Register it with the subscription manager to start receiving data.
public class ConsolidatorDemoAlgorithm : QCAlgorithm
{
	public override void Initialize() 
	{
		// backtest parameters
		SetStartDate(2016, 1, 1);         
		SetEndDate(DateTime.Now);
		
		// cash allocation
		SetCash(25000);
		
		//assets or universe selection
		AddEquity("SPY", Resolution.Minute);
		
		//create a consolidator object; for tradebars; for a timespan of 30 minutes
		var thirtyMinutes = new TradeBarConsolidator(TimeSpan.FromMinutes(30));

		//bind event handler to data consolidated event.
		thirtyMinutes.DataConsolidated += OnHalfHour;
		
		//register the consolidator for data.
		SubscriptionManager.AddConsolidator("SPY", thirtyMinutes);
	}
	
	//event handler for data!
	public void OnHalfHour(object sender, TradeBar bar) {
		Debug(Time.ToString("u") + " " + bar);
	}
	
	public override void OnData(Slice data) 
	{ }
}

The LEAN API also has other consolidator types to handle working with Ticks and RenkoBars:

// From tick data sources            
var tickConsolidator = new TickConsolidator(TimeSpan.FromMinutes(30)); 

//from renko bars
var renkoConsolidator = new RenkoConsolidator(TimeSpan.FromMinutes(30)); 

There are two key points to remember:

  1. Request a smaller resolution than what you want to produce.
  2. In backtesting we only know the bar is ready on the next data point; so it may appear like daily bars are triggered at odd times. In live trading they are scanned to be triggered every second at a minimum.

The raw data of QuantConnect is provided in Tick, Minute, Second, Hour or Daily bars. Using these building blocks you can combine data together to get any other resolution of data required.

 

Sign In to comment in this post.