我写了下面复制的程序。当我尝试在输入文件中使用以下数据执行时:ADBE,USD,NASDAQ CSCO,USD,NASDAQ GOOGL,USD,NASDAQ
它只针对第一支股票 ADBE 运行,之后什么也不做。这个想法是程序应该为输入文件中的每个条目运行,并从 InteractiveBrokers 下载相应的分析师预测并将其写入 XML 文件。
你能帮我找出为什么它只在输入文件的第一行运行的问题吗?
非常感谢。
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import time
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, "", errorCode, "", errorString)
def fundamentalData(self, reqId, fundamental_data):
Filename = str(Symbol) + '_Anal_Est.xml'
print("Filename: ", Filename)
f_out = open(Filename, "w")
f_out.write(fundamental_data)
f_out.close()
def main():
global Symbol
# The input file need to contain in each line: company ticker,currency,exchange - no spaces between them
Company_tickers = open("IB_FD_input.txt", 'r').readlines() # reads a file with companies tickers in one column
#print("Company_tickers: ", Company_tickers)
Number_compnaies = len(Company_tickers)
Company_count = 0
for stock in Company_tickers:
aa = stock.split(",")
Symbol = aa[0].replace(" ", "") # in case there is a space
Currency = aa[1].replace(" ", "")
Exchange = aa[2].replace("\n", "").replace(" ","") # need to remove the \n as it is the last field in the entry line
contract = Contract() # defining the stock to download the fundamental data from IB
contract.symbol = Symbol
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = Currency
contract.primaryExchange = Exchange
print("Contract: ", contract)
Company_count += 1 # To show progress on screen
print("\n\n" + "**********************" + "\n")
print(" " + Symbol + ": # " + str(Company_count) + " / " + str(Number_compnaies))
print("\n" + "**********************" + "\n")
app = TestApp()
app.connect("127.0.0.1", 7496, 0)
time.sleep(10)
app.reqFundamentalData(8001, contract, "RESC", [])
time.sleep(10)
app.run()
if __name__ == "__main__":
main()