Professional Documents
Culture Documents
2017 WB 2633 QuantInsti TradingusingRonInteractiveBrokers PDF
2017 WB 2633 QuantInsti TradingusingRonInteractiveBrokers PDF
Interactive Brokers
2
Agenda
• Overview of R and TWS
• Implementation using R-studio
– Installing the IDE
– Introducing IBrokers package
– Reference sheet for IBrokers
– Viewing account data
– Historical Data and Streaming data access
– Sending Limit order and StopLimit order
– Moving Average cross over strategy – example code
3
R
• Quick prototyping after back-test therefore
minimal code changes required
• Availability of a variety of packages
implementing statistical functions therefore
no need to code from scratch
• Open source
• Single Threaded
4
Why use R for Trading
• Already are familiar with R and use it for other
steps in your trading analysis
• Quickly want to test strategy to figure out
difference between real life and back testing
data
5
Installing R Studio
• Step 1 – install R
6
Configuring TWS
7
R studio - View
8
IBrokers Package
• Authored and maintained by Jeff Ryan.
• Structure
– CallBack, eWrapper, ProcessMessage
• Reference-sheets
> IBrokersRef()
10
Data
• Define contract
• Check contract validity
• Get Data
11
Historical Data
12
Data Processing CALLBACK Function
• Wait on Connection
• Process Message
13
Data –User Defined CALLBACK
14
Sending Limit Order
15
StopLimit Order
• Transmit – order displayed on tws but not sent to exchange, user can
manually transmit order
• lmtPrice – limit price at which the order is sent to exchange
• auxPrice – stop loss price.
16
Toy Strategy Code
17
Warnings and pitfalls
• Risk management module in case the default TWS
risk management system does not catch all strategy
specific risks.
• Test case to be written to ensure strategy does not
run into order sending loops.
• Code block to ensure order price and order quantity
sanity.
• Test cases to run through every time an API upgrade
happens so that you can be sure that nothing is
broken.
• This is real money now.
18
Robust code for live trading
• Requirements
– Handling socket connection losses
– Handling error messages from Interactive Brokers
– Handling Network errors
• Resource available online
– API doc on Interactive Brokers website
– Example code available on github by other people
– Implementation with 2 R instance on github by
censix (https://github.com/censix - please note this is not an endorsement of that specific
implementation and QI is not connected in anyway with the owner of the blog)
19
Web Resources
twsInstrument : R package
http://www.londonr.org/presentations/2013/12/LondonR_-
_Algorithmic_Trading_In_R_-_Malcolm_Sherrington_-_20131203.pdf
20
GOOD LUCK!
Take the Next Step with EPAT™
IB Account Offerings
• Lowest Margin Rates and Commissions • Innovative trading technology trade in
• Security Financing real-time depth of over 100 market centers in 24
availability and rates protect against countries, direct market access to
buy-ins and recalls. stocks, options, futures, forex, bonds,
• Safety of Assets strong balance sheet, ETFs and CFDs from a single account
large relative equity capital, client fund • Speed of Execution and Risk
protection Management
IB API
Your application, our trading system.
Build a complete trading application that connects to our advanced order routing and trading system
using our IB Application Programming Interface (API).
R
Use R’s powerful performance and visualization capabilities in conjunction with your IB API trading
application.
Interactive Brokers LLC is a member NYSE, FINRA and SIPC. Hedge Funds are highly speculative and investors may lose their entire investment.
Supporting documentation for any claims and statistical information will be provided upon request. [1] According to the Barron’s 2016 online broker
review. [2] Based on independent measurements by the Transaction Auditing Group, Inc., (TAG). For additional information see, ibkr.com/info