使用 IbPy 的基础数据

Fundamental Data Using IbPy

我正在尝试使用 IbPY 来提取股票价格及其财务报表。我是 python 的新手,并不完全理解在 IbPy 中调用一些不同方法的复杂性。

我写了一些代码来遍历 SP 500 并为每只股票提取 bid/ask。我希望有人能帮我弄清楚提取财务报表的下一步。

想知道最好的方法吗?

from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from ib.ext.EWrapper import EWrapper
from time import sleep
import csv

with open(r'C:\Users\User\folder\sp500-symbol-list.txt') as f:
reader = csv.reader(f)
 lst = list(reader)


bid_lst=[]
ask_lst = []

start = -1

for x in range(len(lst)):
start = start +1

  def my_callback_handler(msg):
    #print(start)
    inside_mkt_bid = ''
    inside_mkt_ask = ''

    if msg.field == 1:
        inside_mkt_bid = msg.price
        z = ('bid', inside_mkt_bid)
        print(z)
        bid_lst.append(z[1])


    elif msg.field == 2:
        inside_mkt_ask = msg.price
        k=['ask', inside_mkt_ask]
        print(k)
        ask_lst.append(k[1])

tws = ibConnection(port=1111, clientId=000)
tws.register(my_callback_handler, message.tickSize, message.tickPrice)
tws.connect()


c = Contract()
c.m_symbol = lst[start][0]
c.m_secType = 'STK'
c.m_exchange = "SMART"
c.m_currency = "USD"

print(c.m_symbol)
tws.reqMktData(1,c,"",False)
tws.reqFundamentalData(1,c,'ReportsFinStatements')
sleep(1)

tws.disconnect()

有很多无关代码,但您的问题是您没有为基本数据回调实现处理程序。

from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from time import sleep

def fundamentalData_handler(msg):
    print(msg)

def error_handler(msg):
    print(msg)

tws = ibConnection(port=7497, clientId=123)
tws.register(error_handler, message.Error)
tws.register(fundamentalData_handler, message.fundamentalData)
tws.connect()

c = Contract()
c.m_symbol = 'AAPL'
c.m_secType = 'STK'
c.m_exchange = "SMART"
c.m_currency = "USD"

tws.reqFundamentalData(1,c,'ReportsFinStatements')
sleep(2)

tws.disconnect()