Back

Multiple Consolidators

How do you get TradeBars for multiple equities on one consolidator? I have attached a project below to show what I mean.

Update Backtest








namespace QuantConnect
{
/*
* QuantConnect University: Consolidators - Creating custom timespan events.
*
* Consolidators are tools inside of LEAN to merge data into single bars. They are
* very flexible and can generate events which trigger from any timespan.
*/
public class ConsolidatorAlgorithm : QCAlgorithm
{
TradeBar _spyDaily;
TradeBar _gldDaily;

public override void Initialize()
{
SetStartDate(2013, 1, 1);
SetEndDate(DateTime.Now.Date.AddDays(-1));
SetCash(25000);

AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
AddSecurity(SecurityType.Equity, "GLD", Resolution.Minute);

// define our daily trade bar consolidator. we can access the daily bar
// from the DataConsolidated events
var dailyConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(1));

// attach our event handler. the event handler is a function that will be called each time we produce
// a new consolidated piece of data.
dailyConsolidator.DataConsolidated += OnDataDaily;

// this call adds our daily consolidator to the manager to receive updates from the engine
SubscriptionManager.AddConsolidator("SPY", dailyConsolidator);
SubscriptionManager.AddConsolidator("GLD", dailyConsolidator);
}


/// <summary>
/// This is our event handler for our daily trade bar defined above in Initialize(). So each time the consolidator
/// produces a new daily bar, this function will be called automatically. The 'sender' parameter will be the
/// instance of the IDataConsolidator that invoked the event, but you'll almost never need that!
/// </summary>
private void OnDataDaily(object sender, TradeBars consolidated)
{
_spyDaily = consolidated["SPY"];
_gldDaily = consolidated["GLD"];
Log(consolidated.Time.ToString("o") + " >> SPY >> LONG >> 100 >> " + Portfolio["SPY"].Quantity);
}


//Traditional 1 minute events here:
public void OnData(TradeBars data)
{
if (!Portfolio.HoldStock)
{
Order("SPY", 100);
}
}
}
}

 

0

Welcome Bradley; in general you wouldn't do that -- consolidators are cheap and its fine to make one per asset. Consolidating them together like that and you'll get some pretty random looking bars.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


But shouldn't the TradeBars dictionary contain 2 separate TradeBar objects, one for each equity? I am trying to get 2 separate TradeBar objects to be called in the OnDataDaily event handler and to have them have the same date index.

0

Yes, but TradeBars are created by an intermediary operation where the algorithm packs all the TradeBar objects together. When we use consolidators, we don't have that operation so all consolidated bars arrive at the event handler as soon as they are ready.
In the example below, consolidated bars from SPY and GLD arrive at the same event handler, OnDataDaily, but not at the same time. We can check whether both are from the same date and if so continue to the trading logics.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Wow, very useful. How does the consolidator decide which TradeBar is consolidated first? Is it alphabetical order? In this example it chooses to consolidate GLD before SPY.

0

It is safer to assume that it is not deterministic (it is random), since in live trading each bar will be consolidated when a tick that triggers the event arrives and we can observe GLD before SPY or vice-versa.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed