Skip to content

Commit ec69662

Browse files
committed
updated readme file
1 parent 14971e4 commit ec69662

File tree

2 files changed

+285
-25
lines changed

2 files changed

+285
-25
lines changed

README.md

Lines changed: 284 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,287 @@ 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+
- **full_d30**: full_d30 includes Full mode data plus 30 market level quotes.
161+
- **option_greeks**: Contains only option greeks.
162+
163+
#### Functions
164+
1. **constructor MarketDataStreamerV3(apiClient, instrumentKeys, mode)**: Initializes the streamer with optional instrument keys and mode (`full`, `ltpc`, `option_greeks` or `full_d30`).
165+
2. **connect()**: Establishes the WebSocket connection.
166+
3. **subscribe(instrumentKeys, mode)**: Subscribes to updates for given instrument keys in the specified mode. Both parameters are mandatory.
167+
4. **unsubscribe(instrumentKeys)**: Stops updates for the specified instrument keys.
168+
5. **changeMode(instrumentKeys, mode)**: Switches the mode for already subscribed instrument keys.
169+
6. **disconnect()**: Ends the active WebSocket connection.
170+
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.
171+
172+
#### Events
173+
- **open**: Emitted upon successful connection establishment.
174+
- **close**: Indicates the WebSocket connection has been closed.
175+
- **message**: Delivers market updates.
176+
- **error**: Signals an error has occurred.
177+
- **reconnecting**: Announced when a reconnect attempt is initiated.
178+
- **autoReconnectStopped**: Informs when auto-reconnect efforts have ceased after exhausting the retry count.
179+
180+
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.
181+
182+
<br/>
183+
184+
1. Subscribing to Market Data on Connection Open with MarketDataStreamerV3
185+
186+
```python
187+
import upstox_client
188+
189+
def main():
190+
configuration = upstox_client.Configuration()
191+
access_token = <ACCESS_TOKEN>
192+
configuration.access_token = access_token
193+
194+
streamer = upstox_client.MarketDataStreamerV3(
195+
upstox_client.ApiClient(configuration))
196+
197+
def on_open():
198+
streamer.subscribe(
199+
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
200+
201+
def on_message(message):
202+
print(message)
203+
204+
streamer.on("open", on_open)
205+
streamer.on("message", on_message)
206+
207+
streamer.connect()
208+
209+
if __name__ == "__main__":
210+
main()
211+
```
212+
213+
<br/>
214+
215+
2. Subscribing to Instruments with Delays
216+
217+
```python
218+
import upstox_client
219+
import time
220+
221+
222+
def main():
223+
configuration = upstox_client.Configuration()
224+
access_token = <ACCESS_TOKEN>
225+
configuration.access_token = access_token
226+
227+
streamer = upstox_client.MarketDataStreamerV3(
228+
upstox_client.ApiClient(configuration))
229+
230+
def on_open():
231+
streamer.subscribe(
232+
["NSE_EQ|INE020B01018"], "full")
233+
234+
# Handle incoming market data messages\
235+
def on_message(message):
236+
print(message)
237+
238+
streamer.on("open", on_open)
239+
streamer.on("message", on_message)
240+
241+
streamer.connect()
242+
243+
time.sleep(5)
244+
streamer.subscribe(
245+
["NSE_EQ|INE467B01029"], "full")
246+
247+
248+
if __name__ == "__main__":
249+
main()
250+
251+
```
252+
253+
<br/>
254+
255+
3. Subscribing and Unsubscribing to Instruments
256+
257+
```python
258+
import upstox_client
259+
import time
260+
261+
262+
def main():
263+
configuration = upstox_client.Configuration()
264+
access_token = <ACCESS_TOKEN>
265+
configuration.access_token = access_token
266+
267+
streamer = upstox_client.MarketDataStreamerV3(
268+
upstox_client.ApiClient(configuration))
269+
270+
def on_open():
271+
print("Connected. Subscribing to instrument keys.")
272+
streamer.subscribe(
273+
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
274+
275+
# Handle incoming market data messages\
276+
def on_message(message):
277+
print(message)
278+
279+
streamer.on("open", on_open)
280+
streamer.on("message", on_message)
281+
282+
streamer.connect()
283+
284+
time.sleep(5)
285+
print("Unsubscribing from instrument keys.")
286+
streamer.unsubscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"])
287+
288+
289+
if __name__ == "__main__":
290+
main()
291+
```
292+
293+
<br/>
294+
295+
4. Subscribe, Change Mode and Unsubscribe
296+
297+
```python
298+
import upstox_client
299+
import time
300+
301+
def main():
302+
configuration = upstox_client.Configuration()
303+
access_token = <ACCESS_TOKEN>
304+
configuration.access_token = access_token
305+
306+
streamer = upstox_client.MarketDataStreamerV3(
307+
upstox_client.ApiClient(configuration))
308+
309+
def on_open():
310+
print("Connected. Subscribing to instrument keys.")
311+
streamer.subscribe(
312+
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
313+
314+
# Handle incoming market data messages\
315+
def on_message(message):
316+
print(message)
317+
318+
streamer.on("open", on_open)
319+
streamer.on("message", on_message)
320+
321+
streamer.connect()
322+
323+
time.sleep(5)
324+
print("Changing subscription mode to ltpc...")
325+
streamer.change_mode(
326+
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "ltpc")
327+
328+
time.sleep(5)
329+
print("Unsubscribing from instrument keys.")
330+
streamer.unsubscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"])
331+
332+
333+
if __name__ == "__main__":
334+
main()
335+
```
336+
337+
<br/>
338+
339+
5. Disable Auto-Reconnect
340+
341+
```python
342+
import upstox_client
343+
import time
344+
345+
346+
def main():
347+
configuration = upstox_client.Configuration()
348+
access_token = <ACCESS_TOKEN>
349+
configuration.access_token = access_token
350+
351+
streamer = upstox_client.MarketDataStreamerV3(
352+
upstox_client.ApiClient(configuration))
353+
354+
def on_reconnection_halt(message):
355+
print(message)
356+
357+
streamer.on("autoReconnectStopped", on_reconnection_halt)
358+
359+
# Disable auto-reconnect feature
360+
streamer.auto_reconnect(False)
361+
362+
streamer.connect()
363+
364+
365+
if __name__ == "__main__":
366+
main()
367+
```
368+
369+
<br/>
370+
371+
6. Modify Auto-Reconnect parameters
372+
373+
```python
374+
import upstox_client
375+
376+
377+
def main():
378+
configuration = upstox_client.Configuration()
379+
access_token = <ACCESS_TOKEN>
380+
configuration.access_token = access_token
381+
382+
streamer = upstox_client.MarketDataStreamerV3(
383+
upstox_client.ApiClient(configuration))
384+
385+
# Modify auto-reconnect parameters: enable it, set interval to 10 seconds, and retry count to 3
386+
streamer.auto_reconnect(True, 10, 3)
387+
388+
streamer.connect()
389+
390+
391+
if __name__ == "__main__":
392+
main()
393+
```
394+
395+
<br/>
396+
</p>
397+
</details>
398+
399+
<details>
400+
<summary style="cursor: pointer; font-size: 1.2em;">V2</summary>
401+
<p>
402+
122403
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:
123404

124405
```python
@@ -388,6 +669,8 @@ if __name__ == "__main__":
388669
```
389670

390671
<br/>
672+
</p>
673+
</details>
391674

392675
### PortfolioDataStreamer
393676

@@ -451,8 +734,6 @@ if __name__ == "__main__":
451734

452735
<br/>
453736

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-
456737
### Exploring the PortfolioDataStreamer Functionality
457738

458739
#### Functions
@@ -569,3 +850,4 @@ This example demonstrates initializing the PortfolioDataStreamer, connecting it
569850
- [UserFundMarginData](docs/UserFundMarginData.md)
570851
- [WebsocketAuthRedirectResponse](docs/WebsocketAuthRedirectResponse.md)
571852
- [WebsocketAuthRedirectResponseData](docs/WebsocketAuthRedirectResponseData.md)
853+

test/sdk_tests/market_basic_v3.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import time
2-
31
import upstox_client
42
import data_token
53

64
configuration = upstox_client.Configuration()
75
configuration.access_token = data_token.access_token
86
streamer = upstox_client.MarketDataStreamerV3(
9-
upstox_client.ApiClient(configuration), instrumentKeys=["NSE_FO|53023", "NSE_EQ|INE002A01018"], mode="ltpc")
7+
upstox_client.ApiClient(configuration), instrumentKeys=["NSE_EQ|INE528G01035"], mode="full")
108

119
streamer.auto_reconnect(True, 5, 10)
1210

@@ -37,23 +35,3 @@ def reconnecting(data):
3735
streamer.on("reconnecting", reconnecting)
3836
streamer.on("error", error)
3937
streamer.connect()
40-
41-
42-
time.sleep(10)
43-
print("reliance in full")
44-
streamer.change_mode(["NSE_EQ|INE002A01018"], "ful")
45-
46-
time.sleep(10)
47-
print("disconnecting")
48-
streamer.disconnect()
49-
50-
51-
time.sleep(10)
52-
print("change mode of fo to d30")
53-
streamer.change_mode(["NSE_FO|53023"], "full_d30")
54-
55-
56-
57-
time.sleep(10)
58-
streamer.disconnect()
59-

0 commit comments

Comments
 (0)