Unable to fetch updated ltp price for nifty strike

Hi,
I’m trying to calculate points gained after entry but ltp price is not getting updated. needed help…

# Strategy: 5 EMA Scalping for Nifty
from Dhan_Tradehull import Tradehull
import datetime
import time
import talib
import pandas as pd
import pdb

# Login
client_code = "1105ssss"
access_token = "dsafadsfasdfasdfsdfasdw"
tsl = Tradehull(client_code, access_token)
in_sell_trade = "No"
target_price = 30
stop_loss = -15

# Sell Setup
hist_data_5 = tsl.get_historical_data(tradingsymbol='NIFTY', exchange='INDEX', timeframe="5")
df_5 = pd.DataFrame(hist_data_5)
df_5['EMA5'] = talib.EMA(df_5['close'], timeperiod=5)
df_5.set_index('timestamp')

for i in range(1, len(df_5)):
    # pdb.set_trace()
    # print(df_5.iloc[i]['timestamp'])
    if in_sell_trade=="Yes":
        pe_ltp = tsl.get_ltp_data(names=[pe_strike])[pe_strike]
        points = pe_ltp - entry_sell
        if points >= target_price:
            print(f"Sell Target hit! Points gained: {points}")
            in_sell_trade = "No"
            break
        elif points <= stop_loss:
            print(f"Sell Stop loss hit! Points lost: {points}")
            in_sell_trade = "No"
            break
        print(f"{df_5.iloc[i]['timestamp']} : {entry_sell} - {pe_ltp} = {points}")
    if in_sell_trade == "No":    
        completed_candle_sell = df_5.iloc[i-1]
        current_candle_sell = df_5.iloc[i]

        sc1 = (completed_candle_sell['close'] > completed_candle_sell['EMA5']) and (completed_candle_sell['low'] >  completed_candle_sell['EMA5'])
        sc2 = (current_candle_sell['close'] > completed_candle_sell['low'])

        if sc1 and sc2:
            ce_strike, pe_strike, strike = tsl.ATM_Strike_Selection(Underlying='NIFTY', Expiry=0)
            pe_ltp = tsl.get_ltp_data(names=[pe_strike])[pe_strike]
            print(f"Sell signal: {pe_strike}, {strike}, {pe_ltp}")
            entry_sell = pe_ltp
            in_sell_trade = "Yes"
        else:
            print(f"{df_5.iloc[i]['timestamp']} - No Sell signal")

here is the output :

PS D:\TradeHull\Mentorship\Lab> py .\5EMA_Plus.py
Codebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2024-12-30.csv
Got the instrument file
2024-12-26 09:20:00+05:30 - No Sell signal
2024-12-26 09:25:00+05:30 - No Sell signal
2024-12-26 09:30:00+05:30 - No Sell signal
2024-12-26 09:35:00+05:30 - No Sell signal
2024-12-26 09:40:00+05:30 - No Sell signal
2024-12-26 09:45:00+05:30 - No Sell signal
2024-12-26 09:50:00+05:30 - No Sell signal
2024-12-26 09:55:00+05:30 - No Sell signal
2024-12-26 10:00:00+05:30 - No Sell signal
2024-12-26 10:05:00+05:30 - No Sell signal
2024-12-26 10:10:00+05:30 - No Sell signal
2024-12-26 10:15:00+05:30 - No Sell signal
2024-12-26 10:20:00+05:30 - No Sell signal
2024-12-26 10:25:00+05:30 - No Sell signal
2024-12-26 10:30:00+05:30 - No Sell signal
2024-12-26 10:35:00+05:30 - No Sell signal
2024-12-26 10:40:00+05:30 - No Sell signal
2024-12-26 10:45:00+05:30 - No Sell signal
2024-12-26 10:50:00+05:30 - No Sell signal
2024-12-26 10:55:00+05:30 - No Sell signal
2024-12-26 11:00:00+05:30 - No Sell signal
2024-12-26 11:05:00+05:30 - No Sell signal
2024-12-26 11:10:00+05:30 - No Sell signal
2024-12-26 11:15:00+05:30 - No Sell signal
2024-12-26 11:20:00+05:30 - No Sell signal
2024-12-26 11:25:00+05:30 - No Sell signal
Sell signal: NIFTY 02 JAN 23650 PUT, 23650, 104.15
2024-12-26 11:35:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 11:40:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 11:45:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 11:50:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 11:55:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:00:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:05:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:10:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:15:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:20:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:25:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:30:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:35:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:40:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:45:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:50:00+05:30 : 104.15 - 104.15 = 0.0
2024-12-26 12:55:00+05:30 : 104.15 - 104.15 = 0.0
Traceback (most recent call last):
  File ".\5EMA_Plus.py", line 29, in <module>
    pe_ltp = tsl.get_ltp_data(names=[pe_strike])[pe_strike]
  File "C:\Users\sg_la\AppData\Local\Programs\Python\Python38\lib\site-packages\Dhan_Tradehull\Dhan_Tradehull.py", line 637, in get_ltp_data
    time.sleep(2)
KeyboardInterrupt

Hello @Srujan,

The points are not being updated because you are running the script after market hours. You are setting the entry price to the current market price, and at the same time, you are fetching the LTP (Last Traded Price), which remains constant since the market is closed. This causes the value to stay the same. Additionally, check the formula you are using; since the entry price and LTP are the same, the calculated points remain unchanged throughout the loop.

Please verify and correct the logic.

Thanks for the reply Shubham,
I ran this program during market hours only, anyways this works on historical data, see date in output its 26th Dec data… I’m fetching ltp of strike 23650pe when entry conditions are met on Nifty index which is my entry price. if you see the output entry is at 11:30 price is 104.15. this entry price i wanted to compare with ltp of next candles say 11:35, 11:40, … and calculate points so that i can compare it with target/stoploss points. But pe_ltp = tsl.get_ltp_data(names=[pe_strike])[pe_strike] is always returning ltp of 11:30 candle. i don’t understand why…

I think i understood now… get_ltp_data() fetches live ltp, not the historical ltp. what i needed to do is to fetch historical values of strike (23650pe) and then process it… thanks for helping Shubham :slight_smile:

1 Like