from datetime import timedelta, datetime
from QuantConnect.Data.UniverseSelection import *
from QuantConnect.Indicators import *
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel
class TestAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 4)
self.SetEndDate(self.StartDate + timedelta(3))
self.SetCash(10000)
self.UniverseSettings.Resolution = Resolution.Daily # Note: this determines the resolution of data piped into the alpha model
self.UniverseSettings.FillForward = True
self.AddUniverseSelection(LiquidUniverseSelection())
self.AddAlpha(MyAlpha())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.02))
class LiquidUniverseSelection(FundamentalUniverseSelectionModel):
def __init__(self):
super().__init__(False) # Note: set to True if also using SelectFine()
self.last_universe_update = datetime(1900, 1, 1)
# Parameters
self.universe_update_freq_days = 1
self.universe_min_daily_volume = 100000000
self.universe_min_price = 300
def SelectCoarse(self, algo, coarse):
if algo.Time < self.last_universe_update + timedelta(self.universe_update_freq_days):
return Universe.Unchanged
self.last_universe_update = algo.Time
# Filter to liquid stocks
# Note: the coarse dataset is updated nightly and gives data in daily resolution by default
selected = [x.Symbol for x in coarse
if x.Price >= self.universe_min_price
and x.DollarVolume >= self.universe_min_daily_volume]
algo.Log('Universe Updated. Current universe has %d securities.' % len(selected))
return selected
class MyAlpha(AlphaModel):
def __init__(self):
# Parameters
self.universe_momentum_period = 3
self.universe_momentum_resolution = Resolution.Daily
self.portfolio_size = 10
self.momentum_data = {}
def OnSecuritiesChanged(self, algo, changes):
# Remove securities from momentum_data once they are dropped from universe
for security in changes.RemovedSecurities:
self.momentum_data.pop(security, None)
def Update(self, algo, data):
insights = []
# Sort by momentum
for security in algo.ActiveSecurities.Values:
symbol = security.Symbol
if symbol not in self.momentum_data:
history = algo.History(symbol, self.universe_momentum_period, self.universe_momentum_resolution)
self.momentum_data[symbol] = MomentumIndicator(history, self.universe_momentum_period)
else:
self.momentum_data[symbol].Update(algo.Time, security.Close)
mom_data_sorted = sorted(self.momentum_data.items(), key=lambda x: x.self.mom).reverse()
self.momentum_data = dict(mom_data_sorted)
algo.Log('Highest momentum: %s' % str(self.momentum_data[:10]))
# Emit a Up insight if symbol is among the top symbols by momentum
for security in self.momentum_data[:self.portfolio_size]:
insights.append(Insight.Price(security.Symbol, InsightDirection.Up))
return insights
class MomentumIndicator(Momentum):
def __init__(self, history, period):
self.mom = Momentum(period)
for bar in history.itertuples():
self.mom.Update(bar.Index[1], bar.close)
def IsReady(self):
return self.mom.IsReady
def Update(self, time, price):
self.mom.Update(time, price)
Derek Melchin
Hi Nick,
This error is being throw because the algorithm above makes the MomentumIndicator class inherit from the Momentum class. Refer to this related thread for an example algorithm which creates a custom Python indicator. In addition, consider adding a consolidator to have the indicator updated automatically.
Best,
Derek Melchin
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.
Nick C
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!