Some investors are prohibited from using leverage and other investors’ leverage is limited by margin requirements. Their only way to achieve higher returns is to buy more risky stocks which makes these assets more expensive. High-beta and risky assets should therefore deliver lower risk-adjusted returns than low-beta assets. Investors could exploit this inefficiency by using ETFs. This algorithm is going to explore this phenomenon.


The implementation of this algorithm uses the algorithm framework. The algorithm picks 35 country indexes ETFs as the trading universe. As the symbols in the universe don't change over time, we use the ManualUniverseSelectionModel to subscribe the daily data for those symbols.

Beta is a statistical measure of a stock's volatility in relation to the market. Stock analysts use this measure to get a sense of stocks' risk profiles. The formula for calculating beta is the covariance of the return of an asset with the return of the market divided by the variance of the return of the market over a certain period.


def beta(self, asset_return, market_return):
    asset_return = np.array(asset_return, dtype=np.float32)
    market_return = np.array(market_return, dtype=np.float32)
    return np.cov(asset_return, market_return)[0][1]/np.var(market_return)

We use S&P500 ETF as the market measure. The beta for each country is calculated with respect to the SPY using a 1-year rolling window. The SymbolData class saves the one-year rolling window price data.

class SymbolData:
    def __init__(self, symbol):
        self.Symbol = symbol
        self.Price = deque(maxlen=253)

self.assets is a dictionary to save the price series for each country ETF. The key is the ETF symbol. For new symbols added to the algorithm, we request the one-year history data to initialize the price series.

def OnSecuritiesChanged(self, algorithm, changes):
    for added in changes.AddedSecurities:
        if added.Symbol.Value == "SPY":
            self.market_price = deque(maxlen=253)
            hist_SPY = algorithm.History(["SPY"], 500, Resolution.Daily)
            for i in hist_SPY.loc["SPY"].itertuples():

        if added not in self.assets and added.Symbol.Value != "SPY":
            hist = algorithm.History([added.Symbol.Value], 500, Resolution.Daily)
            if not hist.empty:
                self.assets[added.Symbol] = SymbolData(added)
                for i in hist.loc[added.Symbol.Value].itertuples():

    for removed in changes.RemovedSecurities:

In the Alpha model, Update(self, algorithm, data) method updates this model with the latest data from the algorithm. This method is called each time the algorithm receives data for subscribed securities. In this method, we update the price series with new trade bars. The price series is converted to return series. We plug the market return and the country ETF return into the beta formula. ETFs are then ranked in ascending order by their estimated beta. The ranked ETFs are assigned to one of two portfolios: low beta and high beta. Each portfolio contains a quarter of the total assets.

def Update(self, algorithm, data):
    if data.ContainsKey("SPY"):
    for key, value in self.assets.items():
        if data.ContainsKey(key):
    insights = []
    if self.month != algorithm.Time.month:
        self.month = algorithm.Time.month
        beta_values = {}
        market_return = np.diff(np.array(self.market_price))/np.array(self.market_price)[:-1]
        long = None
        for key, value in self.assets.items():
            if key != "SPY" and len(value.Price) == value.Price.maxlen:
                asset_return = np.diff(np.array(value.Price))/np.array(value.Price)[:-1]
                beta_values[key] = self.beta(asset_return, market_return)
        sorted_by_beta = sorted(beta_values, key = lambda x: beta_values[x])

The algorithm shorts the high-beta portfolio and longs the low-beta portfolio. Securities are rebalanced every calendar month. The portfolio construction model is set to emit the target weight monthly. The time period of insight is from the current day to the end of the calendar month. In the Python calendar library, calendar.monthrange(year, month) returns the number of days in a month for the specified year and month. The insight direction is set to be flat for symbols removed from the long/short list at the end of the month.

long = sorted_by_beta[:int(0.25*len(sorted_by_beta))]
short = sorted_by_beta[-int(0.25*len(sorted_by_beta)):]
# day: the weekday of first day of the month
# num_days: number of days in month
day, num_days = calendar.monthrange(algorithm.Time.year, algorithm.Time.month)
insight_period = num_days - - 1
if long and short:
    invested = [x.Key for x in algorithm.Portfolio if x.Value.Invested]
    for i in invested:
        if algorithm.Portfolio[i].IsLong and i not in long:
            insights.append(Insight.Price(i, timedelta(days=1), InsightDirection.Flat))
        if algorithm.Portfolio[i].IsShort and i not in short:
            insights.append(Insight.Price(i, timedelta(days=1), InsightDirection.Flat))
    for i in long:
        insights.append(Insight.Price(i, timedelta(days=insight_period), InsightDirection.Up))
    for i in short:
        insights.append(Insight.Price(i, timedelta(days=insight_period), InsightDirection.Down))


  1. Quantpedia - Beta Factor in Country Equity Indexes