微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 TWS IB 中提取基本数据的最后一段代码丢失

如何解决从 TWS IB 中提取基本数据的最后一段代码丢失

我从 Stackoverflow 中的一个已回答查询获取了以下代码(不幸的是,我无法完全信任,因为我无法再找到该页面)。我对其进行了一些更改以符合我的目的。 我想为股票行情列表提取历史路透社数据 (fundamentalData)。下面的代码工作正常,但它只获取最后的代码数据。我知道我需要建立一个 while 循环,但我尝试了很多次,但都没有奏效。我确定这是一个快速解决方案,但由于我是编码和 Python 的新手,因此我找不到解决方案。任何帮助将不胜感激!

#Import all libriaries 
from ib.opt import ibConnection,message
from time import sleep
import lxml.etree
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from IPython.core.debugger import set_trace
from ibapi import wrapper
from ibapi.client import EClient
from ibapi.common import *  
from ibapi.contract import Contract 

#upload excel file of list of company tickers you want to review
us_list= pd.read_excel(r'C:\TWS API\Request Reuters data\Quant Project IB TWS_test.xlsx',engine='openpyxl')

stocksList = us_list[['TICKER']]
stocksList

def fundamentalData_handler(msg):
    global imported
    imported = msg.data

def error_handler(msg):
    pass

# processing of the lines in financial statements
def extractVauleFromLineItem(fiscalperiod,code): 
    stage1 = fiscalperiod.find(name='lineitem',coacode=code)
    if (not stage1 is None):
        stage2 = stage1.get_text()
        if (stage2 == stage2):
            stage3 = float(stage2)
            if (stage3 == stage3):
                return (stage3)
            else:
                return (0.0)
                        
result = pd.DataFrame(columns =['Year','Ticker','control','TotalRevenue','Grossprofit','CommonSharesOutstanding','DilutednormalizedEPS','totalCash','TotalDebt','Dividends'])

outcomes = []

for i,row in stocksList.iterrows():
    contract = Contract()
    contract.symbol = row['TICKER']
    contract.secType = "STK"
    contract.currency = "USD"
    contract.exchange = "SMART"


    tws = ibConnection("127.0.0.1",port=7497,clientId=901)
    tws.register(error_handler,message.Error)
    tws.register(fundamentalData_handler,message.fundamentalData)
    tws.connect()
    tws.reqFundamentalData(1,contract,'ReportsFinStatements')
    sleep(1)
    tws.disconnect()
    print(contract.symbol) 
    
    soup = BeautifulSoup(imported) # library for processing of the obtained XML data
    data = []
    print(soup.find(name='issueid',type="Ticker").get_text())
    print(soup.find(name='coid',type="CompanyName").get_text()) 
    
    # I found that IB API is not very stable. 
    # Sometimes it returns data of the wrong company. 
    # So the control is important
    print('Control -',contract.symbol == soup.find(name='issueid',type="Ticker").get_text())
    print()
    
    for fiscalperiod in soup.find_all(name="fiscalperiod",type="Annual"):
        year = fiscalperiod['fiscalyear']
        TotalRevenue = extractVauleFromLineItem(fiscalperiod,'RTLR')
        Grossprofit = extractVauleFromLineItem(fiscalperiod,'SGRP')
        CommonSharesOutstanding = extractVauleFromLineItem(fiscalperiod,'QTCO')
        DilutednormalizedEPS = extractVauleFromLineItem(fiscalperiod,'VDES')
        totalCash = extractVauleFromLineItem(fiscalperiod,'OTLO') 
        TotalDebt = extractVauleFromLineItem(fiscalperiod,'STLD')
        Dividends = extractVauleFromLineItem(fiscalperiod,'FCDP')
        
    
        thisYearData = (year,contract.symbol,(contract.symbol == soup.find(name='issueid',type="Ticker").get_text()),TotalRevenue,Grossprofit,CommonSharesOutstanding,totalCash,TotalDebt,Dividends)    
        data.append(thisYearData)
        
    df_data = pd.DataFrame(data,columns =['Year','Dividends'])

    df_data = df_data.sort_values(by=['Year'])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。