Sunteți pe pagina 1din 35

Algorithmic trading with R

Kyle Matoba kyle.matoba.2014@anderson.ucla.edu


Finance department, Anderson School of Management, UC Los Angeles

September 28, 2010

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

1 / 28

Before we start

Identifying by name certain brokerages, assets, programming languages, etc. is a necessity for clarity and concreteness. Please do not mistake this for an endorsement of any of them. I derive no personal gain from their mention, and, to the extent that I simply describing them, I do not recommend them. Please understand that I can take absolutely no responsible for anything that anyone does subsequent to viewing this talk. What I am discussing is staking real money on ones ability to develop and implement a strategy superior to many very smart people with the same goal.

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

2 / 28

Outline

1 Some Background

Algorithmic trading at a high level Individual investing


2 Algorithmic trading in R

Details Implementation
3 Further

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

3 / 28

What is algorithmic trading?

To the best of my knowledge, there is not a well-formed denition of algorithmic trading, so absent any strong guidance I would dene algorithmic trading as a trading strategy that executes trades without human oversight. Statistical models feature prominently in algorithmic trading strategies.

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

4 / 28

Who is trading algorithmically?

Big banks: Bank of America, Citigroup Sell side: Goldman Sachs, Credit Suisse Buy side investment funds (better execution): Fidelity, BlackRock Hedge funds: Famous players include Renaissance Technologies, Citadel, and AQR Market makers: Getco, Madison Tyler Holdings, Susquehanna International Group Small investors: Me, and potentially you!

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

5 / 28

How they are trading algorithmically

As a rule1 : Lots of highly-paid smart people working on sophisticated secret algorithms to be implemented by tons of developers in low level languages on high-tech hardware located proximate to the exchanges servers.

This is almost entirely anecdotal: gleaned from popular reporting, a minimal amount of academic research, the boasting of friends, as well as the puery of rms and headhunters trying to get me to interview.
Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 6 / 28

How they are trading algorithmically

As a rule1 : Lots of highly-paid smart people working on sophisticated secret algorithms to be implemented by tons of developers in low level languages on high-tech hardware located proximate to the exchanges servers. Which begs the question . . .

This is almost entirely anecdotal: gleaned from popular reporting, a minimal amount of academic research, the boasting of friends, as well as the puery of rms and headhunters trying to get me to interview.
Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 6 / 28

How they are trading algorithmically

As a rule1 : Lots of highly-paid smart people working on sophisticated secret algorithms to be implemented by tons of developers in low level languages on high-tech hardware located proximate to the exchanges servers. Which begs the question . . . How should I be trading algorithmically?

This is almost entirely anecdotal: gleaned from popular reporting, a minimal amount of academic research, the boasting of friends, as well as the puery of rms and headhunters trying to get me to interview.
Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 6 / 28

What we will not be discussing tonight

Living in Los Angeles one is already eective excluded from ultra high frequency trading.

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

7 / 28

What we will be discussing tonight

So we rely on our ability to take favorable action on the future evolution of prices.

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

8 / 28

What we will be discussing tonight

So we rely on our ability to take favorable action on the future evolution of prices. A dicult exercise

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

8 / 28

What we will be discussing tonight

So we rely on our ability to take favorable action on the future evolution of prices. A dicult exercise But one at which we have a shot at success

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

8 / 28

And how do we do this?

By building and implementing protable forecasting models. The former is up to you2 , the latter is what I will discuss tonight. Specically, I will guide you through the steps necessary to automatically trade assets using R and the IBrokers library available on CRAN.

2 for those unsure where to begin, pairs trading and market making are natural strategies to read about Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 9 / 28

But rst, a bit of background on investing

How do you and I trade? Through a brokerage: In person or on the phone

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

10 / 28

But rst, a bit of background on investing

How do you and I trade? Through a brokerage: In person or on the phone Online (slow): Scottrade, E-Trade

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

10 / 28

But rst, a bit of background on investing

How do you and I trade? Through a brokerage: In person or on the phone Online (slow): Scottrade, E-Trade Online (fast): Ninjatrader, thinkorswim, or . . .

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

10 / 28

Interactive Brokers

A well-regarded online deep discount broker. Some quick details related to a trading strategy3 At least 18 years of age, not binding in light of: Substantial experience trading desired asset classes measured in years (honor system) At least $25,000 in capital to trade actively (United States law) Must pay at least $30 / month in commissions, or a small ( $10) fee is assessed for access to market data

not exhaustive, read the (large quantities of) ne print


Algorithmic trading with R September 28, 2010 11 / 28

Kyle Matoba (UCLA)

Trading platform

To have a completely automated trading system, what would we need? At a high level: Data access and management A framework for analyzing and implementing trade strategies Order handling and execution Platforms abound: a surfeit of proprietary software, along with some moderate open source work, generally driven by Python or C++. As I will show presently, an Interactive Brokers account + IBrokers package handles the data access and order handling. R evidently excels at the rest.

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

12 / 28

About IBrokers

Implements high level wrappers to the Interactive Brokers Java API in R Still in active development (last release: early September 2010) Some necessary logic not fully supported A few short vignettes, minimal activity on the lists, and some presentations given by Jerey Ryan (the packages author), but still sparsely documented

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

13 / 28

How IBrokers4 works at a high level

1 2

Get some data from the exchanges Take some action based on that data, goto

More concretely, some data might be a price at which trader stands ready to buy or an update to the total volume for the day. some action might involve incorporating this new realization into a model and then taking action on the output of this model. Happens dozens of times a second.

and event processing in general


Algorithmic trading with R September 28, 2010 14 / 28

Kyle Matoba (UCLA)

Here are the necessary steps to get going with R + IBrokers

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

15 / 28

Sign in
Sign in via GUI: TWS or IB Gateway. After an aggravating phone call to technical support I have ascertained that they do not support automated login (e.g. via command line argument), so possibly roll your own.

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

16 / 28

Enable connections

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

17 / 28

Instantiate connection object

In R, create a connection object with the twsConnect command


1 2 library ( IBrokers ) tws < t w s C o n n e c t ( )

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

18 / 28

Start looking at some data


The commands5
1 2 3 t i c k e r < SPY c o n t r a c t < t w s E q u i t y ( t i c k e r , SMART , ISLAND ) reqMktData ( tws , c o n t r a c t )

will dump to the console all of the data coming in from Interactive Brokers, based upon the standard methods given in the default eWrapper argument.
<20100519 <20100519 <20100519 <20100519 <20100519 <20100519 <20100519 <20100519 06:29:01.340431>,id=1,pos=0,operation=insert,side=bid,price=29.67,size=1 06:29:01.379101>,id=1,pos=1,operation=insert,side=bid,price=29.26,size=4 06:29:01.381317>,id=1,pos=2,operation=insert,side=bid,price=29.17,size=34 06:29:01.383101>,id=1,pos=3,operation=insert,side=bid,price=27.68,size=1 06:29:01.384798>,id=1,pos=0,operation=insert,side=ask,price=30.08,size=1 06:29:01.386646>,id=1,pos=1,operation=insert,side=ask,price=30.09,size=2 06:29:01.390194>,id=1,pos=2,operation=insert,side=ask,price=30.95,size=34 06:29:01.392034>,id=1,pos=3,operation=insert,side=ask,price=32.27,size=1

SPY is the ticker for a leading S&P 500 index exchange traded fund
Algorithmic trading with R September 28, 2010 19 / 28

Kyle Matoba (UCLA)

An eWrapper object is composed of an environment in which to store data, accessor functions, and methods executed whenever data of a particular type is received.
> head(eWrapper(), 5) $.Data <environment: 0x102632430> $get.Data function (x) get(x, .Data) <environment: 0x100d4e4e8> $assign.Data function (x, value) assign(x, value, .Data) <environment: 0x100d4e4e8> $remove.Data function (x) remove(x, .Data) <environment: 0x100d4e4e8> $tickPrice function (curMsg, msg, timestamp, file, ...) { symbols <- get.Data("symbols") e_tick_price(NULL, msg, timestamp, file, symbols, ...) } <environment: 0x100d4e4e8>

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

20 / 28

A natural next step is to tweak some of the eWrapper methods; for example, I began by storing incoming data in a circular matrix (within my custom eWrapper()-like function):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 eW$ t i c k P r i c e < f u n c t i o n ( curMsg , msg , timestamp , t i c k T y p e = msg [ 3 ] msg < a s . n u m e r i c ( msg ) d a t a < eW$ g e t . Data ( d a t a ) i n d e x < eW$ g e t . Data ( i n d e x ) d a t a [ i n d e x , ] < NA file , ...) {

file < unlist ( file ) i f ( t i c k T y p e == . t w s T i c k T y p e $BID ) { c a t ( p a s t e ( timestamp , msg [ 5 ] , msg [ 4 ] , , , , , , s e p = , ) , \n , f i l e = f i l e , append = TRUE) d a t a [ i n d e x , 1 : 2 ] < msg [ 5 : 4 ] d a t a [ i n d e x , nc ] < a s . n u m e r i c ( S y s . t i m e ( ) ) } e l s e i f ( t i c k T y p e == . t w s T i c k T y p e $ASK) { c a t ( p a s t e ( timestamp , , , msg [ 4 ] , msg [ 5 ] , , , , s e p = , ) , \n , f i l e = f i l e , append = TRUE) d a t a [ i n d e x , 3 : 4 ] < msg [ 4 : 5 ] d a t a [ i n d e x , nc ] < a s . n u m e r i c ( S y s . t i m e ( ) ) } e l s e i f ( t i c k T y p e == . t w s T i c k T y p e $LAST ) { c a t ( p a s t e ( timestamp , , , , , msg [ 4 ] , , , s e p = , ) , \n , f i l e = f i l e , append = TRUE) d a t a [ i n d e x , 5 ] < msg [ 4 ] d a t a [ i n d e x , nc ] < a s . n u m e r i c ( S y s . t i m e ( ) ) } index < ( index % h i s t length ) + 1 % eW$ a s s i g n . Data ( i n d e x , i n d e x ) eW$ a s s i g n . Data ( d a t a , d a t a ) }

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

21 / 28

Implement trade logic!


Either here, or by modifying the callback that functions like reqMktData take as an argument. The callback is literally an unending loop that listens for data, then passes it to the appropriate method
> deparse(twsCALLBACK)[35:47] [1] " while (TRUE) {" [2] " socketSelect(list(con), FALSE, NULL)" [3] " curMsg <- .Internal(readBin(con, \"character\", 1L, " [4] " NA_integer_, TRUE, FALSE))" [5] " if (!is.null(timestamp)) {" [6] " processMsg(curMsg, con, eWrapper, format(Sys.time(), " [7] " timestamp), file, ...)" [8] " }" [9] " else {" [10] " processMsg(curMsg, con, eWrapper, timestamp, " [11] " file, ...)" [12] " }" [13] " }"

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

22 / 28

For example...

If one stores recent data as I have shown, then a basic strategy might be to periodically ret a time series model and use it to forecast the direction of prices a bit in the future.

Shameless plug: I am wrapping up a paper with Gareth James and Peter Radchenko on an adaptive lasso-like regularization for exactly this sort of problem. Email me for the code, or it will be on CRAN shortly.

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

23 / 28

Placing orders?

Not production-ready, but exported and works. Look at the source of placeOrder().

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

24 / 28

Some things to look at next

.twsIncomingMSG for a mapping of the low level message types to the methods that process them The event handlers IBrokers:::e order status, etc. Recording data with eWrapper.MktData.CSV()

2 3

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

25 / 28

IBrokers resources

The Interactive Brokers API guide


http://www.interactivebrokers.com/php/apiUsersGuide/apiguide.htm

The vignettes on CRAN


http://cran.r-project.org/web/packages/IBrokers/vignettes/

Some assorted talks


www.rinfinance.com/agenda/2010/JeffRyan_Tutorial.pdf https://www.rmetrics.org/files/Meielisalp2009/Presentations/Ryan.pdf

Some scant banter on the lists


RSiteSearch("IBrokers")

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

26 / 28

Thats it. Any questions?

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

27 / 28

Thanks for your time

Further questions, feedback, anecdotes, large cash payments, want to talk more about this? Whatever it is, contact me at: kyle.matoba.2014@anderson.ucla.edu

This presentation will also be available at: http://personal.anderson.ucla.edu/kyle.matoba.2014/

Kyle Matoba (UCLA)

Algorithmic trading with R

September 28, 2010

28 / 28

About the code in this presentation

Everything was run in the environment:


> sessionInfo() R version 2.11.1 (2010-05-31) x86_64-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils other attached packages: [1] IBrokers_0.2-8 xts_0.7-1

datasets

methods

base

zoo_1.6-4

loaded via a namespace (and not attached): [1] grid_2.11.1 lattice_0.18-8

S-ar putea să vă placă și