IB-Matlab User Guide

Version 2.23

November 21, 2024

Fully compatible with:

Windows, Linux, Mac OS

IB TWS versions 962 - 983

MATLAB R2007a - R2024b

© Yair Altman, Octahedron Ltd.

http://UndocumentedMatlab.com/IB-Matlab

???

Undocumented Matlab

The engineering choice for professional Matlab solutions

Table of Contents

DISCLAIMER 4

1 Introduction 5

2 Installation and licensing 7

2.1 Licensing and activation 10

2.2 Switching activated computers 11

2.3 Updating the static Java classpath 12

3 Using IBMatlab 13

3.1 General usage 13

3.2 Contract properties 15

3.3 Returned data format 16

4 Querying account and portfolio data 17

4.1 Account information 17

4.2 Portfolio data 19

5 Querying current market data 22

5.1 Single-quote data 22

5.2 Market depth (Level II) data 28

5.3 Scanner data 30

5.4 Contract details and options chain 35

5.5 Fundamental data 38

6 Querying historical and intra-day data 42

7 Streaming data 49

7.1 Streaming quotes 49

7.2 Realtime bars 55

7.3 Streaming market depth (Level II) data 58

8 Sending trade orders 59

8.1 General usage 59

8.2 Close orders 63

8.3 Order types 64

8.4 Trail orders 67

8.5 Financial Advisor (multi-client) orders 69

8.6 Potential impact of an order (“what-if”) 72

9 Specialized trade orders 73

9.1 VWAP (best-effort) orders 73

9.2 TWAP (best-effort) orders 75

9.3 Bracket (child) orders 76

9.4 Automated orders 79

9.5 Combo orders 81

9.6 Setting special order attributes 85

9.7 Exercising and lapsing options 88

9.8 Algorithmic trading orders 90

10 Accessing and cancelling open trade orders 93

10.1 Retrieving the list of open orders 93

10.2 Modifying open orders 97

10.3 Cancelling open orders 97

11 Processing IB events 98

11.1 Processing events in IB-Matlab 98

11.2 Example – using CallbackExecDetails to track executions 103

11.3 Example – using CallbackTickGeneric to check if a security is shortable 104

11.4 Example – using CallbackContractDetails to get a contract’s full options chain 105

11.5 Example – using CallbackUpdateMktDepth for realtime order-book GUI update 107

12 Tracking trade executions 110

12.1 User requests 110

12.2 Automated log files 113

12.3 Using CallbackExecDetails 114

13 TWS connection parameters 115

14 Handling errors, problems, and IB messages 120

14.1 Messages sent from IB 120

14.2 Ambiguous/invalid security errors 124

14.3 Programmatic errors 127

14.4 Troubleshooting specific problems/errors 130

15 Using the Java connector object 133

15.1 Using the connector object 133

15.2 Programming interface 134

15.3 Usage example 140

16 Sample strategies/models using IB-Matlab 142

16.1 Pairs trading 142

16.2 Using RSI technical indicator 145

17 Frequently-asked questions (FAQ) 150

18 Professional services 154

18.1 Sample program screenshots 155

18.2 About the author (Yair Altman) 158

Appendix A – resources 159

A.1 IB-related resources 159

A.2 MathWorks webinars 160

A.3 Additional open-source Matlab resources 160

Appendix B – change log 161