Notes on Quantitative Trading by Ernest Chan

Reading notes on Ernest Chan's Quantitative Trading: where to hunt for strategy ideas, the quant workflow from idea to deployment, backtesting, and risk and money management.

Resources

  • Code: epchan.com/book

Places to hunt for strat ideas:

Generally,

  1. Look for business schools and their researchs;
  2. Financial sites and blogs;
  3. Trader society, forums, communities;
  4. Twitter and other social media of interest.

Into the Quant-verse, a generalized step-by-step guide

  1. find strategy
  2. backtest strategy
  3. setup deployment infrastructure
  4. build auto-trading bot
  5. risk and money management

Chap 1 The Mindset

  • Key Principles:
    • No Daydreams.
    • This is a skill that need as much skills as any other tasks in life.
    • Make every thing as simple as possible, but not simpler.
    • Don’t trade your life savings

Quant trading, a.k.a. algo trading, is to execute buy/sell using algorithms.

Technical Analysis can have some cross sets with algo trading if the analysis can be quantified.

#KIM:

  • Quant scales up.
  • Don’t delude into overnight riches.
  • Running small businesses, any small businesses, beyond quant, need you to put a lot of time into it initially.
  • Always have a bigger picture about life-and-work balance, meaning, know-hows about quant’s life.
  • The scope of this book.
  • The more regularly you want to realize profits, the shorter the holding periods should be.
  • There are mini-futures backed on the standard futures.
  • Don’t let emotion get in the way and mess-up your strategy.
  • Algo trading is a progressive, not a once-and-for-all or built-and-forget business.

Characteristics for winning strategies

  • Fit your working hours
  • Fit your programming skill
  • Fit your trading capital
  • Fit your goal
    • The more regularly you want to realize profit and gen-income, the shorter the holding period.
  • Check benchmarks
  • Sharpe ratio >= 1 is a minimum, >= 2 if for monthly profit.
  • Small and short drawdowns.
  • Prevent biases:
    • #KIM For small-cap securities, your own order can have market impact to drive/draw the price up/down.
    • If backtesting against index or combos of underlying assets, survivorship-bias is a must-to-avoid.
    • #KIM There are no “super-model” that one-to-rule-them-all.
    • The more rules, more parameters, the more likely it will suffer from data-snooping bias. Simple models are the often the ones that will stand the test of time.
    • Has a niche and specific edge against the market/institutions.

You should look for those strategies that fly under the radar of most institutional investors, for example, strategies that have very low capacities because they trade too often, strategies that trade very few stocks every day, or strategies that have very infrequent positions (such as some seasonal trades in commodity futures described in Chapter 7). Those niches are the ones that are likely to still be profitable because they have not yet been completely arbitraged away by the gigantic hedge funds.

![[Pasted image 20250612200245.png]]

Chapter 3 Backtesting

Skip for the codes that downloads data, compute sharpe ratio and drawdowns analysis.

Look-ahead bias

What is it: “Using a strategy that implicitly uses some premises that could never be accomplished in live trading.”

E.g.:

“Buy stock within 1 percent of day’s low.”

During BACKTESTING, all historical data are retrieved in-place, that’s why look-ahead bias could happen in the first place. And if I have a strategy that “buy when the stock is within 1 percent of the day’s low”, it could work on historical data because it’s de-facto historical and historical data is fixed and already present. In real life, you could only know day’s low so far, you can determine if it’s de facto day’s low before market closes.

Let’s confirm each point:

  • “During BACKTESTING, all historical data are retrieved in-place, that’s why look-ahead bias could happen in the first place.”

    • Absolutely correct. When you download a historical dataset, it’s often a complete table (e.g., date, open, high, low, close for that day). The “day’s low” column for January 1st is there from the start. Your backtesting code might simply read this column without considering when that “low” value actually became known during January 1st. This completeness of historical data is precisely what creates the potential for look-ahead bias.
  • “And if I have a strategy that ‘buy when the stock is within 1 percent of the day’s low’, it could work on historical data because it’s de-facto historical and historical data is fixed.”

    • Precisely. When you run your backtest on historical data, the strategy will find instances where “buy when the stock is within 1 percent of the day’s low” appears to have worked perfectly, because the “day’s low” value for each day is already present and fixed in your historical dataset. Your strategy is effectively given the answer key for each day’s low.
  • “In real life, you could never know day’s low within trading hours.”

    • 100% correct. This is the crucial distinction. In live trading, you only know the day’s low so far. The actual, final “day’s low” is not finalized until the market closes. If your strategy relied on that final value, you would have to wait until after hours to make a trade decision for that day, which defeats the purpose of an intraday strategy.

You’ve captured the essence of look-ahead bias: it’s a fundamental flaw in your backtesting methodology that causes your historical performance to be artificially inflated by implicitly using information that wouldn’t have been available at the moment of a real-time decision.

Data-snooping bias

Chan’s way of saying “overfitting”.

Strategy Refinement

#KIM :

  • Refining strategies without inducing biases and remain simple is more of an art than a science.
  • Whatever changes you made to improve performance on the training set must also improve on the test set.
  • The more known a strategy is, the less edge it will likely to have.
  • Often possible to induce minor variations to boost performance of a strategy. Here’s are some common ways and configs:
    • Exclude pharmaceutical stocks.
    • Exclude ones that have pending merger/acquisition deals.
    • Tweak entry/exit timing.
    • Tweak frequency of trades.
    • Check a variety of assets. Often small-caps maybe more profitable.
    • Tweaks best to be based on fundamental economics or well-studied market phenomenon, instead of being arbitrary.
  • Strategies are never guaranteed to work in real life.
  • It can be impossible to avoid some biases or pitfalls, it’s okay to skip a few precautions. Adapt.
  • Sometimes the edge or the mistake need to take days, if not months to emerge.

Goal:

  • Sync future performance to its past performance.

Chap4: Trading as Retail VS Joint Firm

Retail VS Joint

#glossary Referring “Retail Traders” as “retailers” from now on.

#KIM :

  • Retailers’ leverage are limited by SEC Regulation T, roughly 2 times your equity(Mine is about 4 in IBKR.) All P/L will accrue to retailer.
  • Can join a proprietary trading firm to obtain much higher leverage, still using one’s own capital, but with limited liability, tho profits might be capped to some percent. Such as Bright Trading. Need to pass FINRA exam tho.
  • 3 ways to incorporate trading:
    • Retail
    • Firm
      • By joining;
      • Establish one yourself.
  • How to choose a broker/prop firm:
    • commission rate
    • execution price
    • range of products
    • API or not
    • paper trading or not
    • reputation and financial strength
  • You can opt to open multiple accounts, whether with retail account or prop account.
    • You can be a member of multiple prop firms.
  • Tangible Infrastructure:
    • High Internet
    • Good PC
    • Uninterrupted Power Supply
    • Live newsfeed(optional)
    • VPS(optional)
    • Minitors(hook up to see VPS portfolios, etc. optional)

Chap 5 Execution System

An automated trading system will retrieve up-to-date market data from your brokerage or other data vendors, run a trading algorithm to generate orders, and submit those orders to your brokerage for execution.

Approaches:

  • Fully auto
  • Semi-auto
  • Often need some non-price data, like dividends, earnings report, earning announcements, etc.
  • Data sources(Must reflect most recent data):
    • broker
    • quant trading platform;
    • data vendor’s API

Semi-auto system: ![[Pasted image 20250625182843.png]]

#kim Some upsides/downsides, and things to remember:

  • Have full access to broker’s historical/live data.
  • You can sanity-check with semi-auto system before order submission.
  • Fully auto system minimize human errors and delays.

Full-auto system: ![[Pasted image 20250625183935.png]]

#glossary :

  • Referring “prop” as proprietary from now on.
  • Referring “strategy”(ies) as “strat”(s) from now on.
  • Referring to “transaction costs” as “tc” from now on.
  • Referring to “automated trading system” as “ATS” from now on.

#kim :

  • HTTP GET/POST requests are slower than RESTful API.
  • Mathematically complex strats are difficult to backtest on prop systems like TradeStation.
  • You don’t have to recreate the wheel yourself, there’re programming consultants available. IBKR is one example. Feed range from 50$hour to a fixed 1000-5000 $ fee.
  • Elitetrader.com has also such service.
  • Last resort, upwork.com and alike has freelance programmers.
  • To prevent confidential risk:
    • Sign NDA, but who knows;
    • Most strats are well-exploited already.
    • If your trading capacity outnumber your indie programmer, his/her impact can be minimal;
    • Hire different programmers to build different parts of the system.
  • Some insider info:
    • Institutions typically does not trade any stocks lower than 5$. So, liquidity goes low, higher bid-ask spread, higher commission cost, higher liquidity cost.
  • Ways to minimize tc:
    • Trade stocks higher than 5$;
    • Limit the size of the orders, generally, each order should not exceed 1% of the asset’s average daily volume.
    • to preserve diversification in a portfolio and to minimize market impact, based on the liquidity principle, there are two rules, the governor portfolios, and order size:
      • the largest market cap equity in a portfolio should not be over 10 times the smallest market cap equity.
  • Always paper trading if you built an ATS.
  • Goals of paper trading with an ATS:s
    • Up and running;
    • execute trades
    • compare paper traits and profit and loss with radical results from back testing
    • tweak and config timing issues of your ATS
    • obtain insights on P&L, volatility, capital portfolio size and trade frequency, etc. of you strat.
    • reduce operational issues;
    • reduce biases in the system
    • Compare execution costs;
    • observe market impact.
    • adapt your strat to the regime.

Some examples that illustrates the dramatic impact of regime shifting:

  • since April 9, 2001 all US stocks have been quoted in decimals render what was then statistical arbitrage strategies unprofitable. Read more on this elsewhere(technical)
  • the SEC updated the rules for shorting stocks in February 2010(regulatory).

More on regime shifts on Chap 7.

sATS VS ATS:

  • sATS: Upload order file gened by sATS to the sATS.
  • ATS: auto upload data and transmit orders throughout trading day(s).

After sATS/ATS is built, focus on minimizing TC and paper trading.

When live trading underperform backtest:

  • Debugging
  • Reduce TC
  • Tweak and config strat

Chap 6: Money and Risk Management

Optimal leverage

Read more on leverage notes at:https://www.notion.so/On-Leverage-21d014f3b2ee80b0b335e9a7753e740e?source=copy_link

#glossary :

  • Referring to “drawdowns” as “dd” from now on.

#KIM:

  • All strats suffer from dds ranging from days to years.
  • Kelly can be used to:
    • Determine the optimal allocation of capital;
    • Determine optimal leverage;
    • Risk management

Goal of money and risk management:

  1. Manage the risks in a way that limits your dds to a tolerable level and yet be positioned to use optimal leverage of your equity to achieve max possible growth.
  2. If you have multiple strats, find a way to optimally allocate capital to maximize overall risk-adjusted return.

Chan follows Thorp.E.’s approach to build optimal allocation for a portfolio on securities. But for Chan’s, it’s a portfolio of strats.

Goal for portfolio optimization: maximize long-term wealth; meaning:

Maximizing the long-term log growth rate of the portfolio.

Which meaning:

Avoid the probability of liquidation to zero at all costs.

If the probability of liquidation to zero(or below) is non-zero, the long-term wealth is surely liquidated.

Assumptions here for portfolio optimization #KIM:

  • Reinvest all the profits, thus achieving log-returns.
  • The probability distribution of the returns of each of the trading strat is Gaussian, with a fixed mean and std_dev.(Gaussian distribution, normal distribution)

The approach: $$ \begin{align*} F^=(f_1^,f_2^,…,f_n^)^T\\ F^=C^{-1}M \end{align} $$ Notations:

  • $F/f$: The optimal fraction of the equity.
  • $T$: Transpose.
  • $C$: covariance matrix such that matrix element $C_{ij}$ is the covariance of the returns of the $i^{th}$ and $j^{th}$ strategies,
  • $-1$ indicates matrix inverse.
  • $M=(m_1,m_2,…,m_n)^T$ is the column vector of mean returns of the strats.

#KIM :

  • The returns to this point are one-period, simple(non-log/non-compound), unlevered.

Kelly formula:

$$ f_i=\frac{m_i}{s_i^2} $$

Notation:

  • $f_i$: Optimal leverage/fraction.
  • $m_i$: excess return, given by $u_p-r_f$, In which:
    • $u_p$: Utility rate, which usually given by the return rate of an underlying asset for a given period.
    • $r_f$: risk-free rate. In most ideal context, it’s $0$, for piggy bank. Or $4$ percent, based on US.Treasury 10 year bond.
  • $s_i^2$: Sharpe ratio to the power of 2.

#KIM :

  • Fact: geometric mean of a set of numbers is always smaller than the arithmetic mean.
  • #Extremely_important: Risk ALWAYS decreases long-term growth rate.
  • Retail trading accounts often be capped with a leverage either 2 or 4.
  • $BuyingPower=AccountEquity*L$
  • The same works for portfolio if optimal fraction is needed for each equity in a portfolio.

In case your leverage $L$ is capped to 2 or 4, the fraction $f_i$ of each strat is given by(reduce the overall leverage):

$$ \begin{align*} f_i^{capped} = \\ f_i^{uncapped}\frac{L}{OveralUncappedL}=\\ f_i^{uncapped}\frac {L}{(|f_1|+|f_2|+…+|f_n|)}=\\ f_i^{uncapped}\frac{L}{\sum^1_n{f_n}}\\ \end{align*} $$

Thus, the total fraction would be:

$$ F^*=\sum^1_n{f_n^{capped}}=L $$ Where $L$ should not exceed the Max Leverage set by your broker.

Max log growth rate:

$$ g = r + \frac {S^2}{2} $$

Notation:

  • $S$: Sharpe ratio of your portfolio.
  • $g$: log-growth
  • $r$: risk-free rate.

More notes on leverages here: https://www.notion.so/On-Leverage-21d014f3b2ee80b0b335e9a7753e740e?source=copy_link.

#kim:

  • At the end of the trading day, ATS should update:
    • trailing mean return
    • standard deviation(volatility)
    • equity(capital)
    • portfolio size
  • It’s always safer to have a leverage below full Kelly.
  • The frequency of the update depends on the holding period of the strat.
  • For a daily holding period, generally consider a 6-month lookback period to update.

Risk management

Examples to illustrate how risk management is:

  • August 2007 crisis not only hit hard on stock markets, real estates, but also hedge funds. This is called “financial contagion” effect.
  • In 2021, GME short squeeze caused Melvin Capital more than halved, it disclosed to defunct at 2022.

Read more on financial contagion here

In simple terms, a large loss by one hedge fund causes it to sell off some large positions, if other hedge funds holding similar positions, they sell it too, and the chain goes on.

Market risks:

#KIM Generally, practitioners opt for half-kelly to induce a smaller size of the selling when disaster happen.

#KIM Sometimes even half-kelly is too aggressive, since the model based on many assumptions and premises that might be deviating from reality(Probability distribution of returns is Gaussian, etc.) Fat tails, or “black swans” can render huge impact on P&L.

#KIM Ask yourself, what’s the max one-period drawdown you are willing to suffer, and divide that by the max historical one-period loss, that’s the optimal leverage.

#KIM At a catastrophic event, prices go discontinuously, even with stop loss orders, the orders will be filled at much worse price.

Specific Risks:

#KIM If prices reflect on news or fundamental reasons(earnings, revenues, etc.), the price will go the a new equilibrium price. If not, the price goes drastically without any apparent reasons, likely the large holders are liquidating large positions.

Model Risks:

  • Biases
  • Regime Shifts
  • Too exploited by other traders
  • #KIM Duplicate results by someone else to validate your model.
  • Reduce leverages for model risks, eventually the start might be shutdown.

Natural/Physical Risks:

  • Internet down;
  • Power down;

Psychological Risk:

  • You want to mess up your ATS.
  • Endowment Effect: Demand much more to give up than to acquire
  • Status quo bias: Hold on to a losing position for too long with no rational reason
  • Loss Aversion: Leaving a profitable position too soon. The pain from losing profits outweighs the pleasure from gaining higher profits.(Read more on Daniel, 2011, Thinking Fast and Slow)
  • Mean reversion: Wait for loss to be smaller before they exit.
  • Rep Bias: Factor too much on recent exp or events rather than long-term average.
  • Despair: Your strat is ongoing a prolonged, major drawdown. Update Kellys and equities. Both liquidation and putting more positions are irrational.
  • Greed: A strat runs well and profit-gen well, you want to double the leverage. Keep discipline and stick to the ATS, have respect in black swans.

#KIM In the quant world, there are no strat1.0, 2.0, 3.0s, every strat is discrete and independent, even tough you just modified the risk-free rate to 0.02.

#KIM #Extremely_important Keep the size of your portfolio under control at all times. Keep discipline.

More examples to awe and respect the black swans and disciplines:

  • Long-Term Capital Management in 2000
  • Amaranth Advisors in 2006(caused by individual named Brian Hunter, overleveraging). Read more here.

#KIM #Extremely_important Start with a small portfolio and gradually gain psychological preparedness, discipline and confidence. #KIM #Extremely_important You need to train/test yourself as well as your strat. Train/test your preparedness, discipline and confidence. #KIM #Extremely_important It’s helpful to have other sources of income.

Chap 7 More Insights from within the industry

Goals:

  • Gain more insights;
  • Derive exit strat;
  • Answer: Higher leverages V.S. Higher-beta?

2 categories of starts:

  1. mean-reverting
  2. momentum
    1. slow diffusion of earnings(whether good or bad)
    2. continuous execution of large orders(private equity)
    3. crowd behavior(essentially noone has all the information to make decisions, everyone relies on somebody else.

Cointegration V.S. Correlation for statistical arbitrage

#KIM Stationary cointegrals are the best candidates for mean-reverting strat, but not necessarily.

#KIM Correlation is that two stocks will likely to move in the same direction.

#KIM Where to find stationary cointegral pairs for statistical arbitrage:

  • Stocks(KO VS PEP)
  • Cross forex: CADAUD/AUDCAD
  • Futures(Long-term and short-term contracts)
  • Fixed-income

Factor Models

Denote excess return as $R$, let $R$ be such that:

$$ R = Xb+\mu $$ In which:

  • $X$: $N\times F$ matrix of factors(factor loadings);
  • $b$: $F$ vector of factor returns;
  • $\mu$: $N$ vector of specific returns;

So, $R$ can also be denoted as:

$$ R = ({N}\times{F})F+N $$ #glossary Potential factors for a factor model:

  • factor exposure
  • factor return
  • specific return
  • time-series factors
  • hedge portfolios: long/short
  • beta: Volatility sentivity
  • P/E ratio: price to earnings ratio
  • ROE

To generalize, you can find factors from:

  • economics(Values);
  • fundamentals(P/Es, ROEs, Balance Sheets, etc.);
  • technicals(Previous period’s returns);
  • statistical(Historical returns)

#KIM Example: Fama-French Three-Factor model (Fama and French, 1992)

#KIM WML factor: a momentum factor that measures the return of a hedge portfolio that longs stocks that previously had positive returns and short stocks that previously had negative returns

#KIM Baseground to check your factor models against: the R2 statistic of a good factor model with monthly returns of 1,000 stocks and 50 factors is typically about 30 to 40 percent

What are factor models for:

  • Check whether factors exhibit correlation with stock’s return;
  • Determine how good are your assumptions for the next time period(how does regression fit)
  • Based on above, predict excess returns

Places to get factors data:

#KIM Factor returns must have momentum to make factor models to work.

Exit Strat

Exit strat usually, if not always based on:

  • Fixed holding period(being the default of ANY model);
  • Profit cap;
  • Latest entry signals;
  • Stop price(loss cap)

#KIM Momentum has a finite lifespan. Can usually be discovered through backtest. As the edge is slowly diffusing by the market, the optimal holding period slowly diminishes.

#KIM Momentum strats that depend on news or events are definitely to decrease holding period.

#KIM E. Chan is constantly promoting mean-reverting strats, AKA long/short portfolios.

Denote the mean-reverting spread of the time-series long/short portfolio as $z(t)$, then:

$$ dz(t)=-\theta(z(t)-\mu)dt+dW $$ In which:

  • $\theta$: target $\theta$, find by regression
  • $\mu$: mean value of the spread($z(t)$) over time
  • $dW$: Random Gaussian noise

Solving the equation, we get:

  • $z(t)$ exponentially decay to $\mu$
  • half-life of the decay is $ln(2)/\theta$: the expected time it takes for the spread to revert to half its initial deviation from the mean

Basically, we are to find this half-life to be as our holding period for mean-reverting position.

Thus, the exit strat in this scenario can be based on:

  • $ln(2)/\theta$: The half-life, exit at the end of holding period;
  • $\mu$: The mean-value of the spread, a ready target price, exit when $\mu$ is met.

#KIM Again, E. Chan does not recommend exit by stop loss & assessment by momentum.

Seasonal strat: Calendar effect

Applicable assetes:

  • Equities The edge is becoming diminishing. But good to know.
  • Commodity Futures

Example: A common strat called “Jan Effect”.

  • Ver 1.0 of Jan Effect: Worst loser of the previous calendar year will yield more than the winners of the previous year(small-caps only.)(To be exercised.)

Example: Seasonal Strat: long the stock that performed the best at the same month year(s) earlier and short stocks that performed worst same month year(s) earlier

#KIM seasonal strategies might work better at commodity futures since the driving factor is more real economic needs rather than speculations.

Example: Petro(gas) futures seasonal strat: Buy the gas future that expire in May near the middle of April, and sell it by the end of April.(Natural Gas too.)

#KIM Try different entry and exit dates to see if the profitability holds up.

HFT(High frequency trading)

Go for HFT is aiming for high Sharpe.

#KIM In the industry, HFT might refer to holdings of positions less than a few seconds.

#KIM Early HFT’s were applied to the Forex and then later to futures due to an abundance of liquidity, then to equity market.

#KIM More on HFT:

  1. High-Frequency Trading (HFT) Definition: The author defines HFT as any strategy that doesn’t hold positions overnight, contrasting with stricter definitions that require holding for just a few seconds.
  2. Sharpe Ratio Advantage: HFT strategies achieve superior Sharpe ratios due to the “law of large numbers,” allowing for many small bets that minimize deviation from the mean return, thus enhancing risk-adjusted performance.
  3. Profitability of HFT Strategies: These strategies exploit market inefficiencies or provide liquidity, often resulting in consistent daily profits.
  4. Risk Management and Stability: HFT strategies are less prone to sudden drastic losses because they involve small trade sizes and can quickly deleverage or halt trading during adverse conditions, unlike longer-term strategies.
  5. Challenges in Implementing HFT: The passage emphasizes that backtesting HFT strategies is complex due to high transaction costs and the need for detailed data. Execution speed and infrastructure are critical, making it challenging for independent traders to achieve success in HFT without significant expertise and resources.

High-leverage VS High Beta

#KIM Two ways to increase return of a portfolio:

  • By high leverages
  • By high betas #KIM market is chronically underpricing beta stocks.

Rather than just aim for high-beta, the optimal approach is to opt for low-beta portfolio then leverage up, to achieve the same/converging result of just a high-beta portfolio.

#KIM Risk-parity: allocate capital such that each asset class contributes equally to the total portfolio risk.

Conclusions

#KIM Summary:

  • This book is in most part about statistical arbitrage, not momentum strategies.
  • mean-reverting regimes are more common than trending ones.
  • Momentums are usually triggered by slow-diffusion of news and events, large liquidation, or herdlike behavior.
  • Competition of traders will diminish the edge of an statistical arbitrage strat, as well as the holding period.
  • Strat parameters can be optimized using CPO.
  • Stationary price pairs are ideal for mean-reversing strat.
  • Two or more nonstationaries can form stationary if they cointegrate.
  • Cointegration is not correlation.
  • Factor models are used for modeling how they affect returns linearly.
    • Fama-French 3-factor model
  • Factor models typically have a relatively long holding period long drawdowns due to regime switches
  • exit signal should be created differently for me revision versus momentum strategies
  • stop loss can be suitable for momentum strategies, but not reversal strategies
  • the edge of seasonal trading strategies are diminishing
  • hFT’s rely on the law of large numbers and are more difficult to back test and require more resources
  • holding a highly leverage portfolio of low beta stacks should generate higher long-term compounding growth than holding an unleveraged portfolio of high beta stocks #KIM #Extremely_important whether statistical arbitrages are profitable or not is more of an issue of where and when then whether they’re correct.
Powered by Ignorance.