[英]Interactive Brokers python API - convert ReportsFinSummary to pandas
我正在嘗試做一些我認為很簡單的事情……顯然不是。 我想使用 python ib api 從 ReportFinSummary 中提取一些數據。 我希望能夠使用 DPS 數字。 我正在嘗試用漂亮的湯對 xml 代碼進行排序,沒有任何樂趣。 任何幫助將不勝感激。 艾倫
from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from time import sleep
from bs4 import BeautifulSoup
import pandas as pd
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 = 'RDSA'
c.m_secType = 'STK'
c.m_exchange = "SMART"
c.m_currency = "GBP"
tws.reqFundamentalData(1,c,'ReportsFinSummary')
soup = BeautifulSoup(tws.reqFundamentalData(1,c,'ReportsFinSummary'),'xml')
DPS_Data = soup.find_all('DividendPerShares')
DPS = []
for dates in DPS_Data:
DPS.append(DPS_Data.get_text())
print(pd.DataFrame({'DPS_Data': DPS}))
sleep(2)
tws.disconnect()
這是未經測試的,但應該讓你走上正軌。 您使用的是多年未開發的 IbPy,最好使用本機 API 或 IbPythonic。
from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from time import sleep
from bs4 import BeautifulSoup
import pandas as pd
class IB():
def __init__(self):
pass
def fundamentalData_handler(self, msg):
self.data = msg
def error_handler(self, msg):
print(msg)
def connect(self, port=7497, clientId=123):
self.tws = ibConnection(port, clientId)
self.tws.register(self.error_handler, message.Error)
self.tws.register(self.fundamentalData_handler, message.fundamentalData)
self.tws.connect()
def disconnect(self):
self.tws.disconnect()
def get(self):
c = Contract()
c.m_symbol = 'RDSA'
c.m_secType = 'STK'
c.m_exchange = "SMART"
c.m_currency = "GBP"
self.tws.reqFundamentalData(1, c, 'ReportsFinSummary')
while not hasattr(self, 'data'):
sleep(0.1)
soup = BeautifulSoup(self.data)
DPS_Data = soup.find_all('DividendPerShares')
DPS = [DPS_Data.get_text() for dates in DPS_Data]
return pd.DataFrame({'DPS_Data': DPS})
ib = IB()
ib.connect()
df = ib.get()
ib.disconnect()
print(df)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.