import datetime
import time
import pandas as pd
import talib as ta
from Zerodha_Tradehull import Tradehull
import talib
api_key = ""
api_secret = ""
tsl = Tradehull(api_key, api_secret, "yes")
kite = tsl.kite
watchlist = ['NIFTY' , 'BANKNIFTY']
traded_stocks = []
print("\nStock\t\tSupertrend\tBuyCond\tSellCond")
while True:
print("\n--------------------------------")
current_time = datetime.datetime.now()
if current_time.time() < datetime.time(9, 20):
print("Waiting for market to open...", current_time.time())
time.sleep(30)
continue
if current_time.time() > datetime.time(3, 20):
print("Market closed — exiting all orders", current_time.time())
tsl.market_over_close_all_orders()
break
for stock_name in watchlist:
try:
chart = tsl.get_short_term_hist_data(
name=stock_name, exchange="NSE", interval="minute", oi=False
)
indi = ta.supertrend(chart['high'], chart['low'], chart['close'], 10, 2)
chart = pd.concat([chart, indi], axis=1, join='inner')
cc = chart.iloc[-2]
supertrend_dir = cc['SUPERTd_10_2.0'] # 1 = Bullish, -1 = Bearish
stoploss_level = round(cc['SUPERTl_10_2.0'], 1)
close_price = cc['close']
bc1 = supertrend_dir == 1
sc1 = supertrend_dir == -1
print(f"{stock_name:<10}\t{supertrend_dir:>4}\t{bc1}\t{sc1}")
if bc1 and stock_name not in traded_stocks:
print(f" ENTRY FOUND IN {stock_name}")
quantity = 1
tsl.place_order(variety="regular", exchange="NSE",tradingsymbol=stock_name, transaction_type="BUY",quantity=quantity, product="MIS", order_type="MARKET")
tsl.place_order(variety="regular", exchange="NSE",tradingsymbol=stock_name, transaction_type="SELL",quantity=quantity, product="MIS", order_type="SL-M",trigger_price=stoploss_level)
traded_stocks.append(stock_name)
elif sc1 and stock_name in traded_stocks:
print(f" EXIT SIGNAL IN {stock_name}")
tsl.place_order(variety="regular", exchange="NSE",tradingsymbol=stock_name, transaction_type="SELL",quantity=1, product="MIS", order_type="MARKET")
traded_stocks.remove(stock_name)
except Exception as e:
print(f"Error in {stock_name}: {e}")
time.sleep(60) # check every 1 minute
Hi Shubham,
we need to change the library, talib does not have suertrend function.
use pandas_ta
install it by :
pip install pandas-ta
import pandas_ta as ta
indi = ta.supertrend(df['high'], df['low'], df['close'], 7, 3)
df = pd.concat([df, indi], axis=1, join='inner')
print(df)
'Column1 = this will give full supertrend line values' trend
'Column2 = this will show if st is red or green' direction
'Column3 = show values only for green nan for red' long
'Column4 = show values only for red nan for green' short