簡體   English   中英

在 Jupyter Notebooks 中通過 API 連接到 Interactive Brokers TWS

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM