1 © 2011 The MathWorks, Inc.
Algorithmic Trading with MATLAB®
Martin Demel, Application Engineer
2
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
3
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
“MathWorks consultants were well-
qualified, 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.”
Dr. Norbert Tönder
RWE
Link to user story
RWE headquarters in Essen.
4
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
5
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
6
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
7
Trading decision engine
Models
Trading Rules
Decision Engine
Broker API
Order Routing
Execution
Real-Time Feeds
Event-Based
Live Data
Implementation
Development and testing
Historical Data
End of Day / Intraday
Databases
Files
Strategy Modeling
Research / Algorithms
Calibration
Model Development
Back Testing
Profit / Loss
Risk Exposure
8
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
9
Trading decision engine
Models
Trading Rules
Decision Engine
Broker API
Order Routing
Execution
Real-Time Feeds
Event-Based
Live Data
Implementation
Development and testing
Historical
Data
End of Day / Intraday
Databases
Files
Strategy
Modeling
Research / Algorithms
Calibration
Model Development
Back Testing
Profit / Loss
Risk Exposure
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
10
Task 1: Develop a back testing environment
Goal: Build a back testing environment around historical
data and a preliminary trading rule
Models
Trading Rules
Decision Engine
Broker API
Order Routing
Execution
Real-Time Feeds
Event-Based
Live Data
Implementation
Development and testing
Historical Data
End of Day / Intraday
Databases
Files
Strategy Modeling
Research / Algorithms
Calibration
Model Development
Back Testing
Profit / Loss
Risk Exposure
11
Task 1: Develop a back testing environment
Key tasks
Import data from files
Create a preliminary rule
Test the rule’s performance
Solutions
MATLAB data tools
High-level programming and
pre-built functions
Powerful graphics
environment
0
20
40
60
80
100
1200 20 40 60 80 100 120
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
Max Sharpe Ratio 0.758 for Lead 17 and Lag 120
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
12
Task 2: Expand the scale of the engine
Goal: Move to a higher frequency (minute-by-minute) and
re-calibrate the model
Models
Trading Rules
Decision Engine
Broker API
Order Routing
Execution
Real-Time Feeds
Event-Based
Live Data
Implementation
Development and testing
Historical Data
End of Day / Intraday
Databases
Files
Strategy Modeling
Research / Algorithms
Calibration
Model Development
Back Testing
Profit / Loss
Risk Exposure
13
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
14
Task 3: Rule selection engine
Goal: Develop a rule selection system for instruments
using evolutionary learning
Models
Trading Rules
Decision Engine
Broker API
Order Routing
Execution
Real-Time Feeds
Event-Based
Live Data
Implementation
Development and testing
Historical Data
End of Day / Intraday
Databases
Files
Strategy Modeling
Research / Algorithms
Calibration
Model Development
Back Testing
Profit / Loss
Risk Exposure
15
Task 3: Rule selection engine
Key tasks
Increase number of rules
Incorporate advanced
analytics to select best
combination
16
Working with multiple strategies
Signal 1 Signal 2 Signal 3 AND AND
Should I trade?
Yes Yes No
OR OR
Dempster et. al., Computational learning techniques for intraday fx trading using popular technical indicators,
IEEE Transactions on Neural Networks (2001).
17
Working with multiple strategies
Represent different combinations as bit strings
Signal 1 Signal 2 Signal 3 AND OR
1 0 0 1 0 1 1 1 1 1
Sig
na
l 1
Sig
nal 2
Sig
nal 3
AND OR Signals
Active?
18
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 ]
19
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, …
1000 2000 3000 4000 5000 6000 7000 8000 9000 100000
50
100
150Evolutionary Learning Results, Sharpe Ratio = 2.38
Price (
US
D)
1000 2000 3000 4000 5000 6000 7000 8000 9000 100000
50
100
150
200P
rice (
US
D)
Serial time number
Final Return = 239 (249%)
Position
Cumulative Return
20
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
21
Implementing the Decision Engine
Goal: Evaluate and test the decision engine with real-time
feeds and execution through a messaging bus
Models
Trading Rules
Decision Engine
Broker API
Order Routing
Execution
Real-Time Feeds
Event-Based
Live Data
Implementation
Development and testing
Historical Data
End of Day / Intraday
Databases
Files
Strategy Modeling
Research / Algorithms
Calibration
Model Development
Back Testing
Profit / Loss
Risk Exposure
22
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.
– 3rd party APIs
23
Java Excel .NET Web COM
Deploying Applications with MATLAB
Give MATLAB code
to other users
Share applications
with end users who
do not need MATLAB
– Stand-alone
executables
– Shared libraries
– Software components
.exe .dll
MATLAB Compiler
MATLAB Builder NE
MATLAB Builder EX
MATLAB Builder JA
24
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
25
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