Algorithmic Trading with MATLAB Martin Demel, Application Engineer 2011 The MathWorks, Inc. 1
Agenda Introducing MathWorks Introducting MATLAB (Portfolio Optimization Example) Introducting Algorithmic Trading with MATLAB Break Credit Risk Modeling with MATLAB Risk Management using various VaR computation methods Overview of derivatives pricing capabilities and further financial computing products Q&A 2
RWE Develops and Deploys an Automated System for Natural Gas and Power Trading and Risk Management Challenge Automate business processes for quoting gas contracts and hedging against price fluctuations Solution Engage MathWorks Consulting to develop and deploy to a production environment an automated pricing and risk management system that fits within the company s existing IT infrastructure Results Models created in minutes, not weeks 100% accurate results delivered Technical expertise applied to core business goals Link to user story MathWorks consultants were wellqualified, professional, and fast. They understood not only the technical issues but also the business goals, which is essential when working on a core business system. We got more than we expected from MathWorks Consulting. RWE headquarters in Essen. Dr. Norbert Tönder RWE 3
Challenges when building trading strategies Increasing complexity More data More complicated models Increasing computational speed Push to higher frequency Long deployment cycle (Re)coding is costly and error-prone 4
Agenda Introduction: Algorithmic trading Developing an automated trading decision engine Identify a successful trading rule Extend trading rule set Automate trading rule selection Implementing MATLAB into your production trading environment Wrap up and Q&A 5
The problem at hand: Identifying profitable trading strategies Commodities analyst Developing a trading strategy Multiple trading rules High frequency Management requirements: Tested on historical data Uses sophisticated analytics to identify optimal trading rule combination Integrates with existing data and execution APIs 6
Trading decision engine Development and testing Historical Data End of Day / Intraday Files Databases Strategy Modeling Research / Algorithms Model Development Calibration Back Testing Profit / Loss Risk Exposure Implementation Live Data Real-Time Feeds Event-Based Decision Engine Models Trading Rules Execution Broker API Order Routing 7
Requirements for the trading engine Sophisticated analytics Custom rules & indicators Non-traditional techniques Scalable speed Higher frequency data More trading rules Quick to develop and deploy Try different strategies Embed in trading engine 8
Trading decision engine Goal: Task 1: Build a back testing environment around historical data and a preliminary trading rule Task 2: Move to a higher frequency (minute-by-minute) and re-calibrate the model Task 3: Develop a rule selection system for instruments using evolutionary learning Development and testing Historical Data End of Day / Intraday Files Databases Strategy Modeling Research / Algorithms Model Development Calibration Back Testing Profit / Loss Risk Exposure Implementation Live Data Real-Time Feeds Event-Based Decision Engine Models Trading Rules Execution Broker API Order Routing 9
Task 1: Develop a back testing environment Goal: Build a back testing environment around historical data and a preliminary trading rule Development and testing Historical Data End of Day / Intraday Files Databases Strategy Modeling Research / Algorithms Model Development Calibration Back Testing Profit / Loss Risk Exposure Implementation Live Data Real-Time Feeds Event-Based Decision Engine Models Trading Rules Execution Broker API Order Routing 10
Task 1: Develop a back testing environment Key tasks Import data from files 0.8 Max Sharpe Ratio 0.758 for Lead 17 and Lag 120 Create a preliminary rule 0.6 0.4 0.6 Test the rule s performance 0.2 0 0.4-0.2 0.2-0.4 Solutions MATLAB data tools -0.6-0.8 0 20 40 60 0-0.2-0.4 High-level programming and pre-built functions 80 100 120 0 20 40 60 80 100 120-0.6 Powerful graphics environment 11
Task 2: Expand the scale of the engine Goal: Move to a higher frequency (minute-by-minute) and re-calibrate the model Development and testing Historical Data End of Day / Intraday Files Databases Strategy Modeling Research / Algorithms Model Development Calibration Back Testing Profit / Loss Risk Exposure Implementation Live Data Real-Time Feeds Event-Based Decision Engine Models Trading Rules Execution Broker API Order Routing 12
Task 2: Expand the scale of the engine Key tasks Importing data from databases Increase computational speed Solutions MATLAB data tools: Database Toolbox High-performance computing: Parallel Computing Toolbox, MATLAB Distributed Computing Server 13
Task 3: Rule selection engine Goal: Develop a rule selection system for instruments using evolutionary learning Development and testing Historical Data End of Day / Intraday Files Databases Strategy Modeling Research / Algorithms Model Development Calibration Back Testing Profit / Loss Risk Exposure Implementation Live Data Real-Time Feeds Event-Based Decision Engine Models Trading Rules Execution Broker API Order Routing 14
Task 3: Rule selection engine Key tasks Increase number of rules Incorporate advanced analytics to select best combination 15
Working with multiple strategies Should I trade? Signal 1 AND OR Signal 2 AND OR Signal 3 Yes Yes No Dempster et. al., Computational learning techniques for intraday fx trading using popular technical indicators, IEEE Transactions on Neural Networks (2001). 16
Signal 1 Signal 2 Signal 3 Working with multiple strategies Represent different combinations as bit strings Signal 1 AND Signal 2 OR Signal 3 1 0 0 1 0 1 1 1 1 1 AND OR Signals Active? 17
Building Custom Evolution Algorithms Selection Retain the best performing bit strings from one generation to the next. Favor these for reproduction Crossover parent1 = [ 1 0 1 0 0 1 1 0 0 0 ] parent2 = [ 1 0 0 1 0 0 1 0 1 0 ] child = [ 1 0 0 0 0 1 1 0 1 0 ] Mutation parent = [ 1 0 1 0 0 1 1 0 0 0 ] child = [ 0 1 0 1 0 1 0 0 0 1 ] 18
Price (USD) Price (USD) Task 3: Rule selection engine Key tasks Increase number of rules Incorporate advanced analytics to select best combination Solutions High-level programming MATLAB Toolboxes: Global Optimization, 150 100 50 0 200 150 100 Evolutionary Learning Results, Sharpe Ratio = 2.38 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Final Return = 239 (249%) Position Cumulative Return 50 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Serial time number 19
Agenda Introduction: Algorithmic trading Developing an automated trading decision engine Identify a successful trading rule Extend trading rule set Automate trading rule selection Implementing MATLAB into your production trading environment Wrap up and Q&A 20
Implementing the Decision Engine Goal: Evaluate and test the decision engine with real-time feeds and execution through a messaging bus Development and testing Historical Data End of Day / Intraday Files Databases Strategy Modeling Research / Algorithms Model Development Calibration Back Testing Profit / Loss Risk Exposure Implementation Live Data Real-Time Feeds Event-Based Decision Engine Models Trading Rules Execution Broker API Order Routing 21
Key Tasks Key tasks Read live market data from data feed Connect to trading engine Solutions Datafeed Toolbox Many external APIs.NET, Java, C/C++, etc. 3 rd party APIs 22
Deploying Applications with MATLAB Give MATLAB code to other users MATLAB Compiler Share applications with end users who do not need MATLAB Stand-alone executables Shared libraries.exe.dll MATLAB Builder EX Excel MATLAB Builder JA Java Web MATLAB Builder NE COM.NET Software components 23
Review: Requirements for the trading engine Sophisticated analytics Custom rules & indicators Non-traditional techniques Scalable speed Higher frequency data More trading rules Quick to develop and deploy Try different strategies Embed in trading engine 24
MATLAB s solutions Sophisticated analytics Advanced graphics environment Toolboxes give access to hundreds of new techniques Flexible and customizable Scalable speed Parallel computing solution Quick to develop and deploy High-level programming Automated deployment 25