Folks,<br><br>Sorry for delay, I only caught the thread now - even with a security-card-active account, the web service does not require login at all.<br><br>It works like this:<br><br>- in Account Management, you set up the Flex service, and generate a "token" of some ~30 characters.<br>
- you also set up a report(s) with desired fields, and IB associates an ID(s) with it.<br>- With this token and query ID, you access a xml URL, which starts the report execution and gives a "resultset-ID".<br>- After waiting a bit for execution, you then access a different URL, with the resultset-ID, and retrieve the results<br>
<br>so after setting up the Flex service and report(s), no login is needed at all, security is handled with the "token code". I believe there is also an option to limit access to specific originating IPs<br><br>
below is my python code which may make the process clearer (sorry I'm a beginner Python writer, so it may not be the most elegant - specifically, I write to a local file and then import to a database, I could not get Python to read directly to sqlite)<br>
<br><br><br><br>updateDB.py<br>-------------------------------------------------------------------------------------------------------------------------------------------------------------<br>import sqlite3, csv, urllib.request, urllib.parse, urllib.error, re, time<br>
<br>conn = sqlite3.connect("mydb9.sqlite")<br>cur = conn.cursor()<br><br>token = 632xxxxxxxxxxxxxxxx53730 ; query = 20xxx # this is the token issued by IB<br><br>url = "<a href="https://www.interactivebrokers.com/Universal/servlet/FlexStatementService.SendRequest?t=%s&q=%s">https://www.interactivebrokers.com/Universal/servlet/FlexStatementService.SendRequest?t=%s&q=%s</a>" % (token, query)<br>
ibfile = urllib.request.urlopen(url).read().decode('utf-8') <br><br>pattern = re.compile(r'\d{10}') #Search for 10-digit reference code (resultset code)<br>m = pattern.search(ibfile)<br>reference_code = (m.group()) #retrieve reference code<br>
<br>url = "<a href="https://www.interactivebrokers.com/Universal/servlet/FlexStatementService.GetStatement?q=%s&t=%s&v=2">https://www.interactivebrokers.com/Universal/servlet/FlexStatementService.GetStatement?q=%s&t=%s&v=2</a>" % (reference_code, token) <br>
<br>pattern = re.compile(r'in progress') #If report is not completed, IB returns an <in progress> message<br>for i in range(5): #try 5 times, 40s apart<br> print('Try ', i,)<br> time.sleep(40)<br>
ibfile = urllib.request.urlopen(url).read().decode('utf-8')<br> if pattern.search(ibfile) == None:<br> break<br>print('Succesfully retrieved report')<br><br>ibfile_object = open('ib.csv', 'w')<br>
ibfile_object.write(ibfile.replace(',',''))<br>ibfile_object.close()<br>print('Saved file')<br><br>time.sleep(2) # Give the OS time to close the file<br><br><br># Create table<br>cur.execute('''drop table IF EXISTS tbIB''')<br>
sql = '''create table IF NOT EXISTS tbIB (<br>Symbol TEXT,<br>Description TEXT,<br>AssetClass TEXT,<br>TradeID INTEGER,<br>TradeDate INTEGER,<br>TradeTime INTEGER,<br>Quantity INTEGER,<br>
Multiplier INTEGER,<br>TradePrice REAL,<br>TradeMoney REAL,<br>NetCash REAL,<br>Taxes REAL,<br>IBCommission REAL,<br>CostBasis REAL,<br>FifoPnlRealized REAL,<br>NotesCodes TEXT<br>
)'''<br><br>cur.execute(sql)<br><br>cur.execute('''create unique index IF NOT EXISTS idx_TradeID ON tbIB(TradeID)''')<br><br><br>ibfile_object = open('ib.csv', 'rt')<br>
<br>csvread = csv.reader(ibfile_object, delimiter='|', quotechar='"')<br>for line in csvread:<br> if line != []:<br> cur.execute('insert OR IGNORE into tbIB values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', line)<br>
<br>conn.commit()<br><br>cur.execute('ALTER TABLE tbIB ADD COLUMN oldSymbol TEXT')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN Underlying TEXT')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN OptionType TEXT')<br>
cur.execute('ALTER TABLE tbIB ADD COLUMN QtyInShares INTEGER')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN QtyOpening INTEGER DEFAULT 0')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN QtyClosing INTEGER DEFAULT 0')<br>
cur.execute('ALTER TABLE tbIB ADD COLUMN QtyCumulative INTEGER DEFAULT 0')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN QtyStillOpenTrx INTEGER DEFAULT 0')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN QtyWashMatched INTEGER DEFAULT 0')<br>
cur.execute('ALTER TABLE tbIB ADD COLUMN BasisAdjWash Real DEFAULT 0')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN BasisAdjOption Real DEFAULT 0')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN Trace Text')<br>
cur.execute('ALTER TABLE tbIB ADD COLUMN TraceWash Text')<br>cur.execute('ALTER TABLE tbIB ADD COLUMN TraceOption Text')<br><br><br>cur.close()<br>conn.close()<br>----------------------------------------------------------------------------------------------------------------------------------<br>
<br><br><br><br><br><br><div class="gmail_quote">On Thu, Sep 17, 2009 at 06:35, Ken Feng <span dir="ltr"><<a href="mailto:kfmfe04@gmail.com">kfmfe04@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On 9/17/09, Paul C <<a href="mailto:paulq2o0@yahoo.co.uk">paulq2o0@yahoo.co.uk</a>> wrote:<br>
><br>
> If the TWS is already running, it seems you can get to the IB website<br>
> Account Management without logging in again (i.e. without using the<br>
> security device a second time) by choosing "View->Account Management"<br>
> from the *TWS* menu. It should then just appear in your browser (albeit<br>
> you may not know it, until you go find the browser window). So if<br>
> you're running all week, maybe you're ok? - unless I'm misunderstanding<br>
> something.<br>
><br>
> ( nb, I've just managed to automate retrieval of Trade Records from<br>
> the TWS 'Trades' page using the perl/ X11::GUITest approach, but it's<br>
> probably not robust, and is maybe asking for trouble, so I won't<br>
> elaborate here - it's not really shim related anyway. The fact that<br>
> it's wrapped in html tags I don't see as a problem, and you can get<br>
> the Order_ID/Ref too, so presumably can associate a commission with an<br>
> order sent via the shim [and hence in a MySql table]. But it involves<br>
> sending keystrokes to the running TWS, which I don't much like. )<br>
<br>
</div>That's great! I wasn't aware of that feature - I thought I had to do<br>
it through the website by hand!<br>
<br>
Since I am already using Jemmy to manage the perpetual log-in, I can<br>
use it to bring up the browser - I need to figure out how to scrape it<br>
at that point. I will try to use Jemmy or some java-related tool<br>
since I'm using it to drive TWS.<br>
<br>
Anyhow, thank you for pointing to a way out of this conundrum!<br>
<font color="#888888"><br>
- Ken<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
ts-general mailing list<br>
<a href="mailto:ts-general@trading-shim.org">ts-general@trading-shim.org</a><br>
<a href="http://www.trading-shim.org/mailman/listinfo/ts-general" target="_blank">http://www.trading-shim.org/mailman/listinfo/ts-general</a><br>
</div></div></blockquote></div><br>