I'm new to Quantconnect and trying to implement a value investing old school strategy assisted by TA market timing, specifically using fast/slow EMA cross and RSI to pickup the value shares in the universe.
Flow:
- Create coarse universe from high dollar vol equities
- Slice fine universe with a couple of value investing requitements (only dividend bearing companies, p/e less than 15 etc.)
- Add to final universe if fast/slow EMA cross overs and RSI less than 25 .. ie. its uptrending but had a pull back
import talib
from Selection.EmaCrossUniverseSelectionModel import EmaCrossUniverseSelectionModel
class ModulatedOptimizedProcessor(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2016, 11, 30) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.SetBenchmark("SPY")
#self.UniverseSettings.Resolution = Resolution.Daily
self.UniverseSettings.Leverage = 1
fastPeriod = 10
slowPeriod = 30
count = 10
#self.SetUniverseSelection(EmaCrossUniverseSelectionModel(fastPeriod, slowPeriod, count))
#self.AddUniverse(self.Universe.Index.QC500)
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
def CoarseSelectionFunction(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
filtered = [ x.Symbol for x in sortedByDollarVolume if x.HasFundamentalData ]
return filtered[:50]
def FineSelectionFunction(self, fine):
filtered_fine = [x for x in fine if x.EarningReports.TotalDividendPerShare.ThreeMonths > 0
and x.ValuationRatios.PriceChange1M > 0
and x.ValuationRatios.BookValuePerShare
and x.ValuationRatios.PERatio < 18
and x.ValuationRatios.FCFYield]
# sortedByfactor1 = sorted(filtered_fine, key=lambda x: x.EarningReports.TotalDividendPerShare.ThreeMonths, reverse=True)
# sortedByfactor2 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.PriceChange1M, reverse=False)
# sortedByfactor3 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.BookValuePerShare, reverse=True)
# sortedByfactor4 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.FCFYield, reverse=True)
sortedByPeRatio = sorted(filtered_fine, key=lambda x: x.ValuationRatios.PERatio, reverse=False)
stocks = [ x.Symbol for x in sortedByPeRatio[:50] ]
return stocks
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
# if we have no changes, do nothing
if self._changes is None: return
# liquidate removed securities
for security in self._changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
# we want 15% allocation in each security in our universe
for security in self._changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.15)
self._changes = None
#self.next_rebalance = Expiry.EndOfMonth(self.Time)
# this event fires whenever we have changes to our universe
def OnSecuritiesChanged(self, changes):
self._changes = changes
# if not self.Portfolio.Invested:
# self.SetHoldings("SPY", 1)
Alexandre Catarino
Hi Bernino Lind ,
Here is a link to the docs on Good Design Patterns for Alpha Model Creation. Please abstract from the Alpha part. :-)
The point here is that once the universe is selected, OnSecuritiesChanged is triggered (line 68 of your code). In this method, we create an object that will include the indicators we need (EMA and RSI in your case) and save it in a dictionary keyed by Symbol.
Bernino Lind
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!