英文:
Connecting to Interactive Brokers TWS via API in Jupyter Notebooks
问题
以下是您提供的代码的翻译部分:
我只是尝试找到获取股票和其他数据的方法。下面的代码返回错误1 504未连接
这是市场交易时间的问题,使用jupyter的问题,还是一些错误的问题吗?
如果您可以回答,我提前感谢,我是一个真正的编程初学者,有点迷茫。我已经按照网站上的所需全局设置检查清单进行了操作
来自ibapi.client的导入EClient
来自ibapi.wrapper的导入EWrapper
来自ibapi.contract的导入Contract
导入线程
导入时间
类IBapi(EWrapper,EClient):
def __init__(self):
EClient.__init__(self,self)
def tickPrice(self,reqId,tickType,price,attrib):
if tickType == 2 and reqId == 1:
打印('当前的卖出价为:',price)
def run_loop():
app.run()
app = IBapi()
app.connect('127.0.0.1',7496,10)
#在线程中启动套接字
api_thread = threading.Thread(target = run_loop,daemon = True)
api_thread.start
time.sleep(1)#休眠间隔,以允许时间连接到服务器
#创建合同对象
apple_contract = Contract()
apple_contract.symbol = 'AAPL'
apple_contract.secType = 'STK'
apple_contract.exchange = 'SMART'
apple_contract.currency = 'USD'
#请求市场数据
app.reqMktData(1,apple_contract,“”,False,False,[])
time.sleep(10)#休眠间隔,以允许时间接收价格数据
app.disconnect()
英文:
I am just trying to find ways to get stocks and other data. the code below returns Error 1 504 Not connected
Is it a problem of market hours, of using jupyter, or of some mistakes?
Thank you in advance if you can answer, I am a true coding beginner and I am a bit lost. I followed all the required Global Settings checklist as outlined elsewhere in the website
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import threading
import time
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def tickPrice(self, reqId, tickType, price, attrib):
if tickType==2 and reqId==1:
print('The current ask price is: ', price)
def run_loop():
app.run()
app=IBapi()
app.connect('127.0.0.1', 7496, 10)
#start socket in a thread
api_thread=threading.Thread(target=run_loop, daemon=True)
api_thread.start
time.sleep(1) #sleep interval to allow time for connection to server
#create contract object
apple_contract= Contract()
apple_contract.symbol='AAPL'
apple_contract.secType='STK'
apple_contract.exchange='SMART'
apple_contract.currency='USD'
#request market data
app.reqMktData(1, apple_contract,"", False, False, [])
time.sleep(10) #sleep interval to allow time for incoming price data
app.disconnect()`
答案1
得分: 1
代码中有两个小问题,第一个可能是拼写错误,但第二个将导致线程不启动并返回消息。
...
def tickPrice(self, reqId, tickType, price, attrib):
if tickType == 2 and reqId == 1:
print('当前的卖价是:', price) # 1) 缩进
...
# 在线程中启动套接字
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start() # 2) 括号
...
英文:
There are two small issues with the code, the first is likely a typo but the second will cause it to not start the thread and return messages.
...
def tickPrice(self, reqId, tickType, price, attrib):
if tickType==2 and reqId==1:
print('The current ask price is: ', price) # 1) Indentation
...
#start socket in a thread
api_thread=threading.Thread(target=run_loop, daemon=True)
api_thread.start() # 2) Parenthesis
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论