R Ibrokers twsOPT 用法

发布于 2024-11-30 08:17:36 字数 734 浏览 1 评论 0原文

reqMktData(tws,twsOPT("AAPL 110820C00390000"))

reqMktData(tws,twsOPT("AAPL110820C00390000"))

导致: TWS 消息:2 1 200 未找到请求的安全定义

为什么?

reqMktData(tws,twsSTK("AAPL"))

工作正常。

联机帮助页显示:

twsOption(local,
          expiry="",
          strike="",
          right="",
          exch="SMART",
          primary="",
          currency='USD',
          symbol='',
          multiplier="100",
          include_expired='0',
          conId=0)

TWS 上的期权合约具有某些与标准数据请求不同的规则。

需要本地符号。这可以在 TWS 主屏幕上的合约详细信息下找到,或者通过 www.interactivebrokers.com 网站找到。

由于需要本地符号,所有其他值都是多余的。最好简单地指定本地名称并让 TWS 管理查找。

reqMktData(tws,twsOPT("AAPL 110820C00390000"))

or

reqMktData(tws,twsOPT("AAPL110820C00390000"))

result in:
TWS Message: 2 1 200 No security definition has been found for the request

Why?

reqMktData(tws,twsSTK("AAPL"))

works fine.

The manpage says:

twsOption(local,
          expiry="",
          strike="",
          right="",
          exch="SMART",
          primary="",
          currency='USD',
          symbol='',
          multiplier="100",
          include_expired='0',
          conId=0)

Option contracts on the TWS have certain rules which are different than standard data requests.

The local symbol is required. This can be found on the main TWS screen under contract details, or via the web at www.interactivebrokers.com

Since the local symbol is required, all other values are redundant. It is best to simply specify the local name and let the TWS manage the lookup.

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

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

发布评论

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

评论(2

晒暮凉 2024-12-07 08:17:36

你没有得到正确的本地信息。正确的格式使用六个字符宽的字段(OSI 规范),而您分别尝试使用 5 和 4。

当然,您不需要使用本地,但这需要跨不同参数的更多详细信息。

> reqContractDetails(ibg, twsOPT("AAPL  110820C00390000"))
[[1]]
List of 18
  $ version       : chr "6"
  $ contract      :List of 16
..$ conId          : chr "86896934"
..$ symbol         : chr "AAPL"
..$ sectype        : chr "OPT"
..$ exch           : chr "SMART"
..$ primary        : chr ""
..$ expiry         : chr "20110819"
..$ strike         : chr "390.0"
..$ currency       : chr "USD"
..$ right          : chr "C"
..$ local          : chr "AAPL  110820C00390000"
..$ multiplier     : chr "100"
..$ combo_legs_desc: chr ""
..$ comboleg       : chr ""
..$ include_expired: chr ""
..$ secIdType      : chr ""
..$ secId          : chr ""
..- attr(*, "class")= chr "twsContract"
$ marketName    : chr "AAPL"
$ tradingClass  : chr "AAPL"
$ conId         : chr "86896934"
$ minTick       : chr "0.01"
$ orderTypes    : chr [1:44] "ACTIVETIM" "ADJUST" "ALERT" "ALGO" ...
$ validExchanges: chr [1:12] "SMART" "AMEX" "BATS" "BOX" ...
$ priceMagnifier: chr "1"
$ underConId    : chr "265598"
$ longName      : chr "APPLE INC"
$ contractMonth : chr "201108"
$ industry      : chr "Technology"
$ category      : chr "Computers"
$ subcategory   : chr "Computers"
$ timeZoneId    : chr "EST"
$ tradingHours  : chr "20110817:0930-1600;20110818:0930-1600"
$ liquidHours   : chr "20110817:0930-1600;20110818:0930-1600"

您可以使用 as.twsContract 提取合约:

as.twsContract(reqContractDetails(ibg, twsOPT("AAPL  110820C00390000")))

或者直接调用 reqMktData 请求:

reqMktData(ibg, twsOPT("AAPL  110820C00390000"))

## OR

reqMktData(ibg, twsOPT("",symbol="AAPL",right="C", strike="390", expiry="201108"))

You are not getting the local correct. The correct format uses a field six characters wide (OSI spec), and you are trying 5 and 4, respectively.

You aren't required to use local of course, but that requires more details across different args then.

> reqContractDetails(ibg, twsOPT("AAPL  110820C00390000"))
[[1]]
List of 18
  $ version       : chr "6"
  $ contract      :List of 16
..$ conId          : chr "86896934"
..$ symbol         : chr "AAPL"
..$ sectype        : chr "OPT"
..$ exch           : chr "SMART"
..$ primary        : chr ""
..$ expiry         : chr "20110819"
..$ strike         : chr "390.0"
..$ currency       : chr "USD"
..$ right          : chr "C"
..$ local          : chr "AAPL  110820C00390000"
..$ multiplier     : chr "100"
..$ combo_legs_desc: chr ""
..$ comboleg       : chr ""
..$ include_expired: chr ""
..$ secIdType      : chr ""
..$ secId          : chr ""
..- attr(*, "class")= chr "twsContract"
$ marketName    : chr "AAPL"
$ tradingClass  : chr "AAPL"
$ conId         : chr "86896934"
$ minTick       : chr "0.01"
$ orderTypes    : chr [1:44] "ACTIVETIM" "ADJUST" "ALERT" "ALGO" ...
$ validExchanges: chr [1:12] "SMART" "AMEX" "BATS" "BOX" ...
$ priceMagnifier: chr "1"
$ underConId    : chr "265598"
$ longName      : chr "APPLE INC"
$ contractMonth : chr "201108"
$ industry      : chr "Technology"
$ category      : chr "Computers"
$ subcategory   : chr "Computers"
$ timeZoneId    : chr "EST"
$ tradingHours  : chr "20110817:0930-1600;20110818:0930-1600"
$ liquidHours   : chr "20110817:0930-1600;20110818:0930-1600"

You can either extract the contract using as.twsContract:

as.twsContract(reqContractDetails(ibg, twsOPT("AAPL  110820C00390000")))

Or just call the reqMktData request as is:

reqMktData(ibg, twsOPT("AAPL  110820C00390000"))

## OR

reqMktData(ibg, twsOPT("",symbol="AAPL",right="C", strike="390", expiry="201108"))
╰沐子 2024-12-07 08:17:36

您可以通过使用 R-Forge 上的 twsInstrument 包来避免此类问题

library(twsInstrument)

其中任何一个都会得到 twsContract

getContract("AAPL  111217P00390000") 
getContract("AAPL     111217P00390000") #number of spaces does not matter
getContract("AAPL20111217P00390000") #year can be 4 digits or 2
getContract("AAPL_111217P00390000")
getContract("AAPL111217P00390000")
getContract("AAPL111217P390")
getContract("AAPL_111217P390")
getContract("AAPL_20111217P390")
getContract("AAPL_111217P390.00")

#by conId 
getContract("93189601") 
getContract(93189601) 

所有这些都会给你同样的东西:

> getContract(93189601)
List of 16
 $ conId          : chr "93189601"
 $ symbol         : chr "AAPL"
 $ sectype        : chr "OPT"
 $ exch           : chr "SMART"
 $ primary        : chr ""
 $ expiry         : chr "20111216"
 $ strike         : chr "390"
 $ currency       : chr "USD"
 $ right          : chr "P"
 $ local          : chr "AAPL  111217P00390000"
 $ multiplier     : chr "100"
 $ combo_legs_desc: chr ""
 $ comboleg       : chr ""
 $ include_expired: chr ""
 $ secIdType      : chr ""
 $ secId          : chr ""

不幸的是,你无法获得已经过期的期权的合同详细信息。我不知道这是否是IBrokers的问题,或者Interactive Brokers是否不支持它,但是获取已过期期货的合约详细信息是没有问题的

> getContract("ESM1")
Connected with clientId 100.
Trying to resolve error in contract details. Using include_expired=1
Contract details request complete. Disconnected.
 List of 16
 $ conId          : chr "73462897"
 $ symbol         : chr "ES"
 $ sectype        : chr "FUT"
 $ exch           : chr "GLOBEX"
 $ primary        : chr ""
 $ expiry         : chr "20110617"
 $ strike         : chr "0"
 $ currency       : chr "USD"
 $ right          : chr ""
 $ local          : chr "ESM1"
 $ multiplier     : chr "50"
 $ combo_legs_desc: chr ""
 $ comboleg       : chr ""
 $ include_expired: chr "1"
 $ secIdType      : chr ""
 $ secId          : chr ""

You can avoid these types of problems by using the twsInstrument package on R-Forge

library(twsInstrument)

Any of these will get the twsContract

getContract("AAPL  111217P00390000") 
getContract("AAPL     111217P00390000") #number of spaces does not matter
getContract("AAPL20111217P00390000") #year can be 4 digits or 2
getContract("AAPL_111217P00390000")
getContract("AAPL111217P00390000")
getContract("AAPL111217P390")
getContract("AAPL_111217P390")
getContract("AAPL_20111217P390")
getContract("AAPL_111217P390.00")

#by conId 
getContract("93189601") 
getContract(93189601) 

All of those will give you the same thing:

> getContract(93189601)
List of 16
 $ conId          : chr "93189601"
 $ symbol         : chr "AAPL"
 $ sectype        : chr "OPT"
 $ exch           : chr "SMART"
 $ primary        : chr ""
 $ expiry         : chr "20111216"
 $ strike         : chr "390"
 $ currency       : chr "USD"
 $ right          : chr "P"
 $ local          : chr "AAPL  111217P00390000"
 $ multiplier     : chr "100"
 $ combo_legs_desc: chr ""
 $ comboleg       : chr ""
 $ include_expired: chr ""
 $ secIdType      : chr ""
 $ secId          : chr ""

Unfortunately, you cannot get contract details for options that have already expired. I don't know if this is a problem with IBrokers, or if Interactive Brokers doesn't support it, but getting contract details for already expired futures is no problem

> getContract("ESM1")
Connected with clientId 100.
Trying to resolve error in contract details. Using include_expired=1
Contract details request complete. Disconnected.
 List of 16
 $ conId          : chr "73462897"
 $ symbol         : chr "ES"
 $ sectype        : chr "FUT"
 $ exch           : chr "GLOBEX"
 $ primary        : chr ""
 $ expiry         : chr "20110617"
 $ strike         : chr "0"
 $ currency       : chr "USD"
 $ right          : chr ""
 $ local          : chr "ESM1"
 $ multiplier     : chr "50"
 $ combo_legs_desc: chr ""
 $ comboleg       : chr ""
 $ include_expired: chr "1"
 $ secIdType      : chr ""
 $ secId          : chr ""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文