连接到交互式经纪商TWS通过API在Jupyter Notebooks中

huangapple go评论74阅读模式
英文:

Connecting to Interactive Brokers TWS via API in Jupyter Notebooks

问题

以下是您提供的代码的翻译部分:

我只是尝试找到获取股票和其他数据的方法下面的代码返回错误1 504未连接

这是市场交易时间的问题使用jupyter的问题还是一些错误的问题吗

如果您可以回答我提前感谢我是一个真正的编程初学者有点迷茫我已经按照网站上的所需全局设置检查清单进行了操作

来自ibapi.client的导入EClient
来自ibapi.wrapper的导入EWrapper
来自ibapi.contract的导入Contract

导入线程
导入时间

类IBapiEWrapperEClient):
    def __init__self):
        EClient.__init__selfself
    def tickPriceselfreqIdtickTypepriceattrib):
        if tickType == 2 and reqId == 1
            打印'当前的卖出价为:'price

def run_loop():
    app.run()

app = IBapi()
app.connect'127.0.0.1'749610

#在线程中启动套接字

api_thread = threading.Threadtarget = run_loopdaemon = True
api_thread.start

time.sleep1)#休眠间隔以允许时间连接到服务器

#创建合同对象

apple_contract = Contract()
apple_contract.symbol = 'AAPL'
apple_contract.secType = 'STK'
apple_contract.exchange = 'SMART'
apple_contract.currency = 'USD'

#请求市场数据

app.reqMktData1apple_contract,“”,FalseFalse[]

time.sleep10)#休眠间隔以允许时间接收价格数据
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 
...

huangapple
  • 本文由 发表于 2023年1月9日 04:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定