Create a connection to the IB Trader Workstation℠ and create a market order based on historical and current data for a security. You can also create orders for a different instrument, such as a futures contract.
Before creating the connection, you must enter your credentials and run the IB Trader Workstation℠ application.
要运行此示例,您必须安装Financial Toolbox™。
确保IB TraderWorkstation‖应用程序正在运行,并且启用API连接。在IB Trader工作站遵循以下步骤。
要打开Trader Workstation配置(模拟交易)对话框,请选择File>全局配置。
选择API>设置。
确保这一点启用ActiveX和套接字客户端复选框是选中的。
连接到IB TraderWorkstation‖并创建连接IB.
using the local host and default port number7496.
。
ib = ibtws('',7496);
当。。。的时候接受传入的连接尝试
消息出现在IB TraderWorkstation‖中,单击是。
创建IB TraderWorkstation¶IContract
目的IBContract.
。This object specifies the security. Retrieve data for Microsoft® stock. Specifying聪明
由于Exchange允许交互式Brokers®确定用于数据检索的哪个场所。要澄清任何歧义,请将目的地的主要交换机设置为纳斯达克
。要检索美元计价的库存,将货币类型设置为美元
。Setting currency type is useful when stocks are dual-listed or multi-listed across different jurisdictions.
IBContract = IB.Handle.createContract;IBContract.Symbol =.'msft';IBContract.Sectype =.'stk';IBContract.。exchange ='SMART';IBContract.。primaryExchange ='纳斯达克';IBContract.currency ='美元';
定义最近20个工作日的日期范围,今天不包括。要计算适当的开始和结束日期,此代码使用天空
function from Financial Toolbox™.
bizdayconvention = 13;%i. ..公交车/ 252currentdate =今天;startDate可以= daysadd (currentdate,-20,bizDayConvention); endDate = daysadd(currentdate,-1,bizDayConvention);
Retrieve historical data for the last 20 business days.
HistTradeData =历史记录(IB,IBContract,Startdate,Enddate);
该history
函数接受其他参数,让您获取其他历史数据,如选项隐含的波动,历史波动,出价,卖价或中点。如果您未指定任何内容,则默认情况下,上次交易价格返回。
从合同中检索当前价格数据。
CurrentData = GetData(IB,IBContract)
currentData = struct with fields: LAST_PRICE: 62.8500 LAST_SIZE: 1 VOLUME: 41273 BID_PRICE: 62.8400 BID_SIZE: 17 ASK_PRICE: 62.8600 ASK_SIZE: 12
该IB Trader Workstation℠ supports various order types, including basic types such as limit orders, stop orders, and market orders.
创建IB TraderWorkstation¶我订购
目的ibmktorder.
。此对象指定订单。要购买股票,请指定动作购买
。指定购买100股,设置总量
到100.要创建市场订单,请指定订单类型为MKT.
。
ibmktorder = ib.handle.createOrder;ibmktorder.action =.'购买';ibmktorder.totalquantity = 100;ibmktorder.ordertype =.'MKT';
设置唯一的订单标识符并将顺序发送到交互式Brokers®。
id = OrderID(IB);结果= CreateOrder(IB,IBContract,IBMKTorder,ID)
结果=带字段的结构:状态:'提交'填充:0剩余:0 avg_fill_price:0 perm_id:'1621177315'parent_id:0 last_fill_price:0 client_id:0 why_held:''
You can trade various instruments using the IB Trader Workstation℠ API, including equities, futures, options, futures options, and foreign currencies.
IBFutures.
e-mini标准和穷人的500个期货合约在CME Globex上,2013年12月到期。指定符号es.
,安全类型作为期货合约FUT.
,到期为ayyyymm.
日期格式,交换为globex.
,以及货币美元
。
ibfutures = ib.handle.createContract;IBFutures.Symbol =.'ES';IBFutures.Sectype =.'FUT';IBFutures.。expiry ='201312';%12月ibfutures.exchange =.'globex';IBFutures.。currency ='美元';
检索期货数据并使用该期货数据发送订单getdata
andcreateOrder.
functions.
关闭(IB)
关
|createOrder.
|getdata
|history
|ibtws.