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!).
Tayler Allen
This code does not run when I cloned it. Does it need to be updated?
Alexandre Catarino
Most of the features in that algorithm were incorporated in the API.
Here is a simpler example that you can also find in the open-source project.
Michael H
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!