使用IbPy的基本数据 [英] Fundamental Data Using IbPy
问题描述
我正在尝试使用IbPY拉高股票价格及其财务报表.我是python的新手,并不完全了解在IbPy中调用某些不同方法的复杂性.
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.
我编写了一些代码来遍历SP 500,并为每只股票设置买入/卖出价.我希望有人能够帮助我确定下一步的财务报表编制方法.
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()
推荐答案
有很多无关的代码,但是您的问题是您没有为基本数据回调实现处理程序.
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()
这篇关于使用IbPy的基本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!