How can I define or implement a TII (trend intensity index) indicator inside the algo?
Hi @sunita_sriv
can you share the pine script / math formula for TII.
If we can get the formula, we can simply call the historical data, and apply the function for TII on it to get its values
Hi,
Thank you for your reply. Please find the formula below.

Hi @sunita_sriv ,
Do run the following commands in the cmd -
pip install dhanhq==2.2.0
pip install Dhan-Tradehull==3.3.1
Refer the code for the indicator -
from Dhan_Tradehull import Tradehull
client_code = ""
tsl = Tradehull(client_code, mode="pin_totp", pin="", totp_secret="")
def calculate_ti_indicator(df, period=28, close_col="close"):
"""
TI Indicator calculation based on:
SMA_28 = 28-period Simple Moving Average of Close
SD+ = Sum(Close - SMA_28) when Close > SMA_28
SD- = Sum(SMA_28 - Close) when Close < SMA_28
TI1 = SD+ / (SD+ + SD-) * 100
"""
df = df.copy()
# 1. SMA 28
df[f"SMA_{period}"] = df[close_col].rolling(window=period).mean()
# 2. Positive deviation
df["SD_plus_value"] = 0.0
df.loc[df[close_col] > df[f"SMA_{period}"], "SD_plus_value"] = (df[close_col] - df[f"SMA_{period}"] )
# 3. Negative deviation
df["SD_minus_value"] = 0.0
df.loc[df[close_col] < df[f"SMA_{period}"], "SD_minus_value"] = (df[f"SMA_{period}"] - df[close_col])
# 4. Rolling sum of SD+ and SD-
df["SD_plus"] = df["SD_plus_value"].rolling(window=period).sum()
df["SD_minus"] = df["SD_minus_value"].rolling(window=period).sum()
# 5. Final TI indicator
total_sd = df["SD_plus"] + df["SD_minus"]
df["TI1"] = 0.0
df.loc[total_sd != 0, "TI1"] = (df["SD_plus"] / total_sd) * 100
return df
hist_data = tsl.get_historical_data(tradingsymbol="NIFTY",exchange="INDEX",timeframe="5")
print(hist_data)
hist_data = calculate_ti_indicator(hist_data, period=28)
print(hist_data)
Output -

Thank you so much @Tradehull_Imran
1 Like