I’m getting this error while trying to fetch a commodity data.
‘MCX:NATURALGAS’
Traceback (most recent call last):
File “D:\Python Projects\Trading Investing Learning\Imran Ali full Practice\Imran-Ali-mentorship-classes-.venv\Lib\site-packages\Zerodha_Tradehull\Zerodha_Tradehull.py”, line 205, in get_short_length_hist_data
instrument_token = self.kite.ltp(exchange+“:”+name)[exchange+“:”+name][‘instrument_token’]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: ‘MCX:NATURALGAS’
from Zerodha_Tradehull import Tradehull
from rich import print
import talib, time, pandas as pd, requests
import pandas_ta_classic as pta
api_key = ""
api_secret = ""
# Telegram
telegram_bot_token = ""
personal_telegram_id = ""
# group_telegram_id = ""
url = f"https://api.telegram.org/bot{telegram_bot_token}/sendMessage"
tsl = Tradehull(api_key, api_secret, "yes")
kite = tsl.kite
index_name = {
("NSE", "NIFTY 50"),
("BSE", "SENSEX"),
}
mcx_name = {
("MCX", "CRUDEOIL"),
("MCX", "NATURALGAS")
}
traded_list = []
timeframe = "5minute"
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
while True:
if time.strftime("%H:%M:%S", time.localtime()) >= "09:10:00" and time.strftime("%H:%M:%S", time.localtime()) <= "15:15:00":
for key, value in index_name:
exchange_id = ""
if key == "NSE":
exchange_id = "NFO"
elif key == "BSE":
exchange_id = "BFO"
elif key == "MCX":
exchange_id = "MCX"
multiplier_id = 16 if value == "SENSEX" else 10
expiry_id = 0 if value == "SENSEX" else 1
quantity_id = 20 if value == "SENSEX" else 75
chart = tsl.get_short_length_hist_data(name=value, exchange=key, interval=timeframe)
pd_chart = pd.DataFrame(chart, index=chart['date'])
chart_ltp = round(chart['close'].iloc[-2], 2)
chart_st = pta.supertrend(chart['high'], chart['low'], chart['close'], length=50, multiplier=4) # SuperTrend
SuperTrend = chart_st.iloc[-1, 1] # 1 for long and -1 for short
SuperTrend_ltp = chart_st.iloc[-2, 0] # ltp
if SuperTrend == 1 and value not in traded_list:
pe_atm_option = tsl.get_atm(ltp=SuperTrend_ltp, underlying=value, expiry=expiry_id, script_type="PE")
pe_hedge_option = tsl.get_otm(ltp=SuperTrend_ltp, underlying=value, expiry=expiry_id, script_type="PE", multiplier=multiplier_id)
ltp_pe_atm_option = tsl.get_data_for_single_script(exchange=exchange_id, name=pe_atm_option, call_type="ltp")
ltp_pe_hedge_option = tsl.get_data_for_single_script(exchange=exchange_id, name=pe_hedge_option, call_type="ltp")
send_hedge_order = tsl.place_order(variety="regular", exchange=exchange_id, tradingsymbol=pe_hedge_option, transaction_type="BUY", quantity=quantity_id, product="NRML", order_type="LIMIT", price=ltp_pe_hedge_option)
send_order = tsl.place_order(variety="regular", exchange=exchange_id, tradingsymbol=pe_atm_option, transaction_type="SELL", quantity=quantity_id, product="NRML", order_type="LIMIT", price=ltp_pe_atm_option)
traded_list.append(value)
message=f"Sell {pe_atm_option} at {ltp_pe_atm_option}"
params = {
"chat_id": personal_telegram_id,
"text": message
}
requests.get(url, params=params)
elif SuperTrend == -1 and value not in traded_list:
ce_atm_option = tsl.get_atm(ltp=SuperTrend_ltp, underlying=value, expiry=expiry_id, script_type="CE")
ce_hedge_option = tsl.get_otm(ltp=SuperTrend_ltp, underlying=value, expiry=expiry_id, script_type="CE", multiplier=multiplier_id)
ltp_ce_atm_option = tsl.get_data_for_single_script(exchange=exchange_id, name=ce_atm_option, call_type="ltp")
ltp_ce_hedge_option = tsl.get_data_for_single_script(exchange=exchange_id, name=ce_hedge_option, call_type="ltp")
send_hedge_order = tsl.place_order(variety="regular", exchange=exchange_id, tradingsymbol=ce_hedge_option, transaction_type="BUY", quantity=quantity_id, product="NRML", order_type="LIMIT", price=ltp_ce_hedge_option)
send_order = tsl.place_order(variety="regular", exchange=exchange_id, tradingsymbol=ce_atm_option, transaction_type="SELL", quantity=quantity_id, product="NRML", order_type="LIMIT", price=ltp_ce_atm_option)
traded_list.append(value)
message=f"Sell {ce_atm_option} at {ltp_ce_atm_option}"
params = {
"chat_id": personal_telegram_id,
"text": message
}
requests.get(url, params=params)
elif time.strftime("%H:%M:%S", time.localtime()) >= "15:15:00":
tsl.market_over_close_all_order()
if time.strftime("%H:%M:%S", time.localtime()) >= "09:00:00" and time.strftime("%H:%M:%S", time.localtime()) <= "23:25:00":
for key, value in mcx_name:
chart = tsl.get_short_length_hist_data(name=value , exchange=key , interval="5minute")
