Você está na página 1de 23

Trading using R on

Interactive Brokers

Speaker: Anil Yadav


contact@quantinsti.com
2nd March, 2017
Introduction
Currently managing a portfolio of
equity futures using R & Interactive
Brokers
Algo strategy advisor at iRageCapital
Commodity Portfolio Trader
Trained as mechanical engineer &
post graduation in Business
Administration

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

Robustness and other enhancements

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

Step 2 install R-studio

6
Configuring TWS

7
R studio - View

8
IBrokers Package
Authored and maintained by Jeff Ryan.

Structure
CallBack, eWrapper, ProcessMessage

Reference-sheets
> IBrokersRef()

IBrokers package copyright statement


IBROKERS IS NOT ENDORSED, AFFILIATED, OR CONNECTED TO INTERACTIVE BROKERS, LLC. INTERACTIVE BROKERS IS TRADEMARKED AND PROPERTY OF
INTERACTIVE BROKERS, LLC. IBROKERS COMES WITH NO WARRANTY, EXPRESSED OR IMPLIED, AND IS FOR USE AT YOUR OWN RISK. 9
Copyright 2010. Jerey A. Ryan
Account Details and Positions
Connect to TWS on port

10
Data
Define contract
Check contract validity
Get Data

11
Historical Data

12
Data Processing CALLBACK Function

Wait on Connection

Read binary data

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

Professionals from 35+ countries have benefited from EPAT.


If you want to learn Python to trade through IBs API, then enroll for EPAT now!

Executive Programme in Algorithmic Trading


Get Trained by Industry Experts and Build a Successful Career in Algo!

Register here for the next batch on Algorithmic Trading: www.quantinsti.com/epat


Duration: 16 Weekends, 100+ hours of live sessions

Quantinsti Quantinsti Quantinsti Quantinsti


Interactive Brokers and R an Innovative Approach
to Trading, Portfolio Optimization and Financial Data Mining

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 Rs powerful performance and visualization capabilities in conjunction with your IB API trading
application.

Investors Marketplace To find out more contact


Connect with an established R consultant or
R-based tool for custom solutions.
Ankit Shah, Interactive Brokers
+91 (22) 61289836
ashah@interactivebrokers.com

Follow IB Quant on LinkedIn:


https://www.linkedin.com/company/ib-quant

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 Barrons 2016 online broker
review. [2] Based on independent measurements by the Transaction Auditing Group, Inc., (TAG). For additional information see, ibkr.com/info

Você também pode gostar