Repository navigation
Expand file tree
/
Copy pathbasic_usage.py
More file actions
61 lines (53 loc) · 2.58 KB
/
Copy pathbasic_usage.py
File metadata and controls
61 lines (53 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Look up instruments by ref_id, symbol, Nubra name and structured filters.
Type: read-only
Needs: UAT login via PHONE_NO / MPIN in .env
Expect: instrument count, then the matching instrument records (prices/strikes in paise).
Tested with: nubra-sdk 0.5.4 (UAT)
"""
from nubra_python_sdk.refdata.instruments import InstrumentData
from nubra_python_sdk.start_sdk import InitNubraSdk, NubraEnv
# Initialize the SDK client.
# Use NubraEnv.UAT for testing. Switch to NubraEnv.PROD for live usage.
nubra = InitNubraSdk(NubraEnv.UAT, env_creds=True)
instruments = InstrumentData(nubra)
# Get all NSE instruments as a pandas DataFrame.
instruments_df = instruments.get_instruments_dataframe(exchange="NSE")
print(f"Total instruments: {len(instruments_df)}")
# Get one instrument by ref_id.
# ref_id values can differ between environments; resolve it with get_instrument_by_symbol.
instrument = instruments.get_instrument_by_ref_id(71878, exchange="NSE")
print(instrument)
# Get one instrument by exchange trading symbol.
instrument = instruments.get_instrument_by_symbol("HDFCBANK", exchange="NSE")
# Lookups return a dict with "msg" when nothing is found.
if isinstance(instrument, dict):
raise SystemExit(f"Symbol lookup failed: {instrument['msg']}")
print(instrument)
# Get one instrument by Nubra internal name.
instrument = instruments.get_instrument_by_nubra_name("STOCK_HDFCBANK.NSECM", exchange="NSE")
print(instrument)
# Fetch matching instruments with structured filters.
# Expiries roll, so pick a live one from the master instead of hardcoding a date.
nifty_opts = instruments_df[(instruments_df["asset"] == "NIFTY") & (instruments_df["derivative_type"] == "OPT")]
if nifty_opts.empty:
raise SystemExit("No NIFTY options in the instrument master.")
expiry_int = int(nifty_opts["expiry"].astype(int).min())
strikes = sorted(nifty_opts[nifty_opts["expiry"].astype(int) == expiry_int]["strike_price"].astype(int).unique())
expiry = str(expiry_int)
strike = str(strikes[len(strikes) // 2]) # a mid-range strike, in paise
# get_instruments_by_pattern takes a dict or a list of dicts. All keys are optional:
# exchange, asset, derivative_type, asset_type, expiry, strike_price, option_type, isin.
# expiry (YYYYMMDD) and strike_price (paise) are accepted as int or digit string.
# asset_type is optional, e.g. INDEX_FO for index options.
matches = instruments.get_instruments_by_pattern([
{
"exchange": "NSE",
"asset": "NIFTY",
"derivative_type": "OPT",
"expiry": expiry,
"strike_price": strike,
"option_type": "CE",
"asset_type": "INDEX_FO"
}
])
print(matches)