Used Dhan Dynamice Data api - Resolved!

I had try to make api using phyton unfortunately i am getting this error in this following code.

ERROR:main:An error occurred: ‘dhanhq’ object has no attribute ‘request’
INFO:main:Execution completed

from dhanhq import dhanhq
import requests
import time
import os
import logging

Set up logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(name)

Set the API credentials

client_id = “xxxxxxxxx”
access_token = “xxxxxxxxxx”
user_id = “xxxxxxxxx”
trade_api_key = “xxxxxxxxx”

Initialize the DhanHQ client

client = dhanhq(client_id, access_token)

Set the API endpoints and parameters

data_params = {
“exchange”: “NFO”,
“instrument_type”: “FUTURES,OPTIONS”,
“change_percentage”: 2,
“from_time”: “09:16:00”,
“to_time”: “09:16:00”
}

trade_params = {
“symbol”: “”,
“side”: “BUY”,
“quantity”: 1,
“price”: 0
}

try:
# Make API requests
client.request(method=‘GET’, endpoint=‘/api/v1/data’, params=data_params)
data = client.request.json()

# Extract top gainers and losers
top_gainers = data["data"]["top_gainers"]
top_losers = data["data"]["top_losers"]

# Filter based on open interest change
top_gainers_open_interest = [stock for stock in top_gainers if stock["open_interest_change"] > 7]
top_losers_open_interest = [stock for stock in top_losers if stock["open_interest_change"] > 7]

# Get at-the-money strike and 9:20 AM prices
atm_strike = data["data"]["atm_strike"]
high_price_920 = data["data"]["high_price_920"]
low_price_920 = data["data"]["low_price_920"]

# Print results
logger.info("Top Gainers with open interest change > 7%:")
for stock in top_gainers_open_interest:
    logger.info(f"{stock['symbol']} - {stock['change_percentage']}%")

logger.info("\nTop Losers with open interest change > 7%:")
for stock in top_losers_open_interest:
    logger.info(f"{stock['symbol']} - {stock['change_percentage']}%")

# Take a buy trade at 9:25 AM
if time.time() >= 9.25 * 60 * 60:  # 9:25 AM in seconds
    for stock in top_gainers_open_interest + top_losers_open_interest:
        # Check if 9:20 AM high or low is broken
        if stock["current_price"] > high_price_920 or stock["current_price"] < low_price_920:
            # Place buy orders for at-the-money Option CE and PE
            trade_params["symbol"] = stock["symbol"] + "CE"
            trade_params["price"] = atm_strike
            trade_response = requests.post("(link unavailable)", headers={"Authorization": f"Bearer {trade_api_key}"}, params=trade_params)
            logger.info(f"Buying {stock['symbol']} Option CE at {atm_strike} strike at 9:25 AM")

            trade_params["symbol"] = stock["symbol"] + "PE"
            trade_response = requests.post("(link unavailable)", headers={"Authorization": f"Bearer {trade_api_key}"}, params=trade_params)
            logger.info(f"Buying {stock['symbol']} Option PE at {atm_strike} strike at 9:25 AM")

except Exception as e:
logger.error(f"An error occurred: {str(e)}")
finally:
logger.info(“Execution completed”)

Hello @Kailash_Patel

Welcome to Dhan Community!

From the code, two things you need to look into.

  1. When using the library, you have added a request for an endpoint which is incorrect. There is no API endpoint as /api/v1/data
  2. The data requested itself is not what is present via Dhan APIs. Would ask you to refer to documentation here: Historical Data - DhanHQ Ver 1.0 / API Document

Hope this helps!