Skip to content

Commit 408c0e1

Browse files
Algo id changes (#115)
Update APIs to support optional algo_id parameter in order-related methods
1 parent b68e1a7 commit 408c0e1

File tree

19 files changed

+432
-808
lines changed

19 files changed

+432
-808
lines changed

.github/workflows/semgrep.yml

Lines changed: 0 additions & 40 deletions
This file was deleted.

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ coverage.xml
4747
.hypothesis/
4848
venv/
4949
myenv/
50+
venv_new/
5051
.python-version
5152

5253
# Translations
@@ -72,4 +73,7 @@ target/
7273
git_push.sh
7374

7475
#Intellij
75-
.idea/
76+
.idea/
77+
78+
#cursor
79+
.cursor/

README.md

Lines changed: 25 additions & 281 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.17.0
12+
- Package version: 2.18.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.
@@ -73,7 +73,31 @@ except ApiException as e:
7373
```
7474
To learn more about the sandbox environment and the available sandbox APIs, please visit the [Upstox API documentation - Sandbox](https://upstox.com/developer/api-documentation/sandbox).
7575

76+
## Algo ID Support
7677

78+
The SDK supports passing an algorithm ID for order tracking and management. When provided, the SDK will pass the algo ID as `X-Algo-Id` header.
79+
80+
```python
81+
import upstox_client
82+
from upstox_client.rest import ApiException
83+
84+
configuration = upstox_client.Configuration()
85+
configuration.access_token = 'ACCESS_TOKEN'
86+
87+
api_instance = upstox_client.OrderApiV3(upstox_client.ApiClient(configuration))
88+
body = upstox_client.PlaceOrderV3Request(quantity=1, product="D", validity="DAY", price=0,
89+
instrument_token="NSE_EQ|INE528G01035", order_type="MARKET",
90+
transaction_type="BUY", disclosed_quantity=0, trigger_price=0,
91+
is_amo=False, slice=True)
92+
93+
try:
94+
api_response = api_instance.place_order(body, algo_id="your-algo-id")
95+
print(api_response)
96+
except ApiException as e:
97+
print("Exception when calling OrderApiV3->place_order: %s\n" % e)
98+
```
99+
100+
Other order methods (modify, cancel, etc.) follow the same pattern by accepting an optional `algo_id` as a keyword parameter.
77101

78102
## Examples
79103

@@ -158,9 +182,6 @@ Both functions are designed to simplify the process of subscribing to essential
158182

159183
### MarketDataStreamer
160184

161-
<details>
162-
<summary style="cursor: pointer; font-size: 1.2em;">V3</summary>
163-
<p>
164185

165186
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:
166187

@@ -432,284 +453,7 @@ if __name__ == "__main__":
432453
```
433454

434455
<br/>
435-
</p>
436-
</details>
437-
438-
<details>
439-
<summary style="cursor: pointer; font-size: 1.2em;">V2</summary>
440-
<p>
441-
442-
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:
443-
444-
```python
445-
import upstox_client
446-
447-
def on_message(message):
448-
print(message)
449-
450-
451-
def main():
452-
configuration = upstox_client.Configuration()
453-
access_token = <ACCESS_TOKEN>
454-
configuration.access_token = access_token
455-
456-
streamer = upstox_client.MarketDataStreamer(
457-
upstox_client.ApiClient(configuration), ["NSE_INDEX|Nifty 50", "NSE_INDEX|Nifty Bank"], "full")
458-
459-
streamer.on("message", on_message)
460-
461-
streamer.connect()
462-
463-
464-
if __name__ == "__main__":
465-
main()
466-
```
467-
468-
In this example, you first authenticate using an access token, then instantiate MarketDataStreamer 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.
469-
470-
Feel free to adjust the access token placeholder and any other specifics to better fit your actual implementation or usage scenario.
471-
472-
### Exploring the MarketDataStreamer Functionality
473-
474-
#### Modes
475-
- **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.
476-
- **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.
477-
478-
#### Functions
479-
1. **constructor MarketDataStreamer(apiClient, instrumentKeys, mode)**: Initializes the streamer with optional instrument keys and mode (`full` or `ltpc`).
480-
2. **connect()**: Establishes the WebSocket connection.
481-
3. **subscribe(instrumentKeys, mode)**: Subscribes to updates for given instrument keys in the specified mode. Both parameters are mandatory.
482-
4. **unsubscribe(instrumentKeys)**: Stops updates for the specified instrument keys.
483-
5. **changeMode(instrumentKeys, mode)**: Switches the mode for already subscribed instrument keys.
484-
6. **disconnect()**: Ends the active WebSocket connection.
485-
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.
486-
487-
#### Events
488-
- **open**: Emitted upon successful connection establishment.
489-
- **close**: Indicates the WebSocket connection has been closed.
490-
- **message**: Delivers market updates.
491-
- **error**: Signals an error has occurred.
492-
- **reconnecting**: Announced when a reconnect attempt is initiated.
493-
- **autoReconnectStopped**: Informs when auto-reconnect efforts have ceased after exhausting the retry count.
494-
495-
The following documentation includes examples to illustrate the usage of these functions and events, providing a practical understanding of how to interact with the MarketDataStreamer effectively.
496-
497-
<br/>
498-
499-
1. Subscribing to Market Data on Connection Open with MarketDataStreamer
500-
501-
```python
502-
import upstox_client
503456

504-
def main():
505-
configuration = upstox_client.Configuration()
506-
access_token = <ACCESS_TOKEN>
507-
configuration.access_token = access_token
508-
509-
streamer = upstox_client.MarketDataStreamer(
510-
upstox_client.ApiClient(configuration))
511-
512-
def on_open():
513-
streamer.subscribe(
514-
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
515-
516-
def on_message(message):
517-
print(message)
518-
519-
streamer.on("open", on_open)
520-
streamer.on("message", on_message)
521-
522-
streamer.connect()
523-
524-
if __name__ == "__main__":
525-
main()
526-
```
527-
528-
<br/>
529-
530-
2. Subscribing to Instruments with Delays
531-
532-
```python
533-
import upstox_client
534-
import time
535-
536-
537-
def main():
538-
configuration = upstox_client.Configuration()
539-
access_token = <ACCESS_TOKEN>
540-
configuration.access_token = access_token
541-
542-
streamer = upstox_client.MarketDataStreamer(
543-
upstox_client.ApiClient(configuration))
544-
545-
def on_open():
546-
streamer.subscribe(
547-
["NSE_EQ|INE020B01018"], "full")
548-
549-
# Handle incoming market data messages\
550-
def on_message(message):
551-
print(message)
552-
553-
streamer.on("open", on_open)
554-
streamer.on("message", on_message)
555-
556-
streamer.connect()
557-
558-
time.sleep(5)
559-
streamer.subscribe(
560-
["NSE_EQ|INE467B01029"], "full")
561-
562-
563-
if __name__ == "__main__":
564-
main()
565-
566-
```
567-
568-
<br/>
569-
570-
3. Subscribing and Unsubscribing to Instruments
571-
572-
```python
573-
import upstox_client
574-
import time
575-
576-
577-
def main():
578-
configuration = upstox_client.Configuration()
579-
access_token = <ACCESS_TOKEN>
580-
configuration.access_token = access_token
581-
582-
streamer = upstox_client.MarketDataStreamer(
583-
upstox_client.ApiClient(configuration))
584-
585-
def on_open():
586-
print("Connected. Subscribing to instrument keys.")
587-
streamer.subscribe(
588-
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
589-
590-
# Handle incoming market data messages\
591-
def on_message(message):
592-
print(message)
593-
594-
streamer.on("open", on_open)
595-
streamer.on("message", on_message)
596-
597-
streamer.connect()
598-
599-
time.sleep(5)
600-
print("Unsubscribing from instrument keys.")
601-
streamer.unsubscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"])
602-
603-
604-
if __name__ == "__main__":
605-
main()
606-
```
607-
608-
<br/>
609-
610-
4. Subscribe, Change Mode and Unsubscribe
611-
612-
```python
613-
import upstox_client
614-
import time
615-
616-
def main():
617-
configuration = upstox_client.Configuration()
618-
access_token = <ACCESS_TOKEN>
619-
configuration.access_token = access_token
620-
621-
streamer = upstox_client.MarketDataStreamer(
622-
upstox_client.ApiClient(configuration))
623-
624-
def on_open():
625-
print("Connected. Subscribing to instrument keys.")
626-
streamer.subscribe(
627-
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "full")
628-
629-
# Handle incoming market data messages\
630-
def on_message(message):
631-
print(message)
632-
633-
streamer.on("open", on_open)
634-
streamer.on("message", on_message)
635-
636-
streamer.connect()
637-
638-
time.sleep(5)
639-
print("Changing subscription mode to ltpc...")
640-
streamer.change_mode(
641-
["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"], "ltpc")
642-
643-
time.sleep(5)
644-
print("Unsubscribing from instrument keys.")
645-
streamer.unsubscribe(["NSE_EQ|INE020B01018", "NSE_EQ|INE467B01029"])
646-
647-
648-
if __name__ == "__main__":
649-
main()
650-
```
651-
652-
<br/>
653-
654-
5. Disable Auto-Reconnect
655-
656-
```python
657-
import upstox_client
658-
import time
659-
660-
661-
def main():
662-
configuration = upstox_client.Configuration()
663-
access_token = <ACCESS_TOKEN>
664-
configuration.access_token = access_token
665-
666-
streamer = upstox_client.MarketDataStreamer(
667-
upstox_client.ApiClient(configuration))
668-
669-
def on_reconnection_halt(message):
670-
print(message)
671-
672-
streamer.on("autoReconnectStopped", on_reconnection_halt)
673-
674-
# Disable auto-reconnect feature
675-
streamer.auto_reconnect(False)
676-
677-
streamer.connect()
678-
679-
680-
if __name__ == "__main__":
681-
main()
682-
```
683-
684-
<br/>
685-
686-
6. Modify Auto-Reconnect parameters
687-
688-
```python
689-
import upstox_client
690-
691-
692-
def main():
693-
configuration = upstox_client.Configuration()
694-
access_token = <ACCESS_TOKEN>
695-
configuration.access_token = access_token
696-
697-
streamer = upstox_client.MarketDataStreamer(
698-
upstox_client.ApiClient(configuration))
699-
700-
# Modify auto-reconnect parameters: enable it, set interval to 10 seconds, and retry count to 3
701-
streamer.auto_reconnect(True, 10, 3)
702-
703-
streamer.connect()
704-
705-
706-
if __name__ == "__main__":
707-
main()
708-
```
709-
710-
<br/>
711-
</p>
712-
</details>
713457

714458
### PortfolioDataStreamer
715459

0 commit comments

Comments
 (0)