¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Re: IB question


Stuart Cracraft
 

So how would this code be modified to make it more likely to retrieve data rather than get a data frame error?

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 Net Asset Value Liquidation requests...\n")
time.sleep(3.0)
current_nav = ib_api.all_accounts
ib_api.disconnect()

return(current_nav)

On Jun 13, 2021, at 9:05 AM, Rational-IM <ds@...> wrote:

I have searched in the past for a wait to avoid the use of "time.sleep(x)" when requesting data. However, I didn't find anything useful. Would be great if someone could pitch in with a solution (maybe there is something we can call to let us know that "there are no pending data requests"?).

For my needs (20+ accounts; 500+ positions), 3 seconds has been sufficient to get the data I need. With more clients in the future I might need to increase the wait time or find a solution to check if the full request was completed.

I also use reqHistoricalData to update - every time I run my portfolio buildup tool - 60+ share prices. But because IB allows us to have 50 simultaneous, with a time sleep of 0.1 secs between requests I avoid a "pace violation". So not a big issue in this front either.





Join [email protected] to automatically receive all group messages.