wrighter / ib-scripts

Python scripts that use the Interactive Brokers TWS API
MIT License
45 stars 18 forks source link

Cannot download data despite the IB symbol is correct. #12

Open cheukhin1024 opened 1 year ago

cheukhin1024 commented 1 year ago

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 {}
wrighter commented 1 year ago

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.

alphaDev23 commented 3 months ago

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

cheukhin1024 commented 3 months ago

@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.

alphaDev23 commented 3 months ago

@wrighter thoughts on my comment?

wrighter commented 3 months ago

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.

alphaDev23 commented 3 months ago

I had left off the --exchange parameter.