Hi,
In this code, I need to know how to add the current settled cash available in the connected IB account.
Once i have that, I can go off into Never Never Land just like Peter Pan. :-)
Cheers,
¡ª³§³Ù³Ü²¹°ù³Ù
#!/usr/local/bin/python3
#
import ibapi
def tws_time(): # request time to "wake-up" IB's API
from datetime import datetime
from threading import Thread
import time
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
class ib_class(EWrapper, EClient):
def __init__(self, addr, port, client_id):
EClient. __init__(self, self)
self.connect(addr, port, client_id) # Connect to TWS
thread = Thread(target=self.run) # Launch the client thread
thread.start()
def currentTime(self, cur_time):
t = datetime.fromtimestamp(cur_time)
print('Current TWS date/time: {}\n'.format(t))
def error(self, reqId:TickerId, errorCode:int, errorString:str):
if reqId > -1:
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
ib_api = ib_class('127.0.0.1', 7497, 0)
ib_api.reqCurrentTime() # associated callback: currentTime
time.sleep(0.5)
ib_api.disconnect()
def read_positions(): #read all accounts positions and return DataFrame with information
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
from threading import Thread
import pandas as pd
import time
class ib_class(EWrapper, EClient):
def __init__(self, addr, port, client_id):
EClient.__init__(self, self)
self.connect(addr, port, client_id) # Connect to TWS
thread = Thread(target=self.run) # Launch the client thread
thread.start()
self.all_positions = pd.DataFrame([], columns = ['Account','Symbol', 'Quantity', 'Average Cost', 'Sec Type'])
def error(self, reqId:TickerId, errorCode:int, errorString:str):
if reqId > -1:
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
def position(self, account, contract, pos, avgCost):
index = str(account)+str(contract.symbol)
self.all_positions.loc[index]= account, contract.symbol, pos, avgCost, contract.secType
ib_api = ib_class("127.0.0.1", 7497, 10)
ib_api.reqPositions() # associated callback: position
print("Waiting for IB's API response for accounts positions requests...\n")
time.sleep(3.0)
current_positions = ib_api.all_positions
current_positions.set_index('Account',inplace=True,drop=True) #set all_positions DataFrame index to "Account"
ib_api.disconnect()
return(current_positions)
def read_navs(): #read all accounts NAVs
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
from threading import Thread
import pandas as pd
import time
class ib_class(EWrapper, EClient):
def __init__(self, addr, port, client_id):
EClient.__init__(self, self)
self.connect(addr, port, client_id) # Connect to TWS
thread = Thread(target=self.run) # Launch the client thread
thread.start()
self.all_accounts = pd.DataFrame([], columns = ['reqId','Account', 'Tag', 'Value' , 'Currency'])
def error(self, reqId:TickerId, errorCode:int, errorString:str):
if reqId > -1:
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
def accountSummary(self, reqId, account, tag, value, currency):
index = str(account)
self.all_accounts.loc[index]=reqId, account, tag, value, currency
ib_api = ib_class("127.0.0.1", 7497, 10)
ib_api.reqAccountSummary(0,"All","NetLiquidation") # associated callback: accountSummary
print("Waiting for IB's API response for NAVs requests...\n")
time.sleep(3.0)
current_nav = ib_api.all_accounts
ib_api.disconnect()
return(current_nav)
print("Testing IB's API as an imported library:")
all_positions = read_positions()
print(all_positions)
print()
all_navs = read_navs()
print(all_navs)