【问题标题】:Get SPY share price获取 SPY 股价
【发布时间】:2018-03-25 00:29:25
【问题描述】:

我创建了一个demo 帐户,我正在尝试使用以下代码接收延迟报价,但到目前为止它失败了。

import re
import ib
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
from time import sleep

class Downloader(object):
    field4price = ''

    def __init__(self):
        self.tws = ibConnection('localhost', 7496, 9003)
        self.tws.register(self.tickPriceHandler, 'TickPrice')
        self.tws.connect()
        self._reqId = 5 # current request id

    def tickPriceHandler(self,msg):
        if msg.field == 4:
            self.field4price = msg.price
            #print '[debug]', msg

    def requestData(self,contract): 
        self.tws.reqMarketDataType(3)
        self.tws.reqMktData(self._reqId, contract, '', 1)
        self._reqId+=1

if __name__=='__main__':
    dl = Downloader()
    c = Contract()
    c.m_symbol = 'SPY'
    c.m_secType = 'STK'
    c.m_exchange = 'SMART'
    c.m_currency = 'USD'
    dl.requestData(c)
    sleep(3)
    print('Price - field 4: ', dl.field4price)

当我使用模拟账户时,我必须处理延迟数据,这就是我添加self.tws.reqMarketDataType(3) 的原因(参见link)。我的问题是 dl.field4price 为 SPY 符号返回一个空列表,这是不可能的。在考虑以前的代码时,我怎么能得到 SPY 的股票价格?是我做错了吗?

【问题讨论】:

  • 我不知道 ibpy 是否适用于延迟数据。我不知道延迟数据是否有效。我不知道模拟账户是否有延迟数据。我知道ticktype 是68,而不是4。ibpy 源代码在TickType.py 中没有这种类型(您应该使用它而不是硬编码4)。 IB 从 9.73 开始有一个新的 Python API,可能会引起人们的兴趣。
  • @brian 是的,我知道他们有一个 python API。也许你应该用 IB API 给我看一个类似的代码。
  • 我只是在看文档,上面写着Note: TWS Build 962 or higher is required and API version 9.72.18 or higher is suggested. 我需要更新 TWS 来测试这个,所以不是现在。只需将延迟线添加到此stackoverflow.com/a/42868938/2855515

标签: python interactive-brokers


【解决方案1】:

我不知道你是否知道,但 IB 已经强制升级,所以我现在使用的是 963 版。我刚刚添加了我的建议并将延迟的请求添加到旧样本中。我使用加拿大股票,因为我没有认购,但也许这并不重要。

from ibapi import wrapper
from ibapi.client import EClient
from ibapi.utils import iswrapper #just for decorator
from ibapi.common import *
from ibapi.contract import *
from ibapi.ticktype import *

class TestApp(wrapper.EWrapper, EClient):
    def __init__(self):
        wrapper.EWrapper.__init__(self)
        EClient.__init__(self, wrapper=self)
        self.count = 0

    @iswrapper
    def nextValidId(self, orderId:int):
        print("nextValidOrderId:", orderId)
        self.nextValidOrderId = orderId

        #here is where you start using api
        contract = Contract()
        contract.symbol = "RY"
        contract.secType = "STK"
        contract.currency = "CAD"
        contract.exchange = "SMART"
        self.reqMarketDataType(3)
        self.reqMktData(1101, contract, "", False, None)

    @iswrapper
    def error(self, reqId:TickerId, errorCode:int, errorString:str):
        print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)

    @iswrapper
    def tickPrice(self, reqId: TickerId , tickType: TickType, price: float,
                  attrib:TickAttrib):
        print("Tick Price. Ticker Id:", reqId,
              "tickType:", TickTypeEnum.to_str(tickType),
              "Price:", price)
        #just disconnect after a bit
        self.count += 1
        if self.count > 10 : self.disconnect()

#I use jupyter but here is where you use if __name__ == __main__:
app = TestApp()
app.connect("127.0.0.1", 7497, clientId=123)
print("serverVersion:%s connectionTime:%s" % app.serverVersion(),app.twsConnectionTime()))
app.run()

这是输出的一部分,注意你可以使用ib的枚举类型来获取tick类型的名称。

Error. Id: 1101 Code: 10167 Msg: Requested market data is not subscribed. Displaying delayed market data... Tick Price. Ticker Id: 1101 tickType: DELAYED_BID Price: 97.02 Tick Price. Ticker Id: 1101 tickType: DELAYED_ASK Price: 97.02 Tick Price. Ticker Id: 1101 tickType: DELAYED_LAST Price: 0.0

现在是上午 9 点 40 分,因为我输入了这个,所以市场是开放的,但还没有延迟 15 分钟。需要过滤掉 0.0 的 DELAYED_LAST。我认为我从未见过 0.0 的实时最后一次,所以要小心。

我等到 9:45 我收到了

Tick Price. Ticker Id: 1101 tickType: DELAYED_LAST Price: 97.63

准时。

【讨论】:

    【解决方案2】:

    由于我的开发工作主要在 RTH 之外, 类型 4(最后价格)并不总是可用。 因此,我尝试了 9 型(收盘价)作为替代。 全部信息可以看到here

    亚历克斯

    【讨论】:

      猜你喜欢
      • 2011-03-23
      • 2019-06-27
      • 2018-02-16
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多