【问题标题】:Trying to request fundamental data from TWS API尝试从 TWS API 请求基础数据
【发布时间】:2021-01-11 05:51:09
【问题描述】:

总的来说,我对这个 api 和 python 很陌生,我正在尝试通过交互式经纪人从 TWS api 导入基本数据。

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract

wrapper = EWrapper()
app = EClient(wrapper)
app.connect('127.0.0.1', 7497, clientId=123)

print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))

contract = Contract()
contract.symbol = 'SQ'
contract.secType = 'STK'
contract.currency = 'USD'

app.reqFundamentalData(8001, contract, 'RESC', [])

这是文档:https://interactivebrokers.github.io/tws-api/fundamentals.html

我无法找到此请求的数据是否已导入以及导入到何处。

非常感谢任何可以提供帮助的人。

【问题讨论】:

    标签: python api import python-requests


    【解决方案1】:

    Interactive Brokers API 中有两个主要类处理客户端和服务器之间的消息,它们是:

    1. EClient:负责向IB的服务器发送请求;

    2。 EWrapper:负责接收IB服务器发回的msg。

    reqFundamentalData()函数在EClient类中,负责向IB服务器发送基本数据请求。

    IB服务器收到请求后,会将文本数据发回EWrapper类处理。调用 EWrapper 类中的另一个函数 fundamentalData() 来处理返回的数据。

    并且在向 IB 的服务器发送任何请求之前,必须初始化 EWrapper 和 EClient 类,并且必须重写相关的接收函数以实现其他个性化功能。比如打印接收到的数据,或者将返回的数据保存到任意数据库等

    # Import EWrapper and EClient   
    from ibapi.Contract import Contract    
    from ibapi.wrapper import EWrapper    
    from ibapi.client import EClient    
        
        # Initilizing
        class IBapi(EWrapper, EClient):
            def __init__(self):
                EClient.__init__(self, self)
            
            # Error handling function
            def error(self, reqId, errorCode, errorString):
                print("error: ", reqId, " ", errorCode, " ", errorString)
        
            # Inherite and overwrite fundamentalData() function in EWrapper
            def fundamentalData(self, reqId: int, data: str):
                super().fundamentalData(reqId, data)
                print("FundamentalData Returned. ReqId: {}, Symbol: {},  XML Data: {}".format(
                    reqId, contr.symbol, data))
                # Implement other personalized functions here
        
        tws = IBapi()                       
        tws.connect("127.0.0.1",port=7496, clientId=997)
        time.sleep(2)
        
        c = Contract()
        c.m_symbol = 'MMM'
        c.m_secType = 'STK'
        c.m_exchange = "SMART"
        c.m_currency = "USD"
        
        tws.reqFundamentalData(1, c, 'RESC', [])
        time.sleep(4)
        # Implement other requests
        
        tws.disconnect()
    

    希望对你有帮助。

    【讨论】:

    • 感谢您提供良好的解释和可理解的代码。我不得不用“from ibapi.contract import Contract”替换“from ibapi.Contract”并修复类缩进
    【解决方案2】:

    试试这个“ReportsFinStatements”代码:

    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("127.0.0.1",port=7496, clientId=997)
    tws.register(error_handler, message.Error)
    tws.register(fundamentalData_handler, message.fundamentalData)
    tws.connect()
    
    c = Contract()
    c.m_symbol = 'MMM'
    c.m_secType = 'STK'
    c.m_exchange = "SMART"
    c.m_currency = "USD"
    
    tws.reqFundamentalData(1,c,'ReportsFinStatements')
    
    sleep(2)
    
    tws.disconnect()
    

    【讨论】:

    • 代码不起作用。错误:从 ib.opt 导入 ibConnection,消息 ModuleNotFoundError:没有名为“ib”的模块
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2022-08-02
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多