If I make a call like prices = history(2, '1d', 'close_price'). I get the 2 prices I want. Is there another call I can make to find out the date like 11/3/2014 and 11/4/2014 to go along with the 2 prices ?
thanks in advance
If I make a call like prices = history(2, '1d', 'close_price'). I get the 2 prices I want. Is there another call I can make to find out the date like 11/3/2014 and 11/4/2014 to go along with the 2 prices ?
thanks in advance
history returns a DatetimeIndex, you can use prices.index to get access the dates. prices.index[-1] will always be the current bar's datetime, and in your example, prices.index[0] will be the previous market day's datetime. If you only care about the dates, you can use prices.index[n].date() to extract the date object out of the datetime. Just a note, all the datetimes are in UTC. Hope this helps.
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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.
oh yeah, that works.. Is this in some API doc (I think I checked but did not see) or this is some simple python sytax that someone would know if they know python ? (It is my first time using Python, I only used C++, Java, and Perl before). I am just googling as I go along
It's Pandas data structure. There is a link to Pandas Dataframe documentation in the history API description: https://www.quantopian.com/help#api-history
This one: http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe
I have looked at the dataframe docs Ed referred me to and it make sense to me. What sort of data structure do we have stored in this dataframe? I can tell that we have the price series and sort of a date index as Ed and Joe pointed out. In the dataframe doc, the doc shows what the data structures have by printing out the data. Is there something like that I can do just to see what sort of data extraction is possible ?
I am asking all this because I have solved the lookback problem I have on date but I can't find an answer for my look forward problem. Say today is 10/31/2014, I need to know today is the last (trading) day of the month. I suppose I can use datetime to figure that but I will have to know all the exchange holidays so I won't miscount my dates... if I can just look forward one day and see that tomorrow is November, then I know today is the last trading day.
Would this be of any help? http://pandas.pydata.org/pandas-docs/stable/dsintro.html#indexing-selection
Here is example usage of the APIs mentoined there:
def initialize(context):
context.stock = symbol('AAPL')
def handle_data(context, data):
dframe = history(100, '1d', 'price')[context.stock]
for i, date in enumerate(dframe.index):
if i:
log.debug("%d %s %.2f %.2f" % (i, date, dframe.loc[date], dframe.iloc[i-1]))
if I can just look forward one day and see that tomorrow is November, then I know today is the last trading day.
I didn't quite get your logic here. Even if tomorrow is not November it could be possible that today is the last trading day because tomorrow could be public holiday or week end.
This is how I'd code trading on the last trading day of the month:
from datetime import timedelta
from zipline.utils import tradingcalendar as tcal
def initialize(context):
context.stock = symbol('AAPL')
def last_day_of_month(date):
if date.month == 12:
return date.replace(day=31)
return date.replace(month=date.month+1, day=1) - timedelta(days=1)
def handle_data(context, data):
today = get_datetime().date()
trading_days = tcal.get_trading_days(tcal.canonicalize_datetime(today.replace(day=1)),
tcal.canonicalize_datetime(last_day_of_month(today)))
if today == trading_days[-1].date():
log.debug("last trading day")
# create your orders here
I think I am ok with all the syntax, I just don't know what's inside the dataframe. Let me use your sample to print some stuff out and see if I get it or not. That's probably enough for me to figure out. Thanks
As for the trading day of the month, I look at your zipline.utils import tradingcalendar. It seems like you actually coded the holiday rules and all... BTW, how do I find out what kind of utilities lib we can use in quantopian ? Looks like there are a bunch of them but are we supposed to use them or how do I tell which one I have access to and which one is under development ? (Did I miss some FAQ?)
As for my logic in the code. I was trying to do trading day of the month and it works for me. If a given date is a holiday, it won't even show up in the price series so I can just count the trading day of the month by going back to the previous month. If the month changes, I stop.
My problem is to be able to count remaining trading days of the month. If I can't go forward, I won't be able to tell if a new month has started or not
As for the trading day of the month, I look at your zipline.utils import tradingcalendar. It seems like you actually coded the holiday rules and all...
It's not me, it's zipline developers :)
BTW, how do I find out what kind of utilities lib we can use in quantopian ?
https://www.quantopian.com/help#ide-module-import
As for my logic in the code. I was trying to do trading day of the month and it works for me. If a given date is a holiday, it won't even show up in the price series so I can just count the trading day of the month by going back to the previous month. If the month changes, I stop.
You can't go forward in the real algo. You'll be given today's market data and you'll not be able to look forward. You can do it for historical data returned by history(), but you can't trade in the past.