Documente Academic
Documente Profesional
Documente Cultură
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.
2 / 28
Outline
1 Some Background
Details Implementation
3 Further
3 / 28
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.
4 / 28
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!
5 / 28
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
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
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
Living in Los Angeles one is already eective excluded from ultra high frequency trading.
7 / 28
So we rely on our ability to take favorable action on the future evolution of prices.
8 / 28
So we rely on our ability to take favorable action on the future evolution of prices. A dicult exercise
8 / 28
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
8 / 28
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
10 / 28
How do you and I trade? Through a brokerage: In person or on the phone Online (slow): Scottrade, E-Trade
10 / 28
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 . . .
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
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.
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
13 / 28
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.
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.
16 / 28
Enable connections
17 / 28
18 / 28
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
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>
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 ) }
21 / 28
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.
23 / 28
Placing orders?
Not production-ready, but exported and works. Look at the source of placeOrder().
24 / 28
.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
25 / 28
IBrokers resources
26 / 28
27 / 28
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
28 / 28
datasets
methods
base
zoo_1.6-4