问题
I am trying to use IbPY to pull the price of a stock along with its financial statements. I'm new to python and don't entirely understand the complexities of calling some of the different methods within IbPy.
I wrote some code to loop through the SP 500 and pull the bid/ask for each stock. I was hoping someone might be able to help me figure out the next step to pull the financial statements.
Thoughts on the best way to do that?
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()
回答1:
There's a lot of extraneous code but your problem is you didn't implement a handler for the fundamental data callback.
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()
来源:https://stackoverflow.com/questions/40645067/fundamental-data-using-ibpy