How to get historical data for any futures?

On the official documentation page, it is mentioned that expiry_code is required if data needs to be fetched for any derivative. It also gives a csv file but it only contains 0 in expiry code column on every single row.

For example, if I want to look for HDFCBANK current month future, i.e HDFCBANK JUN FUT, what would be the expiry code? The CSV file only has 0 like I mentioned before. How should I fetch this data? Trading symbol is HDFCBANK-Jun2024-FUT and other params are

symbol: "HDFCBANK-Jun2024-FUT",
exchange_segment: "NSE_FNO",
instrument_type: "FUTSTK",
expiry_code: 0, // Set expiry_code to 0 as specified
from_date: "2024-06-01",
to_date: "2024-06-16",

Sandbox returns 200 response but there is no data. @Hardik

Hello @gegobyte

For futures contract, the expiry code needs to be on the basis of expiry.

You can refer to this link for the expiry code:

1 Like

I have set expiry_code to 1 as you suggested to get the current month future but still not getting any data.

Hello @gegobyte

Over here, symbol needs to be of the underlying which is HDFCBANK only. Do try with this and let us know if you are still facing issue.

1 Like

Thanks, it’s now working. Can you please update this information over documentation so other users who are getting started can know that symbol is just the ticker name and expiry code can have 3 values based on the month’s data they want to fetch.

Sure, will do that.

I am using this code to try and fetch live data for HDFCBANK JUN FUT

from dhanhq import marketfeed
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

client_id = os.getenv('DHAN_CLIENT_ID')
access_token = os.getenv('DHAN_ACCESS_TOKEN')

# Structure for subscribing is ("exchange_segment","security_id")

# Maximum 100 instruments can be subscribed, then use 'subscribe_symbols' function 

instruments = [(2, "44191")]

# Type of data subscription
subscription_code = marketfeed.Depth

# Ticker - Ticker Data
# Quote - Quote Data
# Depth - Market Depth


async def on_connect(instance):
    print("Connected to websocket")

async def on_message(instance, message):
    print("Received:", message)
    instance.subscribe_symbols(19, instruments)

print("Subscription code :", subscription_code)

feed = marketfeed.DhanFeed(client_id,
    access_token,
    instruments,
    subscription_code,
    on_connect=on_connect,
    on_message=on_message)

feed.run_forever()

and getting this in Console

RuntimeWarning: coroutine 'DhanFeed.connect' was never awaited
  self.connect()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Connected to websocket
Connected to websocket
Received: {'type': 'Market Depth', 'exchange_segment': 2, 'security_id': 44191, 'LTP': 1696.5, 'depth': [{'bid_quantity': 1100, 'ask_quantity': 550, 'bid_orders': 1, 'ask_orders': 1, 'bid_price': 1696.5, 'ask_price': 1696.5999755859375}, {'bid_quantity': 550, 'ask_quantity': 6050, 'bid_orders': 1, 'ask_orders': 2, 'bid_price': 1696.4000244140625, 'ask_price': 1696.699951171875}, {'bid_quantity': 550, 'ask_quantity': 1100, 'bid_orders': 1, 'ask_orders': 2, 'bid_price': 1696.3499755859375, 'ask_price': 1696.800048828125}, {'bid_quantity': 2750, 'ask_quantity': 1100, 'bid_orders': 5, 'ask_orders': 2, 'bid_price': 1696.0999755859375, 'ask_price': 1696.9000244140625}, {'bid_quantity': 6050, 'ask_quantity': 2200, 'bid_orders': 6, 'ask_orders': 4, 'bid_price': 1696.0, 'ask_price': 1696.949951171875}]}
Traceback (most recent call last):
  File "/Users/.../backend/live_feed.py", line 105, in <module>
    feed.run_forever()
  File "/Users/.../backend/venv/lib/python3.11/site-packages/dhanhq/marketfeed.py", line 83, in run_forever
    self.loop.run_until_complete(self.connect())
  File "/opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/.../backend/venv/lib/python3.11/site-packages/dhanhq/marketfeed.py", line 99, in connect
    await helper.on_message_received(self.data)
  File "/Users/.../backend/venv/lib/python3.11/site-packages/dhanhq/marketfeed.py", line 47, in on_message_received
    await self.sdk_instance.on_message(self.sdk_instance, response)
  File "/Users/.../backend/live_feed.py", line 94, in on_message
    instance.subscribe_symbols(19, instruments)
  File "/Users/.../backend/venv/lib/python3.11/site-packages/dhanhq/marketfeed.py", line 333, in subscribe_symbols
    asyncio.ensure_future(self.ws.send(self.create_subscription_packet(symbols, feed_request_code, subscribe=True)))
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: DhanFeed.create_subscription_packet() got an unexpected keyword argument 'subscribe'

So receiving depth data for this future as can be seen in the log but immediately it is getting terminated with this error.

Hello @gegobyte

Can you check if the library is updated to the latest version, as this as unexpected keyword error, which shouldn’t occur, unless you have defined the same somewhere in the library yourself.

Yes, I am on the latest version.

@gegobyte

Over here, you are using this line of code. Can you explain what is this for?

I got it from Dhan’s official video on how to set up market feed. Here is the link:

I have removed that line and it’s now working, thank you!

1 Like

This guy on the YouTube has caused more confusion than to be of help. Unfortunately. I could not find a single video where a marketfeed usecase is demonstrated right from the beginning.

Absolutely . Even their own youtube videos showing wrong or outdated information. They should withdraw or update them. They should care for others time. I am not talking about their free services. I paid the subscription amount for data api and struggling for the information. Everything is scattered.

Hello @pinaki248

Welcome to Dhan Community!

Can you tell me what issues are you facing? The idea with the youtube series is for beginners to get started with algo trading, and there are a lot of members in this community itself who have benefitted from same.

On the marketfeed usecase, what is the issue you are facing?

Do you have any link, where it mentions properly to use functions like intraday_daily_minute_charts, intraday_minute_data and historical_daily_data with index options?

Hello @Ram_Ashish_Pal

No, there is no instrument specific sample code. For index options, you need to mention the exchange_segment as BSE_FNO or NSE_FNO depending on exchange.

Also, on instrument type, it needs to be OPTIDX. You can refer to annexure in documentation for the same.