IBmatlab I Lecture Notes PDF
Document Details
Uploaded by HotConceptualArt7576
Wisconsin School of Business
Bjørn Eraker
Tags
Summary
These lecture notes cover the use of IBMatlab, a commercial package that allows interaction between MATLAB and Interactive Brokers TWS (Trader Workstation). The notes cover setting up TWS, accessing market data (live and historical), placing orders (including combo orders), and checking order status. The notes are focused on specific financial market functions.
Full Transcript
IBmatlab I Bjørn Eraker Wisconsin School of Business Finance 730 Bjørn Eraker IBmatlab is a commercial package that provides a high end platform for matlab to interact with Interactive Brokers TWS. It uses the ’wrapper function’ IBmatlab to make various calls to TWS. The function...
IBmatlab I Bjørn Eraker Wisconsin School of Business Finance 730 Bjørn Eraker IBmatlab is a commercial package that provides a high end platform for matlab to interact with Interactive Brokers TWS. It uses the ’wrapper function’ IBmatlab to make various calls to TWS. The function is extremely general, and it will take a variety of different argument and produce corresponding outputs. Bjørn Eraker Setting up TWS To set TWS up so as to allow for incomming connections, you must check the ”enable active X and socket clients” in TWS Bjørn Eraker You can now check that the connection works by using a command such as portfolioData = IBMatlab(’action’,’portfolio’,’type’,’positions’); Bjørn Eraker Structures The output variable ’portfolioData’ is a structure. Structures are variables with ’subvariables’. For example, I can define a structure S.x=2 S.y=3 such that the variable S (structure) has two member variables x and y each taking numerical values (2 and 3). Bjørn Eraker If the connection works it will return a structure: portfolioData = 17 struct array with fields: symbol localSymbol exchange secType currency right expiry strike position marketValue marketPrice averageCost realizedPnL unrealizedPnL contract Bjørn Eraker You can now access the data in several ways. For example, clicking on it will give you a spreadsheet like interface: You can see that I have a couple of spx options positions and 6 futures. Bjørn Eraker Getting live market data You can access live market data for a stock through: data = IBMatlab(’action’,’query’, ’symbol’,’KO’) which gives the latest price data for Coca Cola stock (ticker KO) Bjørn Eraker Getting historical data for a stock You can similarly get historical data: data = IBMatlab(’action’,’history’, ’symbol’,’KO’) which gives the latest price data for Coca Cola stock (ticker KO) Bjørn Eraker The output structure is data = struct with fields: dateNum: [1841 double] dateTime: {1841 cell} open: [1841 double] high: [1841 double] low: [1841 double] close: [1841 double] volume: [1841 double] count: [1841 double] WAP: [1841 double] hasGaps: [1841 logical] Bjørn Eraker We can now plot the data we just gathered against calendar time (data.dateNum) plot(data.dateNum,data.close) datetick Here data.close is the closing prices for each one-minute interval in the data. Bjørn Eraker The plot should look something like 354 352 350 348 346 344 342 340 03:00 06:00 09:00 12:00 15:00 18:00 Note that I used the command ’datetick’ to get the x-axis to display times. Bjørn Eraker Placing orders Here data.close is the closing prices for each one-minute interval in the data. Bjørn Eraker Placing orders A basic stock buy order can be placed as follows: orderId = IBMatlab(’action’,’BUY’,’symbol’,’GOOG’,’quantity’,100,... ’type’,’LMT’, ’limitPrice’,3000); At the time of writing this, the share price of Google was at 3023. The order will then hang in the system and not execute unless the price drops Bjørn Eraker The buy order will now show in TWS: Bjørn Eraker Combo orders A combo order is an order to buy and/or sell multiple securities with a single order. Here’s an example of a combo order to sell a SPY straddle: ord=IBMatlab(’action’,’Sell’,’quantity’,1,... ’SecType’,’OPT’,’multiplier’,100, ’type’,’LMT’, ’limitPrice’, 7.8, ’symbol’,’SPY’,’expiry’,20231208, ’right’,{’Call’,’Put’},... ’strike’,[455,455], ’ComboActions’,{’Buy’,’Buy’}) Note here that the combo-order will sell 1 single 395 strike straddle (quantity 1). The first action parameter ’Sell’ tells IBmatlab to sell the straddle. The ’comboActions’ parameter with argument ’Buy’,’Buy’ seems counterintuitive, but tells IB to not sell-sell (creating a buy). Bjørn Eraker The order window in TWS will look like this Notice here that the limit order is for BOTH the put and the call. The order will execute ONLY if I can get filled on both at the same time. In this case, my limit price is way outside of the current market prices (see next slide). The above screenshot is from 2022. Bjørn Eraker SPY prices on Nov 21, 2022 Bjørn Eraker Here I am trying to sell the straddle at $10 but the prices are 2.16-2.18. To guarantee a fill I would have to sell at 2.16. Bjørn Eraker Checking order status You can inquire about open orders through: data = IBMatlab(’action’,’query’, ’type’,’open’) The ”data” variable will be a structure with order ids and the state of the order. It only returns open (non-executed) orders. Bjørn Eraker Canceling an order You can cancel an order : % If the requested order is still open if ~isempty(IBMatlab(’action’,’query’,’type’,’open’,’OrderId’,orderId)) % Cancel the requested order data = IBMatlab(’action’,’CANCEL’, ’orderID’,orderId); end Here the code first checks if the order is still open - if it has been executed it cannot be canceled, right? Then, if it is open, the order is canceled. Bjørn Eraker