gpt4 book ai didi

python - 获取 SPY 股价

转载 作者:行者123 更新时间:2023-12-01 02:02:22 27 4
gpt4 key购买 nike

我创建了一个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 股票价格?我是否犯了错误?

最佳答案

我不知道你是否已经明白了,但是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 的枚举类型来获取刻度类型的名称。

错误。 ID:1101 代码:10167 消息:请求的市场数据未订阅。显示延迟的市场数据...
勾选价格。代码 ID:1101 报价类型:DELAYED_BID 价格:97.02
勾选价格。代码 ID:1101 报价类型:DELAYED_ASK 价格:97.02
勾选价格。代码 ID:1101 报价类型:DELAYED_LAST 价格:0.0

当我输入此内容时,时间是上午 9:40,因此市场开放,但还没有延迟 15 分钟。 0.0 的 DELAYED_LAST 需要被过滤掉。我想我从来没有见过 0.0 的实时结果,所以要小心。

我等到9:45才收到

勾选价格。代码 ID:1101 报价类型:DELAYED_LAST 价格:97.63

准时。

关于python - 获取 SPY 股价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49471384/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com