Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Earnings Notebook, days since earnings release

I created a notebook with companies that just released earnings, in particular I would like the date in the index to be the first day that the earnings news affected the market, aka asOfDate (say if the earnings were released yesterday after market hours, the asOfDate would be today). Factset provides such a date which I used in the notebook below, but I am having the following problem. I have a column that computes that number of days between today and since the asOfDate, called numDaysSinceRelease. But even when the numDaysSinceRelease is equal to zero, I still see a difference of 1 day between the index date and the as of date. Is there a way to make sure that if asOfDate is equal to 0, that this date equals the date in the date column in the index?

An example: the ticker 'CMD' has and asOfDate of: 2018-05-31 and numDaysSinceRelease = 0. But the index date shows June 1st. I would like CMD to be in the row for 5-31.

3 responses

A simple solution to my problem would be if I could change the day counter in such a way that it counts only the business days since an earnings release. Can anyone suggest a way to do that:. My approach below produces an error:

TypeError: Iterator operand 1 dtype could not be cast from dtype('<M8[ns]') to dtype('<M8[D]') according to the rule 'safe'

class DaysSinceRelease3(CustomFactor):  
    # Only getting the previous quarter's estimize surprise  
    inputs = [fe.Actuals.slice('EPS', 'qf', 0).asof_date.latest]  
    window_length = 1

    def compute(self, today, assets, out, input_1):  
        days =  today.tz_convert('US/Eastern') -  pd.to_datetime(input_1[-1, :]).tz_localize('US/Eastern')  
        #days =  today.tz_convert('US/Eastern') -  pd.to_datetime(input_1[-1, :]).tz_localize('US/Eastern')- timedelta(days=1)  
        #start = dt.date( today)  
        #end = dt.date( pd.to_datetime(input_1[-1, :]) )  
        start =  today  
        end = pd.to_datetime(input_1[-1, :])

        days = np.busday_count(start ,end)  
        out[:] = days.astype('timedelta64[D]')  
        #out[:] = days  

        #pipeline_output.index.set_levels(pipeline_output.index.levels[0]- timedelta(days=1),level=0, inplace=True)

Check out this other post https://www.quantopian.com/posts/count-the-number-of-business-days-since-earnings. There is a built in factor for business days since event. No need for a custom factor. The factor is BusinessDaysSincePreviousEvent (https://www.quantopian.com/docs/api-reference/pipeline-api-reference#quantopian.pipeline.factors.BusinessDaysSincePreviousEvent).

That should do what you want.

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.

Do you think it would be possible to make the asofDate match the index date exactly? Currently, there is always a 1 day difference between the as of date and the date in my index column (maybe to avoid look ahead bias??)