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;
public override void Initialize()
{
SetStartDate(2013, 1, 1);
SetEndDate(DateTime.Now.Date.AddDays(-1));
SetCash(25000);
AddSecurity(SecurityType.Equity, "SPY", 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);
}
/// <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, TradeBar consolidated)
{
_spyDaily = consolidated;
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);
}
}
}
}