Hi @Tradehull_Imran,
I’m trying to implement the 2 candle strategy as per your video. However, I keep running into couple of errors. This is inspite of adhering to the rate limit and calling get_ltp with a gap of 60 secs between two calls.
Error list…
- Exception at calling ltp as {‘status’: ‘failure’, ‘remarks’: {‘error_code’: None, ‘error_type’: None, ‘error_message’: None}, ‘data’: {‘data’: {‘805’: ‘Too many requests. Further requests may result in the user being blocked.’}, ‘status’: ‘failed’}}
exception got in ce_pe_option_df ‘NIFTY’
- UnboundLocalError: cannot access local variable ‘strike’ where it is not associated with a value
- Message: 'Got exception in ce_pe_option_df ’
Arguments: (KeyError(‘NIFTY’),)
I’m pasting the detailed code file and error file below. Kindly help
Code file
import pdb
import time
import datetime
import traceback
import pandas as pd
from pprint import pprint
import talib
# import pandas_ta as pta
import pandas_ta as ta
import warnings,sys
warnings.filterwarnings("ignore")
path = r'C:/Users/krish/OneDrive/Documents/Krishna/2 Areas/Finance/Stock Market/Trading/Trading Courses/Development/AlgoTrading/Dhan/DhanAlgoTrading'
# path = r'../DhanAlgoTrading'
sys.path.insert(0, path)
import credentials as cred
from Dhan_Tradehull_V2 import Tradehull
# ---------------Basic setup for dhan login & Defining Constants------------------------
client_code = cred.CLIENT_ID
token_id = cred.ACCESS_TOKEN
tsl = Tradehull(client_code,token_id)
available_balance = tsl.get_balance()
# leveraged_margin = available_balance*5
max_trades = 5
per_trade_margin = (available_balance/max_trades/5)
max_loss = -available_balance/100
EXPIRY ='26-12-2024'
LOT_SIZE = 3
VOLUME = 10000
# ---------------------------------------------------------------------------------------
traded = "no"
trade_info = {"options_name":None, "qty":None, "sl":None, "CE_PE":None}
while True:
live_pnl = tsl.get_live_pnl()
current_time = datetime.datetime.now().time()
month = datetime.datetime.now().strftime("%b").upper()
if current_time < datetime.time(9, 30):
print("Market yet to open", current_time)
continue
if (current_time > datetime.time(15, 15)) or (live_pnl < max_loss):
order_details = tsl.cancel_all_orders()
print("Market is closed!", current_time)
break
index_chart = tsl.get_historical_data(tradingsymbol=f'NIFTY {month} FUT', exchange='NFO', timeframe="1")
time.sleep(60)
index_ltp = tsl.get_ltp_data(names = [f'NIFTY {month} FUT'])[f'NIFTY {month} FUT']
# if (index_chart.empty):
# time.sleep(60)
# continue
if index_chart is None or index_ltp is None:
print("Data unavailable. Retrying...")
time.sleep(60)
continue
# rsi ------------------------ apply indicators
index_chart['rsi'] = talib.RSI(index_chart['close'], timeperiod=14)
# vwap
index_chart.set_index(pd.DatetimeIndex(index_chart['timestamp']), inplace=True)
index_chart['vwap'] = ta.vwap(index_chart['high'] , index_chart['low'], index_chart['close'] , index_chart['volume'])
# Supertrend
supertrend = ta.supertrend(index_chart['high'], index_chart['low'], index_chart['close'], 10, 2)
index_chart = pd.concat([index_chart, supertrend], axis=1, join='inner')
# vwma
index_chart['pv'] = index_chart['close'] * index_chart['volume']
index_chart['vwma'] = index_chart['pv'].rolling(20).mean() / index_chart['volume'].rolling(20).mean()
first_candle = index_chart.iloc[-3]
second_candle = index_chart.iloc[-2]
running_candle = index_chart.iloc[-1]
# print(index_chart)
# ---------------------------- BUY ENTRY CONDITIONS ----------------------------
buy_condition_1 = first_candle['close'] > first_candle['vwap'] # First Candle close is above VWAP
buy_condition_2 = first_candle['close'] > first_candle['SUPERT_10_2.0'] # First Candle close is above Supertrend
buy_condition_3 = first_candle['close'] > first_candle['vwma'] # First Candle close is above VWMA
buy_condition_4 = first_candle['rsi'] < 80 # First candle RSI < 80
buy_condition_5 = second_candle['volume'] > VOLUME # Second candle Volume should be greater than 50,000 for Nifty and above 125,000 for Bank Nifty
buy_condition_6 = traded == "no"
buy_condition_7 = index_ltp > first_candle['low']
print(f"BUY \t {current_time} \t {buy_condition_1} \t {buy_condition_2} \t {buy_condition_3} \t {buy_condition_4} \t {buy_condition_5} \t {buy_condition_6} \t {buy_condition_7} \t first_candle {str(first_candle['timestamp'].time())}")
# ---------------------------- SELL ENTRY CONDITIONS ----------------------------
sell_condition_1 = first_candle['close'] < first_candle['vwap'] # First Candle close is below VWAP
sell_condition_2 = first_candle['close'] < first_candle['SUPERT_10_2.0'] # First Candle close is below Supertrend
sell_condition_3 = first_candle['close'] < first_candle['vwma'] # First Candle close is below VWMA
sell_condition_4 = first_candle['rsi'] > 20 # First candle RSI < 80
sell_condition_5 = second_candle['volume'] > VOLUME # Second candle Volume should be greater than 50,000 for Nifty and above 125,000 for Bank Nifty
sell_condition_6 = traded == "no"
sell_condition_7 = index_ltp < first_candle['high']
print(f"SELL \t {current_time} \t {sell_condition_1} \t {sell_condition_2} \t {sell_condition_3} \t {sell_condition_4} \t {sell_condition_5} \t {buy_condition_7} \t {sell_condition_7} \t first_candle {str(first_candle['timestamp'].time())} \n")
# pdb.set_trace()
if buy_condition_1 and buy_condition_2 and buy_condition_3 and buy_condition_4 and buy_condition_5 and buy_condition_6 and buy_condition_7:
print("Buy Signal Formed")
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry =EXPIRY)
total_lots = tsl.get_lot_size(ce_name)*LOT_SIZE
# call_buy_orderid = tsl.order_placement(ce_name,'NFO', total_lots, 0, 0, 'MARKET', 'BUY', 'MIS')
traded = "yes"
trade_info['options_name'] = ce_name
trade_info['qty'] = total_lots
trade_info['buy_price'] = tsl.get_ltp_data(names = [ce_name])[ce_name] #call_buy_orderid['price']
trade_info['create_time'] = datetime.datetime.now()
trade_info['sl'] = first_candle['low']
trade_info['CE_PE'] = "CE"
if sell_condition_1 and sell_condition_2 and sell_condition_3 and sell_condition_4 and sell_condition_5 and sell_condition_6 and sell_condition_7:
print("Sell Signal Formed")
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry =EXPIRY)
total_lots = tsl.get_lot_size(pe_name)*LOT_SIZE
# put_buy_orderid = tsl.order_placement(pe_name,'NFO', total_lots, 0, 0, 'MARKET', 'BUY', 'MIS')
traded = "yes"
trade_info['options_name'] = pe_name
trade_info['qty'] = total_lots
trade_info['buy_price'] = tsl.get_ltp_data(names = [pe_name])[pe_name]
trade_info['create_time'] = datetime.datetime.now()
trade_info['sl'] = first_candle['high']
trade_info['CE_PE'] = "PE"
# ---------------------------- check for exit SL/TG-----------------------------
if traded == "yes":
long_position = trade_info['CE_PE'] == "CE"
short_position = trade_info['CE_PE'] == "PE"
if long_position:
stop_loss_hit = index_ltp < trade_info['sl']
target_hit = index_ltp < running_candle['SUPERT_10_2.0']
if stop_loss_hit or target_hit:
# call_exit_orderid = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
if stop_loss_hit:
trade_info['exit_reason'] = 'SL Hit'
else:
trade_info['exit_reason'] = 'Target Hit'
trade_info['sell_price'] = tsl.get_ltp_data(names = [ce_name])[ce_name]
trade_info['exit_time'] = datetime.datetime.now()
print("Order Exited", trade_info)
traded = "no"
# pdb.set_trace()
if short_position:
stop_loss_hit = index_ltp > trade_info['sl']
target_hit = index_ltp > running_candle['SUPERT_10_2.0']
if stop_loss_hit or target_hit:
# put_exit_orderid = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
if stop_loss_hit:
trade_info['exit_reason'] = 'SL Hit'
else:
trade_info['exit_reason'] = 'Target Hit'
trade_info['sell_price'] = tsl.get_ltp_data(names = [pe_name])[pe_name]
trade_info['exit_time'] = datetime.datetime.now()
print("Order Exited", trade_info)
traded = "no"
# pdb.set_trace()
Error details
Sell Signal Formed
Exception at calling ltp as {'status': 'failure', 'remarks': {'error_code': None, 'error_type': None, 'error_message': None}, 'data': {'data': {'805': 'Too many requests. Further requests may result in the user being blocked.'}, 'status': 'failed'}}
exception got in ce_pe_option_df 'NIFTY'
Traceback (most recent call last):
File "C:\Users/krish/OneDrive/Documents/Krishna/2 Areas/Finance/Stock Market/Trading/Trading Courses/Development/AlgoTrading/Dhan/DhanAlgoTrading\Dhan_Tradehull_V2.py", line 489, in ATM_Strike_Selection
ltp = ltp_data[Underlying]
~~~~~~~~^^^^^^^^^^^^
KeyError: 'NIFTY'
--- Logging error ---
Traceback (most recent call last):
File "C:\Users/krish/OneDrive/Documents/Krishna/2 Areas/Finance/Stock Market/Trading/Trading Courses/Development/AlgoTrading/Dhan/DhanAlgoTrading\Dhan_Tradehull_V2.py", line 489, in ATM_Strike_Selection
ltp = ltp_data[Underlying]
~~~~~~~~^^^^^^^^^^^^
KeyError: 'NIFTY'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\krish\anaconda3\Lib\logging\__init__.py", line 1110, in emit
msg = self.format(record)
^^^^^^^^^^^^^^^^^^^
File "c:\Users\krish\anaconda3\Lib\logging\__init__.py", line 953, in format
return fmt.format(record)
^^^^^^^^^^^^^^^^^^
File "c:\Users\krish\anaconda3\Lib\logging\__init__.py", line 687, in format
record.message = record.getMessage()
^^^^^^^^^^^^^^^^^^^
File "c:\Users\krish\anaconda3\Lib\logging\__init__.py", line 377, in getMessage
msg = msg % self.args
~~~~^~~~~~~~~~~
TypeError: not all arguments converted during string formatting
Call stack:
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "c:\Users\krish\anaconda3\Lib\site-packages\ipykernel_launcher.py", line 17, in <module>
app.launch_new_instance()
File "c:\Users\krish\anaconda3\Lib\site-packages\traitlets\config\application.py", line 992, in launch_instance
app.start()
File "c:\Users\krish\anaconda3\Lib\site-packages\ipykernel\kernelapp.py", line 701, in start
self.io_loop.start()
File "c:\Users\krish\anaconda3\Lib\site-packages\tornado\platform\asyncio.py", line 195, in start
self.asyncio_loop.run_forever()
File "c:\Users\krish\anaconda3\Lib\asyncio\windows_events.py", line 321, in run_forever
super().run_forever()
File "c:\Users\krish\anaconda3\Lib\asyncio\base_events.py", line 607, in run_forever
self._run_once()
File "c:\Users\krish\anaconda3\Lib\asyncio\base_events.py", line 1922, in _run_once
handle._run()
File "c:\Users\krish\anaconda3\Lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "c:\Users\krish\anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 534, in dispatch_queue
await self.process_one()
File "c:\Users\krish\anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 523, in process_one
await dispatch(*args)
File "c:\Users\krish\anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 429, in dispatch_shell
await result
File "c:\Users\krish\anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 767, in execute_request
reply_content = await reply_content
File "c:\Users\krish\anaconda3\Lib\site-packages\ipykernel\ipkernel.py", line 429, in do_execute
res = shell.run_cell(
File "C:\Users\krish\AppData\Local\Temp\ipykernel_30488\1990712033.py", line 29, in wrapper
result = old_func(*args, **kwargs)
File "c:\Users\krish\anaconda3\Lib\site-packages\ipykernel\zmqshell.py", line 549, in run_cell
return super().run_cell(*args, **kwargs)
File "c:\Users\krish\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3051, in run_cell
result = self._run_cell(
File "c:\Users\krish\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3106, in _run_cell
result = runner(coro)
File "c:\Users\krish\anaconda3\Lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
coro.send(None)
File "c:\Users\krish\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3311, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "c:\Users\krish\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3493, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "c:\Users\krish\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3553, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-fd9addb455cf>", line 125, in <module>
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry =EXPIRY)
File "C:\Users/krish/OneDrive/Documents/Krishna/2 Areas/Finance/Stock Market/Trading/Trading Courses/Development/AlgoTrading/Dhan/DhanAlgoTrading\Dhan_Tradehull_V2.py", line 542, in ATM_Strike_Selection
self.logger.exception("Got exception in ce_pe_option_df ", e)
Message: 'Got exception in ce_pe_option_df '
Arguments: (KeyError('NIFTY'),)