Contracts(合约)Overview An IBApi.Contract object represents trading instruments such as a stocks, futures or options. 【译】 IBApi.Contract 对象表示金融交易工具,如股票,期货或者期权。 Every time a new request that requires a contract (i.e. market data, order placing, etc.) is sent to TWS, the platform will try to match the provided contract object with a single candidate. 【译】 每次当客户端生成一个合约请求(如市场数据,订单等),TWS平台都会尝试对请求的合同对象匹配一个申请号。 If there is more than one contract matching the same description, TWS will return an error notifying you there is an ambiguity. 【译】 如果有一个以上的合约具备相同的属性(如请求的市场数据,合约代码,类型相同),TWS会返回error信息,告知你的合约存在歧义。 In these cases the TWS needs further information to narrow down the list of contracts matching the provided description to a single element. 【译】 这种情况发生时,用户需要向TWS提交更详细的信息,以便TWS从合约中准确地找出请求的合约对象。 The best way of finding a contract’s description is within the TWS itself. Within the TWS, you can easily check a contract’s description either by double clicking it or through the Contract Info -> Description menu, which you access by right-clicking a contract in TWS: 【译】 准确地找出合约信息的最好方法是直接使用TWS。在TWS界面中,您能轻易的通过点击鼠标,通常可以在 Contract Info -> Description 菜单查找到所要合约的描述信息。
The description will then appear: 【译】 合约的描述信息如下:
Note: you can see the extended contract details by choosing Contract Info -> Details. This option will open a web page showing all available information on the contract. 【译】 备注:你能通过 Contract Info -> Details 查看合约的扩展信息。该操作将打开一个网页,展示合约的所有可用信息。 Whenever a contract description is provided via the TWS API, the TWS will try to match the given description to a single contract. This mechanism allows for great flexibility since it gives the possibility to define the same contract in multiple ways. 【译】 无论何时,TWS都会尝试通过用户提交的合约描述信息,匹配完整的信息。这样的机制提供了一个巨大的灵活性。也就是说,TWS会根据用户的请求尽最大可能的匹配合约信息。 The simplest way to define a contract is by providing its symbol, security type, currency and exchange. The vast majority of stocks, CFDs, Indexes or FX pairs can be uniquely defined through these four attributes. More complex contracts such as options and futures require some extra information due to their nature. Below are several examples for different types of instruments. 【译】 定义一份合约的最简单的方法时通过提交合约的代码(译者注,比如股票交易代码,如浦发银行600000,英特尔intl,谷歌googl)资产类型,货币符号,交易所。绝大多数的股票,差价合约,指数,或外汇可以通过这四个属性进行唯一性定义。而更复杂的合约(如期权和期货),由于其特殊性质需要额外的信息。 【译】 以下是几种不同的例子: See also: - Requesting Contract Details (获取合约详细信息)
- Stock Contract Search (股票合约的查询)
- Basic Contracts (基础合约)
- Spreads (投资组合合约)
Requesting Contract DetailsComplete details about a contract in IB's database can be retrieved using the function IBApi.EClient.reqContractDetails. 【译】 关于合约的完整信息,可以通过IBApi.EClient.reqContractDetails,从IB的数据库中获取。 This includes information about a contracts conID, symbol, local symbol, currency, etc. which is returned in a IBApi.ContractDetails object IBApi.EWrapper.contractDetail. reqContractDetails takes as an argument a Contract object which may uniquely match an argument, and unlike other API functions it can also take a Contract object which matches multiple contracts in IB's database. 【译】 它包含关于一个合约的conID(合约ID),symbol(合约符号), local symbol(本地符号), currency(货币符号)等信息,这通过IBApi.ContractDetails 返回的对象 IBApi.EWrapper.contractDetail。 reqContractDetails方法以合约的对象为参数,(合约描述信息)可以是唯一的,也可以不是,并且与其他的API函数不同,它可以从IB的数据库中匹配出多个合约信息。 (译者注:contractDetail对象,即可以匹配一个对象,也可以匹配多个对象,区别只在于您提交给TWS的数据是否足够完整和精确,例如提交期货合约时,您还需要附加其他的限制条件进行精确查询。) When there are multiple matches, they will each be returned individually to the function IBApi::EWrapper::contractDetails. 【译】 当出现1对多匹配时,每一个合约都将被独立返回给函数IBApi::EWrapper::contractDetails。 Note: Invoking reqContractDetails with a Contract object which has currency = USD will only return US contracts, even if there are non-US instruments which have the USD currency. 【译】 备注:通过合约 Contract 对象激活 reqContractDetails 方法,当合约信息中货币符号为美元 USD 时,将只返回美国的合约内容,无论该合约对象是否还包含了其他货币符号的合约对象。 (注:即便同一个合约标记,在不同的国家都存在,但通过货币符号进行锁定时,只会返回该货币符号对应的合约内容。) Request for Bond details will be returned to IBApi::EWrapper::bondContractDetails instead. Because of bond market data license restrictions, there are only a few available fields to be returned in a bond contract description, namely the minTick, exchange, currency, and short name. 【译】 如果您试图请求一个债券的合约信息,那么它将只被返回给 IBApi::EWrapper::bondContractDetails。因为债券市场受许可条件限制,因此只有少量信息被返回,通常这些信息是:minTick, exchange(交易所), currency(货币), and short name(简称). One particular use of the IBApi::EClient::reqContractDetails function is to request an option chain. See Option Chains for more details. 【译】 关于 IBApi::EClient::reqContractDetails 方法,有一个特殊的用法,就是用它获取期权链信息,关于期权链的更多信息请参考相关章节内容。 BroadTape News List【译】 BroadTape 新闻列表 The example below shows an "incomplete" news IBApi.Contract with no symbol or currency defined. In most cases using such a contract would result in an invalid contract details error since a symbol or localSymbol is required. IBApi.EClient.reqContractDetails will instead use it to obtain the whole BroadTape news chain from the TWS. 【译】 以下样例展示了如何定义 IBApi.Contract 获取一个“非完整”的合约新闻的方式。在多数情况下,需要合约符号(symbol),或者本地符号(localSymbol),这样的合约请求都会导致无效合约内容的错误。 因此,通过IBApi.EClient.reqContractDetails函数,可以从BroadTape新闻链获取到最新消息。 - Contract contract = new Contract();
- contract.secType("NEWS");
- contract.exchange("BT"); //Briefing Trader
- ...
- client.reqContractDetails(211, ContractSamples.NewsFeedForQuery());
复制代码All returned objects will be delivered via IBApi.EWrapper.contractDetails. Once all contracts have been delivered the IBApi.EWrapper.contractDetailsEnd marker will be triggered to notify it. 【译】 所有返回的对象,都会通过IBApi.EWrapper.contractDetails传递(给调用者)。当所有的合约返回给用户后,IBApi.EWrapper.contractDetailsEnd 标记将被激活,告知用户合约信息获取完毕。 - public class EWrapperImpl implements EWrapper {
- ...
- @Override
- public void contractDetails(int reqId, ContractDetails contractDetails) {
- System.out.println(EWrapperMsgGenerator.contractDetails(reqId, contractDetails));
- }
- ...
- @Override
- public void contractDetailsEnd(int reqId) {
- System.out.println("ContractDetailsEnd. "+reqId+"\n");
- }
复制代码
Important: due to the potentially high amount of data resulting from such queries this request is subject to pacing. Although a request such as the above one will be answered immediately, a similar subsequent one will be kept on hold for one minute. This amount of time will increase if more such requests are performed. To prevent this narrow down the amount of eligible contracts by providing an expiration date specifying at least the year (i.e. 2016) or the year and the month (i.e. 201603 for March 2016).
【译】 重要信息:由于此类请求方式会产生大量的数据通信,因此可能导致函数返回需要花费较长时间。尽管如上样例一样的查询会即刻响应,而类似的后续请求则要停滞约1分钟。如果执行更多的类似请求,暂停的时间将会持续增加。为了避免此类情况,建议提交的请求中包含查询的时间段,如2016年,或者某个月如2016年3月,从而获取这些数据。
Stock Contract Search 【译】 股票合约的查询
Starting in API v973.02 and TWS v964, a function IBApi::EClient::reqMatchingSymbols is available to search for stock contracts. The input can be either the first few letters of the ticker symbol, or for longer strings, a character sequence matching a word in the security name. For instance to search for the stock symbol 'IBKR', the input 'I' or 'IB' can be used, as well as the word 'Interactive'. Up to 16 matching results are returned.
【译】 从TWS v964及API v973开始,赢透API提供了一个全新的函数用于查询股票合约,IBApi::EClient::reqMatchingSymbols。 它可以通过股票名称的简写形式(如浦发银行,PFYH;谷歌,Goog),或者股票代码(600000,GOOGL)进行查询,如果以简写形式查询,每一个字母代表每个单词的首字母。例如,“IBKR”是赢透在纳斯达克的股票代码,“I”或者“IB”也可以查询到赢透证券。通过模糊条件进行查询时,最多有16条记录会被返回。 - client.reqMatchingSymbols(211, "IB");
复制代码
Matching stock contracts are returned to IBApi::EWrapper::symbolSamples with information about types of derivative contracts which exist (warrants, options, dutch warrants, futures).
【译】 匹配到的结果,会返回给 IBApi::EWrapper::symbolSamples,如果合约还有衍生品的话,这些信息也会同时被返回给用户(例如,权证,期权,荷兰权证,期货)。
- @Override
- public void symbolSamples(int reqId, ContractDescription[] contractDescriptions) {
- System.out.println("Contract Descriptions. Request: " + reqId + "\n");
- for (ContractDescription cd : contractDescriptions) {
- Contract c = cd.contract();
- StringBuilder derivativeSecTypesSB = new StringBuilder();
- for (String str : cd.derivativeSecTypes()) {
- derivativeSecTypesSB.append(str);
- derivativeSecTypesSB.append(",");
- }
- System.out.print("Contract. ConId: " + c.conid() + ", Symbol: " + c.symbol() + ", SecType: " + c.secType() +
- ", PrimaryExch: " + c.primaryExch() + ", Currency: " + c.currency() +
- ", DerivativeSecTypes:[" + derivativeSecTypesSB.toString() + "]");
- }
- System.out.println();
- }
复制代码
Basic Contracts(基础合约类型) FX Pairs (外汇) - Contract contract = new Contract();
- contract.symbol("EUR");
- contract.secType("CASH");
- contract.currency("GBP");
- contract.exchange("IDEALPRO");
复制代码
Stocks (股票) - Contract contract = new Contract();
- contract.conid(46636665);
- contract.secType("STK");
- //contract.currency("USD");
- //In the API side, NASDAQ is always defined as ISLAND
- contract.exchange("SEHK");
复制代码
For certain smart-routed stock contracts that have the same symbol, currency and exchange, you would also need to specify the primary exchange attribute to uniquely define the contract. This should be defined as the native exchange of a contract, and is good practice for all stocks:
【译】 对于可能存在相同符号,为了更快的获取到对应股票合约信息,除了股票代码,货币符号,交易所,你还需要指定主要的交易所标记,以便唯一地确定对应的合约,具体实现如下: - Contract contract = new Contract();
- contract.symbol("MSFT");
- contract.secType("STK");
- contract.currency("USD");
- contract.exchange("SMART");
- // Specify the Primary Exchange attribute to avoid contract ambiguity
- // (there is an ambiguity because there is also a MSFT contract with primary exchange = "AEB")
- contract.primaryExch("ISLAND");
复制代码
(注:primaryExch 字段指定主要交易所,避免由于交易所符号不清晰,导致歧义。)
As an alternative to defining the exchange and primary exchange fields, it is possible to specify the exchange field as "Exchange:Primary Exchange", e.g. "SMART:NASDAQ". The colon ":" or slash "/" separating this fields is known as the "Component Exchange Separator" and defined in the API Settings in Global Configuration.
【译】 作为主要交易所和交易所字段的替代方法,可以通过指定字段“Exchange:Primary Exchange”的方法,例如:"SMART:NASDAQ",使用“:”或“/”符号区分字段。
Indexes(指数)
ISINs for indices which are available in IB's database are available in the API as of TWS 965+.
【译】 ISINs用于指示在在TWS965+版的IB的数据库中有哪些可用的API。 - Contract contract = new Contract();
- contract.symbol("DAX");
- contract.secType("IND");
- contract.currency("EUR");
- contract.exchange("DTB");
复制代码
CFDs (差价合约) - Contract contract = new Contract();
- contract.symbol("IBDE30");
- contract.secType("CFD");
- contract.currency("EUR");
- contract.exchange("SMART");
复制代码
Futures(期货)
A regular futures contract is commonly defined using an expiry and the symbol field defined as the symbol of the underlying.
【译】 标准期货合约通常使用到期日和使用symbol字段作为基础标记进行定义。 - Contract contract = new Contract();
- contract.symbol("ES");
- contract.secType("FUT");
- contract.currency("USD");
- contract.exchange("GLOBEX");
- contract.lastTradeDateOrContractMonth("201803");
复制代码
By contract the 'local symbol' field is IB's symbol for the future itself (the Symbol within the TWS' Contract Description dialog). Since a local symbol uniquely defines a future, an expiry is not necessary.
【译】 作为对比,“local symbol” 标记,在IB的标记为期货专用(在TWS合约详细信息对话框里)。当使用“local symbol”定义期货时,“合约到期日”字段不再需要。
- Contract contract = new Contract();
- contract.localSymbol("ESU6");
- contract.secType("FUT");
- contract.currency("USD");
- contract.exchange("GLOBEX");
复制代码
- Occasionally, you can expect to have more than a single future contract for the same underlying with the same expiry. To rule out the ambiguity, the contract's multiplier can be given as shown below:
- 【译】 某些时候,您可能期望获得多于单一期货合约的多个拥有共同基础标记及合约到期日(注:例如某一天到期的多个棉花期货合约)。为了处理歧义性,可以设置合约乘数:
复制代码
- Contract contract = new Contract();
- contract.symbol("DAX");
- contract.secType("FUT");
- contract.currency("EUR");
- contract.exchange("DTB");
- contract.lastTradeDateOrContractMonth("201609");
- contract.multiplier("5");
复制代码
It is possible to use continuous futures contracts to receive historical data, real time bars, or contract details (requires TWS v966+). Continuous futures cannot be used with real time data or to place orders.
【译】 可以通过设置连续合约获取某合约的历史数据,时间轴,或者合约详细信息(需要TWS v966+以上版本支持)。连续期货合约不能用于实时数据,或着提交订单。
- Contract contract = new Contract();
- contract.symbol("ES");
- contract.secType("CONTFUT");
- contract.exchange("GLOBEX");
复制代码
The security type "FUT+CONTFUT" can be used to request contract details about the futures and continuous futures on an underlying. This security type cannot be used with other functionality.
【译】 资产类型“FUT+CONTFUT”可以被用于查询合约详细信息及合约历史信息,除此之外不能被其他合约类型使用。 - Contract contract = new Contract();
- contract.symbol("ES");
- contract.secType("FUT+CONTFUT");
- contract.exchange("GLOBEX");
复制代码
Options(股票期权合约)
Options, like futures, also require an expiration date plus a strike and a multiplier:
【译】 如同期货,期权合约也需要到期日,以及一个期权合约乘数(注:也就是获取在某一天到期的某期权不同价位的情况) - Contract contract = new Contract();
- contract.symbol("GOOG");
- contract.secType("OPT");
- contract.currency("USD");
- contract.exchange("BOX");
- contract.lastTradeDateOrContractMonth("20170120");
- contract.right("C");
- contract.strike(615);
- contract.multiplier("100");
复制代码
It is not unusual to find many option contracts with an almost identical description (i.e. underlying symbol, strike, last trading date, multiplier, etc.). Adding more details such as the trading class will help:
【译】 查找几乎拥有相同属性的期权合约(如基础标识,竞价,最近交易时间,乘数等),通过增加更多的信息,可以帮助用户更容易的找到对应的期权合约。 - Contract contract = new Contract();
- contract.symbol("SANT");
- contract.secType("OPT");
- contract.currency("EUR");
- contract.exchange("MEFFRV");
- contract.lastTradeDateOrContractMonth("20190621");
- contract.right("C");
- contract.strike(7.5);
- contract.multiplier("100");
- contract.tradingClass("SANEU");
复制代码
The OCC options symbol can be used to define an option contract in the API through the option's 'local symbol' field.
【译】 使用OCC属性标记,可以在API中用来定义一个期权合约。 - Contract contract = new Contract();
- //Watch out for the spaces within the local symbol!
- contract.localSymbol("C DBK DEC 20 1600");
- contract.secType("OPT");
- contract.exchange("DTB");
- contract.currency("EUR");
复制代码
|