Hi everyone,
I was wondering if anyone could help me out with the following issue. I know it might be something very basic, but I have no idea how to do it:
When using the Research Notebook, I just have to run some basic code like this to get a nice DataFrame with the indicator values:
apo = AbsolutePriceOscillator(10,20,MovingAverageType.Simple)
apo_df = qb.Indicator(apo,spy,252,Resolution.Daily)
apo_df.head()
And I get something like this:
I could use this DataFrame to extract the signal, modify it, concatenate it with some other data, and maybe train an ML model.
How do I do the same in the QC Algorithm?
The method self.Indicator does not exist. I tried expanding the indicator's Window attribute to a large number (say 100) and I can access each value individually, by doing something like:
self.apo[90]
Or somethig along those lines of:
apo_values = []
for value in self.apo:
apo.values.append(value)
But, ¿is there a way to just get a nice DataFrame from the start?
I'm thinking of using one or more indicators as features in an ML algorithm, but I'm basically stuck in this first step.
Thank you very much for any guidance!
Mia Alissi
In the QuantConnect algorithm environment, you can't directly get a DataFrame from an indicator like you can in the Research environment. However, you can create a RollingWindow object to store historical indicator data, and then convert that to a DataFrame if needed.
Here's an example of how you can do this:
In this example, the
OnEndOfAlgorithm
method is used to convert the RollingWindow to a DataFrame when the algorithm finishes. You can adjust this to suit your needs.Remember that the
self.Indicator
method is a helper method in the Research environment for plotting purposes. In the algorithm environment, you need to use the specific indicator constructor for each indicator. You can find more information about this in the indicator reference table in the documentation.Also, note that the RollingWindow stores the most recent data at the front of the window (index 0), and older data is at higher indices. So
self.window[0]
gives the most recent data, andself.window[self.window.Count - 1]
gives the oldest data in the window.Sources:
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.
César Padilla
Hi Mia Alissi ,
This answer helped me a lot. I was wondering if you could help me with something that came up after I tried implementing what you explaied above:
I (think) I got the indicator rolling window going on nicely now:
However, what I am trying to do now is to concatenate the history dataframe of self.spy with the new dataframe I created with your help (apo). I tried creating a new DataFrame called df, but I noticed it is not merging it correctly.
Using the Debug statement I can clearly see that the index for the history dataframe is a DateTime Index while the index for the apo dataframe is a Range Index. I'm guessing this is what's not working.
Any tips on how I can correctly concatenate and get both the OHLC data and the indicator data in the same DataFrame?
Mia Alissi
The issue you're encountering is indeed due to the mismatch in the indices of the two dataframes. The
history
dataframe has a DateTime index, while theapo
dataframe has a Range index.To solve this, you can set the 'Time' column of the
apo
dataframe as its index before concatenating. Here's how you can do it:This will set the 'Time' column as the index of the
apo
dataframe, which should allow it to concatenate correctly with thehistory
dataframe.Remember that
pd.concat
aligns dataframes along their indices. If the indices don't match, you'll get NaN values in the rows where it couldn't find a match.Sources:
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.
César Padilla
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!