Sir , I have written a super trend algo by using super trend indicator code. while testing it is showing lot of errors, please check, sir…
# i am using mcx markets for testing the below code in live markets
# buy trade:
# my stragegy is in 5 mins chart supertrend should be in green and in 1 min chart adx should be above 20
import pdb
from Dhan_Tradehull_V2 import Tradehull
import pandas as pd
import pandas_ta as ta
import talib
import time
import datetime
client_code = "11"
token_id = "11"
tsl = Tradehull(client_code,token_id)
available_balance = tsl.get_balance()
leveraged_margin = available_balance*5
max_trades = 3
per_trade_margin = (leveraged_margin/max_trades)
max_loss = (available_balance*1)/100*-1
# watchlist = ['MOTHERSON', 'OFSS', 'BSOFT', 'CHAMBLFERT', 'DIXON', 'NATIONALUM', 'DLF', 'IDEA', 'ADANIPORTS', 'SAIL', 'HINDCOPPER', 'INDIGO', 'RECLTD', 'PNB', 'HINDALCO', 'RBLBANK', 'GNFC', 'ALKEM', 'CONCOR', 'PFC', 'GODREJPROP', 'MARUTI', 'ADANIENT', 'ONGC', 'CANBK', 'OBEROIRLTY', 'BANDHANBNK', 'SBIN', 'HINDPETRO', 'CANFINHOME', 'TATAMOTORS', 'LALPATHLAB', 'MCX', 'TATACHEM', 'BHARTIARTL', 'INDIAMART', 'LUPIN', 'INDUSTOWER', 'VEDL', 'SHRIRAMFIN', 'POLYCAB', 'WIPRO', 'UBL', 'SRF', 'BHARATFORG', 'GRASIM', 'IEX', 'BATAINDIA', 'AARTIIND', 'TATASTEEL', 'UPL', 'HDFCBANK', 'LTF', 'TVSMOTOR', 'GMRINFRA', 'IOC', 'ABCAPITAL', 'ACC', 'ZYDUSLIFE', 'GLENMARK', 'TATAPOWER', 'PEL', 'LAURUSLABS', 'BANKBARODA', 'KOTAKBANK', 'CUB', 'GAIL', 'DABUR', 'TECHM', 'CHOLAFIN', 'BEL', 'SYNGENE', 'FEDERALBNK', 'NAVINFLUOR', 'AXISBANK', 'LT', 'ICICIGI', 'EXIDEIND', 'TATACOMM', 'RELIANCE', 'ICICIPRULI', 'IPCALAB', 'AUBANK', 'INDIACEM', 'GRANULES', 'HDFCAMC', 'COFORGE', 'LICHSGFIN', 'BAJAJFINSV', 'INFY', 'BRITANNIA', 'M&MFIN', 'BAJFINANCE', 'PIIND', 'DEEPAKNTR', 'SHREECEM', 'INDUSINDBK', 'DRREDDY', 'TCS', 'BPCL', 'PETRONET', 'NAUKRI', 'JSWSTEEL', 'MUTHOOTFIN', 'CUMMINSIND', 'CROMPTON', 'M&M', 'GODREJCP', 'IGL', 'BAJAJ-AUTO', 'HEROMOTOCO', 'AMBUJACEM', 'BIOCON', 'ULTRACEMCO', 'VOLTAS', 'BALRAMCHIN', 'SUNPHARMA', 'ASIANPAINT', 'COALINDIA', 'SUNTV', 'EICHERMOT', 'ESCORTS', 'HAL', 'ASTRAL', 'NMDC', 'ICICIBANK', 'TORNTPHARM', 'JUBLFOOD', 'METROPOLIS', 'RAMCOCEM', 'INDHOTEL', 'HINDUNILVR', 'TRENT', 'TITAN', 'JKCEMENT', 'ASHOKLEY', 'SBICARD', 'BERGEPAINT', 'JINDALSTEL', 'MFSL', 'BHEL', 'NESTLEIND', 'HDFCLIFE', 'COROMANDEL', 'DIVISLAB', 'ITC', 'TATACONSUM', 'APOLLOTYRE', 'AUROPHARMA', 'HCLTECH', 'LTTS', 'BALKRISIND', 'DALBHARAT', 'APOLLOHOSP', 'ABBOTINDIA', 'ATUL', 'UNITDSPR', 'PVRINOX', 'SIEMENS', 'SBILIFE', 'IRCTC', 'GUJGASLTD', 'BOSCHLTD', 'NTPC', 'POWERGRID', 'MARICO', 'HAVELLS', 'MPHASIS', 'COLPAL', 'CIPLA', 'MGL', 'ABB', 'PIDILITIND', 'MRF', 'LTIM', 'PAGEIND', 'PERSISTENT']
watchlist = ['CRUDEOIL NOV FUT', 'NATURALGAS NOV FUT', 'SILVERM NOV FUT']
traded_wathclist = []
while True:
live_pnl = tsl.get_live_pnl()
current_time = datetime.datetime.now().time()
if current_time < datetime.time(9, 30):
print("wait for market to start", current_time)
continue
if (current_time > datetime.time(23, 00)) or (live_pnl < max_loss):
# I_want_to_trade_no_more = tsl.kill_switch('ON')
order_details = tsl.cancel_all_orders()
print("Market is over, Bye Bye see you tomorrow", current_time)
break
for stock_name in watchlist:
time.sleep(0.2)
print(stock_name)
# Conditions that are on 1 minute timeframe
# chart_1 = tsl.get_intraday_data(stock_name, 'MCX', 1) # 1 minute chart # this call has been updated to get_historical_data call,
chart_1 = tsl.get_historical_data(tradingsymbol = stock_name,exchange = 'MCX',timeframe="1")
chart_1['adx'] = talib.ADX(chart_1['high'], chart_1['low'], chart_1['close'], timeperiod=14)
cc_1 = chart_1.iloc[-1] #pandas completed candle of 1 min timeframe
uptrend = cc_1['adx'] >= 20.0
downtrend = cc_1['adx'] <= 19.9
# Conditions that are on 5 minute timeframe
chart_5 = tsl.get_intraday_data(stock_name, 'MCX', 5) # 5 minute chart
chart_5 = tsl.get_historical_data(tradingsymbol = stock_name,exchange = 'MCX',timeframe="5") # this call has been updated to get_historical_data call,
chart_5 ['supertrend'] = ta.supertrend(chart_5['high'], chart_5['low'], chart_5['close'],7,3)
chart_5 = pd.concat([chart_5, supertrend], axis=1, join='inner')
cc_5 = chart_1.iloc[-1]
ub_breakout = cc_5['close'] > cc_5['supertrend']
lb_breakout = cc_5['close'] < cc_5['supertrend']
no_repeat_order = stock_name not in traded_wathclist
max_order_limit = len(traded_wathclist) <= max_trades
if uptrend and ub_breakout and no_repeat_order and max_order_limit:
print(stock_name, "is in uptrend, Buy this script")
sl_price = round((cc_1['close']*0.98),1)
tp_price = round((cc_1['close'] * 1.003), 1) # Take Profit price (0.3% above buy price)
qty = int(per_trade_margin/cc_1['close'])
buy_entry_orderid = tsl.order_placement(stock_name,'MCX', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
sl_orderid = tsl.order_placement(stock_name,'MCX', 1, 0, sl_price, 'STOPMARKET', 'SELL', 'MIS')
tp_orderid = tsl.order_placement(stock_name,'MCX', 1, 0, tp_price, 'LIMIT', 'SELL', 'MIS') # Take Profit Limit Order
traded_wathclist.append(stock_name)
if downtrend and lb_breakout and no_repeat_order and max_order_limit:
print(stock_name, "is in downtrend, Sell this script")
sl_price = round((cc_1['close']*1.02),1)
qty = int(per_trade_margin/cc_1['close'])
buy_entry_orderid = tsl.order_placement(stock_name,'MCX', 1, 0, 0, 'MARKET', 'SELL', 'MIS')
sl_orderid = tsl.order_placement(stock_name,'MCX', 1, 0, sl_price, 'STOPMARKET', 'BUY', 'MIS')
traded_wathclist.append(stock_name)
Error Messages
reading existing file all_instrument 2024-11-11.csv
Got the instrument file
Error at Gettting balance as 'availabelBalance'
Traceback (most recent call last):
File "C:\Users\Admin\Documents\Dhan\8. Session8- 2nd Live Algo\8. Session8- 2nd Live Algo\2nd live Algo\Dhan_Tradehull_V2.py", line 168, in get_live_pnl
pos_book = pd.DataFrame(pos_book_dict)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 709, in __init__
mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 481, in dict_to_mgr
return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 115, in arrays_to_mgr
index = _extract_index(arrays)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 645, in _extract_index
raise ValueError("If using all scalar values, you must pass an index")
ValueError: If using all scalar values, you must pass an index
CRUDEOIL NOV FUT
If using all scalar values, you must pass an index
Traceback (most recent call last):
File "C:\Users\Admin\Documents\Dhan\8. Session8- 2nd Live Algo\8. Session8- 2nd Live Algo\2nd live Algo\Dhan_Tradehull_V2.py", line 259, in get_historical_data
df = pd.DataFrame(ohlc['data'])
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 709, in __init__
mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 481, in dict_to_mgr
return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 115, in arrays_to_mgr
index = _extract_index(arrays)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 645, in _extract_index
raise ValueError("If using all scalar values, you must pass an index")
ValueError: If using all scalar values, you must pass an index
Traceback (most recent call last):
File "Super trend.py", line 62, in <module>
chart_1['adx'] = talib.ADX(chart_1['high'], chart_1['low'], chart_1['close'], timeperiod=14)
TypeError: 'NoneType' object is not subscriptable