¿ªÔÆÌåÓýRead the 'Account & Portfolio Data' section in the IB documentation at , particularly the 'Account Updates¡¯ and 'Account Summary' sections. That will tell you which API to call and which callbacks to handle. ? ? ? From: [email protected] <[email protected]> On Behalf Of Stuart Cracraft via groups.io
Sent: 05 January 2021 16:24 To: [email protected] Subject: [TWS API] querying the cash remaining @ IB account ? Hi, ? Currently I am using this code (provided by the group) to list the current open positions in my paper-trading account at IB. ? My question is, what would I need to add to this query to find out what the remaining cash position is in the account? ? If I can get that, it would be most helpful for being able to calculate the rise and fall of the portfolio. ? Thanks if anybody knows. ? Stuart ? #!/usr/local/bin/python3 ? from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.common import * from ibapi.contract import * ? import os ? class TestApp(EClient, EWrapper): ? ? posns = [] ? ? fname = 'fname.txt' ? ? ? def __init__(self): ? ? ? ? EClient.__init__(self, self) ? ? ? def nextValidId(self, orderId:int): ? ? ? ? print("id", orderId) ? ? ? ? print("starting program") ? ? ? ? self.reqPositions() ? ? ? def error(self, reqId:TickerId, errorCode:int, errorString:str): ? ? ? ? print("Error: ", reqId, "", errorCode, "", errorString) ? ? ? def position(self, account: str, contract: Contract, position: float, avgCost: float): ? ? ? ? self.posns.append((account, contract.symbol, position, avgCost)) ? ? ? ? print(contract.symbol, position) ? ? ? def positionEnd(self): ? ? ? ? print("end, disconnecting") ? ? ? ? self.disconnect() ? ? ? ? ? #write posns to file or delete file if no positions ? ? ? ? if self.posns: #means not empty ? ? ? ? ? ? with open(self.fname, "w") as outfile: ? ? ? ? ? ? ? ? outfile.write('\n'.join(str(posn) for posn in self.posns)) ? ? ? ? else: # no posns so delete file ? ? ? ? ? ? os.remove(self.fname) ? def main(): ? ? app = TestApp() ? ? app.connect("127.0.0.1", 7497, 421) ? ? app.run() ? if __name__ == "__main__": ? ? main() |