So there's a rumor going around that some don't like to add samples directly to their indicators. That's OK! I have a solution for you!! In this project I define a few helper classes and provide a pattern for creating auto updating indicators. The key component here is the IndicatorManager which acts something like a dictionary {symbol -> IndicatorCollection} where an IndicatorCollection is also like a dictionary {name -> IIndicator}. This allows us to access them easily.

In the Initialize() method we can write something like the following:

public override void Initialize()

{

SetStartDate(2014, 11, 01);

SetEndDate(2014, 12, 05);

// this creates an EMA and adds it to the IndicatorManager

// using defaults of 1 day update interval and using closing prices

EMA("SPY", 3);

// this creates an EMA and adds it to the IndicatorManager

// using a 2 day update interval and the low price, we also override the name

// of the indicator to deconflict with the other SPY EMA3

// an important note realized during testing, we may want a way to say '2 bars'

// since a 2 day ema as defined like this ends up with double points over weekends,

// imagine one sample is on a friday, well the next will be monday, but typically

// a 2 day span we would expect it more as '2 bars' or '2 trading days' not '2 calendar days'

EMA("SPY", 3, TimeSpan.FromDays(2), x => x.Low, "2day EMA3");

// we can even define indicators on computed values! Here we define an indicator on

// the average of OHLC data

EMA("SPY", 9, TimeSpan.FromDays(1), x => (x.Open + x.High + x.Low + x.Close)/4);

AddSecurity(SecurityType.Equity, "SPY");

}

Now you'll notice that we didn't even have to save the EMA3 of the SPY into a variable. Under the hood there is a partial class definition which is taking the generated EMA3 of the SPY and adding it to our IndicatorManager.

Now since we don't have access to the actual data pumping mechanisms that are external to QCAlgorithm (the 'thing' that calls OnData( ... )) we'll need to instruct the IndicatorManager to update with each piece of data we see. An important note here is that we've already defined the symbol, the interval, and the actual data source (Open/High/Low/Close/Volume or even computed!).

Author