I'm trying to get 3 days of history for all the subscribed assets using the 'History(3)' method in Python. I was expecting a Dataframe in return but got a class 'QuantConnect.Util.1, Culture=neutral, PublicKeyToken=null' instead. In the research environment this indeed does return a dataframe, however in an algorithm it seems to not. What do I need to do to ensure a dataframe is returned?
My test code is simple.
data_df = context.History(3)
context.Log("history is: {}".format(type(data_df)))
The logs show.
history is: <class 'QuantConnect.Util.1, Culture=neutral, PublicKeyToken=null]]'>
The problem is when I try to do something like
my_closes = data_df.close
I get an error.
Python.Runtime.PythonException:
AttributeError : '1, Culture=neutral, PublicKeyToken=null]]'
object has no attribute 'close'
That code should work and return the 'close' column from the dataframe. This works in the research environment. Is there something I need to maybe import?
LukeI
Two things I can think of just looking at the documentation,
# Get history for all tickers we're subscribed to, at their native resolution slices = self.History(timedelta(7)) # Get history for all tickers we're subscribed to, at a specific resolution slices = self.History(timedelta(7), Resolution.Minute)
Do you need to have timedelta(3) instead of just 3?
Also, How many securities do you have specified? If it's just one then I don't think it returns a dataframe but I could be wrong.
Jing Wu
Hi Dan, you can access the history data as follows
# request 4-day history for AAPL
history = self.History("AAPL", 4, Resolution.Daily)
# get open, high, low, close (history is pandas.DataFrame)
close = history["close"]
If you have multiple symbols
# list of symbols
symbols = ["AAPL", "GOOG", "IBM"]
# request 4-day history for three symbols
history = self.History(symbols, 4, Resolution.Daily)
# get close, open, high, low
aapl_close = history.loc["AAPL"]["close"]
goog_high = history.loc["AAPL"]["high"]
Here goog_high and aapl_close are pandas Series indexed by date
Dan Whitnable
Thank you Lukel and Jingw,
It seems like the plain 'History(3)' doesn't work but 'History(python_list, 3)' does. Where 'python_list' can be a series or a list. This worked.
data_df = context.History(context.my_universe, 3)
context.Log("history is: {}".format(type(data_df)))
and returns
history is: <class 'pandas.core.frame.DataFrame'>
in this case 'context.my_universe' is a list of symbols. The frustrating thing is this format (using a list), while it works in an algorithm, doesn't work in a notebook.
BTW this still returns a DataFrame even if the list has a length of 1. However, it doesn't like an empty list so check the length before using.
Alexandre Catarino
I didn't understand this part: "The frustrating thing is this format (using a list), while it works in an algorithm, doesn't work in a notebook.". Is it the other way around?
In the algorithms, we use the list to tell the API that we want to use the method overload that returns a pandas.DataFrame. If not, it will return a list of Slice, TradeBar or QuoteBar which is common to C# algorithms. In the research, we are only dealing with python, so we kept the simpler overloads (those which don't need to specify the symbols, it assumes all the subscribed universe).
Dan Whitnable
It's peculiar (and as I mentioned frustrating). The basic form of the 'History' method (without explicitly specifying the assets and getting history for all the subscribed assets) works in the research environment. It does NOT work in an algorithm.
However, the list form of the method (specifying a list of symbols) does NOT work in the research environment. It does work however in an algorithm.
The reason I said it was frustrating is I like to debug my code in the research environment (especially since the IDE doesn't have a debugger). With the code not being transportable between environments it makes this approach difficult.
See the attached notebook showing this behavior.
Alfred
Hi
Is it possible to remove equity that i have previously subscribed as i would like to change the subscription list on runtime.?
Dammy Olipede
I'm new here. I am trying to develop a filter for stocks that meet certain criteria using universe. One criterion is for the stock to have gapped up/down more than +/-2% in the pre-market. I am having trouble with the accessing the previous day close for an implicitly defined stock symbol. I intend to use the previous day high to calculate the gap% e.g
if ( (abs (close-price) /close ) * 100) >= 2 :
I have tried using both the 'rolling window' and the 'history data'.
Also, I can't seem to know where to find the data I log. The .txt file I download doesn't contain much. This is making debugging very hard.
Dan Whitnable
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!