这是我如何检查未平仓头寸的示例。如果不存在头寸,则创建一个 txt 文件,如果存在未平仓头寸,则删除 .txt 文件。我的问题是我不知道如何断开它。它也输出错误 -1 见下面的脚本
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from ib.opt import ibConnection
import os.path
from os import path
class CheckPos(EClient, EWrapper):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId:int):
self.reqPositions()
def position(self, account: str, contract: Contract, position: float,
avgCost: float):
super().position(account, contract, position, avgCost)
if position >0:
try:
os.remove("noposition.txt")
except:
print("Open Positons")
else:
try:
open("noposition.txt","w+")
print("File Created Sucessfully")
except:
print("No Open Positions")
def main():
app = CheckPos()
app.connect("127.0.0.1", 7497,421 )
app.run()
if __name__ == "__main__":
main()
输出为 ERROR -1 2104 Market data farm connection is OK:usfuture ERROR -1 2104 Market data farm connection is OK:usfarm ERROR -1 2106 HMDS data farm connection is OK:ushmds File Created Sucessfully