We've taken a strategy from our Strategy Library, the Fundamental Factor Long Short Strategy, and implemented it as a Framework Algorithm with a custom Alpha Model. Alpha Models provide a powerful framework for highly customized design, data manipulation, and insight generation, and we want to continue to demonstrate this here.
Briefly, the Fundamental Factor Long/Short strategy uses fundamental data as measures of value, quality, and momentum. It then ranks all the stocks in the Universe according to the factors and emits Up or Down Insights based on the perceived future direction of price movement.
The first step is to initialize all of the Models we want to use. To select the Equities we want to examine, we employ the FineFundamental Universe Selection Model, which will allow us to create quick Coarse/Fine Selection functions where we can perform some quick filtering. To best replicate the original strategy example, we employ an Equal Weighting Portfolio Construction Model and an Immediate Execution Model. Finally, we add our custom Alpha Model.
def Initialize(self):
self.SetStartDate(2018, 1, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
## Set execution model to mimic market orders
self.SetExecution(ImmediateExecutionModel())
## Set Portfolio Construction model to mimic initital algorithm weighting scheme
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
## Helper variables for universe selection and checking for conditions to update only at the
## beginning of each month
self.num_coarse = 250
self.num_fine = 10
self.lastMonth = -1
self.symbols = None
## Coarse/Fine universe selection model
self.UniverseSettings.Resolution = Resolution.Daily
self.SetUniverseSelection(FineFundamentalUniverseSelectionModel(self.CoarseSelectionFunction, self.FineSelectionFunction, None, None))
## Custom Alpha model where we can perform the trading logic and the filtering
## done based on Fundamental Data
self.AddAlpha(FundamentalFactorAlphaModel(self.lastMonth, self.num_fine))
Once we've established all of the elements of the algorithm, we do some cursory filtering of our Universe, taking the top 250 Equities with the highest dollar volume to ensure liquidity and filter out the stocks without Fundamental Data with a non-negligible price.
def CoarseSelectionFunction(self, coarse):
## If not time to rebalance, keep the same universe
if self.Time.month == self.lastMonth:
return Universe.Unchanged
## Else reassign the month variable and filter
self.lastMonth = self.Time.month
## Select only those with Fundamental Data and a sufficiently large price
## Sort by top dollar volume -- most liquid to least liquid
selected = sorted([x for x in coarse if x.HasFundamentalData and x.Price > 5],
key = lambda x: x.DollarVolume, reverse=True)
return [i.Symbol for i in selected[:self.num_coarse]]
def FineSelectionFunction(self, fine):
## Filter the fine data for equities with non-zero/non-null Value,
## 1-month Price Change, and Book Value per Share
filtered_fine = [x.Symbol for x in fine if x.OperationRatios.OperationMargin.Value > 0
and x.ValuationRatios.PriceChange1M > 0
and x.ValuationRatios.BookValuePerShare > 0]
return filtered_fine
Now that we have the large Universe to score, we perform the scoring inside the Alpha Model. The process is to make three sorted lists to store the stocks, and then use a dictionary to store the score information. For the dictionary, the keys are Symbols and the values are their scores. Finally, we sort the dictionary to get the final rank, and store the top 20 stocks to long in the list self.long and the bottom 20 stocks to short in the list self.short. Finally, we need to emit Insights based on whether we want to take a long or short position, which will inform our Portfolio Construction and Execution models. We first liquidate any securities we're currently invested in and then emit Up Insights for our selected long positions and Down Insights for our short positions.
class FundamentalFactorAlphaModel(AlphaModel):
def __init__(self, num_fine):
## Initialize the various variables/helpers we'll need
self.lastMonth = -1
self.longs = []
self.shorts = []
self.num_fine = num_fine
def Update(self, algorithm, data):
## Return no insights if it's not the month to rebalance
if algorithm.Time.month == self.lastMonth:
return []
self.lastMonth = algorithm.Time.month
## Create empty list of insights
insights = []
## We will liquidate any securities we're still invested in that we don't want to hold a position
## for the next month
for kvp in algorithm.Portfolio:
holding = kvp.Value
symbol = holding.Symbol
if holding.Invested and symbol not in self.longs and symbol not in self.shorts:
insights.append(Insight(symbol, timedelta(30), InsightType.Price, InsightDirection.Flat, None, None))
## Emit Up (buy) Insights for our desired long positions
for symbol in self.longs:
insights.append(Insight(symbol, timedelta(30), InsightType.Price, InsightDirection.Up, 0.01, None))
## Emit Down (sell) Insights for our desired short positions
for symbol in self.shorts:
insights.append(Insight(symbol, timedelta(30), InsightType.Price, InsightDirection.Down, 0.01, None))
return insights
def OnSecuritiesChanged(self, algorithm, changes):
## Get the securities
added = [x for x in changes.AddedSecurities]
## Perform filtering/sorting based on Value, Quality, and Momentum
sortedByfactor1 = sorted(added, key=lambda x: x.Fundamentals.OperationRatios.OperationMargin.Value, reverse=True)
sortedByfactor2 = sorted(added, key=lambda x: x.Fundamentals.ValuationRatios.PriceChange1M, reverse=True)
sortedByfactor3 = sorted(added, key=lambda x: x.Fundamentals.ValuationRatios.BookValuePerShare, reverse=True)
## Create dictionary to store scores
scores = {}
## Assign a score to each stock.
for i,ele in enumerate(sortedByfactor1):
rank1 = i
rank2 = sortedByfactor2.index(ele)
rank3 = sortedByfactor3.index(ele)
scores[ele] = rank1*0.2 + rank2*0.4 + rank3*0.4
## Sort the stocks by their scores
sorted_stock = sorted(scores.items(), key=lambda d:d[1],reverse=False)
sorted_symbol = [x[0] for x in sorted_stock]
## Sort the top stocks into the long_list and the bottom ones into the short_list
self.longs = [x.Symbol for x in sorted_symbol[:self.num_fine]]
self.shorts = [x.Symbol for x in sorted_symbol[-self.num_fine:]]
algorithm.Log('Long: ' + ', '.join(sorted([x.Value for x in self.longs])))
algorithm.Log('Short: ' + ', '.join(sorted([x.Value for x in self.shorts])))
The backtest is attached below. Explore the code, create new ranking schemes, and tinker with Execution, Portfolio Construction, and Risk models!
Bala Vignesh
Thanks Jack, very helpful!
Bala Vignesh
This example is for equal weighted allocation. I am not able to figure out how to use an allocation that is a function of the Insight. (eg Insight weighted). I would like the Insight to be a function of the value of the fundamental factor.. (eg. fcf to price)
I understand that would require the fundamental data to be accessed from inside the Alpha Model. Could you provide an example of Alpha Model that takes in some fundamental data as input -- eg. fcf to price
Many thanks,
Bala
Alexandre Catarino
Hi Bala Vignesh ,
Please check out the Insight-Weighted Portfolio Construction Model in the Create New Algorithm:
self.SetPortfolioConstruction(InsightWeightingPortfolioConstructionModel())
In the Alpha Model, insights must have the Weight attribute as mentioned in the Insight-Weighted PCM description. E.g.: 50% weight:
Insight.Price(symbol, timedelta(1), InsightDirection.Up, None, None, 0.5)
Bala Vignesh
Hi Alexandre, Thanks for the reply. I am getting the hang of it now.
Bala
Miguel Palanca
Hello,
I seem to get a Runtime error caused by Operation ratios in the sortedbyfactor model of the class. Im trying to implement the just alpha model in one of my algos. Should I be putting it anywhere specifically? Is there a special import required to use OperationRatios in an alpha source?
Thanks,
Miguel
Derek Melchin
Hi Miguel,
To access operation ratios in an alpha model, coarse-fine universe selection must be used. For further assistance, please attach a backtest.
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.
Jack Simonson
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!