ib-scripts icon indicating copy to clipboard operation
ib-scripts copied to clipboard

Cannot download data despite the IB symbol is correct.

Open cheukhin1024 opened this issue 2 years ago • 6 comments

I want to download HSI data. (HSI is Hang Seng Index)

I followed the guide https://www.wrighters.io/how-to-get-historical-market-data-from-interactive-brokers-using-python/ to download the HSI Hang Seng index data using the following bash command script:

./download_bars.py --size "1 day" --security-type IND --max-days --exchange HKFE HSI

Yet, there is an error: No security definition has been found for the request . I want to know whether this ib-scripts project support downloading non-US data because all examples are US-related. Thank you!

The following is the full terminal result:

10:59:59,183 ibapi.client INFO sent startApi
10:59:59,183 ibapi.client INFO REQUEST startApi {}
10:59:59,183 ibapi.client INFO SENDING startApi b'\x00\x00\x00\x0871\x002\x000\x00\x00'
10:59:59,183 root INFO Connected
10:59:59,183 root INFO Waiting for thread to finish ...
10:59:59,186 ibapi.wrapper INFO ANSWER nextValidId {'orderId': 1}
10:59:59,186 root INFO nextValidId: 1
10:59:59,186 ibapi.client INFO REQUEST reqContractDetails {'reqId': 1, 'contract': 2416213105680: 0,HSI,IND,,0.0,,,HKFE,,USD,,,False,,combo:}
10:59:59,187 ibapi.client INFO SENDING reqContractDetails b"\x00\x00\x00'9\x008\x001\x000\x00HSI\x00IND\x00\x000.0\x00\x00\x00HKFE\x00\x00USD\x00\x00\x000\x00\x00\x00"
10:59:59,187 ibapi.wrapper INFO ANSWER error {'reqId': -1, 'errorCode': 2104, 'errorString': 'Market data farm connection is OK:usfarm'}
10:59:59,187 ibapi.wrapper ERROR ERROR -1 2104 Market data farm connection is OK:usfarm
10:59:59,187 ibapi.wrapper INFO ANSWER error {'reqId': -1, 'errorCode': 2106, 'errorString': 'HMDS data farm connection is OK:euhmds'}
10:59:59,187 ibapi.wrapper ERROR ERROR -1 2106 HMDS data farm connection is OK:euhmds
10:59:59,187 ibapi.wrapper INFO ANSWER error {'reqId': -1, 'errorCode': 2106, 'errorString': 'HMDS data farm connection is OK:hkhmds'}
10:59:59,188 ibapi.wrapper ERROR ERROR -1 2106 HMDS data farm connection is OK:hkhmds
10:59:59,188 ibapi.wrapper INFO ANSWER managedAccounts {'accountsList': 'U7079876'}
10:59:59,188 ibapi.wrapper INFO ANSWER error {'reqId': -1, 'errorCode': 2106, 'errorString': 'HMDS data farm connection is OK:fundfarm'}
10:59:59,188 ibapi.wrapper ERROR ERROR -1 2106 HMDS data farm connection is OK:fundfarm
10:59:59,189 ibapi.wrapper INFO ANSWER error {'reqId': -1, 'errorCode': 2106, 'errorString': 'HMDS data farm connection is OK:ushmds'}
10:59:59,189 ibapi.wrapper ERROR ERROR -1 2106 HMDS data farm connection is OK:ushmds
10:59:59,189 ibapi.wrapper INFO ANSWER error {'reqId': -1, 'errorCode': 2158, 'errorString': 'Sec-def data farm connection is OK:secdefil'}
10:59:59,189 ibapi.wrapper ERROR ERROR -1 2158 Sec-def data farm connection is OK:secdefil
10:59:59,560 ibapi.wrapper INFO ANSWER error {'reqId': 1, 'errorCode': 200, 'errorString': 'No security definition has been found for the request'}
10:59:59,560 ibapi.wrapper ERROR ERROR 1 200 No security definition has been found for the request
10:59:59,560 root ERROR Error. Id: 1 Code 200 Msg: No security definition has been found for the request
10:59:59,561 root ERROR The security doesn't exist, check your parameters
10:59:59,561 root INFO Sending code 0
10:59:59,562 root INFO Received code 0
10:59:59,562 ibapi.client INFO disconnecting
10:59:59,562 ibapi.wrapper INFO ANSWER connectionClosed {}

cheukhin1024 avatar Mar 27 '23 03:03 cheukhin1024

OK, with a little digging here I think there's two issues. First, the default currency in download_bars.py is USD, so you are trying to download a contract that doesn't exist because the HSI is in HKD.

One way to find this is to run the query_contracts.py script in this project. I updated it just now to spit out a bit more info. So for example:

./src/query_contracts.py --symbol HSI --security-type IND

This spits out the following (along with a lot of other logging)

Security Type: IND Symbol:HSI Currency: HKD
CUSIP:
Primary Exchange:
Details for  - Hang Seng Stock Index
Industry:   Category:   Subcategory:
OrderTypes: ACTIVETIM,AD,ADJUST,ALERT,ALLOC,BASKET,BENCHPX,COND,CONDORDER,DAY,DEACT,DEACTDIS,DEACTEOD,GAT,GTC,GTD,GTT,HID,LMT,NONALGO,OCA,SCALE,SCALERST,WHATIF
ValidExchanges: HKFE

Now if I run the script with HKD as the currency...

./src/download_bars.py --size "1 day" --security-type IND --max-days --exchange HKFE --currency HKD HSI

I get the message that I don't have market data permissions for this contract.

21:16:50,398 root ERROR Error. Id: 3 Code 162 Msg: Historical Market Data Service error message:No market data permissions for HKFE IND

If you pay for market data, you should be able to download historical data then.

Let me know if that works for you. I don't think I'd downloaded non-USD data before.

wrighter avatar Apr 01 '23 02:04 wrighter

I'm getting the same issue for VIX using download_bars.py --size "1 hour" --security-type IND --start-date 19901010 --end-date 20240401 VIX

The following is the output of query_contracts.py --symbol VIX --security-type IND

Security Type: IND Symbol:VIX Currency: USD CUSIP: Primary Exchange:
Details for - CBOE Volatility Index Industry: Indices Category: Volatility Index Subcategory: * OrderTypes: ACTIVETIM,AD,ADJUST,ALERT,ALLOC,BASKET,BENCHPX,COND,CONDORDER,DAY,DEACT,DEACTDIS,DEACTEOD,GAT,GTC,GTD,GTT,HID,LMT,NONALGO,OCA,SCALE,SCALERST,WHATIF ValidExchanges: CBOE

alphaDev23 avatar Apr 02 '24 00:04 alphaDev23

@alphaDev23 This topic has been a long time.

After I posted this 1 year ago, I switched to using 'ib_insync' to get the HSI historical data.

cheukhin1024 avatar Apr 02 '24 01:04 cheukhin1024

@wrighter thoughts on my comment?

alphaDev23 avatar Apr 02 '24 18:04 alphaDev23

I haven’t used ib_insync but it does appear to have a number of contributors and some great features. If it’s working for you, that’s great. Hopefully some of the contributors will be able to carry on with the project since it appears the founder has recently passed, which is terrible news.

I unfortunately don’t have much free time to work on this project.

wrighter avatar Apr 02 '24 18:04 wrighter

I had left off the --exchange parameter.

alphaDev23 avatar Apr 02 '24 22:04 alphaDev23