import time
import pandas as pd
import pandas_ta as ta
from datetime import datetime, time as dtime
from Dhan_Tradehull import Tradehull
# ================= LOGIN =================
CLIENT_CODE = " "
TOKEN_ID = " "
tsl = Tradehull(CLIENT_CODE, TOKEN_ID)
# ================= WATCHLIST =================
equity_symbols = ["APLAPOLLO", "IOC", "ONGC", "SAIL"]
futures_map = {
"APLAPOLLO": "APLAPOLLO MAR FUT",
"IOC": "IOC MAR FUT",
"ONGC": "ONGC MAR FUT",
"SAIL": "SAIL MAR FUT"
}
TARGET_PCT = 0.05
positions = {}
last_processed_candle = {}
# ============================================================
# RESAMPLE
# ============================================================
def convert_to_75min(df):
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['timestamp'] = df['timestamp'].dt.tz_localize(None)
df = df.set_index('timestamp')
df_75 = df.resample('75min', origin='start_day').agg({
'open':'first',
'high':'max',
'low':'min',
'close':'last'
}).dropna()
df_75.reset_index(inplace=True)
return df_75
print("🚀 SUPERTREND TARGET STRATEGY STARTED")
while True:
for eq in equity_symbols:
try:
df = tsl.get_historical_data(eq, "NSE", "1")
if df is None or len(df) < 200:
continue
df_75 = convert_to_75min(df)
if len(df_75) < 3:
continue
# ===== LAST CLOSED CANDLE =====
last_candle_time = df_75['timestamp'].iloc[-2]
if eq in last_processed_candle and last_processed_candle[eq] == last_candle_time:
continue
# ===== SUPERTREND =====
st = ta.supertrend(
df_75['high'], df_75['low'], df_75['close'],
length=10, multiplier=1
)
df_75['dir'] = st['SUPERTd_10_1.0']
prev_dir = df_75['dir'].iloc[-3]
curr_dir = df_75['dir'].iloc[-2]
price = df_75['close'].iloc[-2]
fut_symbol = futures_map[eq]
print(f"{eq} | prev:{prev_dir} curr:{curr_dir} | close:{price}")
# ================= ENTRY =================
if eq not in positions and prev_dir == -1 and curr_dir == 1:
lot = tsl.get_lot_size(fut_symbol)
orderid = tsl.order_placement(
tradingsymbol=fut_symbol,
exchange="NFO",
quantity=lot,
price=0,
trigger_price=0,
order_type="MARKET",
transaction_type="BUY",
trade_type="NRML"
)
if orderid:
positions[eq] = price
print(f"🟢 BUY {fut_symbol} @ {price}")
# ================= EXIT =================
elif eq in positions:
entry = positions[eq]
target = entry * (1 + TARGET_PCT)
if price >= target or curr_dir == -1:
lot = tsl.get_lot_size(fut_symbol)
orderid = tsl.order_placement(
tradingsymbol=fut_symbol,
exchange="NFO",
quantity=lot,
price=0,
trigger_price=0,
order_type="MARKET",
transaction_type="SELL",
trade_type="NRML"
)
if orderid:
print(f"🔴 EXIT {fut_symbol} @ {price}")
del positions[eq]
last_processed_candle[eq] = last_candle_time
except Exception as e:
print(f"❌ Error {eq}: {e}")
time.sleep(60)
Hi @7350982949 ,
Use trade_type = "CNC" instead of ‘NRML’ to place positional orders.