You are on page 1of 19

Trading with R

R User Group - Singapore Chinmay Patil


Futures and Options Trader

Disclaimer
This is not a demonstration of how to trade!

Content
1. 2. 3. 4. Systematic Trading Process Why R for trading (research) R Tools Simple Trading System

Systematic Trading Process


Formulate hypothesis and tests to validate the hypothesis Run the tests in a research platform using available data Refine if promising Reject quickly if not promising Perform parameter robustness and walk forward optimization testing If all tests are passed, implement the strategy for production

Why R for trading (research)


Advantages Disadvantages

Flexible, powerful language Best of class tool-chain Community Open-Source Cross-platform

Not as fast as C (or other low level languages) Single-threaded Limited live deployment options

R Tools

Overview of R tool chain


xts FinancialInstruments TTR
Technical indicators Financial time series data retrieval and management package Functions for tracking trades and positions in portfolios, calculating profit-and-loss by position and portfolio, and tracking performance in a capital account Time series class Financial metadata data structure

quantmod blotter

Overview of R tool chain (contd.)


quantstrat High-level abstraction layer allowing to build and test strategies in very few lines of code PerformanceAnalytics Econometric functions for performance and risk analysis of financial instruments or portfolios. Mainly analyses series of periodic returns

Various stages of backtesting


1. Data Retrieval and Management

2. Data Evaluation
quantstrat, TTR quantstrat quantstrat

XTS, FinancialInstrument, quantmod

3. Defining Signals

4. Defining Rules
5. Backtest
quantstrat, blotter

6. Performance Analysis
PerformanceAnalytics

Simple Trading System


Data : AAPL, daily OHLC data Indicators : Two moving averages Signals : Crossing of two moving averages Rules :
Buy when fast moving average crosses over slow moving average Sell when fast moving average crosses under slow moving average

Resources
Package manuals http://stackoverflow.com/questions/tagged/qua ntstrat IRC
Server : chat.freenode.net Channel : #R-Finance

Contact
chinmay.patil@gmail.com chinmay@indigoatlantic.com

Additional Slides

Data Retrieval and Management


Data source
Bloomberg, Reuters, Yahoo finance, Google finance Proprietary Databases (SQL, CSV) Clean up data (remove unrealistic prices) Convert data to proper format (XTS) XTS (time series data structure ) FinancialInstrument (Security metadata data structure) quantmod (data retrieval and storage) Indexing, mmap, RTAQ (packages for very big data)

Data preparation

Packages

Data Evaluation
Calculate Indicators
Technical Indicators Fundamental Indicators Macro Indicators

Transform Prices
For complex securities like spreads, options etc

Packages
TTR Signal extraction Any timeseries analysis packages

Defining Signals
Identify signals / patterns from various conditions met by price, indicators etc. Signals just indicate that a particular predefined condition is satisfied Packages
quantstrat

Defining Rules
Define how your strategy should handle different signals Rules decide how to act on the signals Decide trade sizes, trade risk etc. Packages
quantstrat PortfolioAnalytics LPSM

Backtest
Run your Rules over historical data to generate historical simulated trades Log generated trade Packages
blotter quantstrat

Performance Analysis
Calculate per trade performance Calculate portfolio equity performance Analyze returns distributions Check if performance metrics are within required risk/reward constraints Packages
blotter PerformanceAnalytics

You might also like