FINANCIAL ENGINEERING CLUB Algorithmic Trading Financial Engineering Club Definition • Algorithmic trading, also called automated trading, black-box trading, or algo-trading, is the use of electronic platforms for entering trading orders with an algorithm which executes pre-programmed trading instructions whose variables may include timing, price, or quantity of the order, or in many cases initiating the order by automated computer programs. Motivation • Algorithms can process larger amounts of data than humans. • Algorithms can make computations and decisions faster than humans. • Algorithms can execute more precisely. • A simple strategy can be automated so that people can focus their time elsewhere. Objectives • Smart Execution • Automating a Strategy Objectives Deals with the execution of an order. • Smart Execution ••Smart SmartExecution Execution • Used by large brokers, asset managers, etc. when placing orders • How can I place a large order and not get screwed? • Automating a Strategy ••Automating Automatingaa Strategy Strategy How To Not Get Screwed • When placing a large order, try to minimize your impact on the market. • Scenario: I want to buy 50,000 shares of Chipotle (CMG) for $33,204,500: a) Put in order for all 50,000 at once b) Break order into 500 100-share lots and post all c) Break order in 500 100-share lots and post over one hour, considering how the market reacts How To Not Get Screwed • When placing a large order, try to minimize your impact on the market. • Scenario: I want to buy 50,000 shares of Chipotle (CMG) for $33,204,500: a) Put in order for all 50,000 at once b) Break order into 500 100-share lots and post all c) Break order in 500 100-share lots and post over one hour, considering how the market reacts How To Not Get Screwed • Impact Driven Algorithms • Reduce the effect that trading has on an asset’s price • Iceberging – split larger order into many smaller ones: • TWAP – Time Weighted Average Price • VWAP – Volume Weighted Average Price • More dynamic derivations • Stops others from knowing you placed a large order and changing their positions, costing you money! Time Weighted Average Price • Attempt to match the benchmark of how an asset price changes over time. • Implementation example: • Buy 10,000 shares in 5 hours • Place order for 500 shares every 15 minutes • Improvements: • Random lot sizes and intervals • Offer more/less orders early on • Adjust size based on market price Volume Weighted Average Price • VWAP is the volume-weighted average. • Benchmark on trading price that gives large volume transactions more weight in deciding the price. • Implementation example: • Buy 10,000 shares • Place order for proportional to volume traded in a 15-minute period every 15-minutes Minimizing Impact Even More • Routing orders to dark pools • • • • Private exchanges for trading securities Not available to general public No transparency Came about to facilitate block trades when we want to minimize market impact Other Algorithms • Cost-Driven Algorithms • Minimize transaction and spread costs • Also try to “time the market” right • Opportunistic Algorithms • • • • Take advantage of favorable market conditions Liquidity driven Pair driven More on this later… It’s a Business • Firms like KCG (formerly GETCO) and Citadel offer execution services • Deliver “price improvement” and “execution speed” Understanding Execution Can Help Your Algorithm • Understanding Execution Can Help Your Algorithm • Improve your fill price • Find others trying to “iceberg” and capitalize on this Finding Hidden Liquidity • Liquidity can be hidden in dark pools or in icebergs. • Guerrilla algorithms try to find icebergs using probabilistic models • Compare size and price of trades vs. order book quotes • Identify patterns in order placement to identify a “source” Readings • Barry Johnson – Algorithmic Trading & DMA Objectives • Smart Execution • Automating a Strategy Objectives • Smart Execution Designing, implementing, testing, and running an • Smart automated Execution trading strategy. • Smart Execution • Automating a Strategy ••Automating Automatingaa Strategy Strategy In the Industry Front Running • Using small lots to find large, possibly iceberged orders. • Send “ping” orders on one exchange to detect a hidden order and front run by changing your order on other exchanges. • Latency is important Automated Market Making • Place a buy limit order and a sell limit order above and below the spread • Capitalize on the spread • When automated, a few pennies of profit per transaction can reach billions. • Some exchanges offer rebates for market makers • Fractions of a penny • Improves market liquidity and narrows bid-ask spreads Statistical Arbitrage • Statistical Arbitrage capitalizes on opportunities that are not arbitrage in the literal sense, but over a long period of time they will statistically be near-riskless profit. • Example – Index Arbitrage: • Compare the price of an ETF to the weighted sum of its components • Capitalize on price discrepancies, can predict the movement of the ETF if there is a price mismatch • Latency sensitive! You can design, test, and run an automated strategy! Finding A Strategy • The general ideas are simple and public • The inner-workings, the securities to pick, the parameters, and the technology are not • Strategies can be found in academic papers, online forums, and blogs Popular Brokers for Automated Trading • Interactive Brokers • TradeStation • NinjaTrader Free Trading Platforms • NinjaTrader • TradeStation (requires brokerage account) • Quantopian • Build Your Own! Getting Market Data • Free Minute Level Data • Yahoo Finance • Google Finance • Free BATS Tick and Quote Data • Netfonds – last 20 days on US Equities • https://github.com/FEC-UIUC/Netfonds-Tick-Capture • FEC – Captured last 3 months and counting • Want free data? This link could be helpful. • Low-cost Live and Historical Data Feeds • Kinetick • TickData • More…. Testing A Strategy • Back Testing – testing a strategy on historical market data • Many trading platforms have built in backtesters • NinjaTrader has a good one • Quantopian has a good backtester for beginners Testing A Strategy • Performance Indicators • Sharpe Ratio – measures strategy performance adjusted for risk • 𝐸 𝑅𝑎 −𝑅𝑏 𝑉𝑎𝑟[𝑅𝑎 −𝑅𝑏 • The average rate of return versus a benchmark divided by the standard deviation of returns. • Why? Risk minimization is important too! • A strategy that goes all in on 1:1 odds and happens to win is not a good strategy. • Max Drawdown – The maximum peak to trough distance on your P&L • Given as a percentage • Used to measure the risk in worst case • Others: Alpha, Beta, Sortino Ratio, etc. Testing A Strategy • Pitfalls: • Overfitting – Performs well on a specific timeframe or security, but bad on the general case. • Strategies can also fail to account for how they will impact the market • Markets change. Strategy may have worked 4 years ago, but not now • Solutions: • • • • Forward Testing – testing a strategy on live market data Incorporate slippage – let some orders be filled at an unfavorable price Having a large and recent data set Train your parameters on a partition of your test data, verify accuracy on the remaining portion Momentum Following • Idea: A security on an uptrend/downtrend will continue on an uptrend/downtrend. • Simple implementation: • Take the derivative and second derivative of a moving average. • When a threshold first/second derivative is crossed: • Buy/sell security in an amount proportional to these two parameters. Mean Reversion • Idea: Two or more securities that are co-integrated will revert to their mean ratio when a divergence occurs. • Simple implementation: • Identify two co-integrated securities (i.e XLE and PFE) • Run a linear regression on XLE vs PFE on the last X days • If current spread is 2 standard deviations above the regression: • Buy XLE, sell PFE • If current spread is 2 standard deviations below regression: • Sell XLE, Buy PFE • If current spread is within 0.5 standard deviations of the regression: • Liquidate your position Readings • Quantitative Trading: How to Build Your Own Algorithmic Trading Business – Ernie Chan • Algorithmic Trading & DMA – Barry Johnson • Inside the Black Box – Rishi Narang • Pairs Trading: Quantitative Methods and Analysis – Ganapathy Vidyamurthy Quantopian • https://www.quantopian.com/posts/mean-reversion-algorithm-for-clubuse • A mean reversion strategy between XLE and PFE: • Regresses the last X days, computes current spread’s Z-Score, and compares the Z-Score to a threshold to make trading decision. • TODO – For the next 15 minutes, improve the sharpe ratio, max drawdown, and/or percent returns by tweaking the parameters at the top or manipulating the trading logic!