Algo StockOI data unable to fetch

@Tradehull_Imran sir need your help I am not able to get stock OI data
I need previous day and current day OI data i have tried so many times but getting an error could you please help and please fix this issue and guide me what code need to write

can you please provide me previous day OI data and current day OI data i can fetch the both the data please provide me correct code

can you please provide me full code so i can print the OI data please help.

Watchlist = [‘PAGEIND’, ‘CHAMBLFERT’, ‘BOSCHLTD’, ‘SHREECEM’, ‘ABBOTINDIA’, ‘DIXON’, ‘MARUTI’, ‘ULTRACEMCO’, ]

traded_watchlist = # Track traded stocks
active_position = None # Track active stock position
trade_info = {“options_name”: None, “qty”: None, “entry_price”: None, “sl”: None, “target”: None}
in_trading = False # Flag to indicate if the system is in an active trade

profit_target = 2000 # Profit target in INR
start_time = datetime.time(9, 18) # Time to start trading
end_time = datetime.time(23, 20) # Time to close positions
algo_end_time = datetime.time(23, 30) # Time to end the algo

Track the number of buys and sells

buy_count = 0
sell_count = 0

while True:
try:
current_time = datetime.datetime.now().time()

    # Check if it's the time to start trading
    if current_time >= start_time and current_time <= end_time and not in_trading:
        for stock_name in watchlist:  # Start iterating through the watchlist
            if stock_name in traded_watchlist:
                continue  # Skip the stock if it has already been traded

            time.sleep(2)
            print(f"Scanning {stock_name}")

            chart_5 = tsl.get_historical_data(tradingsymbol=stock_name, exchange='NSE', timeframe="5")
            if chart_5.empty:
                print(f"No data for {stock_name}")
                continue

            # Assuming the chart_5 contains data for previous and current prices
            previous_close = chart_5['close'].iloc[-2]  # Previous day's close price
            current_ltp = chart_5['close'].iloc[-1]  # Current day's last traded price

            # Get previous and current volume data
            previous_volume = chart_5['volume'].iloc[-2]  # Previous day's volume
            current_volume = chart_5['volume'].iloc[-1]  # Current day's volume

            # Fetch Open Interest (OI) data for the stock
            previous_oi = tsl.get_open_interest(stock_name, date=datetime.datetime.now() - datetime.timedelta(days=1))
            current_oi = tsl.get_open_interest(stock_name, date=datetime.datetime.now())

            # Print the values of previous close, current LTP, and volumes for debugging
            print(f"Stock: {stock_name} | Previous Close: {previous_close} | Current LTP: {current_ltp} | Previous Volume: {previous_volume} | Current Volume: {current_volume}")
            print(f"Stock: {stock_name} | Previous OI: {previous_oi} | Current OI: {current_oi}")

            # Calculate percentage change in price
            price_change_percent = ((current_ltp - previous_close) / previous_close) * 100

            # Calculate percentage change in volume
            volume_change_percent = ((current_volume - previous_volume) / previous_volume) * 100

            # Calculate OI percentage change
            oi_change_percent = ((current_oi - previous_oi) / previous_oi) * 100

            # Print price and volume change for debugging
            print(f"Stock: {stock_name} | Price Change: {price_change_percent:.2f}% | Volume Change: {volume_change_percent:.2f}% | OI Change: {oi_change_percent:.2f}%")

@Tradehull_Imran please help i am unable to get OI data please helpme

which code i need your help please provide me

Hello @Rahul_singh1

You can raise your query here, if it’s related to the Algo Youtube Series:

Hi @Rahul_singh1

use the below code:

name = "TCS 27 FEB 3900 CALL"

quote_data = tsl.get_quote(name)

oi_data = quote_data[name]['oi']

@Tradehull_Imran sir need your help, i have tried so many times but i am not able to get this data

I am trying to fetch the Change in OI (Open Interest) data in Python, but I have not been able to retrieve the correct number/errors. Could you please help me with the correct method to calculate and fetch the Change in OI?

For your reference, I have attached a snippet of NSE OI data. Additionally, if possible, could you provide Dhan API code to fetch live Change in OI data in Python?

If feasible, a video tutorial would be greatly helpful in understanding the process better.

Looking forward to your guidance.

please provide a code do to fetch OI data??

@Tradehull_Imran not able to get option data

(Pdb++) atm_strike, option_chain = tsl.get_option_chain(Underlying=“NIFTY”, exchange=“INDEX”, expiry=0,num_strikes=10)
*** AttributeError: ‘Tradehull’ object has no attribute ‘get_option_chain’

can you please help

import pdb
import time
import datetime
import traceback
from Dhan_Tradehull_V2 import Tradehull
import pandas as pd
import talib
import pandas_ta as ta # For Supertrend
import warnings
import xlwings as xw

---------------for Dhan login ----------------

client_code = “11”
token_id = “eyJ0eXAiOiJKV1QiLCJhbyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzQyNTM5NzA4LCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwNDUxNDk2MSJ9.tKt-vBK_31XsSfcsEALrPdgp1eM7a37TeEm4CkE6oxUdCnwnKnTxtwfAVvSEmbxy-HxFkNtWChV6_jNo8tGkzw”
tsl = Tradehull(client_code, token_id)

pdb.set_trace()

atm_strike, option_chain = tsl.get_option_chain(Underlying=“NIFTY”, exchange=“INDEX”, expiry=0,num_strikes=10)

Hi @Rahul_singh1 ,

Do update codebase on your system

  • Open Command Prompt: Press Win, type cmd, and press Enter.
  • Install Dhan-Tradehull: Run pip install Dhan-Tradehull
  • Confirm the installation by running pip show Dhan-Tradehull

Guide to use the updated codebase:
Video reference : https://www.youtube.com/watch?v=P9iPYShakbA

@Tradehull_Imran can you please help

code is unable to buying CE strike getting an error:
Time: 2025-02-25 16:05:20, Current LTP: 350.5
CE Conditions: False, True
PE Conditions: True, True, True, False, False, False (504.0 > 926.0), True
CE Buying signal formed
Exception at getting Expiry list as Check the Tradingsymbol
Unable to find the correct Expiry for NATURALGASM
Error in the main trading loop: cannot unpack non-iterable NoneType object
Traceback (most recent call last):
File “MCX.py.py”, line 172, in
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying=‘NATURALGASM’, Expiry=0)
TypeError: cannot unpack non-iterable NoneType object

ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying=‘NATURALGASM’, Expiry=0)
not buying the CE getting an error can you please help me