Learn Algo Trading with Python | Codes | Youtube Series

Very Good Afternoon Sir, I have enough money in my Dhan Account. But not able to Subscribe for Dhan API.
It is generating token but, its showing the following error sir.

C:\Users\LENOVO PC\Desktop\RP\Dhan Algo\DHAN Advanced Algo Trading Series\Session 9\Session 9\Dhan Option Chain\Dhan Option Chain>py "1. Download_Historical_data.py"
Codebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2025-04-29.csv
Got the instrument file
Exception in Getting OHLC data as {'status': 'failure', 'remarks': {'error_code': 'DH-902', 'error_type': 'Invalid_Access', 'error_message': 'HTTP Status 451. User has not subscribed to Data APIs or does not have access to Trading APIs. Kindly subscribe to Data APIs to be able to fetch Data.'}, 'data': {'errorType': 'Invalid_Access', 'errorCode': 'DH-902', 'errorMessage': 'HTTP Status 451. User has not subscribed to Data APIs or does not have access to Trading APIs. Kindly subscribe to Data APIs to be able to fetch Data.'}}
Traceback (most recent call last):
  File "1. Download_Historical_data.py", line 31, in <module>
    ATM_time_data = index_data[index_data['timestamp']==start_time]
TypeError: 'NoneType' object is not subscriptable

[
106xauto

148xauto
](Dhan Web - Made for Fast Online Trading and Investing Platform)

120xauto

DhanHQ Data APIs Subscription Successful

Dear Babu,

You have successfully subscribed to DhanHQ Data APIs. You can now access real-time market feed across segments along with historical data too.

Your subscription will be auto-renewed every 30 days. You can check and modify subscription plan from Dhan Web.

If you need help, feel free to reach out to our team by writing to

help@dhan.co we’re always available to assist you!

I am not able to understand, why the error is showing like that sir?
VBR Prasad

Subscription Completed, now getting th same old error. I have downloaded the Dhan_Tradehull_V2 file in this folder and tried. StarT_Date is not available in Dhan_Tradehull file. But it’s available only in Dhan_tradehull_V2 file. This si the reason , I got this file in my Current Folder of Session-9 of Advanced Series sir. Still Getting the same error.

C:\Users\LENOVO PC\Desktop\RP\Dhan Algo\DHAN Advanced Algo Trading Series\Session 9\Session 9\Dhan Option Chain\Dhan Option Chain>py "1. Download_Historical_data.py"
Codebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2025-04-29.csv
Got the instrument file
Traceback (most recent call last):
  File "1. Download_Historical_data.py", line 38, in <module>
    ATM_Strike = round(ATM_close/step)*step
NameError: name 'ATM_close' is not defined

@Tradehull_Imran @Dhan @RahulDeshpande

Hi everyone,

Is there a plan to teach us how to backtest our strategy?

Please create a few videos so we can determine which approach works best for which instrument and when.

Thanks

@Tradehull_Imran

Hi sir,

my excel files breaks connection with python code frequently and throws this error.

Error : (-2147417851, ‘The server threw an exception.’, None, None)

how to reconnect excel with python code?

Hi @Vasili_Prasad ,

In pdb check if your able to fetch data from the date you want to download the data:

index_data = tsl.get_historical_data(tradingsymbol=index_name, exchange='INDEX', timeframe="1")

Hi @Qaisar ,

Excel may busy or not responding, Multiple threads accessing Excel . You can save the excel before re accessing it using

 wb.save('your_file.xlsx')

Hi @Qaisar ,

Refer the below video :

Hi @Dhan team,
FYI,
In continuation to previous thread,
There was no greeks PE data between the following window (Nifty).
Wed Apr 30 14:08 2025
Wed Apr 30 14:44 2025
~ 40 minutes.
Whereas it was showing valid data for CE between this window… :hushed:

Sir, I am a new trader and went through Session 3. tried installing the libraries and executed file Dhan_codebase usage with my client id and Token. Following error appears: ModuleNotFoundError: No module named ‘talib’

Hi @Dr_Gauri_Shirodker ,

1. Install Precompiled Binary:

  • Download the matching .whl file for your Python version from this unofficial site:
    Christoph Gohlke If you’re using Python 3.10 (64-bit), download TA_Lib‑0.4.0‑cp310‑cp310‑win_amd64.whl

2. Install via pip:

Open your terminal or command prompt where the file is downloaded and run:

pip install TA_Lib‑0.4.0‑cp310‑cp310‑win_amd64.whl

Thanks @anandc . Will try resampling

hi @Tradehull_Imran sir,

while using tsl.ATM_Strike_Selection() or tsl.ITM_Strike_Selection(), i get strike in 50s… what shud I do to get these strikes only in 100s… like 24000, 24100, 24200, 24300 and likes and NOT like 24050, 24150,24250,.. and likes…

I am using a Mac, and the installation process is not very clear to me. How can I get help with this, and how can I update it for Mac?

Please help with error

Hi @anandc ,

You need to explicitly change in Dhan_Tradehull file.

Locate the below line at the beginning of the code:

self.index_step_dict = {'MIDCPNIFTY':25,'SENSEX':100,'BANKEX':100,'NIFTY': 50, 'NIFTY 50': 50, 'NIFTY BANK': 100, 'BANKNIFTY': 100, 'NIFTY FIN SERVICE': 50, 'FINNIFTY': 50}

replace it with:

self.index_step_dict = {'MIDCPNIFTY':25,'SENSEX':100,'BANKEX':100,'NIFTY': 100, 'NIFTY 50': 100, 'NIFTY BANK': 100, 'BANKNIFTY': 100, 'NIFTY FIN SERVICE': 50, 'FINNIFTY': 50}
1 Like

Hi @iabhinavshukla ,

Refer the below thread:
https://private-poc.madefortrade.in/t/learn-algo-trading-with-python-codes-youtube-series/32718/1266?u=tradehull_imran

Hi @Mehul_Sheth ,

tsl.get_intraday_data was a old method to get historical data… it gives data only for today, which makes problem if we want to create indicators on it.

Dhanhq was upgraded and now we can use get_historical_data which gives data for last 5 working days.

Retrieve historical or intraday data:

Get Historical Data

  • tsl.get_historical_data(tradingsymbol: str, exchange: str, timeframe: str, debug: str = “NO”)
    • Arguments:
      • tradingsymbol (str): The trading symbol for the instrument you want to fetch data for (e.g., ‘NIFTY’, ‘ACC’).
      • exchange (str): The exchange where the instrument is traded (e.g., ‘NSE’, ‘INDEX’).
      • timeframe (str): The timeframe for the data. It can be:
        • ‘1’ for 1-minute candles
        • ‘5’ for 5-minute candles
        • ‘15’ for 15-minute candles
        • ‘25’ for 25-minute candles
        • ‘60’ for 60-minute candles
        • ‘DAY’ for daily candles
      • debug (optional, str): Set to “YES” to enable detailed API response logging. Default is “NO”.
    • Sample Code:
data = tsl.get_historical_data(tradingsymbol='NIFTY', exchange='INDEX', timeframe="DAY") 
data = tsl.get_historical_data(tradingsymbol='ACC', exchange='NSE', timeframe="1")

@Tradehull_Imran , till friday it was running fine, now Getting below error:

Traceback (most recent call last):
File “MACD_strategy_Nifty_1min.py”, line 73, in
all_ltp = tsl.get_ltp_data(names = watchlist)
AttributeError: ‘numpy.bool_’ object has no attribute ‘get_ltp_data’

can u help me fix this issue..?