trading using r on interactive brokers - low-cost online ... · pdf filespeaker: anil yadav ....

23
Speaker: Anil Yadav [email protected] 2 nd March, 2017 Trading using R on Interactive Brokers

Upload: truonganh

Post on 29-Mar-2018

220 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Speaker: Anil Yadav [email protected]

2nd March, 2017

Trading using R on Interactive Brokers

Page 2: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

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

Page 3: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

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

Page 4: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

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

Page 5: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

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

Page 6: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Installing R Studio • Step 1 – install R

• Step 2 – install R-studio

6

Page 7: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Configuring TWS

7

Page 8: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

R studio - View

8

Page 9: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

IBrokers Package • Authored and maintained by Jeff Ryan.

• Structure

– CallBack, eWrapper, ProcessMessage

• Reference-sheets

9

> 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. Copyright 2010. Jeffrey A. Ryan

Page 10: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Account Details and Positions • Connect to TWS on port

10

Page 11: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Data • Define contract • Check contract validity • Get Data

11

Page 12: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Historical Data

12

Page 13: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Data Processing CALLBACK Function

• Wait on Connection

• Read binary data

• Process Message

13

Page 14: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Data –User Defined CALLBACK

14

Page 15: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Sending Limit Order

15

Page 16: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

StopLimit Order

16

• 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.

Page 17: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Toy Strategy Code

17

Page 18: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

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

Page 19: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

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

Page 20: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Web Resources

20

twsInstrument : R package http://www.londonr.org/presentations/2013/12/LondonR_-_Algorithmic_Trading_In_R_-_Malcolm_Sherrington_-_20131203.pdf

Page 21: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

GOOD LUCK!

Page 22: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

Take the Next Step with EPAT™

Professionals from 35+ countries have benefited from EPAT™. If you want to learn Python to trade through IB’s 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

Page 23: Trading using R on Interactive Brokers - Low-Cost Online ... · PDF fileSpeaker: Anil Yadav . contact@quantinsti.com . 2. nd. March, 2017 . Trading using R on Interactive Brokers

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

IB Account Offerings

• Lowest Margin Rates and Commissions • Security Financing real-time depth of

availability and rates protect against buy-ins and recalls.

• Safety of Assets strong balance sheet, large relative equity capital, client fund protection

• Innovative trading technology trade in over 100 market centers in 24 countries, direct market access to stocks, options, futures, forex, bonds, ETFs and CFDs from a single account

• Speed of Execution and Risk 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.

Investors’ Marketplace Connect with an established R consultant or R-based tool for custom solutions.

To find out more contact Ankit Shah, Interactive Brokers +91 (22) 61289836 [email protected] Follow IB Quant on LinkedIn: https://www.linkedin.com/company/ib-quant

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