Hi Valery,
You can use the BusinessDaysUntilNextEarnings and BusinessDaysSincePreviousEarnings factors from EventVestor's Earnings Calendar dataset. (https://www.quantopian.com/data/eventvestor/earnings_calendar)
In this case, here's how you find stocks that had an earnings date 3 or less business days ago:
# For full information on the dataset please visit Quantopian data at
# https://www.quantopian.com/data/eventvestor/earnings_calendar
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
# Both Free & Paid versions will be accessed through the same
# namespace
from quantopian.pipeline.data.eventvestor import EarningsCalendar
from quantopian.pipeline.data.eventvestor.factors import (
BusinessDaysUntilNextEarnings,
BusinessDaysSincePreviousEarnings
)
def initialize(context):
context.spy = sid(8554)
pipe = Pipeline()
pipe = attach_pipeline(pipe, name='risk')
# EarningsCalendar.X is the actual date of the announcement
# E.g. 9/12/2015
pipe.add(EarningsCalendar.next_announcement.latest, 'next')
pipe.add(EarningsCalendar.previous_announcement.latest, 'prev')
# BusinessDaysX is the integer days until or after the closest
# announcement. So if AAPL had an earnings announcement yesterday,
# prev_earnings would be 1. If it's the day of, it will be 0.
# For BusinessDaysUntilNextEarnings(), it is common that the value
# is NaaN because we typically don't know the precise date of an
# earnings announcement until about 15 days before
ne = BusinessDaysUntilNextEarnings()
pe = BusinessDaysSincePreviousEarnings()
pipe.add(ne, 'next_earnings')
pipe.add(pe, 'prev_earnings')
# The number of days before/after an announcement that you want to
# avoid an earnings for.
pipe.set_screen((pe <= 3))
def before_trading_start(context, data):
# Here, we're updating the pipeline of our securities
# as well as grabbing the stocks that have an upcoming
# earnings announcement date.
context.results = pipeline_output('risk').iloc[:200]
log.info(context.results.iloc[:5])
update_universe(context.results.index)
def handle_data(context, data):
pass
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.