Repository navigation
Expand file tree
/
Copy pathbasic_usage.py
More file actions
63 lines (54 loc) · 2.43 KB
/
Copy pathbasic_usage.py
File metadata and controls
63 lines (54 loc) · 2.43 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
62
63
"""Place a 1-share ICICIBANK limit order at LTP.
Type: mutating (UAT)
Needs: UAT login via env creds; market open for a live LTP
Expect: LTP and order summary in rupees; test order(s) are cancelled at the end if still open.
Tested with: nubra-sdk 0.5.4 (UAT)
"""
import time
from nubra_python_sdk.start_sdk import InitNubraSdk, NubraEnv
from nubra_python_sdk.refdata.instruments import InstrumentData
from nubra_python_sdk.marketdata.market_data import MarketData
from nubra_python_sdk.trading.trading_data import NubraTrader
from nubra_python_sdk.trading.trading_enum import ExchangeEnum
# Use NubraEnv.UAT for testing. Switch to NubraEnv.PROD for live usage.
nubra = InitNubraSdk(NubraEnv.UAT, env_creds=True)
instruments = InstrumentData(nubra)
market_data = MarketData(nubra)
trader = NubraTrader(nubra)
instrument = instruments.get_instrument_by_symbol("ICICIBANK", exchange=ExchangeEnum.NSE)
if isinstance(instrument, dict):
raise SystemExit(instrument["msg"]) # symbol not found
tick_size = instrument.tick_size # paise
def to_tick(price):
return int(round(price / tick_size) * tick_size)
ltp = market_data.quote(ref_id=instrument.ref_id, levels=1).orderBook.last_traded_price # paise
print(f"LTP: Rs {ltp / 100:.2f}")
result = trader.create_order({
"refId": instrument.ref_id,
"qty": 1,
"side": "BUY",
"deliveryType": "IDAY",
"priceType": "LIMIT",
"validityType": "DAY",
"isMultiLeg": False,
"executionMode": "ENTRY",
"entryPrice": to_tick(ltp),
"stratTags": ["python-sdk-v3-basic-usage"], # One tag only, hyphens only.
})
for o in result.orders:
price = f"Rs {o.entryPrice / 100:.2f}" if o.entryPrice else "market"
print(f"Order {o.intentOrderId}: {o.status or 'SUBMITTED'}, qty={o.orderQty}, price={price}")
if o.rejectionMsg:
print(" Rejected:", o.rejectionMsg)
# Clean up: cancel the test order(s) if still working.
time.sleep(2) # orders reach the book ~1-2s after create
ids = [o.intentOrderId for o in result.orders]
live = [o for o in trader.get_order(ids) or [] if o.status not in ("EXECUTED", "REJECTED", "CANCELLED", "EXPIRED")]
if live:
for attempt in range(3):
try:
print("Cancel:", trader.cancel_orders_sentinel([{"orderId": o.intentOrderId} for o in live]))
break
except Exception as err: # the exchange may still be processing the order
print(f"Cancel not accepted yet ({err}); retrying in 3s")
time.sleep(3)