首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试从TWS API请求基础数据

尝试从TWS API请求基础数据
EN

Stack Overflow用户
提问于 2020-05-25 18:05:23
回答 2查看 975关注 0票数 1

一般来说,我对这个api和python非常陌生,我正在尝试通过交互式代理从TWS api导入基础数据。

代码语言:javascript
复制
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

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

会非常感谢任何能帮上忙的人。

EN

回答 2

Stack Overflow用户

发布于 2021-01-10 20:08:07

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

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

2. EWrapper:负责接收IB服务器发回的消息。

reqFundamentalData()函数位于EClient类中,负责向IB的服务器发送基础数据请求。

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

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

代码语言:javascript
复制
# 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()

希望能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2020-06-06 18:38:47

对“ReportsFinStatements”试试这段代码:

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62000131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档