Hi @Himansshu_Joshi ,
The flow and code seems to be right.
Hi @Himansshu_Joshi ,
The flow and code seems to be right.
Hi @Siddhesh_Amrute ,
We will need to consider the Day timeframe data and resample it for weekly or monthly.
Hi @Sameer_Gore ,
The error KeyError: 'orderId' occurs because the key 'orderId' does not exist in the dictionary order["data"]
Check if it is existing with w=different naming convention using print(order["data"])
try:
order_id = order["data"]["orderId"]
print(f"Order ID: {order_id}")
except KeyError:
print("KeyError: 'orderId' not found in response.")
Hi @anandc ,
Do send your code, we will review the same.
Ideally yes the data should not mismatch we will run it on our end also to see if its in order.
also let me know if you are using Dhan-Tradehull 3.0.6.
The exchange mapping is handled in several places in Dhan_TradeHull_V2. For example, when fetching historical data the code does this:
"NSE": self.Dhan.NSE"NFO": self.Dhan.FNO"BFO": "BSE_FNO""CUR": self.Dhan.CUR"BSE": self.Dhan.BSE"MCX": self.Dhan.MCX"INDEX": self.Dhan.INDEX'NSE': "NSE"'BSE': "BSE"'NFO': "NSE"'BFO': "BSE"'MCX': "MCX"'CUR': "NSE""NIFTY": "NSE""BANKNIFTY": "NSE""FINNIFTY": "NSE""MIDCPNIFTY": "NSE""BANKEX": "BSE""SENSEX": "BSE"Can somebody explain the need for this. What do i have to select when placing order for Nifty and for Sensex in the following
try:
order_response = None
for attempt in range(3):
order_response = tsl.order_placement(
tradingsymbol=selected_strike,
*exchange='FOR NIFTY??FOR SENSEX??',*
quantity=lots,
price=0,
trigger_price=0,
order_type="MARKET",
transaction_type="BUY",
trade_type="MIS"
)
Hi @m0rph1s ,
For NIFTY - you can specify exchange= ‘NFO’, and
for Sensex - exchange = ‘BFO’
@Tradehull_Imran @Hardik please explain this error why am i not getting my P AND L
ERROR:root:got exception in pnl as Failed to get pnl data
Traceback (most recent call last):
File “/usr/local/lib/python3.11/dist-packages/Dhan_Tradehull/Dhan_Tradehull.py”, line 342, in get_live_pnl
raise Exception(“Failed to get pnl data”)
Exception: Failed to get pnl data
got exception in pnl as Failed to get pnl data
Failed to get PnL data: 0
<Dhan_Tradehull.Dhan_Tradehull.Tradehull object at 0x788aabbc6ed0> PnL: None
Hi @rahulcse56
yes the observation is seems to be correct,
if the per second rate limit is 10 then for per min it can be max 1000…
maybe… “we can call more than 10 calls per second for short bursts as long as we don’t exceed 1000 calls within a minute.”
Tagging @Hardik @Dhan for same , to share more clarity on this.
Hi @Akshay_Bawane @Siddhesh_Amrute
To achieve this you can try workaround as add extra column in your excel or separate tab for managing Fundamental details and then fetch this information in your logic.
You can update this information manually on periodic basis like monthly or quarterly as it will not change frequently.
This approach is correct and this can be used till fundamental data feature is added in api.
Raising this as a feature request for dhanhq , Tagging @Hardik @Dhan for the same.
also for getting other fundamental data we can use Alpha vantage : https://www.alphavantage.co/
Hi @Dev_Goswami
Pnl function is working correctly on our end, can you share more details as well so that we can replicate the issue on our end as well.
Dear @Tradehull_Imran sir Required the above same ‘Fair value gap’ strategy in Algo code
pls also help me in making full code
Dear @Tradehull_Imran sir Facing issue in Fetching historical data and intraday data as well. i also dropped a mail at ur given mail id. Pls Help
previous_hist_data = tsl.get_historical_data(‘ACC’,‘NSE’,3)
intraday_hist_data = tsl.get_intraday_data(‘ACC’,‘NSE’,1)
below is error:
File “F:\Python Project\AlgoExecution\Dhan_Tradehull.py”, line 222, in get_historical_data
df = pd.DataFrame(ohlc[‘data’])
File “C:\Users\Sony Vaio\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py”, line 817, in init
raise ValueError(“DataFrame constructor not properly called!”)
ValueError: DataFrame constructor not properly called!
intraday_minute_data() missing 2 required positional arguments: ‘from_date’ and ‘to_date’
Traceback (most recent call last):
File “F:\Python Project\AlgoExecution\Dhan_Tradehull.py”, line 253, in get_intraday_data
ohlc = self.Dhan.intraday_minute_data(str(security_id),exchangeSegment,instrument_type)
TypeError: intraday_minute_data() missing 2 required positional arguments: ‘from_date’ and ‘to_date’
[0] > ←[33;01mf:\python project\algoexecution\dhan_codebase usage.py←[00m(←[36;01m32←[00m)()
→ ce_name, pe_name, strike = tsl.ATM_Strike_Selection(‘NIFTY’,‘05-09-2024’)
Hi @Tradehull_Imran Sir,
Already sent to help@dhan.co. Btw, this is it,
if stkname == "NIFTY" or stkname == "SENSEX":
exchange = "INDEX"
if stkname == "NIFTY":
qty = 75
elif stkname == "SENSEX":
qty = 20
TRADE_TIMEFRAME = "1"
while True:
data = first_client.get_historical_data(stkname, exchange, TRADE_TIMEFRAME)
data['timestamp'] = pd.to_datetime(data['timestamp'])
today = datetime.now().date()
trad_data = data[data["timestamp"].dt.date == today]
print(trad_data)
..
logic come here, so not shared
...
**IN NO WAY IM ABLE TO PLACE ORDERS, KINDLY HELP @Tradehull_Imran **
async def execute_trade(symbol, signal_type):
log(f"Executing trade for {symbol} as {signal_type}...", "debug")
if active_trade_flags.get(symbol, False):
log(f"Trade already active for {symbol}.", "error")
return None
# Fetch the validated option symbol using ITM_Strike_Selection.
res = await retry_api_call(tsl.ITM_Strike_Selection, Underlying=symbol, Expiry_Index=0, ITM_count=1)
log(f"ITM_Strike_Selection response for {symbol}: {res}", "debug")
ce_strike, pe_strike, _, _ = res if res is not None else (None, None, None, None)
selected_strike = ce_strike if signal_type.upper() == "CALL" else pe_strike
if not selected_strike:
log(f"Invalid ITM strike for {symbol}.", "error")
return None
# Use the validated symbol exactly as returned.
ltp = (await retry_api_call(tsl.get_ltp_data, [selected_strike])).get(selected_strike, 0)
if ltp <= 0:
log(f"Invalid LTP for {selected_strike}: {ltp:.2f}.", "error")
return None
capital = tsl.get_balance()
lot_size = LOT_SIZES.get(symbol, 1)
lots = max(1, int(capital // (ltp * lot_size)))
log(f"Placing order for {symbol}: Strike: {selected_strike}, LTP: ₹{ltp:.2f}, Lots: {lots}", "info")
try:
order_exchange = ORDER_EXCHANGE.get(symbol, "NFO")
# Attempt order placement and log the complete API response for debugging.
order_response = None
for attempt in range(3):
order_response = await throttled_run_in_executor(
lambda: tsl.order_placement(
tradingsymbol=selected_strike,
exchange=order_exchange,
quantity=lots,
price=0, # For market orders, price is set to 0.
trigger_price=0,
order_type="MARKET",
transaction_type="BUY",
trade_type="MIS"
)
)
log(f"Order placement attempt {attempt+1} response for {selected_strike}: {order_response}", "debug")
if order_response:
break
await asyncio.sleep(1)
if not order_response:
log("Order failed: No response from API.", "error")
return None
if order_response.get("status") == "failure":
# Log the entire response to understand what error details are available.
log(f"Order placement failed. Full API response: {order_response}", "error")
raise Exception(f"Order failed: {order_response.get('message', 'Unknown error')}")
log(f"Trade executed for {symbol}. Order ID: {order_response.get('order_id', 'UNKNOWN')}", "info")
active_trade_flags[symbol] = True
return {
"order_response": order_response,
"order_id": order_response.get("order_id", "UNKNOWN_ORDER"),
"option_symbol": selected_strike,
"entry_price": ltp,
"signal_type": signal_type
}
except Exception as e:
log(f"Trade execution error for {symbol}: {e}", "error")
return None
📌 Order Debug Info:
➡️ Tradingsymbol: FINNIFTY 27 MAR 23000 PUT
➡️ Exchange: NFO | Mapped: NSE_FNO
➡️ Security ID: 37392
➡️ Quantity: 1 | Price: 323.95 | Order Type: MARKET
➡️ Product Type: MIS | Transaction Type: BUY
❌ Got exception in place_order: ⚠️ Order failed: Unknown error
@Tradehull_Imran
Sir,How to print 20 level market depth data.
it is in binary format.
how to print in readable format sir
Hi @anandc ,
Currently OCO order is not supported instead you can use Bracket orders. Use the below code to place a bracket order.
buy_orderid = tsl.order_placement(tradingsymbol = 'ACC', exchange = 'NSE', quantity = 1, price = 0, trigger_price = 0, order_type = 'MARKET', transaction_type = 'BUY', trade_type = 'BO', bo_profit_value=50, bo_stop_loss_value=50)
@Tradehull_Imran
Sir, i’m able to print 20 Level market depth data. It, is in binary format.
b’L\x01)\x015\x05\x00\x00\x00\x00\x00\x0033333\xb6\x9a@\x87\x00\x00\x00\x02\x00\x00\x00fffff\xb5\x9a@Y\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\xb5\x9a@\x03\x00\x00\x00\x01\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb4\x9a@\x1e\x01\x00\x00\x03\x00\x00\x00\x9a\x99\x99\x99\x99\xb4\x9a@\x1b\x00\x00\x00\x01\x00\x00\x00fffff\xb4\x9a@\xa9\x00\x00\x00\x03\x00\x00\x0033333\xb4\x9a@3\x01\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\xb4\x9a@\xbc\x00\x00\x00\x08\x00\x00\x00\x9a\x99\x99\x99\x99\xb3\x9a@\x16\x01\x00\x00\x07\x00\x00\x00fffff\xb3\x9a@M\x01\x00\x00\x04\x00\x00\x0033333\xb3\x9a@\xa6\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\xb3\x9a@!\x02\x00\x00\x08\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb2\x9a@\xa3\x00\x00\x00\x03\x00\x00\x00\x9a\x99\x99\x99\x99\xb2\x9a@\xd6\x00\x00\x00\x04\x00\x00\x00fffff\xb2\x9a@y\x00\x00\x00\x04\x00\x00\x0033333\xb2\x9a@\x8a\x04\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\xb2\x9a@Q\x02\x00\x00\x07\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb1\x9a@\x1a\x01\x00\x00\x05\x00\x00\x00\x9a\x99\x99\x99\x99\xb1\x9a@\xfd\x00\x00\x00\x05\x00\x00\x00fffff\xb1\x9a@\xcb\x00\x00\x00\x02\x00\x00\x00L\x013\x015\x05\x00\x00\x00\x00\x00\x00fffff\xb6\x9a@\x01\x00\x00\x00\x01\x00\x00\x00fffff\xb7\x9a@\x05\x00\x00\x00\x01\x00\x00\x00\x9a\x99\x99\x99\x99\xb7\x9a@\x0b\x00\x00\x00\x02\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb7\x9a@6\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\xb8\x9a@\xb8\x02\x00\x00\x05\x00\x00\x0033333\xb8\x9a@A\x06\x00\x00\x07\x00\x00\x00fffff\xb8\x9a@\x1a\t\x00\x00\n\x00\x00\x00\x9a\x99\x99\x99\x99\xb8\x9a@i\x03\x00\x00\x05\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb8\x9a@\x7f\x02\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x00\xb9\x9a@"\x06\x00\x00\n\x00\x00\x0033333\xb9\x9a@b\t\x00\x00\x08\x00\x00\x00fffff\xb9\x9a@\x05\x02\x00\x00\x04\x00\x00\x00\x9a\x99\x99\x99\x99\xb9\x9a@\x1a\x02\x00\x00\x06\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb9\x9a@\xb6\n\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x00\xba\x9a@\xc5\x04\x00\x00\x08\x00\x00\x0033333\xba\x9a@\xae\x05\x00\x00\n\x00\x00\x00fffff\xba\x9a@\xe7\x00\x00\x00\x05\x00\x00\x00\x9a\x99\x99\x99\x99\xba\x9a@\xcc\t\x00\x00\n\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xba\x9a@r\x05\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\xbb\x9a@\x15\x02\x00\x00\x05\x00\x00\x00’
Please sir , write a python function to convert it into readable format sir.
Hi @Dev_Goswami ,
There seems to be no issue in pnl function, kindly once run it in local system and let us know if you still face any issues with screenshot.
Hi @SUKANTA_MONDAL1 and @Vikas_Chaudhary,
Do share the complete steps, so we can share the pseudocode ‘Fair Value Gap’.