如何使用getContract下载twsInstrument的历史数据?
从交互式经纪商下载数据时,一些未来合约可以正确下载,而另一些则不能。
R 控制台命令:
icegasoil_feb <- getContract("GOILG2")
Connected with clientId 100.
Error in buildIBcontract(symbol = instrument, tws = tws, addIBslot = addIBslot, :
Could not create valid twsContract.
GOI may not be a valid CASH. Disconnected.
使用 getBAT 时的下一个错误是:
getBAT("ZWH2")
Connected with clientId 120.
waiting for TWS reply on ZW ............failed.
waiting for TWS reply on ZW ....failed.
waiting for TWS reply on ZW ....failed.
Disconnecting ...
NULL
Failure:
1: In errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106, :
Historical Market Data Service error message:No data of type DayChart is available for
the exchange 'CBOT' and the security type 'Futures' and '5 d' and '1 min'
able for the exchange 'CBOT' and the security type 'Futures' and '5 d' and '1 min'
When downloading data from interactive brokers some future contracts can be downloaded properly others not.
R console command:
icegasoil_feb <- getContract("GOILG2")
Connected with clientId 100.
Error in buildIBcontract(symbol = instrument, tws = tws, addIBslot = addIBslot, :
Could not create valid twsContract.
GOI may not be a valid CASH. Disconnected.
Next error when using getBAT is :
getBAT("ZWH2")
Connected with clientId 120.
waiting for TWS reply on ZW ............failed.
waiting for TWS reply on ZW ....failed.
waiting for TWS reply on ZW ....failed.
Disconnecting ...
NULL
Failure:
1: In errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106, :
Historical Market Data Service error message:No data of type DayChart is available for
the exchange 'CBOT' and the security type 'Futures' and '5 d' and '1 min'
able for the exchange 'CBOT' and the security type 'Futures' and '5 d' and '1 min'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果更新就不会出现您遇到的第一个问题
金融工具。
在修订版 888 之前,
FinancialInstrument:::parse_id
--由 twsInstrument 内部使用——会想到像这样的符号
“GOILG2”的 root_id 应该为“GO”,因为它会将“ILG2”视为
4 个字符后缀,类似于盈透证券用于单个字符的后缀
股票期货。解决这个问题的一种方法是使用下划线分隔
root_id 来自 suffix_id,这样
parse_id
就不必处理不明确的 ID。因此,
getContract("GOIL_G2")
应该有效,并且仍然有效仪器 ID 的推荐格式。也就是说,如果你更新
FinancialInstrument,它将按原样工作。
第二个问题有点棘手。基本上不止一份合同
发现对应于“ZWH2”并且使用了“错误”的(场内交易
而不是电子的)。在找到解决方案之前,请允许我给出
一点背景。
twsInstrument 包的构建目的是使用 Interactive
经纪商帮助我更新我已经拥有的工具的元数据
使用 FinancialInstrument 包定义。
它将利用所拥有的信息来收集更多信息。
当您使用
getContract
时,它将首先在本地搜索twsContract
。如果找不到它,那么它将查看仪器元数据是否已定义
在
FinancialInstrument:::.instrument
环境中。如果是的话,该信息将用于创建可以传递给的
twsContract
的外壳IBrokers:::reqContractDetails
,它将填补缺失的部分。如果有没有此符号的工具定义,则
FinancialInstrument:::parse_id
将找出
IBrokers:::reqContractDetails
所需的信息。如果盈透证券有多个与所给信息相匹配的合约,它将
返回所有这些的列表。不幸的是,当我
写了 twsInstrument。因此,仅使用列表的第一个元素。
FWIW,IB API 似乎确实试图明智地返回哪个合约
首先,但是当它给你带来不同的结果时,这实际上会导致挫败感
例如,合同取决于您最后查看的合同。
就您而言,您要求提供有关“ZWH2”的数据。第一份合同是
reqContractDetails
返回的将是在“CBOT”上交易的未来,但当您从您收到的错误消息可以看出,数据不可用。那是
因为您确实想要在“ECBOT”上交易的。下面展示了如何
查看
IBrokers:::reqContractDetails
返回的长度为 2 的列表。确保您获得所需合同的方法是使用足够的信息
reqContractDetails 找不到多个匹配项。
例如,
define_futures
使工具的primary_id
基于以下值twsContract 中的“本地”。在本例中,它是“ZW_MAR12”。如果你想要
id 为“ZWH2”,您可以使用
FinancialInstrument:::instrument_attr
更改它。或者,您可以仅使用 FinancialInstrument 定义工具
确保提供交换的软件包:
最后,如果您有 twsInstrument 版本 233 或更高版本,则以下内容
还将致力于定义工具:
twsInstrument(twsFUT("ZW", "ECBOT", "201203"))
我会早点回复,但我不经常访问。你会得到一个
如果您将问题发送至
r-sig-finance 列表,或直接发送给我(我的电子邮件地址在描述中)
包的文件)。请注意,twsInstrument 仍在开发中。
The first problem you encountered will not occur if you update
FinancialInstrument.
Prior to revision 888,
FinancialInstrument:::parse_id
--which is used internally by twsInstrument -- would have thought a Symbol like
"GOILG2" should have a root_id of "GO" because it would have seen "ILG2" as a
4 character suffix similar to the ones that Interactive Brokers uses for single
stock futures. One way around this is to use an underscore to separate the
root_id from the suffix_id so that
parse_id
does not have to deal with anambiguous id. So,
getContract("GOIL_G2")
should have worked, and is stillthe recommended format for instrument ids. That said, if you update
FinancialInstrument, it will work as is.
The second problem is a little trickier. Basically, more than one contract
was found that corresponds to "ZWH2" and the "wrong" one was used (pit-traded
instead of the electronic). Before getting to the solution, allow me to give
a little background.
The twsInstrument package was built with the intention of using Interactive
Brokers to help me update the metadata of instruments that I had already
defined with the FinancialInstrument package.
It will take what information it has and use that to gather more information.
When you use
getContract
it will first search locally for thetwsContract
.If it cannot find it, then it will see if instrument metadata has been defined
in the
FinancialInstrument:::.instrument
environment. If so, that informationwill be used to create a shell of a
twsContract
that can be passed toIBrokers:::reqContractDetails
, which will fill in the missing parts. If thereis no instrument definition for this Symbol, then
FinancialInstrument:::parse_id
will figure out the information required by
IBrokers:::reqContractDetails
.If Interactive Brokers has several contracts that match the info given, it will
return a list of all of them. Unfortunately, I did not realize this when I
wrote twsInstrument. So, only the first element of the list will be used.
FWIW, the IB API does appear to try to be smart about which contract it returns
first, but that can actually cause frustrations when it is giving you a different
contract depending on which contract you looked at last, for example.
In your case, you are asking for data on "ZWH2". The first contract that
reqContractDetails
returns will be the future that trades on "CBOT", but as youcan see from the error message you got, that data is not available. That's
because you really want the one that trades on "ECBOT". The following shows how
to see the list of length 2 that
IBrokers:::reqContractDetails
returns.The way to ensure that you get the contract you want is to use enough information
that reqContractDetails does not find more than one match.
e.g.
define_futures
makes theprimary_id
of the instrument based off the value of"local" in the
twsContract
. In this case, it is "ZW_MAR12". If you want theid to be "ZWH2", you can change it with
FinancialInstrument:::instrument_attr
Alternatively, you could define the instrument using only the FinancialInstrument
package making sure to provide the exchange:
Finally, if you have revision 233 or later of twsInstrument, the following
will also work to define the instrument:
twsInstrument(twsFUT("ZW", "ECBOT", "201203"))
I would have responded sooner, but I don't visit SO all that often. You'll get a
quicker response about twsInstrument if you send your question to either the
r-sig-finance list, or directly to me (my e-mail address is in the DESCRIPTION
file of the package). Please note that twsInstrument is still under development.