Introduction to Quantitative/Algorithmic trading: from Technical Analysis to Statistical Arbitrage PQFC trading Seminar part II Xiaoguang Wang PhD Candidate, Department of Statistics Purdue University April 21, 2014 Xiaoguang Wang (PQFC) Quantitative Trading 1 / 60 Important Disclosures The following presentation is for educational purpose only. All symbols and trading ideas discussed in the presentation are for demonstration only and are not recommendations. Active trading is not suitable for everyone. Past performance does not guarantee future results. Xiaoguang Wang (PQFC) Quantitative Trading 2 / 60 Outline 1 Introduction to Quantitative Trading 2 Quantitative Trading using Technical Analysis Technical Indicator Algorithm design and implementation 3 Optimizing your quantitative trading strategies Back testing 4 Quantitative Trading using more advanced tools Statistical Arbitrage Xiaoguang Wang (PQFC) Quantitative Trading 3 / 60 What is Quantitative Trading? Trading strategies based on quantitative analysis which rely on mathematical computations and number crunching to identify trading opportunities. The process consists of thorough examination of vast databases searching for repeating patternspersistent occurrences of a phenomenon, correlations among liquid assets. Price and volume are two of the more common data inputs used in quantitative analysis as the main inputs to mathematical models. It is generally used by financial institute and hedge funds. Nowadays, quantitative trading is also commonly used by individual investors. Xiaoguang Wang (PQFC) Quantitative Trading 4 / 60 Main types of quantitative trading strategies High frequency trading Algorithmic trading using technical analysis Liquidity trading (such as Convertible arbitrage) Statistical arbitrage Mean-reverting Pair trading (long & short; cointegration; Equity risk-neutral) Volatility trading/arbitrage Xiaoguang Wang (PQFC) Quantitative Trading 5 / 60 Steps to construct a quantitative trading strategy Strategy Identification: Finding a strategy, exploiting an edge and deciding on trading frequency Strategy Backtesting: Obtaining data, analyzing strategy performance and removing biases Execution System: Linking to a brokerage, automating the trading and minimizing transaction costs Risk Management: Optimal capital allocation, ”bet size”/Kelly criterion and trading psychology Xiaoguang Wang (PQFC) Quantitative Trading 6 / 60 Technical Analysis Philosophy of Technical Analysis: Market action discounts everything. Prices move in trends: A trend in motion is more likely to continue than to reverse. (An adaptation of Newtons first law of motion.) History repeats itself. A trend is assumed to be in effect until it gives definite signals that it has reversed. The market is more psychological than logical. (To know more information about technical analysis basics, please go to my website.) Xiaoguang Wang (PQFC) Quantitative Trading 7 / 60 Main types of technical indicators Trend detective indicators: Moving Average systems, Bollinger Bands, parabolic SAR, Commodity Channel Index, ZigZag Oscillation indicators: MACD, RSI, RVI, Stochastic Oscillator, Williams percent range Volume indicators: Volumes, On balance volume, Accumulation, Distribution. Others: Pivot system, Fibonacci retracement system. Xiaoguang Wang (PQFC) Quantitative Trading 8 / 60 Moving Average Simple Moving Average (SMA): the unweighted mean of the previous n data. pM + pM−1 + · · · + pM−(n−1) SMA = n Weighted Moving Average (WMA): weights decrease in arithmetical progression WMAM = npM + (n − 1)pM−1 + · · · + 2pM−n+1 + pM−n+1 n + (n − 1) + · · · + 2 + 1 Exponential Moving Average (EMA): weights decrease exponentially S1 = Y1 for t > 1, Xiaoguang Wang (PQFC) St = αYt−1 + (1 − α)St−1 Quantitative Trading 9 / 60 Moving average system A Moving Average Crossover System refers to plotting multiple moving average indicators of different periods on one price chart. A Moving Average Envelop system plots two bands around a moving average, staggered by a specific percentage rate. The two main principals for trading strategies based on a moving average system are: Moving average indicators of important periods (such as 20,60,100,200) offer support or resistance for price process. Short-period (fast) moving average indicator up-crosses long-period (slow) moving average indicator indicating the beginning or speed-up of uptrend, and vice verse. Xiaoguang Wang (PQFC) Quantitative Trading 10 / 60 Moving Average Crossover Crossover between price and indicator Xiaoguang Wang (PQFC) Crossover between MAs Quantitative Trading 11 / 60 Moving Average Envelope in the chart below, a 5% envelope is placed around a 25-day moving average. Notice how the move often reverses direction after approaching one of the levels. A price move beyond the band can signal a period of exhaustion, and traders will watch for a reversal toward the center average. Xiaoguang Wang (PQFC) Quantitative Trading 12 / 60 Bollinger bands Drawback of MA envelope: the bandwidth is fixed regardless of the price dynamics, while volatility of price is stochastic. Improvement: Bollinger Band, which consists of: an N-period moving average (MA) an upper band at K times an N-period standard deviation above the moving average (MA + Kσ) a lower band at K times an N-period standard deviation below the moving average (MA Kσ) Typical values for N and K are 20 and 2, respectively. The default choice for the average is a simple moving average. Usually the same period is used for both the middle band and the calculation of standard deviation. Xiaoguang Wang (PQFC) Quantitative Trading 13 / 60 Bollinger bands S&P 500 with 20-day, two-standard-deviation Bollinger Bands, %b and bandwidth. Xiaoguang Wang (PQFC) Quantitative Trading 14 / 60 MACD The Moving Average Convergence Divergence (MACD) technique is a trend-following momentum indicator which combines two exponential averages of past prices into two lines: the MACD line and the signal line. The MACD line is constructed as the difference between two exponential moving averages computed using last m and n closing prices (n > m). 1 1 EMALt−1 , EMAL0 = P0 EMALt = Pt + 1 − n n 1 1 EMASt = Pt + 1 − EMASt−1 , EMAS0 = P0 m m MACDt = EMASt − EMALt , Xiaoguang Wang (PQFC) Quantitative Trading MACD0 = 0 15 / 60 MACD - cont. The signal line, SLt is a k-period exponential moving average of the MACD line 1 1 SLt−1 , SL0 = 0 SLt = MACDt + 1 − k k Taking the difference between the MACD and the signal line we obtain the MACD-Histogram (MACDH) indicator, which highlights variations in the spread between the fast and the slow lines MACDHt = MACDt − SLt The most popular parameters in the MACD and MACDH computations are m = 12, n = 26, k = 9. Xiaoguang Wang (PQFC) Quantitative Trading 16 / 60 MACD - signal line crossover The chart above shows IBM with its 12-day EMA (green), 26-day EMA (red) and the 12,26,9 MACD in the indicator window. There were eight signal line crossovers in six months: four up and four down. Xiaoguang Wang (PQFC) Quantitative Trading 17 / 60 MACD - center line crossover The chart above shows Pulte Homes (PHM) with at least four centerline crosses in nine months. The resulting signals worked well because strong trends emerged with these centerline crossovers. Xiaoguang Wang (PQFC) Quantitative Trading 18 / 60 MACD - center line crossover Above is a chart of Cummins Inc (CMI) with seven centerline crossovers in five months. In contrast to Pulte Homes, these signals would have resulted in numerous whipsaws because strong trends did not materialize after the crossovers. Xiaoguang Wang (PQFC) Quantitative Trading 19 / 60 MACD - center line crossover This chart shows 3M (MMM) with a bullish centerline crossover in late March 2009 and a bearish centerline crossover in early February 2010. This signal lasted 10 months. In other words, the 12-day EMA was above the 26-day EMA for 10 months. This was one strong trend. Xiaoguang Wang (PQFC) Quantitative Trading 20 / 60 MACD - Divergences The MACD turned up with a bullish divergence with a signal line crossover in early December. Google confirmed a reversal with resistance breakout. Xiaoguang Wang (PQFC) Quantitative Trading 21 / 60 MACD - Divergences Above we see Gamestop (GME) with a large bearish divergence from August to October. On the price chart, notice how broken support turned into resistance on the throwback bounce in November (red dotted line). This throwback provided a second chance to sell or sell short. Xiaoguang Wang (PQFC) Quantitative Trading 22 / 60 MACD - Divergences This chart shows the S&P 500 ETF (SPY) with four bearish divergences from August to November 2009. Despite less upside momentum, the ETF continued higher because the uptrend was strong. Its MACD (momentum) may have been less positive (strong) as the advance extended, but it was still largely positive. Xiaoguang Wang (PQFC) Quantitative Trading 23 / 60 MACD - Conclusions Brings together momentum and trend in one indicator Chartists looking for more sensitivity may try a shorter short-term moving average and a longer long-term moving average. MACD(5,35,5) is more sensitive than MACD(12,26,9) and might be better suited for weekly charts. The MACD is not particularly good for identifying overbought and oversold levels. MACD does not have any upper or lower limits to bind its movement. MACD Line is calculated using the actual difference between two moving averages. This means MACD values are dependent on the price of the underlying security. The MACD values for a $20 stocks may range from -1.5 to 1.5, while the MACD values for a $100 may range from -10 to +10. Xiaoguang Wang (PQFC) Quantitative Trading 24 / 60 Stochastic Oscillator Developed by George C. Lane in the late 1950s, the Stochastic Oscillator is a momentum indicator that shows the location of the close relative to the high-low range over a set number of periods. According to an interview with Lane, the Stochastic Oscillator ”doesn’t follow price, it doesn’t follow volume or anything like that. It follows the speed or the momentum of price. As a rule, the momentum changes direction before price.” As such, bullish and bearish divergences in the Stochastic Oscillator can be used to foreshadow reversals. This was the first, and most important, signal that Lane identified. Lane also used this oscillator to identify bull and bear set-ups to anticipate a future reversal. Because the Stochastic Oscillator is range bound, is also useful for identifying overbought and oversold levels. Xiaoguang Wang (PQFC) Quantitative Trading 25 / 60 Stochastic Oscillator: Calculation %K=(Current Close-Lowest Low)/(Highest High-Lowest Low)*100 %D = 3-day SMA of %K Lowest Low = lowest low for the look-back period Highest High = highest high for the look-back period %K is multiplied by 100 to move the decimal point two places Xiaoguang Wang (PQFC) Quantitative Trading 26 / 60 Stochastic Oscillator: Three types Fast Stochastic Oscillator: Fast %K = %K basic calculation Fast %D = 3-period SMA of Fast %K Slow Stochastic Oscillator: Slow %K = Fast %K smoothed with 3-period SMA Slow %D = 3-period SMA of Slow %K The Full Stochastic Oscillator is a fully customizable version of the Slow Stochastic Oscillator. Users can set the look-back period, the number of periods to slow %K and the number of periods for the %D moving average. The default parameters: Fast Stochastic Oscillator (14,3), Slow Stochastic Oscillator (14,3) and Full Stochastic Oscillator (14,3,3). Xiaoguang Wang (PQFC) Quantitative Trading 27 / 60 Stochastic Oscuallator: Three types Xiaoguang Wang (PQFC) Quantitative Trading 28 / 60 Stochastic Oscuallator: Overbought/Oversold Yahoo! (YHOO) with the Full Stochastic Oscillator (20,5,5). A longer look-back period (20 days versus 14) and longer moving averages for smoothing (5 versus 3) produce a less sensitive oscillator with fewer signals. Xiaoguang Wang (PQFC) Quantitative Trading 29 / 60 Stochastic Oscuallator: Overbought/Oversold with adjustments Crown Castle (CCI) with the Full Stochastic Oscillator (20,5,5). Overbought readings were ignored because the bigger trend was up. Trading in the direction of the bigger trend improves the odds. Xiaoguang Wang (PQFC) Quantitative Trading 30 / 60 Stochastic Oscuallator: Overbought/Oversold It is sometimes necessary to increase sensitivity to generate signals. Xiaoguang Wang (PQFC) Quantitative Trading 31 / 60 Stochastic Oscuallator: Divergences International Gaming Tech (IGT) with a bullish divergence in February-March 2010 Xiaoguang Wang (PQFC) Quantitative Trading 32 / 60 Stochastic Oscuallator: Divergences Kohls (KSS) with a bearish divergence in April 2010 Xiaoguang Wang (PQFC) Quantitative Trading 33 / 60 Stochastic Oscuallator: Bull Bears Set-up Network Appliance (NTAP) with a bull set-up in June 2009 A set-up is not a signal. The set-up foreshadows a tradable low in the near future. NTAP declined below its June low and the Stochastic Oscillator moved below 20 to become oversold. Traders could have acted when the Stochastic Oscillator moved above its signal line, above 20 or above 50. Xiaoguang Wang (PQFC) Quantitative Trading 34 / 60 Stochastic Oscuallator: Bull Bears Set-up Motorola (MOT) with a bear set-up in November 2009 Notice that the Stochastic Oscillator did not make it back above 80 and turned down below its signal line in mid December. Xiaoguang Wang (PQFC) Quantitative Trading 35 / 60 RSI Developed J. Welles Wilder, the Relative Strength Index (RSI) is a momentum oscillator that measures the speed and change of price movements. RSI oscillates between zero and 100. Traditionally, and according to Wilder, RSI is considered overbought when above 70 and oversold when below 30. Signals can also be generated by looking for divergences, failure swings and centerline crossovers. RSI can also be used to identify the general trend. Xiaoguang Wang (PQFC) Quantitative Trading 36 / 60 RSI: Calculations RSI = 100 − 100 1 + RS RS = Average Gain/Average Loss The very first calculations for average gain and average loss are simple 14 period averages. First Average Gain = Sum of Gains over the past 14 periods / 14. First Average Loss = Sum of Losses over the past 14 periods / 14 The second, and subsequent, calculations are based on the prior averages and the current gain loss: Average Gain = [(previous Average Gain) x 13 + current Gain] / 14. Average Loss = [(previous Average Loss) x 13 + current Loss] / 14. Xiaoguang Wang (PQFC) Quantitative Trading 37 / 60 Algorithm Designs Before optimizing or improving your trading strategies using quantitative methods, you need to first translate your trading ideas into clear algorithms ready to be implemented. The algorithm can be simple or complicated depending on your trading ideas. A simple example: current time t. If positiont−1 ≥ 0 and RSIt−1 > 70 and RSIt < 70, then close buying positions and short sell 1 more share. If positiont−1 ≤ 0 and RSIt−1 < 30 and RSIt > 30, then close shorting positions and buy 1 more share t = t + 1. Xiaoguang Wang (PQFC) Quantitative Trading 38 / 60 Another example: range-breakthrough Trading idea: after price process stays quietly in a very narrow range for a significant period of time, the price break out the range in one direction with strong motivation. Then we expect the price continue to follow this direction and build up a trend. Things to think about for algorithm design: How to define ”narrow”? What is ”a significant period of time”? What is ”strong motivation”? When to close the position? How to set up stop-loss? Xiaoguang Wang (PQFC) Quantitative Trading 39 / 60 Range-breakthrough example: paramerization To solve those questions above, we can introduce some parameters whose values can be determined by users or after doing back testing on historical data. Illustrations: Narrow parameter: range width W Period of time: at least N days Strong motivation: Price closing above or below the range for more than the amount X . Conditions to realize your profits: for long positions, once Closet < Closet−1 and Closet−1 < Closet−2 . For short positions, vice verse. Stop-Loss parameter: Stop positions once the floating P&L is below Y dollars. Xiaoguang Wang (PQFC) Quantitative Trading 40 / 60 Range-breakthrough example: Algorithm range = False If High(N) − Low (N) ≤ W , then range = True and UB = High(N), LB = Low (N). If Positiont == 0 and range = True and Closet − UB > X , then buy 1 share at market and set range = False. If Positiont == 0 and range = True and LB − Closet > X , then short 1 share at market and set range = False. If profits < Y then close current positions. If Positiont > 0 and Closet < Closet−1 and Closet−1 < Closet−2 , sell to cover. If Positiont < 0 and Closet > Closet−1 and Closet−1 > Closet−2 , buy to cover. Xiaoguang Wang (PQFC) Quantitative Trading 41 / 60 Algorithm Implementations After designing the algorithms, the next step is to implement the algorithm with a specific programming language. Popular choices are: Excel VBA, C++, Matlab, or some platform-based language, such as Meta Trader 4 (5), TradeStation, etc. Xiaoguang Wang (PQFC) Quantitative Trading 42 / 60 Meta Trader 4 Register and download the software for free: http://www.metatrader4.com/ Programming Language: MQL4 or MQL5 http://www.mql4.com/ MQL4 Documentation and reference Back test: Ctrl + R Xiaoguang Wang (PQFC) Quantitative Trading 43 / 60 Optimize trading strategies Once you implement the trading algorithms with a programming language, you are able to run the algorithm over historical data to evaluate its quality. In most cases, the results of your trading algorithm without optimization might look not very exciting. But do not just think that your trading idea does bot work out and throw it away. The problems might come from inappropriate set-up of parameter values, or related to the trading product you use, or the inappropriate time period you use when testing. And most of the issues above (and much more than that) can be solved by optimizing your strategy. Xiaoguang Wang (PQFC) Quantitative Trading 44 / 60 Steps to optimize trading strategies Do back-testing and optimize important parameters. Pay attention to the situations of Maximum Drawdown and Maximum Consecutive Loss. Add some filter steps to avoid those extremely bad situations. Back test your algorithm on different time periods, to evaluate its stability and finally settle down parameter values. Do forward-testing of the optimized algorithm for a significant period of time before you put your real money on it. After you formally take use of the algorithm with real money, you still need to evaluate the algorithm with the new coming data from time to time to check if the market situations change greatly or not. Xiaoguang Wang (PQFC) Quantitative Trading 45 / 60 Important things to remember The time period you use should be long enough to guarantee the statistical significance of your testing results. But keep in mind that newer data should have higher importance. Do not just maximize your balance. There are still many other important factors in evaluating trading strategies, such as: Balance Maximum Drawdown Consecutive maximum loss percentage of wins Sharp ratio, percentage of maximum profits over total profits Margin level required Try your algorithm on similar products, on different time periods. Xiaoguang Wang (PQFC) Quantitative Trading 46 / 60 Back-testing example illustration with MetaTrade 4: Moving Average trading system The trading idea of the MA system to be tested is very simple: open positions when price crossover with the MA line of a given period N. And close positions when price crossovers the MA of another period M. You can download this trading strategy online from the link below: http://articles.mql4.com/download/11538 There are many other well-designed trading strategy you may download from the MQL4 community. Some of them are very good candidates after some slight adjustments. You can try to evaluate and improve those strategies by yourself! Xiaoguang Wang (PQFC) Quantitative Trading 47 / 60 MA strategy example - cont. After running the algorithm, the results look very disappointing. As shown in the graph above, we keep losing money, consistently.. Xiaoguang Wang (PQFC) Quantitative Trading 48 / 60 MA strategy example - cont Now let’s see how optimization can make bad things good. As shown above, we set up the parameter optimization ranges based on the underlying product we test on, i.e. the EURUSD 1 hour data. Xiaoguang Wang (PQFC) Quantitative Trading 49 / 60 MA strategy example - cont From the optimization results, we choose the one that performs the best considering both profits and maximum drawdown as shown above. Note that the total number of trades is also relatively large, which gives us more confidence from the statistical sense. Xiaoguang Wang (PQFC) Quantitative Trading 50 / 60 MA strategy example -cont The parameter values corresponding to the optimal case are as below: Stop Loss = 1290 Take Profit = 370 MA open period = 13 MA close period = 40 Xiaoguang Wang (PQFC) Quantitative Trading 51 / 60 MA strategy example - cont Now we use the optimized parameter values as input and run the algorithm again. As shown above, now the algorithm turns out to be a very good one. Xiaoguang Wang (PQFC) Quantitative Trading 52 / 60 MA strategy example - cont Further look into the statistical details of the trading algorithm, we find that this algorithm looks pretty good in all kinds of perspectives. And you can even further improve this trading strategy by looking at on price chart the trade which realized the consecutive maximum loss. And you notice that it is because the algorithm keeps sending short sell orders several times in an uptrend market. Then you can try to avoid this problem by adding a filter. For example, use a smoother MA line as a filter line and do not go short while price is above this MA line. Xiaoguang Wang (PQFC) Quantitative Trading 53 / 60 Forward test can be totally different After this simple example, you might think that designing trading strategy is very easy given that such a simple MA system can turn out to be so good. But before being too optimistic, let me remind you several things: Past performance can not GUARANTEE future results! Try your optimized algorithm on a different time period to see if it still works well. Back testing ignored the execution time delay, transaction fees, potential market liquidity issue and many other factors that can matter a lot in real-time trading. So use your optimized algorithm to do a forward-testing for a significant period of time before putting money on it. Xiaoguang Wang (PQFC) Quantitative Trading 54 / 60 More accurate tools Technical analysis has already been well-known in the market. Meanwhile, opportunities left using simple technical tools to make money become fewer and fewer. You can use what you learn in your MATH courses or STAT courses to design more accurate trading algorithms. Brain storm: Moving average is just a simple time-series model. Then why not use other time series models? Stochastic Oscillator or RSI tries to measure the volatility of price process, then why not use better model to estimator volatility more accurately? While trying to improve the probability of winning in your algorithms, have you ever thought about using probability distributions of all kinds of random events in the market? What you need is just an event with high probability. Why always analyzing one asset price process? How about create a new (swap) process by combining several asset prices together? Can we design some indicators representing fundamental factors? Xiaoguang Wang (PQFC) Quantitative Trading 55 / 60 What is Statistical Arbitrage? In academic literature, ”statistical arbitrage” is opposed to (deterministic) arbitrage. In deterministic arbitrage, a sure profit can be obtained from being long some securities and short others. In statistical arbitrage, there is a statistical mispricing of one or more assets based on the expected value of these assets. In other words, statistical arbitrage conjectures statistical mispricings of price relationships that are true in expectation, in the long run when repeating a trading strategy. Among those who follow the hedge fund industry, ”statistical arbitrage” refers to a particular category of hedge funds (other categories include global macro, convertible arbitrage, and so on). In this narrower sense, statistical arbitrage is often abbreviated as Stat Arb or StatArb. According to Andrew Lo, StatArb ”refers to highly technical short-term mean-reversion strategies involving large numbers of securities, very short holding periods (measured in days to seconds), and substantial computational, trading, and information technology infrastructure”. Xiaoguang Wang (PQFC) Quantitative Trading 56 / 60 Main types of StatArb Mean-reverting Pair trading (long & short; cointegration; Equity risk-neutral) Volatility trading/arbitrage Xiaoguang Wang (PQFC) Quantitative Trading 57 / 60 StatArb: Improvements of technical analysis Many statistical arbitrage strategies can be considered as improvements on technical analysis based strategies: StatArb says that it’s better to do mean-reverting strategies on a stationary process (go and read your time series textbook about the definition of stationarity) StatArb says that volatility process is stochastic which can be modeled pretty well with a nonlinear time series model (such as GARCH). StatArb says that modeling of some stochastic quantity is the first step to generate trading ideas. StatArb says that you should take use of the statistics you learn to do everything instead of thinking naively. For example: what does ”significant” mean in statistics? Why not use cross-validation when doing back-testing? Xiaoguang Wang (PQFC) Quantitative Trading 58 / 60 Two homework assignments for you Combine the S&P 500 index future and the Gold future into a stationary process; verify the process you designed is stationary; then use mean-reverting idea to design trading strategies (you can calculate RSI on the stationary process to generate mean-reverting trading signals; or you can design a new tool to measure the price deviation from the mean); optimize your trading strategy by back-testing (use cross-validation) Use USDJPY hourly data from 2010 - 2013 to estimator weekly realized volatility series; fit a time-series model on the volatility series; generate trading ideas based on the time series model predictions; optimize your trading strategy by back-testing (use cross-validation) Xiaoguang Wang (PQFC) Quantitative Trading 59 / 60 Conclusion Investing your time and efforts is more important than investing your money! Thank you! Xiaoguang Wang (PQFC) Quantitative Trading 60 / 60