使用TWS API获取所有子账户的未实现PnL



我正试图找到Interactive Brokers中所有子账户的未实现PnL。由于这是我第一次使用TWS API,我不确定我做错了什么。

通读API文档后,EClient上的reqPnL方法似乎是获得我想要的东西的理想方法。下面是我尝试的代码:

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

class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
self.reqPnL(17001, 'All', '')
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def pnl(self, reqId: int, dailyPnL: float, unrealizedPnL: float, realizedPnL: float):
print("Daily PnL Single. ReqId:", reqId, "Position:", decimalMaxString(pos),
"DailyPnL:", floatMaxString(dailyPnL), "UnrealizedPnL:", floatMaxString(unrealizedPnL),
"RealizedPnL:", floatMaxString(realizedPnL), "Value:", floatMaxString(value))
self.cancelPnL(17001)

def main():
app = TestApp()
app.connect("127.0.0.1", 7496, 0)
app.run()

if __name__ == "__main__":
main()
我得到的结果是:
Error:  -1   504   Not connected
Error:  -1   2104   Market data farm connection is OK:hfarm
Error:  -1   2104   Market data farm connection is OK:eufarmnj
Error:  -1   2104   Market data farm connection is OK:cashfarm
Error:  -1   2104   Market data farm connection is OK:usfuture
Error:  -1   2104   Market data farm connection is OK:afarm
Error:  -1   2104   Market data farm connection is OK:jfarm
Error:  -1   2104   Market data farm connection is OK:usfarm.nj
Error:  -1   2104   Market data farm connection is OK:eufarm
Error:  -1   2104   Market data farm connection is OK:usopt
Error:  -1   2104   Market data farm connection is OK:usfarm
Error:  -1   2106   HMDS data farm connection is OK:euhmds
Error:  -1   2106   HMDS data farm connection is OK:hkhmds
Error:  -1   2106   HMDS data farm connection is OK:fundfarm
Error:  -1   2106   HMDS data farm connection is OK:ushmds
Error:  -1   2158   Sec-def data farm connection is OK:secdefhk

我不知道我做错了什么。我觉得要么我忘记了一个Ewrapper函数,要么我不应该为ReqId使用17001。请帮助。

由于尚未调用连接,因此无法在初始化中发出请求。试着在nextvaliddid回调之后调用它:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from threading import Timer
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId: int):
self.orderId = orderId
self.reqPnL(17001, 'All', '')
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def pnl(self, reqId: int, dailyPnL: float, unrealizedPnL: float, realizedPnL: float):
print("Daily PnL Single. ReqId:", reqId, "Position:", decimalMaxString(pos),
"DailyPnL:", floatMaxString(dailyPnL), "UnrealizedPnL:", floatMaxString(unrealizedPnL),
"RealizedPnL:", floatMaxString(realizedPnL), "Value:", floatMaxString(value))
self.cancelPnL(17001)
def main():
app = TestApp()
app.connect("127.0.0.1", 7496, 0)
app.run()
if __name__ == "__main__":
main()

相关内容

  • 没有找到相关文章

最新更新