4
$\begingroup$

I am trying to download data from Interactive Broker using the code below and I am able to create the connection with the Trader Work Station (I get a "True" after con.connect()) but there is no output, i.e. the dataDownload variable becomes empty.

Anyone know why this can be?

# Adapted from: http://godelsmarket.blogspot.co.uk/2012/07/non-gui-ib-historical-data-downloader.html

from time import sleep, strftime, localtime
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message

new_symbolinput = ['EUR.USD']
newDataList = []
dataDownload = []

def historical_data_handler(msg):
  global newDataList
  #print msg.reqId, msg.date, msg.open, msg.high, msg.low, msg.close, msg.volume
  if ('finished' in str(msg.date)) == False:
    new_symbol = new_symbolinput[msg.reqId]
    dataStr = '%s, %s, %s, %s, %s, %s, %s' % (new_symbol, strftime("%Y-%m-%d %H:%M:%S", localtime(int(msg.date))), msg.open, msg.high, msg.low, msg.close, msg.volume)
    newDataList = newDataList + [dataStr]
  else:
    new_symbol = new_symbolinput[msg.reqId]
    filename = 'minutetrades' + new_symbol + '.csv'
    csvfile = open('csv_day_test/' + filename,'wb')
    for item in newDataList:
      csvfile.write('%s \n' % item)
    csvfile.close()
    newDataList = []
    global dataDownload
    dataDownload.append(new_symbol)

con = ibConnection()
con.register(historical_data_handler, message.historicalData)
con.connect()

symbol_id = 0
for i in new_symbolinput:
  print i
  qqq = Contract()
  qqq.m_symbol = i
  qqq.m_secType = 'STK'
  qqq.m_exchange = 'SMART'
  qqq.m_currency = 'USD'
  con.reqHistoricalData(symbol_id, qqq, '', '1 D', '1 min', 'TRADES', 1, 2)
  symbol_id = symbol_id + 1
  sleep(0.5)

print dataDownload
$\endgroup$
0

1 Answer 1

3
$\begingroup$

You'll want to adjust the contract inputs you're sending to IB

secType should be CASH, exchange should be IDEALPRO, and add in whatToShow = "MIDPOINT" or "BID" or "ASK"

$\endgroup$
5
  • $\begingroup$ Yes, you are right, that part was wrong. THANKS!! $\endgroup$
    – Escachator
    Mar 25, 2015 at 13:33
  • $\begingroup$ Happy to hear, no problem! $\endgroup$ Mar 25, 2015 at 18:34
  • $\begingroup$ I am still having the same issues after adjusting the inputs, any idea why I might be getting an empty string? Thanks! $\endgroup$
    – JordanBelf
    May 8, 2015 at 17:20
  • $\begingroup$ Are you subscribed to the data that you want to download? Are you sure your contract inputs are correct? $\endgroup$
    – Escachator
    May 12, 2015 at 7:26
  • $\begingroup$ Hi JordanBelf, need more info. I'd guess its syntax error on your part. Using 'CASH' and 'IDEALPRO' ? $\endgroup$ May 13, 2015 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.