Top Banner
Algorithmic trading with R Kyle Matoba [email protected] Finance department, Anderson School of Management, UC Los Angeles September 28, 2010 Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 1 / 28
35

IBrokers Presentation

Sep 21, 2014

Download

Documents

wbastin1083

IBrokers Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IBrokers Presentation

Algorithmic trading with R

Kyle [email protected]

Finance department, Anderson School of Management, UC Los Angeles

September 28, 2010

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 1 / 28

Page 2: IBrokers Presentation

Before we start

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

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 2 / 28

Page 3: IBrokers Presentation

Outline

1 Some BackgroundAlgorithmic trading at a high levelIndividual investing

2 Algorithmic trading in RDetailsImplementation

3 Further

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 3 / 28

Page 4: IBrokers Presentation

What is algorithmic trading?

To the best of my knowledge, there is not a well-formed definitionof algorithmic trading, so absent any strong guidance I woulddefine algorithmic trading as a trading strategy that executestrades without human oversight.Statistical models feature prominently in algorithmic tradingstrategies.

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 4 / 28

Page 5: IBrokers Presentation

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 RenaissanceTechnologies, Citadel, and AQR

Market makers: Getco, Madison Tyler Holdings, SusquehannaInternational Group

Small investors: Me, and potentially you!

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 5 / 28

Page 6: IBrokers Presentation

How they are trading algorithmically

As a rule1:Lots of highly-paid smart people working on sophisticated secretalgorithms to be implemented by tons of developers in low levellanguages on high-tech hardware located proximate to theexchange’s servers.

Which begs the question . . . How should I be tradingalgorithmically?

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

Page 7: IBrokers Presentation

How they are trading algorithmically

As a rule1:Lots of highly-paid smart people working on sophisticated secretalgorithms to be implemented by tons of developers in low levellanguages on high-tech hardware located proximate to theexchange’s servers.Which begs the question . . .

How should I be tradingalgorithmically?

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

Page 8: IBrokers Presentation

How they are trading algorithmically

As a rule1:Lots of highly-paid smart people working on sophisticated secretalgorithms to be implemented by tons of developers in low levellanguages on high-tech hardware located proximate to theexchange’s servers.Which begs the question . . . How should I be tradingalgorithmically?

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

Page 9: IBrokers Presentation

What we will not be discussing tonight

Living in Los Angeles one is already effective excluded from ultrahigh frequency trading.

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 7 / 28

Page 10: IBrokers Presentation

What we will be discussing tonight

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

A difficult exerciseBut one at which we have a shot at success

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 8 / 28

Page 11: IBrokers Presentation

What we will be discussing tonight

So we rely on our ability to take favorable action on the futureevolution of prices.A difficult exercise

But one at which we have a shot at success

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 8 / 28

Page 12: IBrokers Presentation

What we will be discussing tonight

So we rely on our ability to take favorable action on the futureevolution of prices.A difficult exerciseBut one at which we have a shot at success

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 8 / 28

Page 13: IBrokers Presentation

And how do we do this?

By building and implementing profitable forecasting models. Theformer is up to you2, the latter is what I will discuss tonight.Specifically, I will guide you through the steps necessary toautomatically trade assets using R and the IBrokers libraryavailable on CRAN.

2for those unsure where to begin, “pairs trading” and “market making” arenatural strategies to read aboutKyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 9 / 28

Page 14: IBrokers Presentation

But first, 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

Page 15: IBrokers Presentation

But first, 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

Page 16: IBrokers Presentation

But first, 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

Page 17: IBrokers Presentation

Interactive Brokers

A well-regarded online “deep discount” broker. Some quick detailsrelated to a trading strategy3

At least 18 years of age, not binding in light of:

Substantial experience trading desired asset classes measuredin years (honor system)

At least $25,000 in capital to trade actively (United Stateslaw)

Must pay at least $30 / month in commissions, or a small(≈ $10) fee is assessed for access to market data

3not exhaustive, read the (large quantities of) fine printKyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 11 / 28

Page 18: IBrokers Presentation

Trading platform

To have a completely automated trading system, what would weneed? 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 withsome moderate open source work, generally driven by Python orC++. As I will show presently, an Interactive Brokers account +IBrokers package handles the data access and order handling. Revidently excels at the rest.

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 12 / 28

Page 19: IBrokers Presentation

About IBrokers

Implements high level wrappers to the Interactive BrokersJava API in R

Still in active development (last release: early September2010)

Some necessary logic not fully supported

A few short vignettes, minimal activity on the lists, and somepresentations given by Jeffrey Ryan (the package’s author),but still sparsely documented

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 13 / 28

Page 20: IBrokers Presentation

How IBrokers4 works at a high level

1 Get some data from the exchanges

2 Take some action based on that data, goto ↑

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

4and event processing in generalKyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 14 / 28

Page 21: IBrokers Presentation

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

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 15 / 28

Page 22: IBrokers Presentation

Sign in

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

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 16 / 28

Page 23: IBrokers Presentation

Enable connections

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 17 / 28

Page 24: IBrokers Presentation

Instantiate connection object

In R, create a connection object with the twsConnect command

1 l i b r a r y ( I B r o k e r s )2 tws <− twsConnect ( )

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 18 / 28

Page 25: IBrokers Presentation

Start looking at some data

The commands5

1 t i c k e r <− ”SPY”2 c on t r a c t <− twsEqu i t y ( t i c k e r , ”SMART” , ”ISLAND” )3 reqMktData ( tws , c o n t r a c t )

will dump to the console all of the data coming in from InteractiveBrokers, based upon the standard methods given in the defaulteWrapper argument.

<20100519 06:29:01.340431>,id=1,pos=0,operation=insert,side=bid,price=29.67,size=1<20100519 06:29:01.379101>,id=1,pos=1,operation=insert,side=bid,price=29.26,size=4<20100519 06:29:01.381317>,id=1,pos=2,operation=insert,side=bid,price=29.17,size=34<20100519 06:29:01.383101>,id=1,pos=3,operation=insert,side=bid,price=27.68,size=1<20100519 06:29:01.384798>,id=1,pos=0,operation=insert,side=ask,price=30.08,size=1<20100519 06:29:01.386646>,id=1,pos=1,operation=insert,side=ask,price=30.09,size=2<20100519 06:29:01.390194>,id=1,pos=2,operation=insert,side=ask,price=30.95,size=34<20100519 06:29:01.392034>,id=1,pos=3,operation=insert,side=ask,price=32.27,size=1

5“SPY” is the ticker for a leading S&P 500 index exchange traded fundKyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 19 / 28

Page 26: IBrokers Presentation

An eWrapper object is composed of an environment in which tostore data, accessor functions, and methods executed wheneverdata 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

Page 27: IBrokers Presentation

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 eW$ t i c k P r i c e <− f u n c t i o n ( curMsg , msg , t imestamp , f i l e , . . . ) {2 t i ckType = msg [ 3 ]3 msg <− as . numeric (msg )4 data <− eW$get . Data ( ” data ” )5 i ndex <− eW$get . Data ( ” i ndex ” )6 data [ index , ] <− NA78 f i l e <− u n l i s t ( f i l e )9 i f ( t i ckType == . twsTickType$BID){

10 cat ( paste ( t imestamp , msg [ 5 ] , msg [ 4 ] , ”” , ”” , ”” ,11 ”” , ”” , sep = ” , ” ) , ”\n” , f i l e = f i l e , append = TRUE)12 data [ index , 1 : 2 ] <− msg [ 5 : 4 ]13 data [ index , nc ] <− as . numeric ( Sys . t ime ( ) )14 }15 e l s e i f ( t i ckType == . twsTickType$ASK) {16 cat ( paste ( t imestamp , ”” , ”” , msg [ 4 ] , msg [ 5 ] , ”” ,17 ”” , ”” , sep = ” , ” ) , ”\n” , f i l e = f i l e , append = TRUE)18 data [ index , 3 : 4 ] <− msg [ 4 : 5 ]19 data [ index , nc ] <− as . numeric ( Sys . t ime ( ) )20 }21 e l s e i f ( t i ckType == . twsTickType$LAST) {22 cat ( paste ( t imestamp , ”” , ”” , ”” , ”” , msg [ 4 ] , ”” ,23 ”” , sep = ” , ” ) , ”\n” , f i l e = f i l e , append = TRUE)24 data [ index , 5 ] <− msg [ 4 ]25 data [ index , nc ] <− as . numeric ( Sys . t ime ( ) )26 }27 i ndex <− ( i ndex %% h i s t l eng th ) + 128 eW$ a s s i g n . Data ( ” i ndex ” , i ndex )29 eW$ a s s i g n . Data ( ” data ” , data )30 }

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 21 / 28

Page 28: IBrokers Presentation

Implement trade logic!

Either here, or by modifying the callback that functions likereqMktData take as an argument. The callback is literally anunending loop that listens for data, then passes it to theappropriate 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

Page 29: IBrokers Presentation

For example...

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

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

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 23 / 28

Page 30: IBrokers Presentation

Placing orders?

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

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 24 / 28

Page 31: IBrokers Presentation

Some things to look at next

1 .twsIncomingMSG for a mapping of the low level messagetypes to the methods that process them

2 The event handlers IBrokers:::e order status, etc.

3 Recording data with eWrapper.MktData.CSV()

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 25 / 28

Page 32: IBrokers Presentation

IBrokers resources

The Interactive Brokers API guidehttp://www.interactivebrokers.com/php/apiUsersGuide/apiguide.htm

The vignettes on CRANhttp://cran.r-project.org/web/packages/IBrokers/vignettes/

Some assorted talkswww.rinfinance.com/agenda/2010/JeffRyan_Tutorial.pdf

https://www.rmetrics.org/files/Meielisalp2009/Presentations/Ryan.pdf

Some scant banter on the listsRSiteSearch("IBrokers")

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 26 / 28

Page 33: IBrokers Presentation

That’s it. Any questions?

Kyle Matoba (UCLA) Algorithmic trading with R September 28, 2010 27 / 28

Page 34: IBrokers Presentation

Thanks for your time

Further questions, feedback, anecdotes, large cash payments, wantto talk more about this? Whatever it is, contact me at:[email protected]

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

Page 35: IBrokers Presentation

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 datasets methods base

other attached packages:

[1] IBrokers_0.2-8 xts_0.7-1 zoo_1.6-4

loaded via a namespace (and not attached):

[1] grid_2.11.1 lattice_0.18-8