Back

Custom data for indicators

Someone please post an example of how to feed indicators, let's say moving average with custom data as I have started to get into new engine and it seems it will require a lot of time to understand how architecture works exactly. Examples are faster, haha :)
Update Backtest








Hey Tadas! Here's something I threw together for you as an example. I'll also give a brief overview of all the moving parts here.

The indicators were designed to receive automatic updates from the Lean engine. This usually happens through registration like the following line:

RegisterIndicator(symbol, indicator, resolution);

Under the hood what we're doing is creating an object whose job it is to consolidate your data, and then feed it to your indicator on the requested resolution. This is easy for us to do when your data is of type TradeBar, which is the normal type for data supplied by the Lean Engine.

Things get a little more complicated with custom data. With custom data we don't know anything about the type, so you're forced to provide us with the object that will do the consolidation. This object is called a data consolidator and implements the IDataConsolidator interface. When we call the RegisterIndicator function, what we're really doing is subscribing the consolidator for data and telling the consolidator to update our indicator for us when he has a new piece of consolidated data. This is done through C# events. Check out the Data Consolidation example algorithm for more on what consolidators can do.

So what I've done in this example is come up with a new consolidator that will work on types with the following properties defined: Time, Open, High, Low, Close, Volume (OHLCV), no matter what type it is. I use dynamics to get the OHLCV data out of your custom data object (such as Quandl) and into a TradeBar, which can then be fed to the standard TradeBarConsolidator.

If you have any questions or comments on how all this works, please don't hesitate to ask!

Let me know if this helps your problem!
2


Nice chart- Interesting to see the convergence of the high-lows during certain periods.

I think most generic data is just a time-value. Maybe if it doesn't inherit from TradeBar we can just consolidate the Values over time? I'll add this to the git issues list - as a proposed feature.
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.


Michael,

1) "5 period EMA, where the period is 10 days"? Feeding 10 day TradeBars into ema period 5, I am correct here?
2) "YAHOO/INDEX_SPY" - Can't get what symbol data it feeds into those EMAs? I.e. SPY, but what the kigic of this long string?
3) OnData(Object data) - this gives custom data as I understand. Probably I can overload with additional Tradebars data to get suual Daily values or not?
4) Why this is required? Plot(SPY_QuandlCode, emaClose.Name, data.Value); As Plot(SPY_QuandlCode, emaClose, emaLow, emaHigh); has same name, different values, plot is for latest.

Well, that's it for now :)
0

1. Yes, and if you check out the plot you'll see it step since it gets updated every 10 trading days. I chose this since I only had daily data, but the same would apply for any resolution.

2. The "YAHOO/INDEX_SPY" is a Quandl code. There's a section here about the codes. Also, if you check out the "Quandl Importer" project in the university, that may give you more experience importing custom data.

3. Since we're using custom data we use a custom data handler, OnData(MyCustomDataType data). In this project I didn't subscribe for regular data through the AddSecurity method. If I had, then I would also have an OnData(TradeBars data) method.

4. Is your question about the different method signatures?

The first method Plot(SPY_QuandlCode, emaClose.Name, data.Value) is the usual Plot method which takes a chart name, a series name,and a value. The second is an overload, which accepts a chart name and indicators. Here's the line in git.

Is your question about these lines?

// without this line the plot doesn't show up in the ui
Plot(SPY_QuandlCode, emaClose.Name, data.Value);


I believe this may be a bug, I haven't had time to investigate it. Without this call to Plot here I wasn't getting any plots, but with it, the plots worked.
1

Cool, it rises a question, can I use TradeBars (regular values) in some formula and then feed them as custom data into indicators?
0

Tadas, you'll have to explain a little further. I'm not sure I fully understand what you're trying to do.

Possibly be a little more specific as to what you're trying to accomplish and maybe I could throw an example together for you.
0

Also, I put a change in that will allow the short code indicator functions (EMA, SMA, ect...) to work with custom data. It will use the Value property of your custom data as input to the indicator.

Also, you may want to check out this other thread.
0

OK, let's say I need to get data from QC Tradebars, then manipulate with it and then give into indicators. As example: EMA(5) OF(Conslidated bars(10) Tradebars Math.Abs(data[symbol].Close-data[symbol].Open)).
0

Everything is possible @Tadas :) For advanced stuff you might need to put the building blocks together yourself though. Recommend checking out the documentation and underlying code for the indicators - TLDR; call the ComputeNextValue() function with your value.
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.


I just submitted a change to allow the RegisterIndicator function for 1D indicators to accept a selector. This will allow you to define an indicator that works on the difference of close to open using a line like the following:


var ema = new ExponentialMovingAverage(5);
RegisterIndicator(symbol, ema, Resolution.Daily, x =>
{
var btc = (Bitcoin)x;
return Math.Abs(btc.Close - btc.Open);
});


This code will produce the exponential moving average of the absolute difference between the close and the open.
1

Looks cool and flexible now, thank you, Michael, will play with it when web IDE will get this update.
0

Well, actually, can't get this to work. Previous version like sma.AddSample(any_custom_data) was easier to use. So I will see inside architecture.
0

You can still use a similar pattern. See the attached sample project where I manually pump data into the indicator.



// defined in Initialize()
sma = new SimpleMovingAverage(10);

// used in OnData
sma.update(tradeBar.Time, tradeBar.Value);
0


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