IBrokers 要求历史期货合约数据?

发布于 2024-12-07 06:13:17 字数 547 浏览 0 评论 0原文

我试图请求历史期货数据,但对于初学者来说,ibrokers.pdf 文档的记录不够充分。 例如 Gold Miny 合约 Dec11 NYSELIFFE:

goldminy<-twsFuture("YG","NYSELIFFE","201112",multiplier="33.2")
reqHistoricalData(conn,
Contract= "goldminy",
endDateTime"",
barSize = "1 S",
duration = "1 D",
useRTH = "0",
whatToShow = "TRADES","BID", "ASK", "BID_ASK",
timeFormat = "1",
tzone = "",
verbose = TRUE,
tickerId = "1",
eventHistoricalData,
file)

我也不知道如何正确指定一些数据参数?

显示什么?我需要日期、时间、出价、出价、询问、询问尺寸、最后、最后尺寸、交易量

tickerID ?

事件历史数据?

文件 ?

I tried to request historical futures data but for a beginner the ibrokers.pdf document is not well enough documented.
example Gold Miny Contract Dec11 NYSELIFFE:

goldminy<-twsFuture("YG","NYSELIFFE","201112",multiplier="33.2")
reqHistoricalData(conn,
Contract= "goldminy",
endDateTime"",
barSize = "1 S",
duration = "1 D",
useRTH = "0",
whatToShow = "TRADES","BID", "ASK", "BID_ASK",
timeFormat = "1",
tzone = "",
verbose = TRUE,
tickerId = "1",
eventHistoricalData,
file)

I also don't know how to specify some of the data parameters correctly ?

whatToShow ? i need Date,Time,BidSize,Bid,Ask,AskSize,Last,LastSize,Volume

tickerID ?

eventHistoricalData ?

file ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

扶醉桌前 2024-12-14 06:13:17

我编写了 twsInstrument 包(在 RForge 上)来缓解这些问题头痛。
如果您提供任何合理的信息,getContract 将为您找到合同。这些格式中的任何一种都应该有效:
“YG_Z1”、“YG_Z11”、“YGZ1”、“YGZ11”、“YGZ2011”、“YGDEC2011”、“YG_DEC2011”等
(您也可以使用 conId,或给它一个仪器对象,或者工具对象的名称)

> library(twsInstrument)
> goldminy <- getContract("YG_Z1")
Connected with clientId 100.
Contract details request complete. Disconnected.
> goldminy
List of 16
 $ conId          : chr "42334455"
 $ symbol         : chr "YG"
 $ sectype        : chr "FUT"
 $ exch           : chr "NYSELIFFE"
 $ primary        : chr ""
 $ expiry         : chr "20111228"
 $ strike         : chr "0"
 $ currency       : chr "USD"
 $ right          : chr ""
 $ local          : chr "YG   DEC 11"
 $ multiplier     : chr "33.2"
 $ combo_legs_desc: chr ""
 $ comboleg       : chr ""
 $ include_expired: chr "0"
 $ secIdType      : chr ""
 $ secId          : chr ""

我没有订阅 NYSELIFFE 的市场数据,因此我将使用 Dec 2011 年 e-mini S&P 未来为本答案的其余部分。

可以获取这样的历史数据

tws <- twsConnect()
hist.data <- reqHistoricalData(tws, getContract("ES_Z1"))

这将为您返回这些列,并且全部都是“TRADES”数据,

> colnames(hist.data)
[1] "ESZ1.Open"    "ESZ1.High"    "ESZ1.Low"     "ESZ1.Close"   "ESZ1.Volume" 
[6] "ESZ1.WAP"     "ESZ1.hasGaps" "ESZ1.Count"  

whatToShow 必须是“TRADES”、“BID”、“ASK”或“其中之一”出价_问价'。如果您的请求使用whatToShow='BID',那么您将获得 BID 价格的 OHLC 等。 “BID_ASK”表示卖价将用于最高价,买价将用于最低价。

既然您说这个小插图太高级了,那么值得重复的是,盈透证券将历史数据请求限制为每 60 秒 6 次。因此,您应该在每个请求之间暂停 10 秒(或者为了获取大量数据,我通常在发出 3 个请求后暂停 30 秒,这样如果我有某个东西的 BID 数据,我也可能有它的 ASK 数据)

函数 getBAT将下载 BID、ASK 和 TRADES 数据,并仅将这些数据的收盘值合并到单个 xts 对象中,如下所示:

> getBAT("ES_Z1")
Connected with clientId 120.
waiting for TWS reply on ES ............. done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ES .... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ES .... done.
Pausing 10 seconds between requests ...
Disconnecting ... 
[1] "ES_Z1"
> tail(ES_Z1)
                    ES.Bid.Price ES.Ask.Price ES.Trade.Price ES.Mid.Price
2011-09-27 15:09:00      1170.25      1170.50        1170.50     1170.375
2011-09-27 15:10:00      1170.50      1170.75        1170.50     1170.625
2011-09-27 15:11:00      1171.25      1171.50        1171.25     1171.375
2011-09-27 15:12:00      1171.50      1171.75        1171.50     1171.625
2011-09-27 15:13:00      1171.25      1171.50        1171.25     1171.375
2011-09-27 15:14:00      1169.75      1170.00        1170.00     1169.875
                    ES.Volume
2011-09-27 15:09:00      6830
2011-09-27 15:10:00      4509
2011-09-27 15:11:00      4902
2011-09-27 15:12:00      6089
2011-09-27 15:13:00      6075
2011-09-27 15:14:00     14380

您要求提供 LastSize 和 Volume。 getBAT 返回的“交易量”是柱线时间内的交易总量。因此,对于 1 分钟柱,它是该 1 分钟内发生的总交易量。

这是不使用 twsInstrument 的答案:
我几乎可以肯定这会起作用,但正如我所说,我没有所需的市场数据订阅,所以我无法测试。

reqHistoricalData(tws, twsFuture("YG","NYSELIFFE","201112"))

再次使用 e-mini S&P:

> mydata <- reqHistoricalData(tws, twsFuture("ES","GLOBEX","201112"), barSize='1 min', duration='5 D', useRTH='0', whatToShow='TRADES')
waiting for TWS reply on ES .... done.

> head(mydata)
                    ESZ1.Open ESZ1.High ESZ1.Low ESZ1.Close ESZ1.Volume ESZ1.WAP ESZ1.hasGaps ESZ1.Count
2011-09-21 15:30:00   1155.25   1156.25  1155.00    1155.75        3335  1155.50            0        607
2011-09-21 15:31:00   1155.75   1156.25  1155.50    1155.75         917  1155.95            0        164
2011-09-21 15:32:00   1155.75   1156.25  1155.50    1156.00         859  1155.90            0        168
2011-09-21 15:33:00   1156.00   1156.25  1155.50    1155.75         642  1155.83            0        134
2011-09-21 15:34:00   1155.50   1156.00  1155.25    1155.25        1768  1155.65            0        232
2011-09-21 15:35:00   1155.25   1155.75  1155.25    1155.25         479  1155.45            0         94

您尝试的问题之一是,如果您使用的 barSize 为“1 S”,则您的持续时间不能大于“60 S”,请参阅 IB历史数据限制

I wrote the twsInstrument package (on RForge) to alleviate these sorts of headaches.
getContract will find the contract for you if you give it anything reasonable. Any of these formats should work:
"YG_Z1", "YG_Z11", "YGZ1", "YGZ11", "YGZ2011", "YGDEC2011", "YG_DEC2011", etc.
(also you could use the conId, or give it an instrument object, or the name of an instrument object)

> library(twsInstrument)
> goldminy <- getContract("YG_Z1")
Connected with clientId 100.
Contract details request complete. Disconnected.
> goldminy
List of 16
 $ conId          : chr "42334455"
 $ symbol         : chr "YG"
 $ sectype        : chr "FUT"
 $ exch           : chr "NYSELIFFE"
 $ primary        : chr ""
 $ expiry         : chr "20111228"
 $ strike         : chr "0"
 $ currency       : chr "USD"
 $ right          : chr ""
 $ local          : chr "YG   DEC 11"
 $ multiplier     : chr "33.2"
 $ combo_legs_desc: chr ""
 $ comboleg       : chr ""
 $ include_expired: chr "0"
 $ secIdType      : chr ""
 $ secId          : chr ""

I don't have a subscription to market data for NYSELIFFE, so I will use the Dec 2011 e-mini S&P future for the rest of this answer.

You could get historical data like this

tws <- twsConnect()
hist.data <- reqHistoricalData(tws, getContract("ES_Z1"))

This will give you back these columns, and it will all be 'TRADES' data

> colnames(hist.data)
[1] "ESZ1.Open"    "ESZ1.High"    "ESZ1.Low"     "ESZ1.Close"   "ESZ1.Volume" 
[6] "ESZ1.WAP"     "ESZ1.hasGaps" "ESZ1.Count"  

whatToShow must be one of 'TRADES', 'BID', 'ASK', or 'BID_ASK'. If your request uses whatToShow='BID' then you will get the OHLC etc. of the BID prices. "BID_ASK" means that the Ask price will be used for the High and the Bid price will be used for the Low.

Since you said the vignette was too advanced, it bears repeating that Interactive Brokers limits historical data requests to 6 every 60 seconds. So you should pause for 10 seconds between each request (or for getting lots of data I usually pause for 30 seconds after I make 3 requests so that if I have BID data for something I am also likely have ASK data for it)

The function getBAT will download the BID, ASK and TRADES data, and merge together only the closing values of those into a single xts object that looks like this:

> getBAT("ES_Z1")
Connected with clientId 120.
waiting for TWS reply on ES ............. done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ES .... done.
Pausing 10 seconds between requests ...
waiting for TWS reply on ES .... done.
Pausing 10 seconds between requests ...
Disconnecting ... 
[1] "ES_Z1"
> tail(ES_Z1)
                    ES.Bid.Price ES.Ask.Price ES.Trade.Price ES.Mid.Price
2011-09-27 15:09:00      1170.25      1170.50        1170.50     1170.375
2011-09-27 15:10:00      1170.50      1170.75        1170.50     1170.625
2011-09-27 15:11:00      1171.25      1171.50        1171.25     1171.375
2011-09-27 15:12:00      1171.50      1171.75        1171.50     1171.625
2011-09-27 15:13:00      1171.25      1171.50        1171.25     1171.375
2011-09-27 15:14:00      1169.75      1170.00        1170.00     1169.875
                    ES.Volume
2011-09-27 15:09:00      6830
2011-09-27 15:10:00      4509
2011-09-27 15:11:00      4902
2011-09-27 15:12:00      6089
2011-09-27 15:13:00      6075
2011-09-27 15:14:00     14380

You asked for both LastSize and Volume. The "Volume" that getBAT returns is the total amount traded over the time of the bar. So, with 1 minute bars, it's the total volume that took place in that 1 minute.

Here's an answer that doesn't use twsInstrument:
I'm almost certain this will work, but as I said, I don't have the required market data subscription, so I can't test.

reqHistoricalData(tws, twsFuture("YG","NYSELIFFE","201112"))

Using the e-mini S&P again:

> mydata <- reqHistoricalData(tws, twsFuture("ES","GLOBEX","201112"), barSize='1 min', duration='5 D', useRTH='0', whatToShow='TRADES')
waiting for TWS reply on ES .... done.

> head(mydata)
                    ESZ1.Open ESZ1.High ESZ1.Low ESZ1.Close ESZ1.Volume ESZ1.WAP ESZ1.hasGaps ESZ1.Count
2011-09-21 15:30:00   1155.25   1156.25  1155.00    1155.75        3335  1155.50            0        607
2011-09-21 15:31:00   1155.75   1156.25  1155.50    1155.75         917  1155.95            0        164
2011-09-21 15:32:00   1155.75   1156.25  1155.50    1156.00         859  1155.90            0        168
2011-09-21 15:33:00   1156.00   1156.25  1155.50    1155.75         642  1155.83            0        134
2011-09-21 15:34:00   1155.50   1156.00  1155.25    1155.25        1768  1155.65            0        232
2011-09-21 15:35:00   1155.25   1155.75  1155.25    1155.25         479  1155.45            0         94

One of the problems with your attempt is that if you're using a barSize of '1 S', your duration cannot be greater than '60 S' See IB Historical Data Limitations

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