Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Prior day's close

i searched the posts and i've looked through the api for the answer to this. so it isn't lack of effort that is causing me to ask about this. i'm new and not python experienced.

i'd like to access the prior day's closing price. it is an input i'd like to use for a minute price strategy. how do i access it?

i've thought about mavg(1), but my understanding is that it is based on the price of the minute bar from 24 hours ago (e.g., if it is 10:01a, then it is the price from 10:01a prior trading day). is this right?

9 responses

Hi - in handle_data, you would need to record the value into context yourself. If you use batch_transform, you can work with a trailing window of data, so you can index back as far as the window will let you.

Disclaimer

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.

thx. i'll see if i can implement that.

will that batch_transform give me eod close prices? or will it give me the minute bar from the prior trading day? that wasn't clear from the docs. actually the api docs seemed to indicate that it would give me the minute bar from the prior trading day (e.g., if it is 10:01, then the 10:01 minute bar from the prior trading day). or does the prices data panel give me something for which i can get the 4p price. sorry if this is something that is trivial.

window_length and refresh_period are expressed in trading days. We fill the window with 1 row for each bar sent to the batch transform. The bar size is set in your UI config for he backtest. If you run with the default of daily, you will have one daily bar per trading day in the window. If you use minutely, you will have 390 minute bars per day in the window. The minute bars will be marked 9:31 through 4:00pm for full trading days.

thanks,
fawce

i think that i have taken care of getting the end of day price via the batch transform using some code that i found and testing things out. as part of that, i checked the prices that i got from quantopian against yahoo prices. they were close, but did not match. shouldn't closing prices match for equities? please help.

Hi,

Which security were you checking? We send fully adjusted prices, taking into account the splits and mergers that occur during the test period. The free services don't adjust. We posted an example of a particularly active stock here: https://www.quantopian.com/posts/smooth-data-for-iac

thanks,
fawce

was looking at gld and ibm. they were close, but often differed by few pennies. i don't believe this to be an adjustment issue; gld in particular does not pay a dividend nor any corp actions during the period.

i'm guessing that there is a difference in determining closing price. perhaps the exchange does not use the last price but uses some sort of moc auction close or a volume weighted average. another possibility is that the closes are given by a particular exchange, say nyse, but you use the last traded price that might have printed on an ecn.

i'm using this data for ibm http://finance.yahoo.com/q/hp?s=ibm&a=00&b=1&c=2012&d=00&e=21&f=2012&g=d
and checking against the backtest data for the same period.

thoughts?

i've been banging my head against this for a while. i think that i am too python ignorant. my code is included at the end.
i have two issues -- first, how do i get the last trading bar of the day? it is not clear to me how to use the array to access it. i've tried data['price'][390] but that fails. i know that the price that i use in the main routine (firstPrice using [0] is not getting the eod of price).

on a narrower note, it fails in runtime in the batch_transform stating that no item named security 3766 exists.i know it is related to this line because it worked before i added it.

import datetime  
import pytz  
def initialize(context):  
    context.stocks = [sid(3766),sid(28368)]  
    context.IBM=sid(3766)  
    context.SLV = sid(28368)  
    utc = pytz.timezone('UTC')  
    context.d = datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=utc)

def handle_data(context, data):  
    prices = endpoints(data, context.stocks)  
    if prices is not None:  
        firstPrice = prices[context.IBM][0]  
        #curDate = data[context.IBM].datetime.replace(hour=21,minute=0,second=0,microsecond=0)  
        log.info('time : {t}, cur price {pc}, close price {p}'.format(t=data[context.IBM].datetime,pc=data[context.IBM].price,p=firstPrice))  
        #if data[context.GLD].datetime==curDate:  
         #   log.info('hi')  
    #log.info('p3: {d}  {p}'.format(d=data[sid(3766)].datetime,p=data[sid(3766)].price))  
    #log.info('end-of-day')  
    #cp = data[sid(3766)].close_price  
    #log.info('p3: {p}'.format(p=cp))  
    #ma = data[sid(3766)].mavg(1)  
    #log.info('p4: {p}'.format(p=ma))  


@batch_transform(refresh_period=1, window_length=1)  
def endpoints(data, stocks):  
    prices = data['price']  
    log.info('p1: {p}  time {t}'.format(p=prices,t=data[sid(3766)].datetime))  
    return prices  

Hi,

Sorry for the delay in replying, we've been really heads down cranking on lots of new features.

I think you want something along these lines in the batch transform (I tried to comment things to explain a bit more):

@batch_transform(refresh_period=1, window_length=1)  
def endpoints(datapanel):  
    # datapanel is a pandas datapanel, which has three dimensions:  
    # items - keys are strings - price, volume, etc. The value is a pandas dataframe.  
    # each dataframe has two dimensions - rows indexed by datetime, cols by sid.  
    prices = datapanel['price']  
    # prices is a dataframe with a DatetimeIndex for the rows. All the data columns share  
    # the one index. To get the last price for IBM, we select that column from the dataframe  
    # and index from the end of the column. To get the last date, we pull the last value from  
    # the dataframe's index.  
    log.info('p1: {p}  time {t}'.format(p=prices[sid(3766)][-1],t=prices[sid(3766)].index[-1]))  
    return prices  

Does this clear things up a bit?

thanks,
fawce