I am trying to get account balance and store it as a varible. The accountSummary value has the balance in it. I am using print to verify the var bal has the data stored in it. Print(bal) will display the information in the terminal but won't print it to file balance.txt. If the orderid example from the earlier executed code is working correctly why not the Balance?
from ibapi import wrapper
from ibapi.client import EClient
from ibapi.utils import iswrapper #just for decorator
from ibapi.common import *
import math
import os.path
from os import path
class TestApp(wrapper.EWrapper, EClient):
posns = []
fname = 'fname.txt'
def __init__(self):
wrapper.EWrapper.__init__(self)
EClient.__init__(self, wrapper=self)
@iswrapper
def nextValidId(self, orderId:int):
print("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
file = open("orders.txt","w")
file.write(str(orderId))
file.close()
# here is where you start using api
self.reqAccountSummary(9002, "All", "$LEDGER")
@iswrapper
def accountSummary(self, reqId:int, account:str, tag:str, value:str, currency:str):
bal=(value)
file = open("Balance.txt","w")
file.write(bal)
file.close()
@iswrapper
def accountSummaryEnd(self, reqId:int):
# now we can disconnect
self.disconnect()
def main():
app = TestApp()
app.connect("127.0.0.1", 7497, clientId=123)
app.run()
if __name__ == "__main__":
main()
You are overwriting the file every time and not looking for Balance. Use this
def accountSummary(self, reqId:int, account:str, tag:str, value:str, currency:str):
if tag == "CashBalance": # or whatever you want
with open('Balance.txt', 'a+') as file: #append, create if doesn't exist
file.write("%s, %s, %s, %s\n" % (account, tag, value, currency))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.