Hello!
Quick quesiton on getting a single historical value (for example). Let's say I'm looking for the close of IBM at 2019-05-23 00:00:00. If I use the following code to create the history for this - how can I call that single index via the DataFrame (this is in py) to get this value. It may just be me not knowing much about DataFrames, but it seems like I can't call this via the index.
self.SetStartDate(2019, 5, 24)
self.SetCash(100000)
date1 = datetime.datetime(2019, 5, 23,0,0,0)
self.AddEquity("IBM", Resolution.Minute)
df = self.History(self.Symbol("IBM"), 2880, Resolution.Minute)
#what I thought would work but does not:
self.Log(df.loc[date1,'close'])
Any help would be appreciated! I'm sure I'm doing something silly here.
Best,
SC
Alexandre Catarino
Hi Stephen ,
The History request data frame is index by Symbol and datetime, if we want the series with all the columns of a given multi-index:
df1 = df.loc["IBM"].loc[date1]
or if we want the close price of a given datetime:
df2 = df.loc[(slice(None), date1),:].close
Stephen
Hi Alex!
Thank you, yet again, for one of your responses. But, this doesn't seem to do the trick for the close price. I get the following runtime:
During the algorithm initialization, the following exception has occurred: Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the 2019-05-23 00:00:00 key exist in the collection and/or that collection is not empty.
at Initialize in main.py:line 13
KeyError : Timestamp('2019-05-23 00:00:00')
But it seems like that timestamp should be within my dataframe from my historical call, so I'm confused still!
import datetime class HorizontalResistanceProcessor(QCAlgorithm): def Initialize(self): self.SetStartDate(2019, 5, 24) # Set Start Date self.SetCash(100000) # Set Strategy Cash date1 = datetime.datetime(2019, 5, 23,0,0,0) self.AddEquity("IBM", Resolution.Minute) df = self.History(self.Symbol("IBM"), 2880, Resolution.Minute) self.Log(date1) self.Log('-----------') df2 = df.loc[(slice(None), date1),:].close def OnData(self, data): '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here. Arguments: data: Slice object keyed by symbol containing the stock data ''' # if not self.Portfolio.Invested: # self.SetHoldings("SPY", 1)
Alexandre Catarino
Hi Stephen ,
The algorithm is trying to get a date/time that is not in the historical data.
Since it is a request for Equity data, we shouldn't expect data at 23:00 since the market is closed.
For all the date/time in a data frame, we can use:
index = df.index.get_level_values('time')
Stephen
Oh, this makes sense. I was actually looking to attain some pre-market values. Particuarly a 0900 value. Does the history call not have any pre-market available at all?
Alexandre Catarino
Hi Stephen ,
You can subscribe to data that includes extended market hours:
date1 = datetime.datetime(2019, 5, 23,9,0,0) symbol = self.AddEquity("IBM", Resolution.Minute, Market.USA, True, 1, True).Symbol df = self.History(symbol, 2880) df2 = df.loc[(slice(None), date1),:].close
df2:
symbol time IBM R735QTJ8XC9X 2019-05-23 09:00:00 132.358568
Stephen
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!