class UncoupledMultidimensionalReplicator(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 5, 8) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.AddEquity('SPY', Resolution.Daily)
self.AddAlpha(InvestorSentimentSurveyAlphaModel(self))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
class InvestorSentimentSurveyAlphaModel(AlphaModel):
def __init__(self, algorithm):
## Add Quandl data for AAII Investor Sentiment Survey
self.bullBearSpread = algorithm.AddData(QuandlData, 'AAII/AAII_SENTIMENT',Resolution.Daily).Symbol
self.sma = algorithm.SMA(self.bullBearSpread, 10, Resolution.Daily)
def Update(self, algorithm, data):
if self.sma.IsReady:
algorithm.Plot('Custom Data', 'SMA', self.sma.Current.Value)
insights = []
# Return if no data
if not data.ContainsKey(self.bullBearSpread): return insights
# This Alpha model uses the Bull-Bear spread from AAII Investor Sentiment Data.
# A Bull-Bear spread is the difference in percentage between bullish investors and the percentage of bearish investors.
# A positive Bull-Bear spread might be a leading indicator that predicts an equity market rally.
# Similarly, a negative Bull-Bear spread might be a leading indicator that predicts an equity market selloff.
spread = data[self.bullBearSpread].Value
if spread > 0:
insights.append(Insight.Price('SPY', timedelta(1), InsightDirection.Up))
return insights
def OnSecuritiesChanged(self,algorithm,changes):
## The Quandl Symbol, self.bullBearSpread, will appear in changes.AddedSecurities
pass
class QuandlData(PythonQuandl):
def __init__(self):
## Retrieve the data from the the Quandl object, specifying the data field used on Quandl
self.ValueColumnName = "BULL-BEAR SPREAD"