kotakneoapi 2.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kotakneoapi-2.3.0/LICENSE +21 -0
- kotakneoapi-2.3.0/PKG-INFO +502 -0
- kotakneoapi-2.3.0/README.md +451 -0
- kotakneoapi-2.3.0/kotakneoapi.egg-info/PKG-INFO +502 -0
- kotakneoapi-2.3.0/kotakneoapi.egg-info/SOURCES.txt +50 -0
- kotakneoapi-2.3.0/kotakneoapi.egg-info/dependency_links.txt +1 -0
- kotakneoapi-2.3.0/kotakneoapi.egg-info/requires.txt +24 -0
- kotakneoapi-2.3.0/kotakneoapi.egg-info/top_level.txt +1 -0
- kotakneoapi-2.3.0/neo_api_client/__init__.py +79 -0
- kotakneoapi-2.3.0/neo_api_client/__version__.py +4 -0
- kotakneoapi-2.3.0/neo_api_client/api_client.py +30 -0
- kotakneoapi-2.3.0/neo_api_client/circuit_breaker.py +309 -0
- kotakneoapi-2.3.0/neo_api_client/exceptions.py +468 -0
- kotakneoapi-2.3.0/neo_api_client/logger.py +197 -0
- kotakneoapi-2.3.0/neo_api_client/neo_api.py +1030 -0
- kotakneoapi-2.3.0/neo_api_client/py.typed +0 -0
- kotakneoapi-2.3.0/neo_api_client/rate_limiter.py +282 -0
- kotakneoapi-2.3.0/neo_api_client/req_data_validation.py +307 -0
- kotakneoapi-2.3.0/neo_api_client/rest.py +369 -0
- kotakneoapi-2.3.0/neo_api_client/retry.py +234 -0
- kotakneoapi-2.3.0/neo_api_client/services/__init__.py +9 -0
- kotakneoapi-2.3.0/neo_api_client/services/client_ip.py +40 -0
- kotakneoapi-2.3.0/neo_api_client/services/limits.py +32 -0
- kotakneoapi-2.3.0/neo_api_client/services/login.py +41 -0
- kotakneoapi-2.3.0/neo_api_client/services/logout.py +24 -0
- kotakneoapi-2.3.0/neo_api_client/services/margin.py +70 -0
- kotakneoapi-2.3.0/neo_api_client/services/modify_order.py +65 -0
- kotakneoapi-2.3.0/neo_api_client/services/order.py +95 -0
- kotakneoapi-2.3.0/neo_api_client/services/order_history.py +31 -0
- kotakneoapi-2.3.0/neo_api_client/services/order_report.py +50 -0
- kotakneoapi-2.3.0/neo_api_client/services/portfolio.py +37 -0
- kotakneoapi-2.3.0/neo_api_client/services/positions.py +25 -0
- kotakneoapi-2.3.0/neo_api_client/services/quotes.py +52 -0
- kotakneoapi-2.3.0/neo_api_client/services/scrip_master.py +37 -0
- kotakneoapi-2.3.0/neo_api_client/services/scrip_search.py +206 -0
- kotakneoapi-2.3.0/neo_api_client/services/totp.py +73 -0
- kotakneoapi-2.3.0/neo_api_client/services/trade_report.py +22 -0
- kotakneoapi-2.3.0/neo_api_client/settings.py +297 -0
- kotakneoapi-2.3.0/neo_api_client/utils/neo_utility.py +118 -0
- kotakneoapi-2.3.0/neo_api_client/utils/scrip_cache.py +49 -0
- kotakneoapi-2.3.0/neo_api_client/utils/urls.py +28 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/feed/__init__.py +25 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/feed/client.py +833 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/feed/exceptions.py +43 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/feed/models.py +177 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/feed/protocol.py +290 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/orderfeed/__init__.py +31 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/orderfeed/client.py +344 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/orderfeed/exceptions.py +31 -0
- kotakneoapi-2.3.0/neo_api_client/websocket/orderfeed/models.py +112 -0
- kotakneoapi-2.3.0/pyproject.toml +174 -0
- kotakneoapi-2.3.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kotak Neo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kotakneoapi
|
|
3
|
+
Version: 2.3.0
|
|
4
|
+
Summary: Official Python SDK for Kotak Neo Trading APIs
|
|
5
|
+
Author-email: Kotak Neo <support@kotakneo.com>
|
|
6
|
+
Maintainer: Dhruv Agarwal
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/Kotak-Neo/kotak-neo-python
|
|
9
|
+
Project-URL: Repository, https://github.com/Kotak-Neo/kotak-neo-python
|
|
10
|
+
Project-URL: Issues, https://github.com/Kotak-Neo/kotak-neo-python/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/Kotak-Neo/kotak-neo-python/tree/main/docs
|
|
12
|
+
Keywords: kotak,neo,trading,stocks,broker,api,market,equity,websocket
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: numpy<3,>=1.26
|
|
28
|
+
Requires-Dist: pandas<3,>=2.2
|
|
29
|
+
Requires-Dist: PyJWT<3,>=2.10.1
|
|
30
|
+
Requires-Dist: httpx[http2]<1,>=0.27
|
|
31
|
+
Requires-Dist: websocket-client<2,>=1.9.0
|
|
32
|
+
Requires-Dist: structlog<27,>=24.1.0
|
|
33
|
+
Requires-Dist: tenacity<10,>=8.2.3
|
|
34
|
+
Requires-Dist: python-decouple<4,>=3.8
|
|
35
|
+
Requires-Dist: pyotp>=2.9.0
|
|
36
|
+
Requires-Dist: websockets>=12.0
|
|
37
|
+
Requires-Dist: pydantic>=2.0.0
|
|
38
|
+
Provides-Extra: dev
|
|
39
|
+
Requires-Dist: pytest>=9.0; extra == "dev"
|
|
40
|
+
Requires-Dist: pytest-cov>=7.0; extra == "dev"
|
|
41
|
+
Requires-Dist: respx>=0.21; extra == "dev"
|
|
42
|
+
Requires-Dist: coverage>=7.10; extra == "dev"
|
|
43
|
+
Requires-Dist: ruff==0.16.0; extra == "dev"
|
|
44
|
+
Requires-Dist: mypy>=1.17; extra == "dev"
|
|
45
|
+
Requires-Dist: build>=1.3.0; extra == "dev"
|
|
46
|
+
Requires-Dist: twine>=6.0.0; extra == "dev"
|
|
47
|
+
Requires-Dist: bandit>=1.7.6; extra == "dev"
|
|
48
|
+
Requires-Dist: safety>=3.0.0; extra == "dev"
|
|
49
|
+
Requires-Dist: pre-commit>=3.6.0; extra == "dev"
|
|
50
|
+
Dynamic: license-file
|
|
51
|
+
|
|
52
|
+
# Kotak Neo API - Python SDK
|
|
53
|
+
|
|
54
|
+
Official Python SDK for Kotak Neo Trading APIs - a modern, well-tested trading client for the Kotak Neo platform.
|
|
55
|
+
|
|
56
|
+
[](https://www.python.org/downloads/)
|
|
57
|
+
[](https://pypi.org/project/kotakneoapi/)
|
|
58
|
+
[](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/LICENSE)
|
|
59
|
+
|
|
60
|
+
> **This is the actively maintained Python SDK**, superseding
|
|
61
|
+
> [`kotak-neo-api-v2`](https://github.com/Kotak-Neo/kotak-neo-api-v2) (now legacy).
|
|
62
|
+
> Already on `kotak-neo-api-v2`? See the
|
|
63
|
+
> **[Migration Guide](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/MIGRATION.md)**.
|
|
64
|
+
|
|
65
|
+
## Features
|
|
66
|
+
|
|
67
|
+
✅ **Authentication** - TOTP-based secure login with 2FA
|
|
68
|
+
✅ **Order Management** - Place, modify, cancel orders (Regular/AMO)
|
|
69
|
+
✅ **Portfolio & Positions** - Real-time holdings, positions, and limits
|
|
70
|
+
✅ **Market Data** - Live quotes, scrip master, search functionality
|
|
71
|
+
✅ **SFeed WebSocket Streaming** - Modern async/await live market feed with typed messages, enriched with `trading_symbol`
|
|
72
|
+
✅ **HTTP/2 Transport** - REST calls use HTTP/2 (via httpx) with automatic HTTP/1.1 fallback
|
|
73
|
+
✅ **Optional Reliability Utilities** - Opt-in rate limiting, plus retry and circuit-breaker helpers
|
|
74
|
+
✅ **Comprehensive Error Handling** - Detailed exception hierarchy with input validation
|
|
75
|
+
✅ **Type Safety** - Full mypy type checking support
|
|
76
|
+
✅ **Extensive Testing** - 100% test coverage (unit, integration, and E2E tests)
|
|
77
|
+
|
|
78
|
+
## Installation
|
|
79
|
+
|
|
80
|
+
### From PyPI
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pip install kotakneoapi
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### For Development (Local Installation)
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
# Clone the repository
|
|
90
|
+
git clone https://github.com/Kotak-Neo/kotak-neo-python.git
|
|
91
|
+
cd kotak-neo-python
|
|
92
|
+
|
|
93
|
+
# Install in development/editable mode
|
|
94
|
+
pip install -e .
|
|
95
|
+
|
|
96
|
+
# Or install with development dependencies
|
|
97
|
+
pip install -e ".[dev]"
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Quick Start
|
|
101
|
+
|
|
102
|
+
### Prerequisites
|
|
103
|
+
|
|
104
|
+
1. **Get Consumer Key (REQUIRED)**: Login to Kotak NEO app/web → **Invest** tab → **Trade API** card → Generate application → Copy the token
|
|
105
|
+
- This token is used in the Authorization header for all API requests
|
|
106
|
+
- Authentication will fail without this token
|
|
107
|
+
2. **Register for TOTP**: Visit https://www.kotaksecurities.com/platform/kotak-neo-trade-api/ → Register for TOTP → Scan QR code with authenticator app (Google Authenticator, Authy, etc.)
|
|
108
|
+
|
|
109
|
+
### Authentication
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from neo_api_client import NeoAPI
|
|
113
|
+
|
|
114
|
+
# Initialize the client
|
|
115
|
+
client = NeoAPI(
|
|
116
|
+
consumer_key="your-consumer-key-token", # Token from NEO app Trade API card
|
|
117
|
+
environment="prod", # production (default)
|
|
118
|
+
access_token=None, # Optional
|
|
119
|
+
neo_fin_key=None, # Optional
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# Step 1: Login with TOTP
|
|
123
|
+
login_response = client.totp_login(
|
|
124
|
+
mobile_number="+919876543210", # Your registered mobile with country code
|
|
125
|
+
ucc="YOUR_UCC", # Find in NEO app/web under Profile section
|
|
126
|
+
totp="123456", # 6-digit code from authenticator app (changes every 30 seconds)
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
# Step 2: Validate with MPIN to complete authentication
|
|
130
|
+
validate_response = client.totp_validate(mpin="123456") # Your trading MPIN
|
|
131
|
+
|
|
132
|
+
# Place an order
|
|
133
|
+
order_response = client.place_order(
|
|
134
|
+
exchange_segment="nse_cm",
|
|
135
|
+
product="CNC",
|
|
136
|
+
price="1500.00",
|
|
137
|
+
order_type="L",
|
|
138
|
+
quantity="10",
|
|
139
|
+
validity="DAY",
|
|
140
|
+
trading_symbol="RELIANCE-EQ",
|
|
141
|
+
transaction_type="B",
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
# Get real-time quotes
|
|
145
|
+
quotes = client.quotes(
|
|
146
|
+
instrument_tokens=[{"instrument_token": "1333", "exchange_segment": "nse_cm"}], quote_type="all"
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Logout
|
|
150
|
+
client.logout()
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Documentation
|
|
154
|
+
|
|
155
|
+
### 📚 [Complete API Documentation](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/README.md)
|
|
156
|
+
|
|
157
|
+
Detailed documentation for all SDK functions with examples and real API responses.
|
|
158
|
+
|
|
159
|
+
#### Quick Links
|
|
160
|
+
|
|
161
|
+
**Authentication**
|
|
162
|
+
- [TOTP Login](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/authentication/totp_login.md) | [TOTP Validate](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/authentication/totp_validate.md) | [What's My IP](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/authentication/whatsmyip.md) | [Logout](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/authentication/logout.md)
|
|
163
|
+
|
|
164
|
+
**Order Management**
|
|
165
|
+
- [Place Order](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/orders/place_order.md) | [Modify Order](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/orders/modify_order.md) | [Cancel Order](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/orders/cancel_order.md)
|
|
166
|
+
- [Order Report](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/orders/order_report.md) | [Order History](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/orders/order_history.md) | [Trade Report](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/orders/trade_report.md)
|
|
167
|
+
|
|
168
|
+
**Portfolio & Positions**
|
|
169
|
+
- [Holdings](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/portfolio/holdings.md) | [Positions](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/portfolio/positions.md)
|
|
170
|
+
- [Limits](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/portfolio/limits.md) | [Margin Required](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/portfolio/margin_required.md)
|
|
171
|
+
|
|
172
|
+
**Market Data**
|
|
173
|
+
- [Quotes](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/market_data/quotes.md) | [Scrip Master](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/market_data/scrip_master.md) | [Search Scrip](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/market_data/search_scrip.md)
|
|
174
|
+
|
|
175
|
+
**WebSocket**
|
|
176
|
+
- Market data (SFeed): [Market Feed (Subscribe/Unsubscribe)](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/websocket/market_feed.md)
|
|
177
|
+
- Order & positions: [Order Feed](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/websocket/order_feed.md)
|
|
178
|
+
- Full guide: [SFeed WebSocket](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/websocket.md)
|
|
179
|
+
|
|
180
|
+
### 📖 Guides & Documentation
|
|
181
|
+
|
|
182
|
+
**Upgrading:**
|
|
183
|
+
- **[Migration Guide (v2.0.2 → v2.3.0)](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/MIGRATION.md)** - Upgrade existing code to the latest version
|
|
184
|
+
|
|
185
|
+
**Installation:**
|
|
186
|
+
- **[Installation Overview](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/installation/README.md)** - All installation options
|
|
187
|
+
- **[Installation Reference](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/INSTALLATION_REFERENCE.md)** - Complete platform guide
|
|
188
|
+
- **[Local Installation](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/installation/local-install.md)** - Current method (before PyPI publish)
|
|
189
|
+
- **[Platform-Specific Guides](https://github.com/Kotak-Neo/kotak-neo-python/tree/main/docs/installation)** - Windows, macOS, Linux, VS Code
|
|
190
|
+
|
|
191
|
+
**Publishing & Distribution:**
|
|
192
|
+
- **[TestPyPI Upload Guide](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/TESTPYPI_UPLOAD_GUIDE.md)** - Upload to TestPyPI for testing
|
|
193
|
+
- **[Publishing Guide](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/PUBLISHING.md)** - Publish to production PyPI
|
|
194
|
+
|
|
195
|
+
**API Documentation:**
|
|
196
|
+
- **[Complete API Reference](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/README.md)** - All SDK functions
|
|
197
|
+
- **[SFeed WebSocket Guide](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/websocket.md)** - Async streaming client, protocol & migration
|
|
198
|
+
- **[All Guides](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/README.md)** - Complete guide index
|
|
199
|
+
|
|
200
|
+
## WebSocket Streaming Example (SFeed)
|
|
201
|
+
|
|
202
|
+
Live market data is delivered through the modern async/await **SFeed** WebSocket
|
|
203
|
+
client. It uses `async for` iteration and returns type-safe Pydantic messages,
|
|
204
|
+
each enriched with its `trading_symbol` (resolved from the subscribe ack).
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
import asyncio
|
|
208
|
+
from neo_api_client import NeoAPI
|
|
209
|
+
from neo_api_client.websocket.feed import WsToken, SFeedScrip
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
async def main():
|
|
213
|
+
client = NeoAPI(consumer_key="your-consumer-key-token", environment="prod")
|
|
214
|
+
client.totp_login(mobile_number="+919876543210", ucc="YOUR_UCC", totp="123456")
|
|
215
|
+
client.totp_validate(mpin="123456")
|
|
216
|
+
|
|
217
|
+
# create_websocket() builds a SFeedWebSocket from the current session
|
|
218
|
+
async with client.create_websocket() as ws:
|
|
219
|
+
# Batch-subscribe any number of instruments in a single call
|
|
220
|
+
await ws.subscribe_scrips([
|
|
221
|
+
WsToken("nse_cm", "Nifty 50"),
|
|
222
|
+
WsToken("nse_cm", "11536"),
|
|
223
|
+
])
|
|
224
|
+
|
|
225
|
+
async for message in ws:
|
|
226
|
+
if isinstance(message, SFeedScrip):
|
|
227
|
+
print(
|
|
228
|
+
f"{message.trading_symbol} ({message.instrument_token}) "
|
|
229
|
+
f"LTP: {message.last_traded_price}"
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
asyncio.run(main())
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
> **Note:** The SFeed client works out of the box — its dependencies
|
|
237
|
+
> (`websockets`, `pydantic`) ship with the base install. The legacy callback-based
|
|
238
|
+
> WebSocket (`client.subscribe(...)`, `on_message`, etc.) was **removed in v2.2.0** —
|
|
239
|
+
> see the [SFeed WebSocket guide](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/guides/websocket.md) for the full API and a
|
|
240
|
+
> migration reference.
|
|
241
|
+
|
|
242
|
+
## Order & Position Streaming Example
|
|
243
|
+
|
|
244
|
+
Order-lifecycle events and live position updates stream over a separate
|
|
245
|
+
async/await WebSocket, `create_order_feed()`. It returns type-safe `OrderUpdate` /
|
|
246
|
+
`PositionUpdate` messages.
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
import asyncio
|
|
250
|
+
from neo_api_client import NeoAPI
|
|
251
|
+
from neo_api_client.websocket.orderfeed import OrderUpdate, PositionUpdate, OrderStatus
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
async def main():
|
|
255
|
+
client = NeoAPI(consumer_key="your-consumer-key-token", environment="prod")
|
|
256
|
+
client.totp_login(mobile_number="+919876543210", ucc="YOUR_UCC", totp="123456")
|
|
257
|
+
client.totp_validate(mpin="123456")
|
|
258
|
+
|
|
259
|
+
# create_order_feed() connects to wss://<baseurl>/realtime using the session
|
|
260
|
+
async with client.create_order_feed() as feed:
|
|
261
|
+
async for message in feed:
|
|
262
|
+
if isinstance(message, OrderUpdate):
|
|
263
|
+
print(f"order {message.data.order_no} -> {message.data.order_status}")
|
|
264
|
+
elif isinstance(message, PositionUpdate):
|
|
265
|
+
print(f"position {message.data.symbol}")
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
asyncio.run(main())
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
> Full reference: [Order & Position Feed](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/docs/functions/websocket/order_feed.md).
|
|
272
|
+
|
|
273
|
+
## Exception Handling
|
|
274
|
+
|
|
275
|
+
```python
|
|
276
|
+
from neo_api_client import (
|
|
277
|
+
NeoAPIException,
|
|
278
|
+
AuthenticationError,
|
|
279
|
+
ValidationError,
|
|
280
|
+
RateLimitError,
|
|
281
|
+
NetworkError,
|
|
282
|
+
OrderError,
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
try:
|
|
286
|
+
response = client.place_order(...)
|
|
287
|
+
except AuthenticationError:
|
|
288
|
+
print("Authentication failed - please login again")
|
|
289
|
+
except ValidationError as e:
|
|
290
|
+
print(f"Invalid parameters: {e}")
|
|
291
|
+
except RateLimitError:
|
|
292
|
+
print("Rate limit exceeded - please retry after some time")
|
|
293
|
+
except OrderError as e:
|
|
294
|
+
print(f"Order placement failed: {e}")
|
|
295
|
+
except NeoAPIException as e:
|
|
296
|
+
print(f"API error: {e}")
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## Environment Setup
|
|
300
|
+
|
|
301
|
+
Create a `.env` file for credentials (copy from `.env.example`):
|
|
302
|
+
|
|
303
|
+
```bash
|
|
304
|
+
# Consumer Key from NEO app (REQUIRED - Used in Authorization header)
|
|
305
|
+
# Get it: NEO app → Invest → Trade API → Generate application → Copy token
|
|
306
|
+
NEO_CONSUMER_KEY=your-consumer-key-token
|
|
307
|
+
|
|
308
|
+
# Your registered mobile number with country code
|
|
309
|
+
NEO_MOBILE_NUMBER=+919876543210
|
|
310
|
+
|
|
311
|
+
# Your UCC (User Client Code) from NEO app Profile section
|
|
312
|
+
NEO_UCC=YOUR_UCC
|
|
313
|
+
|
|
314
|
+
# TOTP secret key (base32 string from QR code during TOTP registration)
|
|
315
|
+
# This is NOT the 6-digit code - it's the secret key from authenticator setup
|
|
316
|
+
NEO_TOTP_SECRET=YOUR_TOTP_SECRET_KEY
|
|
317
|
+
|
|
318
|
+
# Your trading MPIN
|
|
319
|
+
NEO_MPIN=123456
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
**How to get credentials:**
|
|
323
|
+
- **Consumer Key**: NEO app → Invest → Trade API → Generate application → Copy token
|
|
324
|
+
- **UCC**: NEO app → Profile section
|
|
325
|
+
- **TOTP Secret**: https://www.kotaksecurities.com/platform/kotak-neo-trade-api/ → Register for TOTP → Note the secret from QR code setup
|
|
326
|
+
|
|
327
|
+
## Performance Benchmarks
|
|
328
|
+
|
|
329
|
+
Average API response times (production environment):
|
|
330
|
+
|
|
331
|
+
| API Function | Avg Latency |
|
|
332
|
+
|--------------|-------------|
|
|
333
|
+
| Login & Authentication | 134-367 ms |
|
|
334
|
+
| Order Operations | 67-71 ms |
|
|
335
|
+
| Portfolio & Positions | 68-77 ms |
|
|
336
|
+
| Market Data (Quotes) | 289 ms |
|
|
337
|
+
| Margin Calculation | 110 ms |
|
|
338
|
+
| Scrip Master | 1250 ms |
|
|
339
|
+
|
|
340
|
+
*Tested on production environment with real API calls*
|
|
341
|
+
|
|
342
|
+
## Common Parameters
|
|
343
|
+
|
|
344
|
+
### Exchange Segments
|
|
345
|
+
- `nse_cm` - NSE Cash Market
|
|
346
|
+
- `bse_cm` - BSE Cash Market
|
|
347
|
+
- `nse_fo` - NSE Futures & Options
|
|
348
|
+
- `bse_fo` - BSE Futures & Options
|
|
349
|
+
- `mcx_fo` - MCX Commodities
|
|
350
|
+
- `cde_fo` - Currency Derivatives (market data/quotes only — **not** accepted by `place_order`/`margin_required`, which don't support this segment)
|
|
351
|
+
|
|
352
|
+
### Product Types
|
|
353
|
+
- `CNC` - Cash & Carry (Delivery)
|
|
354
|
+
- `MIS` - Margin Intraday Square-off
|
|
355
|
+
- `NRML` - Normal (Carry Forward)
|
|
356
|
+
- `MTF` - Margin Trading Facility
|
|
357
|
+
|
|
358
|
+
Note: `place_order`/`modify_order` only accept these four exact codes (Bracket
|
|
359
|
+
and Cover orders are no longer supported).
|
|
360
|
+
|
|
361
|
+
### Order Types
|
|
362
|
+
- `L` - Limit Order
|
|
363
|
+
- `MKT` - Market Order
|
|
364
|
+
- `SL` - Stop Loss Limit
|
|
365
|
+
- `SL-M` - Stop Loss Market
|
|
366
|
+
|
|
367
|
+
### Transaction Types
|
|
368
|
+
- `B` - Buy
|
|
369
|
+
- `S` - Sell
|
|
370
|
+
|
|
371
|
+
### Validity Types
|
|
372
|
+
- `DAY` - Valid for the day
|
|
373
|
+
- `IOC` - Immediate or Cancel
|
|
374
|
+
|
|
375
|
+
## Architecture
|
|
376
|
+
|
|
377
|
+
Always on for every request:
|
|
378
|
+
|
|
379
|
+
- **HTTP/2 Transport** - REST calls run over HTTP/2 (via `httpx`) with connection pooling and automatic HTTP/1.1 fallback
|
|
380
|
+
- **Structured Logging** - Request/response tracking with correlation IDs
|
|
381
|
+
- **Type Safety** - Full mypy type checking support
|
|
382
|
+
|
|
383
|
+
Optional reliability utilities (shipped, tested, and importable, but **not wired
|
|
384
|
+
into the request path by default** — you opt in):
|
|
385
|
+
|
|
386
|
+
- **Rate Limiter** - Token-bucket throttling (per second/minute/hour) to avoid tripping API quotas. Enable with `RESTClientObject(..., enable_rate_limiting=True)`.
|
|
387
|
+
- **Retry Logic** - Exponential backoff with jitter for transient errors, via the `with_retry` / `create_retry_decorator` decorators in `neo_api_client.retry`.
|
|
388
|
+
- **Circuit Breaker** - `CircuitBreaker` in `neo_api_client.circuit_breaker` to stop calling a failing service and let it recover.
|
|
389
|
+
|
|
390
|
+
## Development
|
|
391
|
+
|
|
392
|
+
### Setup
|
|
393
|
+
|
|
394
|
+
```bash
|
|
395
|
+
# Clone repository
|
|
396
|
+
git clone https://github.com/Kotak-Neo/kotak-neo-python.git
|
|
397
|
+
cd kotak-neo-python
|
|
398
|
+
|
|
399
|
+
# Install dependencies
|
|
400
|
+
pip install -e ".[dev]"
|
|
401
|
+
|
|
402
|
+
# Setup pre-commit hooks
|
|
403
|
+
pre-commit install
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### Testing
|
|
407
|
+
|
|
408
|
+
```bash
|
|
409
|
+
# Run all tests
|
|
410
|
+
pytest
|
|
411
|
+
|
|
412
|
+
# Run with coverage
|
|
413
|
+
pytest --cov=neo_api_client --cov-report=html
|
|
414
|
+
|
|
415
|
+
# Run smoke tests (requires .env configuration)
|
|
416
|
+
python tests/e2e/smoke_test.py
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
> **SDK contributors:** the smoke/integration test runners can target an internal
|
|
420
|
+
> environment via the `NEO_ENVIRONMENT` variable. Copy `.env.dev.example` to `.env`
|
|
421
|
+
> for that setup. This is not needed by normal SDK users — the client always uses
|
|
422
|
+
> production by default.
|
|
423
|
+
|
|
424
|
+
### Code Quality
|
|
425
|
+
|
|
426
|
+
```bash
|
|
427
|
+
# Format code
|
|
428
|
+
ruff format .
|
|
429
|
+
|
|
430
|
+
# Lint code
|
|
431
|
+
ruff check .
|
|
432
|
+
|
|
433
|
+
# Type checking
|
|
434
|
+
mypy neo_api_client
|
|
435
|
+
|
|
436
|
+
# Security scan
|
|
437
|
+
bandit -r neo_api_client
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
## Requirements
|
|
441
|
+
|
|
442
|
+
- **Python**: 3.10 or higher
|
|
443
|
+
- **Core Dependencies**: numpy, pandas, PyJWT, httpx[http2], websocket-client, structlog, tenacity, python-decouple, pyotp, websockets, pydantic
|
|
444
|
+
|
|
445
|
+
See [pyproject.toml](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/pyproject.toml) for complete dependency list.
|
|
446
|
+
|
|
447
|
+
## Repository Structure
|
|
448
|
+
|
|
449
|
+
```
|
|
450
|
+
kotak-neo-python/
|
|
451
|
+
├── neo_api_client/ # Main package
|
|
452
|
+
│ ├── services/ # API service modules
|
|
453
|
+
│ ├── websocket/ # WebSocket implementation
|
|
454
|
+
│ ├── utils/ # Utility functions
|
|
455
|
+
│ ├── neo_api.py # Main NeoAPI class
|
|
456
|
+
│ ├── exceptions.py # Exception hierarchy
|
|
457
|
+
│ └── ... # Core modules
|
|
458
|
+
├── tests/ # Test suite
|
|
459
|
+
│ ├── unit/ # Unit tests
|
|
460
|
+
│ ├── integration/ # Integration tests
|
|
461
|
+
│ └── e2e/ # End-to-end tests
|
|
462
|
+
├── docs/ # Documentation
|
|
463
|
+
│ ├── functions/ # API function docs
|
|
464
|
+
│ └── installation/ # Installation guides
|
|
465
|
+
└── pyproject.toml # Project configuration
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
## Support
|
|
469
|
+
|
|
470
|
+
- **Documentation**: [GitHub Docs](https://github.com/Kotak-Neo/kotak-neo-python/tree/main/docs)
|
|
471
|
+
- **Issues**: [GitHub Issues](https://github.com/Kotak-Neo/kotak-neo-python/issues)
|
|
472
|
+
- **Email**: support@kotakneo.com
|
|
473
|
+
|
|
474
|
+
## Contributing
|
|
475
|
+
|
|
476
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
477
|
+
|
|
478
|
+
1. Fork the repository
|
|
479
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
480
|
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
|
481
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
482
|
+
5. Open a Pull Request
|
|
483
|
+
|
|
484
|
+
## License
|
|
485
|
+
|
|
486
|
+
MIT License - see [LICENSE](https://github.com/Kotak-Neo/kotak-neo-python/blob/main/LICENSE) file for details.
|
|
487
|
+
|
|
488
|
+
## Disclaimer
|
|
489
|
+
|
|
490
|
+
This is the official SDK for Kotak Neo Trading APIs. Trading in financial markets involves substantial risk. Users are responsible for their own trading decisions and should thoroughly test their strategies before live trading.
|
|
491
|
+
|
|
492
|
+
**⚠️ Risk Warning**: As per SEBI study, 9 out of 10 individual traders in equity F&O segment incur net losses. Please trade responsibly.
|
|
493
|
+
|
|
494
|
+
## Changelog
|
|
495
|
+
|
|
496
|
+
See [CHANGELOG.md](https://github.com/Kotak-Neo/kotak-neo-python/releases) for version history and updates.
|
|
497
|
+
|
|
498
|
+
---
|
|
499
|
+
|
|
500
|
+
**Version**: 2.3.0
|
|
501
|
+
**Status**: Production/Stable
|
|
502
|
+
**Built with ❤️ by Kotak Neo Team**
|