OPen Interest Data not retrieved using Talib indicator

Since we dont have OI column, i couldn't retireieve 
option_df['avg_OI'] = talib.MA(option_df['open_interest'], timeperiod=20,matype=0)

i could able to see the data in ur video..

Need ur input on this ..
thanks.

Make sure you are entering correct key for open interest.

print(option_chain[‘data’][0].keys()) # available keys

Hi @Sudhakar_Prabhakar ,

Do update the the codebase referring the below link and please retry -

Thank you sir for the detailed explanation..previously i used version 3.11..as you said i have changed to 3.12.9..
my intention to get the open interest data from a particular strike ..
still i’m facing the error..

name=“NIFTY 24 FEB 25450 CALL”

option_df = tsl.get_historical_data(tradingsymbol = name,exchange = 'NFO',timeframe="15")

option_df[‘avg_price’] = talib.MA(option_df[‘close’], timeperiod=20) > Works Fine
option_df[‘avg_OI’] = talib.MA(option_df[‘open_interest’], timeperiod=20,matype=0) –>Not Working

error message below:

Traceback (most recent call last):
File “C:\Users\Admin\PycharmProjects\PythonProject1.venv\Lib\site-packages\pandas\core\indexes\base.py”, line 3641, in get_loc
return self._engine.get_loc(casted_key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “pandas/_libs/index.pyx”, line 168, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/index.pyx”, line 197, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/hashtable_class_helper.pxi”, line 7668, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas/_libs/hashtable_class_helper.pxi”, line 7676, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘open_interest’

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “C:\Users\Admin\PycharmProjects\PythonProject1.venv\Scripts\syntaxes.py”, line 49, in
option_df[‘avg_OI’] = talib.MA(option_df[‘open_interest’], timeperiod=20,matype=0)
~~~~~~~~~^^^^^^^^^^^^^^^^^
File “C:\Users\Admin\PycharmProjects\PythonProject1.venv\Lib\site-packages\pandas\core\frame.py”, line 4378, in getitem
indexer = self.columns.get_loc(key)
^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Admin\PycharmProjects\PythonProject1.venv\Lib\site-packages\pandas\core\indexes\base.py”, line 3648, in get_loc
raise KeyError(key) from err
KeyError: ‘open_interest’

used the same logic as in ur video..

i got stuck here and need ur assistance on this…Thanks

Hi @Sudhakar_Prabhakar ,

Make sure the column ‘open_interest’ is present in option_df, do print the data present in option_df and share the screenshot-

option_df[‘avg_OI’] = talib.MA(option_df[‘open_interest’], timeperiod=20,matype=0)

Sir,

Thanks for the response..

Please find the below screenshot..

strike price: NIFTY 24 FEB 25450 CALL

Data populated correct..couldn’t validate the OI column..

gettinf error message below:
option_df[‘avg_OI’] = talib.MA(option_df[‘open_interest’], timeperiod=20,matype=0)

indexer = self.columns.get_loc(key)
^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Admin\PycharmProjects\PythonProject1.venv\Lib\site-packages\pandas\core\indexes\base.py”, line 3648, in get_loc
raise KeyError(key) from err
KeyError: ‘open_interest’

OI is not supported on get_historical_data function by the package. Use Dhan official python package to get OI. github . com/dhan-oss/DhanHQ-py/blob/main/src/dhanhq/_historical_data.py#L71

thanks for the reply..do you have any demo on how to use the historical data to get the code by using DhanHQ..Thanks

Sir,
could not install the latest version of Dhan-Tradehull 3.2.0..
it says the latest version not in public domain…i hope thats the reason where i couldn’t get the OI data and other functionality related to it..

i could able to install only the below version..

Name: Dhan_Tradehull
Version: 3.0.6

Official python package API docs. dhanhq . co/docs/DhanHQ-py/_historical_data/#dhanhq._historical_data.HistoricalData.historical_daily_data

Try following as it seems Dhan Python docs are old as well and not having OI parameter.


import requests

url = “https://api.dhan.co/v2/charts/historical”

headers = {
“Content-Type”: “application/json”,
“access-token”: “JWT”  # Replace with your actual JWT token
}

payload = {
“securityId”: “1333”,
“exchangeSegment”: “NSE_EQ”,
“instrument”: “EQUITY”,
“expiryCode”: 0,
“oi”: True,
“fromDate”: “2022-01-08”,
“toDate”: “2022-02-08”
}

response = requests.post(url, headers=headers, json=payload)

print(“Status Code:”, response.status_code)
print(“Response:”, response.text)

Try “pre” pip install --pre dhanhq to get the latest official python package v2.2.0 or else older version will be installed.

Hi @Sudhakar_Prabhakar ,

Run the below commands in cmd and make sure you have installed both these versions-

pip install --pre dhanhq
pip install Dhan-Tradehull==3.2.0

Confirm the installation by running the below commands-

pip show dhanhq
pip show Dhan-Tradehull

Below is the screenshot on to to get historical data with oi-

Sir,

Thanks a lot for your input..i could able to get the OI data after installing the required packages.

Thanks.

1 Like