I looked lots of posts and the documentation and it seems that what I am trying to do should work, but I am stuck for the past few days :(
All I'm trying to do is to get a list of high volatility stocks based on their earnings date.
I keep running in circles about this error:
Runtime Error: Trying to perform a summation, subtraction, multiplication or division between 'EarningReportsFileDate' and 'datetime.timedelta' objects throws a TypeError exception. To prevent the exception, ensure that both values share the same type.
at fine_selection_function
earnings_release_date = f.EarningReports.file_date + timedelta(days=7)
import numpy as np
from AlgorithmImports import *
class EarningsVolatilityAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) # Define start date
self.SetCash(100000) # Set initial cash
self.universe_settings.Resolution = Resolution.Daily
self.AddUniverseSelection(QC500UniverseSelectionModel())
self.AddUniverse(self.fine_selection_function)
def fine_selection_function(self, fine):
today = self.Time.date()
filtered_by_earnings = []
for f in fine:
if f.EarningReports and f.EarningReports.file_date:
earnings_release_date = f.EarningReports.file_date + timedelta(days=7)
if today > earnings_release_date:
filtered_by_earnings.append(f)
high_vol_candidates = []
days_before_earnings = 5
days_after_earnings = 5
volatility_threshold = 0.03
for stock in filtered_by_earnings:
symbol = stock.Symbol
earnings_date = stock.EarningReports.file_date
window_start = earnings_date - timedelta(days=days_before_earnings)
window_end = earnings_date + timedelta(days=days_after_earnings)
historical_data = self.History(symbol, days_before_earnings + days_after_earnings + 1, Resolution.Daily)
if not historical_data.empty:
historical_volatility = np.std(historical_data['close'].pct_change().dropna())
if historical_volatility > volatility_threshold:
avg_volume = historical_data['volume'].mean()
if avg_volume >= 200000:
high_vol_candidates.append(symbol)
return high_vol_candidates
Ashutosh
Hi mike smith
The error is because you are attempting to add timedelta(days=7) to QuantConnect.Data.Fundamental.EarningReportsFileData object. You need to tweak that line to use the object value rather than the object itself to avoid the error:
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.
Mike smith
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!