Can you check the case today for Bracket order for scalping strategy
Hi @Aijaz_Ahmad
Backtesting is an advanced concept that requires a deep understanding of codes. I plan to cover it gradually as we advance.
use tsl.get_historical_data()
Do not use tsl.get_intraday_data(), we will be removing this function in future.
Hi @Kalpeshh_Patel
Share the complete code you are using,
This portion is okey, error seems to be somewhere else
if (index_chart == None):
time.sleep(60)
continue
Hi Folks,
Episode 8 is out, and many of you might have already tuned in!
Just a heads-upâonly two more episodes remain in this series.
The incredible interest and enthusiasm weâve seen from all of you have been truly inspiring, and weâre excited to bring even more engaging content your way.
Weâd love to hear your suggestions: What would you like to learn next?
Let us know your thoughts!
Hi
@Tradehull_Imran
Here I am attaching my file
Please do correction
There are two errors
1 if (index_chart == None):
** time.sleep(60)**
** continue**
C:\Algo Practice\Session 8>py â2 candle theory Algo - Both Side Pseudocode V2.pyâ
File â2 candle theory Algo - Both Side Pseudocode V2.pyâ, line 37
if (index_chart == None):
^
SyntaxError: invalid syntax
2. - ltp not detected
C:\Algo Practice\Session 8>py âRSI Option V2.pyâ
Codebase Version 2.1
-----Logged into Dhan-----
reading existing file all_instrument 2024-11-25.csv
Got the instrument file
available_balance 275667.47
BUY 09:55:23.121570 True False False cc_1 09:45:00
SELL 09:55:23.121570 True True True cc_1 09:45:00
NIFTY NOV FUT Buy PUT
Traceback (most recent call last):
File âRSI Option V2.pyâ, line 146, in
price_has_moved_20_pct = ltp > (trade_info[âentry_priceâ])*1.2
NameError: name âltpâ is not defined
Hi @Scooby_Doo
If we keep keep the SL and Target very close, the order is rejected by Dhan Risk Management System.
Actually, this is a good feature from Risk perspective.
However , if you want to scalp, use Cover/LIMIT or Trigger orders.
see : BO.mp4 - Google Drive
-
we can use index_ltp instead of just ltp (as this variable was never defined)
-
We need to check if chart data is empty for both
index_chart_5 and index_chart -
Till now we have not studied how to manage multiple scripts in Algo. There is a framework to it that I will need to teach before it can be implementeed, so I have removed for loop from code.
-
Do not post your credentials while submitting code on forum
import pdb
import traceback
import time
import datetime
from Dhan_Tradehull_V2 import Tradehull
import pandas as pd
from pprint import pprint
import pandas_ta as pta
import talib
import pandas_ta as ta
import warnings
warnings.filterwarnings("ignore")
# ---------------for dhan login ----------------
client_code = "xxxxxxxxx"
token_id = "xxxxxxxx"
tsl = Tradehull(client_code,token_id)
available_balance = tsl.get_balance()
print("available_balance", available_balance)
index_name = 'BANKNIFTY NOV FUT'
traded = "no"
trade_info = {"options_name":None, "qty":None, "sl":None, "CE_PE":None, "entry_price":None , "Trailed":None}
while True:
current_time = datetime.datetime.now().time()
if current_time < datetime.time(9,20):
print("wait for market to start", current_time)
continue
if (current_time > datetime.time(23, 25)):
print("Market is over, Bye Bye see you tomorrow", current_time)
break
index_chart = tsl.get_historical_data(tradingsymbol=index_name, exchange='NFO', timeframe="15")
time.sleep(3)
index_chart_5 = tsl.get_historical_data(tradingsymbol = index_name,exchange = 'NFO',timeframe="5")
index_ltp = tsl.get_ltp_data(names = ['NIFTY NOV FUT','BANKNIFTY NOV FUT'])
if index_chart.empty or index_chart_5.empty:
time.sleep(60)
continue
index_chart['rsi'] = talib.RSI(index_chart['close'], timeperiod=14) #pandas
supertrend = ta.supertrend(index_chart_5['high'], index_chart_5['low'], index_chart_5['close'], 10, 3)
index_chart_5 = pd.concat([index_chart_5, supertrend], axis=1, join='inner')
pdb.set_trace()
# time.sleep(60)
# continue
# print(index_chart,'scanning',)
cc_1 = index_chart.iloc[-1] #pandas completed candle of 15 min timeframe
cc_2 = index_chart.iloc[-2]
cc_3 = index_chart.iloc[-3]
cc5_2 = index_chart_5.iloc[-2]
# ---------------------------- BUY ENTRY CONDITIONS ----------------------------
up1 = cc_2['rsi'] > 60
up2 = cc_3['rsi'] < 60
up3 = cc_2['high'] < cc_1['close']
print(f"BUY\t {current_time} \t {up1} \t {up2} \t {up3} \t cc_1 {str(cc_1['timestamp'].time())}")
# pdb.set_trace()
# ---------------------------- SELL ENTRY CONDITIONS ----------------------------
dt1 = cc_2['rsi'] < 90 # This is for tral basis
dt2 = cc_3['rsi'] > 90 # This is for tral basis
dt3 = cc_2['low'] > cc_1['close']
print(f"SELL\t {current_time} \t {dt1} \t {dt2} \t {dt3} \t cc_1 {str(cc_1['timestamp'].time())} \n")
if up1 and up2 and up3:
print(index_name, "Buy CALL")
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry ='28-11-2024')
lot_size = tsl.get_lot_size(ce_name)*1
entry_orderid = tsl.order_placement(ce_name,'NFO', lot_size, 0, 0, 'MARKET', 'BUY', 'MIS')
traded = "yes"
trade_info['options_name'] = ce_name
trade_info['qty'] = lot_size
trade_info['sl'] = cc_2['low']
trade_info['CE_PE'] = "CE"
time.sleep(1)
trade_info['entry_price'] = tsl.get_executed_price(orderid=trade_info['entry_orderid'])
if dt1 and dt2 and dt3:
print(index_name, "Buy PUT")
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry ='28-11-2024')
lot_size = tsl.get_lot_size(pe_name)*1
entry_orderid = tsl.order_placement(pe_name,'NFO', lot_size, 0, 0, 'MARKET', 'BUY', 'MIS')
traded = "yes"
trade_info['options_name'] = pe_name
trade_info['qty'] = lot_size
trade_info['sl'] = cc_2['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:
price_has_moved_20_pct = index_ltp > (trade_info['entry_price'])*1.2
position_has_not_been_trailed = trade_info['Trailed'] is None
if price_has_moved_20_pct and position_has_not_been_trailed:
trade_info['sl'] = trade_info['entry_price']
trade_info['Trailed'] = "yes_I_have_trailed"
sl_hit = index_ltp < trade_info['sl']
tg_hit = index_ltp < cc5_2['SUPERT_10_3.0']
if sl_hit or tg_hit:
print("Order Exited", trade_info)
exit_orderid = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
# pdb.set_trace()
if short_position:
price_has_moved_20_pct = index_ltp > (trade_info['entry_price'])*1.2
position_has_not_been_trailed = trade_info['Trailed'] is None
if price_has_moved_20_pct and position_has_not_been_trailed:
trade_info['sl'] = trade_info['entry_price']
trade_info['Trailed'] = "yes_I_have_trailed"
sl_hit = index_ltp < trade_info['sl']
tg_hit = index_ltp < cc5_2['SUPERT_10_3.0']
if sl_hit or tg_hit:
print("Order Exited", trade_info)
exit_orderid = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
# pdb.set_trace()
Hi @Tradehull_Imran,
Even I am facing the same issue, If I use BO then I get same error and If I use CO it only allows you to place SL only and not target.
I understand
So
Can you please delete this file
If Possible
Hello @Tradehull_Imran
How do i Get order id of the last trade executed?
Hi everyone, @Francis_Antony
If you are new to the series and watching it from start, do follow this sequence.
This will help you to upgrade to all the latest changes.
Code Name | Video Link | Activity to complete |
---|---|---|
1. Session1 - Introduction | https://www.youtube.com/watch?v=4QmdVg2pPGs | No action |
2. Session2 - Installation | https://www.youtube.com/watch?v=YAyIoDJYorA | complete the installtion |
3. Session3 - Codebase | https://www.youtube.com/watch?v=DPWU5wLuYcQ | No action, just study the video |
Dhan_Tradehull_V2 and Upgraded LTP | https://www.youtube.com/watch?v=HLiEpNZSD80 | Practice code given, |
4. Session4- Python Part 1 | https://www.youtube.com/watch?v=9KicRvYFLlU | all concepts practice |
5. Session5- Python Part 2 | https://www.youtube.com/watch?v=_UKH2mml_Dg | practice the code for Ltp Based Strike Selection using Dhan_Tradehull_V2 |
6. Session6- 1st Live Algo | https://www.youtube.com/watch?v=bF6IjRumWZE | Practice the algo |
7. Session7- Python Basics Part 3 | https://www.youtube.com/watch?v=cZ3twt8ZScc | Practice code |
8. Session8- 2nd Live Algo | https://www.youtube.com/watch?v=fnmHrmWgBh0 | Implement multitimeframe algo |
9. Session9- 3rd Live Algo | https://www.youtube.com/watch?v=5qw8GLFxu98 | Implement breakout algo |
Hi @Aijaz_Ahmad
You can use Cover Orders and Targets can be placed in memory,
this way target is not sent to Dhan, its stored only in python memory, when the ltp crosses the target level, we will send the exit order.
Like this,
if traded == "yes":
long_position = trade_info['CE_PE'] == "CE"
short_position = trade_info['CE_PE'] == "PE"
if long_position:
sl_hit = tsl.get_order_status(orderid=trade_info['entry_orderid'])
tg_hit = index_ltp > trade_info['tg']
if sl_hit
print("Target Hit", trade_info)
pdb.set_trace()
if tg_hit:
print("Target Hit", trade_info)
exit_orderid = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
pdb.set_trace()
Hi @Scooby_Doo
use below code
orderbook = tsl.get_orderbook()
orderbook = pd.DataFrame(orderbook)
last_trade = orderbook.iloc[0]
last_trade_orderid = last_trade['orderId']
Hi @Tradehull_Imran,
But if we use CO then wonât we need to continously check LTP if the target is reached, also can u please share CO code?
Also, wonât it create rate limt issue if we continously check LTP.
I am unable to get ce_name, pe_name
Message: 'Got exception in ce_pe_option_df â
Arguments: (KeyError(âNIFTYâ),)
exception got in ce_pe_option_df âNIFTYâ
Traceback (most recent call last):
File â/home/mohan/Dhan-Alog-Trading/examples/Dhan_Tradehull_V2.pyâ, line 513, 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 â/home/mohan/Dhan-Alog-Trading/examples/mydemo.pyâ, line 19, in
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying =âNIFTYâ,Expiry =â28-11-2024â)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File â/home/mohan/Dhan-Alog-Trading/examples/Dhan_Tradehull_V2.pyâ, line 568, in ATM_Strike_Selection
return None, None, strike
^^^^^^
UnboundLocalError: cannot access local variable âstrikeâ where it is not associated with a value
Hello @Tradehull_Imran,
It seems stock option strike step has been changed by NSE. we can use the below updated list in v2.
{âNIFTYâ: 50, âBANKNIFTYâ: 100,âFINNIFTYâ: 50, âAARTIINDâ: 10,âABBâ: 100,âABBOTINDIAâ: 250, âABCAPITALâ: 2.5, âABFRLâ: 5, âACCâ: 20, âADANIENTâ: 20, âADANIPORTSâ: 20, âALKEMâ: 100, âAMBUJACEMâ: 10, âAPOLLOHOSPâ: 50, âAPOLLOTYREâ: 10, âASHOKLEYâ: 2.5, âASIANPAINTâ: 20, âASTRALâ: 20, âATULâ: 100, âAUBANKâ: 10, âAUROPHARMAâ: 20, âAXISBANKâ: 10, âBAJAJ-AUTOâ: 100, âBAJAJFINSVâ: 20, âBAJFINANCEâ: 100, âBALKRISINDâ: 50, âBANDHANBNKâ: 2.5, âBANKBARODAâ: 2.5, âBATAINDIAâ: 10, âBELâ: 5, âBERGEPAINTâ: 5, âBHARATFORGâ: 20, âBHARTIARTLâ: 20, âBHELâ: 5, âBIOCONâ: 5, âBOSCHLTDâ: 500, âBPCLâ: 5, âBRITANNIAâ: 50, âBSOFTâ: 10, âCANBKâ: 1, âCANFINHOMEâ: 10, âCHAMBLFERTâ: 10, âCHOLAFINâ: 20, âCIPLAâ: 20, âCOALINDIAâ: 5, âCOFORGEâ: 100, âCOLPALâ: 50, âCONCORâ: 10, âCOROMANDELâ: 20, âCROMPTONâ: 5, âCUBâ: 2.5, âCUMMINSINDâ: 50, âDABURâ: 5, âDALBHARATâ: 20, âDEEPAKNTRâ: 50, âDIVISLABâ: 50, âDIXONâ: 250, âDLFâ: 10, âDRREDDYâ: 10, âEICHERMOTâ: 50, âESCORTSâ: 50, âEXIDEINDâ: 10, âFEDERALBNKâ: 2.5, âGAILâ: 2.5, âGLENMARKâ: 20, âGMRINFRAâ: 1, âGNFCâ: 10, âGODREJCPâ: 20, âGODREJPROPâ: 50, âGRANULESâ: 10, âGRASIMâ: 20, âGUJGASLTDâ: 10, âHALâ: 100, âHAVELLSâ: 20, âHCLTECHâ: 20, âHDFCAMCâ: 50, âHDFCBANKâ: 10, âHDFCLIFEâ: 10, âHEROMOTOCOâ: 100, âHINDALCOâ: 10, âHINDCOPPERâ: 5, âHINDPETROâ: 5, âHINDUNILVRâ: 20, âICICIBANKâ: 10, âICICIGIâ: 20, âICICIPRULIâ: 10, âIDEAâ: 1, âIDFCFIRSTBâ: 1, âIEXâ: 2.5, âIGLâ: 10, âINDHOTELâ: 10, âINDIAMARTâ: 50, âINDIGOâ: 50, âINDUSINDBKâ: 20, âINDUSTOWERâ: 10, âINFYâ: 20, âIOCâ: 2.5, âIPCALABâ: 20, âIRCTCâ: 10, âITCâ: 5, âJINDALSTELâ: 10, âJKCEMENTâ: 50, âJSWSTEELâ: 10, âJUBLFOODâ: 10, âKOTAKBANKâ: 20, âLALPATHLABâ: 50, âLAURUSLABSâ: 10, âLICHSGFINâ: 10, âLTâ: 50, âLTFâ: 2.5, âLTIMâ: 50, âLTTSâ: 50, âLUPINâ: 20, âM&Mâ: 50, âM&MFINâ: 5, âMANAPPURAMâ: 2.5, âMARICOâ: 10, âMARUTIâ: 100, âMCXâ: 100, âMETROPOLISâ: 20, âMFSLâ: 20, âMGLâ: 20, âMOTHERSONâ: 2.5, âMPHASISâ: 50, âMRFâ: 500, âMUTHOOTFINâ: 20, âNATIONALUMâ: 2.5, âNAUKRIâ: 100, âNAVINFLUORâ: 50, âNESTLEINDâ: 20, âNMDCâ: 5, âNTPCâ: 5, âOBEROIRLTYâ: 20, âOFSSâ: 250, âONGCâ: 5, âPAGEINDâ: 500, âPELâ: 20, âPERSISTENTâ: 100, âPETRONETâ: 5, âPFCâ: 10, âPIDILITINDâ: 20, âPIINDâ: 50, âPNBâ: 1, âPOLYCABâ: 100, âPOWERGRIDâ: 5, âPVRINOXâ: 20, âRAMCOCEMâ: 10, âRBLBANKâ: 2.5, âRECLTDâ: 10, âRELIANCEâ: 10, âSAILâ: 2.5, âSBICARDâ: 5, âSBILIFEâ: 20, âSBINâ: 10, âSHREECEMâ: 250, âSHRIRAMFINâ: 50, âSIEMENSâ: 100, âSRFâ: 20, âSUNPHARMAâ: 20, âSUNTVâ: 10, âSYNGENEâ: 10, âTATACHEMâ: 20, âTATACOMMâ: 20, âTATACONSUMâ: 10, âTATAMOTORSâ: 10, âTATAPOWERâ: 5, âTATASTEELâ: 2.5, âTCSâ: 50, âTECHMâ: 20, âTITANâ: 50, âTORNTPHARMâ: 50, âTRENTâ: 100, âTVSMOTORâ: 50, âUBLâ: 20, âULTRACEMCOâ: 100, âUNITDSPRâ: 20, âUPLâ: 10, âVEDLâ: 10, âVOLTASâ: 20, âWIPROâ: 5, âZYDUSLIFEâ: 20}
Hi
@Tradehull_Imran
I deploy the algo
But
After trade execute it gives error
NIFTY NOV FUT Buy CALL
Traceback (most recent call last):
File âRSI Option V3.pyâ, line 101, in
trade_info[âentry_priceâ] = tsl.get_executed_price(orderid=trade_info[âentry_orderidâ])
KeyError: âentry_orderidâ
And while I start again algo
For same candle order once again execute
Please solve the error
Thank you
Hi @thakurmhn
this code seems to be working fine on my end
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry ='28-11-2024')
- Do share the complete files
- also send the output for pip show dhanhq