If you add a custom data by AddData, can you get the value 1 day ago or the value 2 days ago in OnData?
def Initialize(self):
...
signal_subscription = self.AddData(SignalData, "SIGNALS", Resolution.Daily)
...
def OnData(self, data):
...
//retrieve previous data
Mia Alissi
To access historical data for custom data added via
AddData
in theOnData
method, you can use theself.History()
method. Here's how you can achieve this:Initialize the Custom Data: Ensure that your custom data is added using the
AddData
method in theInitialize
method.Access Historical Data: In the
OnData
method, use theself.History()
method to request historical data for the custom data symbol.Specify the Time Frame: Define the time frame for which you want to retrieve historical data, such as 1 day ago or 2 days ago.
Handle the DataFrame: The
self.History()
method returns a DataFrame, so handle it accordingly to extract the required historical values.Check for Data Availability: Ensure that the DataFrame is not empty before attempting to access specific data points.
Extract Specific Data Points: Use the DataFrame's indexing capabilities to access the data for specific days, such as 1 day ago or 2 days ago.
Here's an example implementation:
In this example,
self.History()
is used to request the last 3 days of historical data for the custom data symbol. The values from 1 and 2 days ago are accessed using the DataFrame'siloc
method. Make sure to adjust the column name'value'
to match the actual column name in your custom data.Sources: Using Data - QuantConnect.com, Custom Data - QuantConnect.com, Custom Data Type History Algorithm, Key Concepts - QuantConnect.com, Custom Data - QuantConnect.com
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.
Norris Codman
Thank you for the solution. However, the history often becomes empty although there seem to be enough data in the csv. What is the problem?
self.log(f"History data:\n{history}")
>>
 History data: Empty DataFrame Columns: [] Index: []
Louis Szeto
Hi Norris
We recommend using a RollingWindow to cache the data.
Best
Louis
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.
Norris Codman
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!