[英]Connecting to Interactive Brokers TWS via API in Jupyter Notebooks
我只是想找到获取股票和其他数据的方法。 下面的代码返回错误 1 504 未连接
是市场时间的问题,使用jupyter的问题,还是一些错误?
如果你能回答,先谢谢你,我是一个真正的编码初学者,我有点迷茫。 我遵循了网站其他地方概述的所有必需的全局设置清单
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()`
代码有两个小问题,第一个可能是拼写错误,第二个会导致它无法启动线程和返回消息。
...
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
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.