javaaddpath ib-matlab.jar; import com.ib.client.*; callback = IBCallback(); ib = EClientSocket(callback); ib.eConnect('127.0.0.1', 7496, 0); % Server Version:45 % TWS Time at connection:20100531 12:15:56 EST hod = Contract() % % hod = % % com.ib.client.Contract@eb8007 % hod.m_conId = 48168448; ib.reqHistoricalData(2, hod, '20100528 16:00:00', '1 W', 11, 'TRADES', 1, 1) % No method 'reqHistoricalData' with matching signature found for class % 'com.ib.client.EClientSocket'. %% Max: 11 should be a string - documentation is always a little misleading ib.reqHistoricalData(2, hod, '20100528 16:00:00', '1 W', '11', 'TRADES', 1, 1) %% Max: We have not written the data callback or error callback, so no idea what happened %% Max: now use the attached callback with signature given in the next line callback.historicalData = 'PrintHistoricalData' % % callback = % % IBCallback@d51c6c % ib.reqHistoricalData(2, hod, '20100520 16:00:00', '1 W', '11', 'TRADES', 1, 1) %% Max: We have not written the error callback, so no idea what happened %% Max: Maybe it was an error with there being no TSE data, let's try NYSE gs = Contract() % % gs = % % com.ib.client.Contract@15a557a % gs.m_conId = 4627828 % % gs = % % com.ib.client.Contract@15a557a % ib.reqHistoricalData(2, gs, '20100520 16:00:00', '1 W', '11', 'TRADES', 1, 1) %% Max: Hmm that didn't work either, but we still have no error callback, so let's write one %% Max: now use the attached callback with signature given in the next line callback.error = 'Error' % % callback = % % IBCallback@d51c6c % ib.reqHistoricalData(2, gs, '20100512 16:00:00', '1 W', '11', 'TRADES', 1, 1) %Error validating request:-'yb' : cause - Please enter exchange %% Max: much better! It turns out m_conId is not enough to specify a contract %% Max: let's try specifying it with more readable fields instead of m_conId hod = Contract() % % hod = % % com.ib.client.Contract@544a45 % hod.m_exchange = 'TSE'; hod.m_localSymbol = 'HOD'; hod.m_currency = 'CAD'; hod.m_secType = 'STK'; ib.reqHistoricalData(2, hod, '20100512 16:00:00', '1 W', '11', 'TRADES', 1, 1) % Error validating request:-'yb' : cause - Historical data bar size setting is invalid. Legal ones are: 1 secs, 5 secs, 15 secs, 30 secs, 1 min, 2 mins, 3 mins, 5 mins, 15 mins, 30 mins, 1 hour, 1 day, 1 week, 1 month, 3 months, 1 year %% Max: it looks like '11' was also wrong, let's try '1 day' ib.reqHistoricalData(2, hod, '20100519 16:00:00', '1 W', '1 day', 'TRADES', 1, 1) % HMDS data farm connection is OK:ushmds2a % Historical Market Data Service error message:No market data permissions for TSE STK %% Max: I don't have access to TSE data apparently %% Max: I'll download it for Goldman just to show how it works gs = Contract() % % gs = % % com.ib.client.Contract@af94c4 % gs.m_exchange = 'SMART'; gs.m_currency = 'USD'; gs.m_localSymbol = 'GS'; gs.m_secType = 'STK'; ib.reqHistoricalData(2, gs, '20100513 16:00:00', '1 W', '1 day', 'TRADES', 1, 1) % reqID = 2, date = 20100507, open = 145.55, high = 146.55, low = 141.5, close = 143.13, volume = 202894, count = 118817, WAP = 143.91, hasGaps = 0 % reqID = 2, date = 20100510, open = 149.09, high = 149.9, low = 141.2, close = 144, volume = 216456, count = 117019, WAP = 144.55, hasGaps = 0 % reqID = 2, date = 20100511, open = 142.43, high = 145.5, low = 141.55, close = 142.04, volume = 146334, count = 79918, WAP = 143.17, hasGaps = 0 % reqID = 2, date = 20100512, open = 144.24, high = 147.65, low = 142.28, close = 147.38, volume = 145018, count = 77721, WAP = 145.2, hasGaps = 0 % reqID = 2, date = 20100513, open = 148.36, high = 148.45, low = 144.42, close = 144.59, volume = 110566, count = 62491, WAP = 146.02, hasGaps = 0 % reqID = 2, date = finished-20100506 16:00:00-20100513 16:00:00, open = -1, high = -1, low = -1, close = -1, volume = -1, count = -1, WAP = -1, hasGaps = 0 %% Max: Cool! it worked %% Max: Now let's try 1 second bars ib.reqHistoricalData(2, gs, '20100513 16:00:00', '1800 S', '1 secs', 'TRADES', 1, 1) %{ reqID = 2, date = 20100513 15:30:00, open = 144.97, high = 144.99, low = 144.96, close = 144.96, volume = 16, count = 14, WAP = 144.97, hasGaps = 0 reqID = 2, date = 20100513 15:30:01, open = 144.96, high = 144.96, low = 144.95, close = 144.95, volume = 5, count = 4, WAP = 144.95, hasGaps = 0 reqID = 2, date = 20100513 15:30:02, open = 144.95, high = 144.95, low = 144.95, close = 144.95, volume = 1, count = 1, WAP = 144.95, hasGaps = 0 reqID = 2, date = 20100513 15:30:03, open = 144.95, high = 144.95, low = 144.95, close = 144.95, volume = 0, count = 0, WAP = 144.95, hasGaps = 0 reqID = 2, date = 20100513 15:30:04, open = 144.95, high = 144.95, low = 144.95, close = 144.95, volume = 0, count = 0, WAP = 144.95, hasGaps = 0 ... CUT OFF HERE reqID = 2, date = 20100513 15:59:58, open = 144.53, high = 144.53, low = 144.5, close = 144.53, volume = 4, count = 3, WAP = 144.52, hasGaps = 0 reqID = 2, date = 20100513 15:59:59, open = 144.54, high = 144.77, low = 144.51, close = 144.59, volume = 54, count = 22, WAP = 144.61, hasGaps = 0 reqID = 2, date = finished-20100513 15:30:00-20100513 16:00:00, open = -1, high = -1, low = -1, close = -1, volume = -1, count = -1, WAP = -1, hasGaps = 0 %} %% Max: Cool! that worked too %% Max: it looks like the end of the download is signalled by an event with open = -1. That's useful. %% Max: now use the attached callback with signature given in the next line callback.historicalData = 'HistoricalData' global histData histData = {} ib.reqHistoricalData(2, gs, '20100513 16:00:00', '1800 S', '1 secs', 'TRADES', 1, 1) %% Max: wait for it to print "Done". You can see the dimension of histData change on the top right %% Max: This is the data that was downloaded: histData %{ histData = '20100513 15:30:00' [144.9700] [144.9900] [144.9600] [144.9600] [ 16] [14] [144.9700] '20100513 15:30:01' [144.9600] [144.9600] [144.9500] [144.9500] [ 5] [ 4] [144.9500] '20100513 15:30:02' [144.9500] [144.9500] [144.9500] [144.9500] [ 1] [ 1] [144.9500] '20100513 15:30:03' [144.9500] [144.9500] [144.9500] [144.9500] [ 0] [ 0] [144.9500] '20100513 15:30:04' [144.9500] [144.9500] [144.9500] [144.9500] [ 0] [ 0] [144.9500] ... CUT OFF HERE %} histData{1,1} % % ans = % % 20100513 15:30:00 % histData{2,2} % % ans = % % 144.9600 %