Skip to content

Commit 1ab5c44

Browse files
authored
Merge pull request #85 from upstox/v3-websocket
init commit for v3 websocket
2 parents 0d68f01 + 856c43c commit 1ab5c44

File tree

12 files changed

+729
-7
lines changed

12 files changed

+729
-7
lines changed

README.md

Lines changed: 284 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The official Python client for communicating with the <a href="https://upstox.co
99
Upstox API is a set of rest APIs that provide data required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (using Websocket), and more, with the easy to understand API collection.
1010

1111
- API version: v2
12-
- Package version: 2.9.0
12+
- Package version: 2.10.0
1313
- Build package: io.swagger.codegen.v3.generators.python.PythonClientCodegen
1414

1515
This Python package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project.
@@ -119,6 +119,286 @@ Both functions are designed to simplify the process of subscribing to essential
119119

120120
### MarketDataStreamer
121121

122+
<details>
123+
<summary style="cursor: pointer; font-size: 1.2em;">V3</summary>
124+
<p>
125+
126+
The `MarketDataStreamerV3` interface is designed for effortless connection to the market WebSocket, enabling users to receive instantaneous updates on various instruments. The following example demonstrates how to quickly set up and start receiving market updates for selected instrument keys:
127+
128+
```python
129+
import upstox_client
130+
131+
def on_message(message):
132+
print(message)
133+
134+
135+
def main():
136+
configuration = upstox_client.Configuration()
137+
access_token = <ACCESS_TOKEN>
138+
configuration.access_token = access_token
139+
140+
streamer = upstox_client.MarketDataStreamerV3(
141+
upstox_client.ApiClient(configuration), ["NSE_INDEX|Nifty 50", "NSE_INDEX|Nifty Bank"], "full")
142+
143+
streamer.on("message", on_message)
144+
145+
streamer.connect()
146+
147+
148+
if __name__ == "__main__":
149+
main()
150+
```
151+
In this example, you first authenticate using an access token, then instantiate MarketDataStreamerV3 with specific instrument keys and a subscription mode. Upon connecting, the streamer listens for market updates, which are logged to the console as they arrive.
152+
153+
Feel free to adjust the access token placeholder and any other specifics to better fit your actual implementation or usage scenario.
154+
155+
### Exploring the MarketDataStreamerV3 Functionality
156+
157+
#### Modes
158+
- **ltpc**: ltpc provides information solely about the most recent trade, encompassing details such as the last trade price, time of the last trade, quantity traded, and the closing price from the previous day.
159+
- **full**: The full option offers comprehensive information, including the latest trade prices, D5 depth, 1-minute, 30-minute, and daily candlestick data, along with some additional details.
160+
- **option_greeks**: Contains only option greeks.
161+
162+
#### Functions
163+
1. **constructor MarketDataStreamerV3(apiClient, instrumentKeys, mode)**: Initializes the streamer with optional instrument keys and mode (`full`, `ltpc` or `option_greeks`).
164+
2. **connect()**: Establishes the WebSocket connection.
165+
3. **subscribe(instrumentKeys, mode)**: Subscribes to updates for given instrument keys in the specified mode. Both parameters are mandatory.
166+
4. **unsubscribe(instrumentKeys)**: Stops updates for the specified instrument keys.
167+
5. **changeMode(instrumentKeys, mode)**: Switches the mode for already subscribed instrument keys.
168+
6. **disconnect()**: Ends the active WebSocket connection.
169+
7. **auto_reconnect(enable, interval, retryCount)**: Customizes auto-reconnect functionality. Parameters include a flag to enable/disable it, the interval(in seconds) between attempts, and the maximum number of retries.
170+
171+
#### Events
172+
- **open**: Emitted upon successful connection establishment.
173+
- **close**: Indicates the WebSocket connection has been closed.
174+
- **message**: Delivers market updates.
175+
- **error**: Signals an error has occurred.
176+
- **reconnecting**: Announced when a reconnect attempt is initiated.
177+
- **autoReconnectStopped**: Informs when auto-reconnect efforts have ceased after exhausting the retry count.
178+
179+
The following documentation includes examples to illustrate the usage of these functions and events, providing a practical understanding of how to interact with the MarketDataStreamerV3 effectively.
180+
181+
<br/>
182+
183+
1. Subscribing to Market Data on Connection Open with MarketDataStreamerV3
184+
185+
```python
186+
import upstox_client
187+
188+
def main():
189+
configuration = upstox_client.Configuration()
190+
access_token = <ACCESS_TOKEN>
191+
configuration.access_token = access_token
192+
193+
streamer = upstox_client.MarketDataStreamerV3(
194+
upstox_client.ApiClient(configuration))
195+
196+
def on_open():
197+
streamer.subscribe(
198+
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
199+
200+
def on_message(message):
201+
print(message)
202+
203+
streamer.on("open", on_open)
204+
streamer.on("message", on_message)
205+
206+
streamer.connect()
207+
208+
if __name__ == "__main__":
209+
main()
210+
```
211+
212+
<br/>
213+
214+
2. Subscribing to Instruments with Delays
215+
216+
```python
217+
import upstox_client
218+
import time
219+
220+
221+
def main():
222+
configuration = upstox_client.Configuration()
223+
access_token = <ACCESS_TOKEN>
224+
configuration.access_token = access_token
225+
226+
streamer = upstox_client.MarketDataStreamerV3(
227+
upstox_client.ApiClient(configuration))
228+
229+
def on_open():
230+
streamer.subscribe(
231+
["NSE_EQ|INE020B01018"], "full")
232+
233+
# Handle incoming market data messages\
234+
def on_message(message):
235+
print(message)
236+
237+
streamer.on("open", on_open)
238+
streamer.on("message", on_message)
239+
240+
streamer.connect()
241+
242+
time.sleep(5)
243+
streamer.subscribe(
244+
["NSE_EQ|INE467B01029"], "full")
245+
246+
247+
if __name__ == "__main__":
248+
main()
249+
250+
```
251+
252+
<br/>
253+
254+
3. Subscribing and Unsubscribing to Instruments
255+
256+
```python
257+
import upstox_client
258+
import time
259+
260+
261+
def main():
262+
configuration = upstox_client.Configuration()
263+
access_token = <ACCESS_TOKEN>
264+
configuration.access_token = access_token
265+
266+
streamer = upstox_client.MarketDataStreamerV3(
267+
upstox_client.ApiClient(configuration))
268+
269+
def on_open():
270+
print("Connected. Subscribing to instrument keys.")
271+
streamer.subscribe(
272+
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
273+
274+
# Handle incoming market data messages\
275+
def on_message(message):
276+
print(message)
277+
278+
streamer.on("open", on_open)
279+
streamer.on("message", on_message)
280+
281+
streamer.connect()
282+
283+
time.sleep(5)
284+
print("Unsubscribing from instrument keys.")
285+
streamer.unsubscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"])
286+
287+
288+
if __name__ == "__main__":
289+
main()
290+
```
291+
292+
<br/>
293+
294+
4. Subscribe, Change Mode and Unsubscribe
295+
296+
```python
297+
import upstox_client
298+
import time
299+
300+
def main():
301+
configuration = upstox_client.Configuration()
302+
access_token = <ACCESS_TOKEN>
303+
configuration.access_token = access_token
304+
305+
streamer = upstox_client.MarketDataStreamerV3(
306+
upstox_client.ApiClient(configuration))
307+
308+
def on_open():
309+
print("Connected. Subscribing to instrument keys.")
310+
streamer.subscribe(
311+
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
312+
313+
# Handle incoming market data messages\
314+
def on_message(message):
315+
print(message)
316+
317+
streamer.on("open", on_open)
318+
streamer.on("message", on_message)
319+
320+
streamer.connect()
321+
322+
time.sleep(5)
323+
print("Changing subscription mode to ltpc...")
324+
streamer.change_mode(
325+
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "ltpc")
326+
327+
time.sleep(5)
328+
print("Unsubscribing from instrument keys.")
329+
streamer.unsubscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"])
330+
331+
332+
if __name__ == "__main__":
333+
main()
334+
```
335+
336+
<br/>
337+
338+
5. Disable Auto-Reconnect
339+
340+
```python
341+
import upstox_client
342+
import time
343+
344+
345+
def main():
346+
configuration = upstox_client.Configuration()
347+
access_token = <ACCESS_TOKEN>
348+
configuration.access_token = access_token
349+
350+
streamer = upstox_client.MarketDataStreamerV3(
351+
upstox_client.ApiClient(configuration))
352+
353+
def on_reconnection_halt(message):
354+
print(message)
355+
356+
streamer.on("autoReconnectStopped", on_reconnection_halt)
357+
358+
# Disable auto-reconnect feature
359+
streamer.auto_reconnect(False)
360+
361+
streamer.connect()
362+
363+
364+
if __name__ == "__main__":
365+
main()
366+
```
367+
368+
<br/>
369+
370+
6. Modify Auto-Reconnect parameters
371+
372+
```python
373+
import upstox_client
374+
375+
376+
def main():
377+
configuration = upstox_client.Configuration()
378+
access_token = <ACCESS_TOKEN>
379+
configuration.access_token = access_token
380+
381+
streamer = upstox_client.MarketDataStreamerV3(
382+
upstox_client.ApiClient(configuration))
383+
384+
# Modify auto-reconnect parameters: enable it, set interval to 10 seconds, and retry count to 3
385+
streamer.auto_reconnect(True, 10, 3)
386+
387+
streamer.connect()
388+
389+
390+
if __name__ == "__main__":
391+
main()
392+
```
393+
394+
<br/>
395+
</p>
396+
</details>
397+
398+
<details>
399+
<summary style="cursor: pointer; font-size: 1.2em;">V2</summary>
400+
<p>
401+
122402
The `MarketDataStreamer` interface is designed for effortless connection to the market WebSocket, enabling users to receive instantaneous updates on various instruments. The following example demonstrates how to quickly set up and start receiving market updates for selected instrument keys:
123403

124404
```python
@@ -388,6 +668,8 @@ if __name__ == "__main__":
388668
```
389669

390670
<br/>
671+
</p>
672+
</details>
391673

392674
### PortfolioDataStreamer
393675

@@ -451,8 +733,6 @@ if __name__ == "__main__":
451733

452734
<br/>
453735

454-
This example demonstrates initializing the PortfolioDataStreamer, connecting it to the WebSocket, and setting up an event listener to receive and print order updates. Replace <ACCESS_TOKEN> with your valid access token to authenticate the session.
455-
456736
### Exploring the PortfolioDataStreamer Functionality
457737

458738
#### Functions
@@ -569,3 +849,4 @@ This example demonstrates initializing the PortfolioDataStreamer, connecting it
569849
- [UserFundMarginData](docs/UserFundMarginData.md)
570850
- [WebsocketAuthRedirectResponse](docs/WebsocketAuthRedirectResponse.md)
571851
- [WebsocketAuthRedirectResponseData](docs/WebsocketAuthRedirectResponseData.md)
852+

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
long_description = (this_directory / "README.md").read_text()
1919

2020
NAME = "upstox-python-sdk"
21-
VERSION = "2.9.0"
21+
VERSION = "2.10.0"
2222
# To install the library, run the following
2323
#
2424
# python setup.py install

test/sdk_tests/data_token.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
access_token = ""
1+
access_token = ""
2+
3+
4+
sample_instrument_key = [
5+
"NSE_EQ|INE528G01035", "NSE_EQ|INE399C01030", "BSE_EQ|INE055E01034", "NCD_FO|14245", "NSE_EQ|INE169A01031",
6+
"NSE_EQ|INE437A01024", "NSE_EQ|INE044A01036", "NSE_EQ|INE868B01028", "NSE_EQ|INE202E08219", "NSE_EQ|INE044A01036",
7+
"NSE_EQ|INE257A01026", "NSE_EQ|INE155A01022", "BSE_EQ|INE040A08575", "BSE_EQ|INE040A08567", "NSE_EQ|INE084A01016",
8+
"NSE_EQ|INE121A01024", "NSE_EQ|INE0OGZ15015", "BSE_EQ|INE0OGZ15015", "NSE_EQ|INE364U01010", "NSE_EQ|INE765G01017",
9+
"NSE_EQ|INE239A01024", "BSE_EQ|INE721A07SI5", "NSE_EQ|INE832A01018", "NSE_EQ|INE127D01025", "BCD_FO|2119087",
10+
"BSE_EQ|IN2920210480", "NSE_EQ|INE726G01019", "NSE_FO|114127", "NSE_EQ|INE340Z01019", "NSE_FO|114126",
11+
"NSE_FO|114125", "NSE_FO|114128", "NSE_EQ|IN2120230163", "NSE_FO|38516", "NSE_FO|114133", "NSE_FO|114132",
12+
"BSE_EQ|INE338I14GR3", "BSE_EQ|INE360L01017", "MCX_FO|431658", "MCX_FO|431657", "BSE_EQ|INE896W08020",
13+
"MCX_FO|431656", "BSE_EQ|INE214D01021", "NSE_EQ|IN4920240186", "NSE_EQ|INF789F01GW9", "NSE_EQ|INE0CJF01024",
14+
"NSE_EQ|IN2120230189", "BSE_EQ|INE896W08012", "MCX_FO|431668", "MCX_FO|431667", "MCX_FO|431666",
15+
"BSE_EQ|IN001129C021", "NSE_EQ|IN4920240194", "NSE_EQ|INE389Z07054", "BSE_EQ|INE748A01016", "BSE_EQ|IN000942C036",
16+
"BSE_EQ|INE148I07SM6", "NSE_EQ|INF789F01GV1", "NSE_EQ|INE128A01029", "NSE_EQ|IN2120230197", "NSE_FO|128727",
17+
"NSE_FO|128726", "NSE_FO|128729", "NSE_FO|128728", "BSE_EQ|INE03W107090", "BSE_EQ|INE148I07RY3",
18+
"BSE_EQ|INE651J07689", "BSE_FO|1172370", "BSE_FO|1172372", "BSE_FO|1172375", "NSE_EQ|IN2220230022",
19+
"NSE_EQ|INE508G01029", "NSE_FO|114077", "NSE_FO|114078", "NSE_FO|114079", "NSE_FO|114080", "NSE_FO|114081",
20+
"NSE_FO|38584", "NSE_FO|38585", "NSE_FO|38591", "NSE_FO|38592", "NSE_FO|38599"
21+
]

test/sdk_tests/market_basic_v3.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import upstox_client
2+
import data_token
3+
4+
configuration = upstox_client.Configuration()
5+
configuration.access_token = data_token.access_token
6+
instruments = data_token.sample_instrument_key
7+
streamer = upstox_client.MarketDataStreamerV3(
8+
upstox_client.ApiClient(configuration), instrumentKeys=instruments[0:70], mode="option_greeks")
9+
10+
streamer.auto_reconnect(True, 5, 10)
11+
12+
13+
def on_open():
14+
print("on open message")
15+
16+
17+
def close(a, b):
18+
print(f"on close message {a}")
19+
20+
21+
def message(data):
22+
print(f"on message message{data}")
23+
24+
25+
def error(er):
26+
print(f"on error message= {er}")
27+
28+
29+
def reconnecting(data):
30+
print(f"reconnecting event= {data}")
31+
32+
33+
streamer.on("open", on_open)
34+
streamer.on("message", message)
35+
streamer.on("close", close)
36+
streamer.on("reconnecting", reconnecting)
37+
streamer.on("error", error)
38+
streamer.connect()

upstox_client/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from upstox_client.api.websocket_api import WebsocketApi
3030
# import websocket interfaces into sdk package
3131
from upstox_client.feeder.market_data_streamer import MarketDataStreamer
32+
from upstox_client.feeder.market_data_streamer_v3 import MarketDataStreamerV3
3233
from upstox_client.feeder.portfolio_data_streamer import PortfolioDataStreamer
3334
# import ApiClient
3435
from upstox_client.api_client import ApiClient

0 commit comments

Comments
 (0)