如何使用getContract下载twsInstrument的历史数据?

发布于 2024-12-21 14:22:46 字数 935 浏览 1 评论 0原文

从交互式经纪商下载数据时,一些未来合约可以正确下载,而另一些则不能。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2024-12-28 14:22:46

如果更新就不会出现您遇到的第一个问题
金融工具。

在修订版 888 之前,FinancialInstrument:::parse_id --
由 twsInstrument 内部使用——会想到像这样的符号
“GOILG2”的 root_id 应该为“GO”,因为它会将“ILG2”视为
4 个字符后缀,类似于盈透证券用于单个字符的后缀
股票期货。解决这个问题的一种方法是使用下划线分隔
root_id 来自 suffix_id,这样 parse_id 就不必处理
不明确的 ID。因此, getContract("GOIL_G2") 应该有效,并且仍然有效
仪器 ID 的推荐格式。也就是说,如果你更新
FinancialInstrument,它将按原样工作。

> require("twsInstrument")
> getContract("GOILG2")
Connected with clientId 100.
Checking to see if other 'type's have a pre-defined currency.
Request complete: GOIL FUT USD.
Disconnected.
List of 16
$ conId          : chr "34134707"
$ symbol         : chr "GOIL"
$ sectype        : chr "FUT"
$ exch           : chr "IPE"
$ primary        : chr ""
$ expiry         : chr "20120210"
$ strike         : chr "0"
$ currency       : chr "USD"
$ right          : chr ""
$ local          : chr "GOILG2"
$ multiplier     : chr "100"
$ combo_legs_desc: chr ""
$ comboleg       : chr ""
$ include_expired: chr "0"
$ secIdType      : chr ""
$ secId          : chr ""

第二个问题有点棘手。基本上不止一份合同
发现对应于“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 的列表。

require("IBrokers")
fut <- twsContract()
fut$symbol <- 'ZW'
fut$sectype <- 'FUT'
fut$expiry <- '201203'
fut$currency <- 'USD'
tws <- ConnectIB()
reqContractDetails(tws, fut)
twsDisconnect(tws)

确保您获得所需合同的方法是使用足够的信息
reqContractDetails 找不到多个匹配项。
例如,

> define_futures("ZW", "ECBOT", "201203")
Connected with clientId 100.
Request complete: ZW FUT USD.
Disconnected.
[1] "ZW_MAR12"

> getBAT("ZW_MAR12")
Connected with clientId 120.
waiting for TWS reply on ZW ....... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ZW .... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ZW .... done.
Pausing 10 seconds between requests ...
Disconnecting ... 
[1] "ZW_MAR12"

define_futures 使工具的 primary_id 基于以下值
twsContract 中的“本地”。在本例中,它是“ZW_MAR12”。如果你想要
id 为“ZWH2”,您可以使用 FinancialInstrument:::instrument_attr 更改它。

> instrument_attr("ZW_MAR12", "primary_id", "ZWH2")
>  # Now your original code will work
> getBAT("ZWH2")
Connected with clientId 120.
waiting for TWS reply on ZW ....... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ZW .... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ZW .... done.
Pausing 10 seconds between requests ...
Disconnecting ... 
[1] "ZWH2"

或者,您可以仅使用 FinancialInstrument 定义工具
确保提供交换的软件包:

future("ZW", currency("USD"), 5000, exchange='ECBOT')
future_series("ZWH2")
getBAT("ZWH2")

最后,如果您有 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 an
ambiguous id. So, getContract("GOIL_G2") should have worked, and is still
the recommended format for instrument ids. That said, if you update
FinancialInstrument, it will work as is.

> require("twsInstrument")
> getContract("GOILG2")
Connected with clientId 100.
Checking to see if other 'type's have a pre-defined currency.
Request complete: GOIL FUT USD.
Disconnected.
List of 16
$ conId          : chr "34134707"
$ symbol         : chr "GOIL"
$ sectype        : chr "FUT"
$ exch           : chr "IPE"
$ primary        : chr ""
$ expiry         : chr "20120210"
$ strike         : chr "0"
$ currency       : chr "USD"
$ right          : chr ""
$ local          : chr "GOILG2"
$ multiplier     : chr "100"
$ combo_legs_desc: chr ""
$ comboleg       : chr ""
$ include_expired: chr "0"
$ secIdType      : chr ""
$ secId          : chr ""

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 the twsContract.
If it cannot find it, then it will see if instrument metadata has been defined
in the FinancialInstrument:::.instrument environment. If so, that information
will be used to create a shell of a twsContract that can be passed to
IBrokers:::reqContractDetails, which will fill in the missing parts. If there
is 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 you
can 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.

require("IBrokers")
fut <- twsContract()
fut$symbol <- 'ZW'
fut$sectype <- 'FUT'
fut$expiry <- '201203'
fut$currency <- 'USD'
tws <- ConnectIB()
reqContractDetails(tws, fut)
twsDisconnect(tws)

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("ZW", "ECBOT", "201203")
Connected with clientId 100.
Request complete: ZW FUT USD.
Disconnected.
[1] "ZW_MAR12"

> getBAT("ZW_MAR12")
Connected with clientId 120.
waiting for TWS reply on ZW ....... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ZW .... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ZW .... done.
Pausing 10 seconds between requests ...
Disconnecting ... 
[1] "ZW_MAR12"

define_futures makes the primary_id of the instrument based off the value of
"local" in the twsContract. In this case, it is "ZW_MAR12". If you want the
id to be "ZWH2", you can change it with FinancialInstrument:::instrument_attr

> instrument_attr("ZW_MAR12", "primary_id", "ZWH2")
>  # Now your original code will work
> getBAT("ZWH2")
Connected with clientId 120.
waiting for TWS reply on ZW ....... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ZW .... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ZW .... done.
Pausing 10 seconds between requests ...
Disconnecting ... 
[1] "ZWH2"

Alternatively, you could define the instrument using only the FinancialInstrument
package making sure to provide the exchange:

future("ZW", currency("USD"), 5000, exchange='ECBOT')
future_series("ZWH2")
getBAT("ZWH2")

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文