以下は、私がオープンポジションをチェックする方法のサンプルです。ポジションが存在しない場合は 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()
output is 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