Here is a sample of how I check for open positions. If no positions exist it creates a txt file and if open positions exist it del the .txt file. My problem is I don't know how to disconnect it . It is also outputting Error -1 see below script
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