IB-Matlab User Guide
Total Page:16
File Type:pdf, Size:1020Kb
IB-Matlab User Guide Version 2.17 March 25, 2021 Fully compatible with: Windows, Linux, Mac OS IB TWS versions 962 - 983 MATLAB R2006a - R2021a © Yair Altman http://UndocumentedMatlab.com/IB-Matlab Undocumented Matlab unbelievable features; unbelievable quality; unbelievable cost effectiveness; unbelievable service IB-Matlab User Guide 2 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 4 Querying account and portfolio data ............................................................................. 16 4.1 Account information ..................................................................................................... 16 4.2 Portfolio data ............................................................................................................... 18 5 Querying current market data ........................................................................................ 21 5.1 Single-quote data .......................................................................................................... 21 5.2 Market depth (Level II) data ......................................................................................... 25 5.3 Scanner data ................................................................................................................. 27 5.4 Contract details and options chain ............................................................................... 32 5.5 Fundamental data ......................................................................................................... 35 6 Querying historical and intra-day data .......................................................................... 39 7 Streaming data ............................................................................................................... 46 7.1 Streaming quotes .......................................................................................................... 46 7.2 Realtime bars ................................................................................................................ 51 7.3 Streaming market depth (Level II) data ........................................................................ 54 8 Sending trade orders ...................................................................................................... 55 8.1 General usage............................................................................................................... 55 8.2 Close orders ................................................................................................................. 59 8.3 Order types ................................................................................................................... 60 8.4 Trail orders .................................................................................................................. 63 8.5 Financial Advisor (multi-client) orders ........................................................................ 65 8.6 Potential impact of an order (“what-if”) ..................................................................... 68 9 Specialized trade orders ................................................................................................. 69 9.1 VWAP (best-effort) orders ............................................................................................ 69 9.2 TWAP (best-effort) orders ............................................................................................ 71 9.3 Bracket (child) orders .................................................................................................. 72 9.4 Automated orders ......................................................................................................... 75 9.5 Combo orders ............................................................................................................... 77 9.6 Setting special order attributes .................................................................................... 81 9.7 Exercising and lapsing options .................................................................................... 83 9.8 Algorithmic trading orders ........................................................................................... 85 IB-Matlab User Guide 3 10 Accessing and cancelling open trade orders ................................................................ 88 10.1 Retrieving the list of open orders ............................................................................... 88 10.2 Modifying open orders ............................................................................................... 92 10.3 Cancelling open orders .............................................................................................. 92 11 Processing IB events .................................................................................................... 93 11.1 Processing events in IB-Matlab .................................................................................. 93 11.2 Example – using CallbackExecDetails to track executions ........................................ 98 11.3 Example – using CallbackTickGeneric to check if a security is shortable ................. 99 11.4 Example – using CallbackContractDetails to get a contract’s full options chain .... 100 11.5 Example – using CallbackUpdateMktDepth for realtime order-book GUI update .. 102 12 Tracking trade executions .......................................................................................... 105 12.1 User requests ............................................................................................................ 105 12.2 Automated log files ................................................................................................... 108 12.3 Using CallbackExecDetails ...................................................................................... 109 13 TWS connection parameters ...................................................................................... 110 14 Handling errors, problems, and IB messages ............................................................ 115 14.1 Messages sent from IB .............................................................................................. 115 14.2 Ambiguous/invalid security errors ........................................................................... 118 14.3 Programmatic errors ................................................................................................ 121 14.4 Troubleshooting specific problems/errors ................................................................ 124 15 Using the Java connector object ................................................................................ 127 15.1 Using the connector object ....................................................................................... 127 15.2 Programming interface ............................................................................................ 128 15.3 Usage example ......................................................................................................... 134 16 Sample strategies/models using IB-Matlab ............................................................... 136 16.1 Pairs trading ............................................................................................................ 136 16.2 Using RSI technical indicator .................................................................................. 139 17 Frequently-asked questions (FAQ) ............................................................................ 144 18 Professional services .................................................................................................. 148 18.1 Sample program screenshots .................................................................................... 149 18.2 About the author (Yair Altman) ................................................................................ 152 Appendix A – resources.................................................................................................. 153 A.1 IB-related resources ................................................................................................... 153 A.2 MathWorks webinars ................................................................................................. 154 A.3 Additional open-source Matlab resources ................................................................. 154 Appendix B – change log ..............................................................................................