How to build a strategy on Ichimoku cloud baseline indicator and cloud , candle type renko, default box size 1% of LTP of instrument in 1 min timeframe
Hi @GautamB
use this pseudocode for reference
from Dhan_Tradehull import Tradehull
from rich import print
import pandas as pd
import datetime
import time
import pretty_errors
import pdb
# -------------------------------------------- Setup --------------------------------------------
client_code = "YOUR_CLIENT_CODE"
token_id = "YOUR_ACCESS_TOKEN"
tsl = Tradehull(client_code, token_id, mode="access_token")
watchlist = ["RELIANCE", "SBILIFE"]
status = {'traded': None, 'symbol': None}
orderbook = {name: status.copy() for name in watchlist}
ENTRY_TIME = datetime.time(9, 20)
EXIT_TIME = datetime.time(15, 15)
RENKO_PCT = 0.01 # 1% box size of LTP
TIMEFRAME = '1' # 1-minute source candles
QTY = 1
# -------------------------------------------- Renko + Ichimoku --------------------------------------------
def build_renko(chart, box_size):
bricks, price = [], chart.iloc[0]['close']
for _, row in chart.iterrows():
c = row['close']
while c >= price + box_size:
price += box_size
bricks.append({'open': price - box_size, 'close': price, 'high': price,
'low': price - box_size, 'direction': 1, 'timestamp': row['timestamp']})
while c <= price - box_size:
price -= box_size
bricks.append({'open': price + box_size, 'close': price, 'high': price + box_size,
'low': price, 'direction': -1, 'timestamp': row['timestamp']})
return pd.DataFrame(bricks)
def add_ichimoku(df, tenkan=9, kijun=26, senkou=52):
h, l = df['high'], df['low']
df['tenkan'] = (h.rolling(tenkan).max() + l.rolling(tenkan).min()) / 2
df['kijun'] = (h.rolling(kijun).max() + l.rolling(kijun).min()) / 2 # baseline
df['span_a'] = ((df['tenkan'] + df['kijun']) / 2).shift(kijun)
df['span_b'] = ((h.rolling(senkou).max() + l.rolling(senkou).min()) / 2).shift(kijun)
df['cloud_top'] = df[['span_a', 'span_b']].max(axis=1)
df['cloud_bot'] = df[['span_a', 'span_b']].min(axis=1)
return df
# -------------------------------------------- Main Loop --------------------------------------------
while True:
current_time = datetime.datetime.now().time()
if current_time < ENTRY_TIME:
print(f"{current_time} Waiting for the market to open")
time.sleep(30)
continue
if current_time > EXIT_TIME:
print(f"{current_time} Market over — exiting loop")
break
for name in watchlist:
ltp_data = tsl.get_ltp_data(names=[name])
ltp = ltp_data[name]
box_size = round(ltp * RENKO_PCT, 2)
chart = tsl.get_historical_data(tradingsymbol=name, exchange='NSE', timeframe=TIMEFRAME)
renko = build_renko(chart, box_size)
if len(renko) < 55:
continue
renko = add_ichimoku(renko)
rc = renko.iloc[-1] # last completed Renko brick
cloud_top = rc['cloud_top']
cloud_bot = rc['cloud_bot']
price = rc['close']
# -------------------------------------------- Buy Conditions --------------------------------------------
bc1 = price > cloud_top # above cloud
bc2 = rc['tenkan'] > rc['kijun'] # baseline bullish (TK cross)
bc3 = rc['direction'] == 1 # green Renko brick
bc4 = orderbook[name]['traded'] is None
# -------------------------------------------- Sell Conditions --------------------------------------------
sc1 = price < cloud_bot # below cloud
sc2 = rc['tenkan'] < rc['kijun'] # baseline bearish
sc3 = rc['direction'] == -1 # red Renko brick
sc4 = orderbook[name]['traded'] is None
if bc1 and bc2 and bc3 and bc4:
limit_price = round(ltp * 1.002, 1)
try:
entry_orderid = tsl.order_placement(
tradingsymbol=name, exchange='NSE', quantity=QTY,
price=limit_price, trigger_price=0,
order_type='LIMIT', transaction_type='BUY', trade_type='MIS')
orderbook[name]['symbol'] = name
orderbook[name]['entry'] = limit_price
orderbook[name]['sl'] = round(rc['kijun'], 2) # baseline as SL
orderbook[name]['traded'] = 'BUY'
print(f"{current_time} {name} BUY — above cloud, TK > Kijun, green Renko")
except Exception as e:
print(f"Error placing entry order: {e}")
elif sc1 and sc2 and sc3 and sc4:
limit_price = round(ltp * 0.998, 1)
try:
entry_orderid = tsl.order_placement(
tradingsymbol=name, exchange='NSE', quantity=QTY,
price=limit_price, trigger_price=0,
order_type='LIMIT', transaction_type='SELL', trade_type='MIS')
orderbook[name]['symbol'] = name
orderbook[name]['entry'] = limit_price
orderbook[name]['sl'] = round(rc['kijun'], 2)
orderbook[name]['traded'] = 'SELL'
print(f"{current_time} {name} SELL — below cloud, TK < Kijun, red Renko")
except Exception as e:
print(f"Error placing entry order: {e}")
time.sleep(60) # re-check every 1 min (matches source timeframe)
1 Like