Unable to fetch data of nifty and place order in FNO

this is the code can someone help me @Hardik @RahulDeshpande

from dhanhq import dhanhq

Replace with your client id and access token

client_id = ‘*****’
access_token = ‘****’

dhan = dhanhq(client_id, access_token)

def get_exchange(dhan, exchange):
if exchange == “bsc”:
return dhan.BSC
elif exchange == “mcx”:
return dhan.MCX
elif exchange == “nse”:
return dhan.NSE

def get_side(dhan, side):
if side == “buy”:
return dhan.BUY
elif side == “sell”:
return dhan.SELL

def place_fno_order(dhan, symbol, exchange, side, quantity, order_type, price):
try:
exchange_segment = get_exchange(dhan, exchange)
side = get_side(dhan, side)
order_id = dhan.place_order(
security_id=symbol, # stock id
exchange_segment=exchange_segment,
transaction_type=side,
quantity=quantity,
order_type=order_type,
product_type=dhan.INTRA, # INTRA, CNC (equity), MTF
price=price
)
print(f"Order placed successfully. Order ID: {order_id}“)
except Exception as e:
print(f"Failed to place order: {e}”)

stock = token_dict[‘NIFTY’][‘NSE’][‘SEM_SMST_SECURITY_ID’] # type: ignore
trading_symbol = get_trading_symbol_by_security_id(token_dict, stock)
print(f"Stock is buying: {stock} and symbol is {trading_symbol}")

to place order down

symbol = stock
exchange = ‘nse’
side = ‘buy’
quantity = 1
order_type = ‘market’
price = 0 # Not required for market order
instrument_type = None # Automatically determined based on order_type
expiry_date = ‘6-JUN-2024’
strike_price = 23200

Place F&O order

place_fno_order(dhan, symbol, exchange, side, quantity, order_type, price)

@PravinJ @iamshrimohan

Hello @devesh

Can you tell us what is the error you are getting when running this script? Since this is a custom script, can look into the part if the API is failing or throwing error.

Hi, devesh ji.

What is the error you are getting?.

After running the code, the code is set to punch an order for nifty50 but It pounch order for an random stock

As nifty50 has same SEM_SMST_SECURITY_ID
As stock name ABB

Hello @devesh

You can not punch an order into Nifty 50 itself, as it is an index and not a direct tradable instrument. I am assuming you put Security ID as 13 with exchange segment as NSE_EQ and this is the reason why order was executed in ABB.

Can you confirm which instrument are you trying to place an order for?

I wanted to pounch order for nifty50

@devesh

Got it. As mentioned, Nifty 50 is itself not tradeable. You can either look for Nifty ETFs or trade in Nifty Futures. You can also buy all Nifty stocks in index proportions, but then the amount of order will be large.

Then what is this about?

I know his making a straddle. But to make straddle he has to buy and sell a perticular strikpricr, then how he is available to buy and sell in index using strick price

@devesh

If you wish to build algo using straddle, then you are essentially trading Nifty Options. You can fetch security ID as per the strike price you want to trade in along with type of option and expiry.

Each of these are individual instrument with different Security ID assigned, and you will have to place order accordingly.

Can u share the source code/ basis structure of code to fetch the data of security I’d as per strick price.

@devesh

I do not have code snippet for this, maybe someone from community can look into @Sammy

It’s going to be a basic pandas code only.

@devesh your code is correct however , you may would like to try to change for following:

Use " Hyphen" in security ID to get the ID number for exmple:
LUPIN-Jun2024-1240-PE

Breakdown of it will goes like
“Stock-Expiry_Month-Strike-CE/PE”

Considering the sheet as a Database, above method will act as primary key and will provides you the ID of a stock.

1 Like

Just to add here , I use above method personally. Since i exclusively trade on NSE Derivatives , So i filtered out my sheet as follows:

So u mean instead of

SEM_SMST_SECURITY_ID

I should put

SEM_SMST_Hyphen

??

No ,
I mean you have to set the order of string as I explained it earlier and once you have it , you have to find it in SEMS column and print the whole line against it or the specific values