Back to Community
WSJ Example Algorithm

The Wall Street Journal published an example quantitative strategy walk-though today, and we're posting the same strategy here so you can play around with it. A version of this was originally developed by our lecturer Max for use in the lecture series.

This example strategy is meant to convey all the different components of a professional quantitative trading algorithm, from universe selection to alpha definition to portfolio optimization. Lectures on various parts of the process can be found here:

https://www.quantopian.com/lectures#Universe-Selection
https://www.quantopian.com/lectures#Factor-Analysis

Because this is a simplified strategy for use as a walkthrough, you shouldn't take the performance here as representative. Instead think about it as a template for learning more and filling it out with your own ideas.

Clone Algorithm
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
"""This algorithm demonstrates the concept of long-short equity. It uses a
technical factor to rank equities in our universe. It then longs the top of
the ranking and shorts the bottom.
For information on long-short equity strategies, please see the corresponding
lecture on our lectures page:
https://www.quantopian.com/lectures

WARNING: These factors were selected because they worked in the past over the
specific time period we choose. We do not anticipate them working in the
future. In practice finding your own factors is the hardest part of developing
any long-short equity strategy. This algorithm is meant to serve as a
framework for testing your own ranking factors.

This algorithm was developed as part of
Quantopian's Lecture Series. Please direct any
questions, feedback, or corrections to [email protected]
"""

from quantopian.algorithm import attach_pipeline, pipeline_output, order_optimal_portfolio
from quantopian.pipeline import Pipeline
from quantopian.pipeline.factors import CustomFactor, SimpleMovingAverage, AverageDollarVolume, RollingLinearRegressionOfReturns
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.filters.morningstar import IsPrimaryShare
from quantopian.pipeline.classifiers.morningstar import Sector

import numpy as np
import pandas as pd

from quantopian.pipeline.filters import Q1500US
import quantopian.experimental.optimize as opt

# Constraint Parameters
MAX_GROSS_LEVERAGE = 1.0
NUM_LONG_POSITIONS = 300
NUM_SHORT_POSITIONS = 300

# Here we define the maximum position size that can be held for any
# given stock.
MAX_SHORT_POSITION_SIZE = 2*1.0/(NUM_LONG_POSITIONS + NUM_SHORT_POSITIONS)
MAX_LONG_POSITION_SIZE = 2*1.0/(NUM_LONG_POSITIONS + NUM_SHORT_POSITIONS)

# Risk Exposures
MAX_SECTOR_EXPOSURE = 0.10
MAX_BETA_EXPOSURE = 0.20


class Reversion(CustomFactor):
    """
    Here we define a basic mean reversion factor using a CustomFactor. We
    take a ratio of the last close price to the average price over the
    last 60 days. A high ratio indicates a high price relative to the mean
    and a low ratio indicates a low price relative to the mean.
    """
    inputs = [USEquityPricing.close]
    window_length = 60

    def compute(self, today, assets, out, prices):
        out[:] = -prices[-1] / np.mean(prices, axis=0)


def make_pipeline():
    """
    Create and return our pipeline.
    We break this piece of logic out into its own function to make it easier to
    test and modify in isolation.
    """

    # Create our mean reversion factor by taking the negative of a momentum factor
    reversion = Reversion()

    # Classify all securities by sector so that we can enforce sector neutrality later
    sector = Sector()

    # Screen out non-desirable securities by defining our universe.
    universe = Q1500US()

    # By applying a mask to the rank computations, we remove any stocks that failed
    # to meet our initial criteria **before** computing ranks.  This means that the
    # stock with rank 10.0 is the 10th-lowest stock that was included in the Q1500US.
    factor_rank = reversion.rank(mask=universe).zscore()

    # Build Filters representing the top and bottom 150 stocks by our ranking system.
    # We'll use these as our tradeable universe each day.
    longs = factor_rank.top(NUM_LONG_POSITIONS)
    shorts = factor_rank.bottom(NUM_SHORT_POSITIONS)

    # The final output of our pipeline should only include
    # the top/bottom 300 stocks by our criteria
    long_short_screen = (longs | shorts)

    # Define any risk factors that we will want to neutralize
    # We are chiefly interested in market beta as a risk factor so we define it using
    # Bloomberg's beta calculation
    # Ref: https://www.lib.uwo.ca/business/betasbydatabasebloombergdefinitionofbeta.html
    beta = 0.66 * RollingLinearRegressionOfReturns(
        target=sid(8554),
        returns_length=5,
        regression_length=260,
        mask=long_short_screen
    ).beta + 0.33*1.0

    # Create pipeline
    pipe = Pipeline(
        columns={
            'longs': longs,
            'shorts': shorts,
            'factor_rank': factor_rank,
            'reversion': reversion,
            'sector': sector,
            'market_beta': beta
        },
        screen=long_short_screen
    )
    return pipe


def initialize(context):
    # Here we set our slippage and commisions. Set slippage
    # and commission to zero to evaulate the signal-generating
    # ability of the algorithm independent of these additional
    # costs.
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0))
    set_slippage(slippage.VolumeShareSlippage(volume_limit=1, price_impact=0))
    context.spy = sid(8554)

    attach_pipeline(make_pipeline(), 'long_short_equity_template')

    # Schedule my rebalance function
    schedule_function(func=rebalance,
                      date_rule=date_rules.month_start(),
                      time_rule=time_rules.market_open(hours=0, minutes=30),
                      half_days=True)
    # record my portfolio variables at the end of day
    schedule_function(func=recording_statements,
                      date_rule=date_rules.every_day(),
                      time_rule=time_rules.market_close(),
                      half_days=True)


def before_trading_start(context, data):
    # Call pipeline_output to get the output
    # Note: this is a dataframe where the index is the SIDs for all
    # securities to pass my screen and the columns are the factors
    # added to the pipeline object above
    context.pipeline_data = pipeline_output('long_short_equity_template')


def recording_statements(context, data):
    # Plot the number of positions over time.
    record(num_positions=len(context.portfolio.positions))


# Called at the start of every month in order to rebalance
# the longs and shorts lists
def rebalance(context, data):
    # Optimize API
    pipeline_data = context.pipeline_data
    todays_universe = pipeline_data.index

    # Extract from pipeline any specific risk factors you want
    # to neutralize that you have already calculated
    risk_factor_exposures = pd.DataFrame(
        {
            'market_beta': pipeline_data.market_beta.fillna(1.0)
        }
    )
    # We fill in any missing factor values with a market beta of 1.0.
    # We do this rather than simply dropping the values because we have
    # want to err on the side of caution. We don't want to exclude
    # a security just because it's missing a calculated market beta,
    # so we assume any missing values have full exposure to the market.

    # Here we define our objective for the Optimize API. We have
    # selected MaximizeAlpha because we believe our combined factor
    # ranking to be proportional to expected returns. This routine
    # will optimize the expected return of our algorithm, going
    # long on the highest expected return and short on the lowest.
    objective = opt.MaximizeAlpha(pipeline_data.factor_rank)

    # Define the list of constraints
    constraints = []
    # Constrain our maximum gross leverage
    constraints.append(opt.MaxGrossLeverage(MAX_GROSS_LEVERAGE))
    # Require our algorithm to remain dollar neutral
    constraints.append(opt.DollarNeutral())
    # Add a sector neutrality constraint using the sector
    # classifier that we included in pipeline and the
    # MAX_SECTOR_EXPOSURE set at the the top of the algorithm
    constraints.append(
        opt.NetPartitionExposure.with_equal_bounds(
            labels=pipeline_data.sector,
            min=-MAX_SECTOR_EXPOSURE,
            max=MAX_SECTOR_EXPOSURE,
        )
    )
    # Take the risk factors that you extracted above and
    # list your desired max/min exposures to them - the
    # MAX_BETA_EXPOSURE was set at the top of the algorithm
    neutralize_risk_factors = opt.WeightedExposure(
        loadings=risk_factor_exposures,
        min_exposures={'market_beta': -MAX_BETA_EXPOSURE},
        max_exposures={'market_beta': MAX_BETA_EXPOSURE}
    )
    constraints.append(neutralize_risk_factors)

    # With this constraint we enforce that no position can make up
    # greater than MAX_SHORT_POSITION_SIZE on the short side and
    # no greater than MAX_LONG_POSITION_SIZE on the long side. This
    # ensures that we do not overly concentrate our portfolio in
    # one security or a small subset of securities.
    constraints.append(
        opt.PositionConcentration.with_equal_bounds(
            min=-MAX_SHORT_POSITION_SIZE,
            max=MAX_LONG_POSITION_SIZE
        )
    )

    # Put together all the pieces we defined above by passing
    # them into the order_optimal_portfolio function. This handles
    # all of our ordering logic, assigning appropriate weights
    # to the securities in our universe to maximize our alpha with
    # respect to the given constraints.
    order_optimal_portfolio(
        objective=objective,
        constraints=constraints,
        universe=todays_universe
    )
There was a runtime error.
Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

11 responses

Great PR! I think you've posted elsewhere, but what is the thinking about the weighted estimate of beta. 33% = 1.0, 66% = linear regression?

It's listed here:
https://www.lib.uwo.ca/business/betasbydatabasebloombergdefinitionofbeta.html

The idea is that your historical snapshot of beta will give you some information about it, but you should also have a strong prior that everything tends to 1.0. Think of it as a poor man's bayesian model incorporating some weight on recent behavior and some weight on everything being 1.0.

I've replaced beta with volatility in some of my efforts. While some of a stock's volatility may be idiosyncratic, mostly it's related to beta, and is a more stable parameter to estimate.

Q had a built in Factor called AnnualizedVolatilty which can replace RollingLinearRegressionOfReturns in Delaney's example.

Yup figuring out causality is a really interesting problem. If A causes B and you try to regulate B, you'll often fail. A needs to be regulated for B to be kept in check. Of course in reality it's all a complicated web of causality and not that simple. Would be really interesting to see an empirical comparison of residuals when using historical beta and historical vol to forecast future beta.

Ps a model with 66% historic beta and 33% beta 1.0, is probably the same as a capital allocation to two models, one estimating beta, and one assuming 1.0. You can run these two side by side in one algo by creating two alphas, then adding their signals.

Yup I think the math boils out in the wash, but doing them together likely saves some amount of computing time.

Well here's some good news, return is higher, 61% instead of 46% when compared to the amount risked. A maximum of just 750k of the 1M was exchanged for long or put at risk in the form of shorting, and it means the template can be regarded as 15% further above the benchmark than what we see, by this measure of what was used. Also with more of the available cash put to work, alpha etc will be higher. Not familiar with order_optimal_portfolio() I can only say that increasing max gross leverage to around 1.25 or so is one way to utilize more of the initial value and see that.

On line 81, shouldn't zscore be called before the ranking? It looks like the order of operations is reversed.

Good catch! zscore should be called before ranking. rank returns an integral float, however, so calling zscore on it will not impact the algorithm here. If we were dealing with multiple factors, this would have been a more concerning bug as we would want to standardize all of our factors before ranking or performing any sort of aggregation so we could fairly compare them. In this case, it's definitely a typo, but it will have no effect. Thanks for pointing it out!

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

Hi Maxwell,

Thank you for your example algorithm

I would like to set leverage as a constraint but without invoking (i) number of positions and (ii) beta and alpha neutrality as constraints too.

How would you amend the code to do this? Could you give me an example algorithm using this wall street example?

Thanks

Great share! I will play around with this for a bit and see if I can leverage it to my advantage.