|
| 1 | +# Market Stream feed websocket client |
| 2 | + |
| 3 | +This Python project demonstrates how to connect to the Upstox Websocket API for streaming live market data. It fetches market data for a list of instrument keys and decodes the incoming protobuf data to a JSON format. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +These instructions will help you run the sample websocket client. |
| 8 | + |
| 9 | +### Prerequisites |
| 10 | + |
| 11 | +Before you can run this script, you need to have Python 3.8 or later installed on your system. If you haven't installed Python yet, you can download it from the official website: |
| 12 | + |
| 13 | +[Download Python](https://www.python.org/downloads/) |
| 14 | + |
| 15 | +You will also need to install several Python packages: |
| 16 | + |
| 17 | +- `upstox-python-sdk` |
| 18 | +- `websockets` |
| 19 | +- `asyncio` |
| 20 | +- `protobuf` |
| 21 | + |
| 22 | +You can install these packages using pip, a package manager for Python. Open a terminal and enter the following command: |
| 23 | + |
| 24 | +```sh |
| 25 | +pip install upstox-python-sdk websockets asyncio protobuf |
| 26 | +``` |
| 27 | + |
| 28 | +### Protocol Buffers (Protobuf) Classes Generation |
| 29 | + |
| 30 | +Generate the Protobuf classes in Python from `.proto` file. |
| 31 | + |
| 32 | +Before you can generate the Protobuf classes, you need to download the [proto file](https://assets.upstox.com/feed/market-data-feed/v1/MarketDataFeed.proto) and install the Protocol Buffers compiler (protoc). |
| 33 | + |
| 34 | +To download the Protocol Buffers compiler, go to the [Google Protocol Buffers GitHub repository](https://github.com/protocolbuffers/protobuf/releases) and download the appropriate `protoc-<version>-<os>.zip` file for your operating system. Extract the ZIP file and add the `bin` directory to your system PATH. |
| 35 | + |
| 36 | +For example, on a Unix-like system, you can add the directory to your PATH like this: |
| 37 | + |
| 38 | +```bash |
| 39 | +export PATH=$PATH:/path/to/protoc/bin |
| 40 | +``` |
| 41 | + |
| 42 | +You can confirm that the compiler is correctly installed by opening a new terminal window and running the following command: |
| 43 | + |
| 44 | +``` |
| 45 | +protoc --version |
| 46 | +``` |
| 47 | + |
| 48 | +This should print the protoc version. |
| 49 | + |
| 50 | +#### Generate Protobuf classes |
| 51 | + |
| 52 | +Navigate to the directory containing your .proto files and run the following command: |
| 53 | + |
| 54 | +``` |
| 55 | +protoc --python_out=. *.proto |
| 56 | +``` |
| 57 | + |
| 58 | +This will generate .py files for each .proto file in the directory. |
| 59 | + |
| 60 | +In your Python code, you can now import the generated classes like any other Python module. For example, if you have a file MarketDataFeed.proto and you've generated MarketDataFeed_pb2.py, you can import it like this: |
| 61 | + |
| 62 | +``` |
| 63 | +import MarketDataFeed_pb2 as pb |
| 64 | +``` |
| 65 | + |
| 66 | +Sample class (MarketDataFeed_pb2.py) included as part of this repo. |
| 67 | + |
| 68 | +### Configuration |
| 69 | + |
| 70 | +The script requires an Upstox API access token for authorization. You will need to specify your Upstox API access token in the Python script. Look for the line below and replace 'ACCESS_TOKEN' with your actual access token. |
| 71 | + |
| 72 | +``` |
| 73 | +configuration.access_token = 'ACCESS_TOKEN' |
| 74 | +``` |
| 75 | + |
| 76 | +### Running the Script |
| 77 | + |
| 78 | +After installing the prerequisites and setting up your access token, you can run the script. Navigate to the directory containing the script and run the following command: |
| 79 | + |
| 80 | +``` |
| 81 | +python3 websocket_client.py |
| 82 | +``` |
| 83 | + |
| 84 | +Replace websocket_client.py with the name of your Python script. |
| 85 | + |
| 86 | +## Understanding the Code |
| 87 | + |
| 88 | +The script first sets up an SSL context and an OAuth2 access token for authorization. It fetches the authorized redirect URI from the Upstox server and uses this to establish a connection to the Websocket server. |
| 89 | + |
| 90 | +The script sends a subscription request for "NSE_INDEX|Nifty Bank" and "NSE_INDEX|Nifty 50". When it receives data from the server, it decodes the protobuf data into a FeedResponse object, converts this object into a dictionary, and then prints the dictionary. |
| 91 | + |
| 92 | +## Support |
| 93 | + |
| 94 | +If you encounter any problems or have any questions about this project, feel free to open an issue in this repository. |
| 95 | + |
| 96 | +## Disclaimer |
| 97 | + |
| 98 | +This is a sample script meant for educational purposes. It may require modifications to work with your specific requirements. |
| 99 | + |
| 100 | +Please replace `'ACCESS_TOKEN'` with your actual access token and `websocket_client.py` with the name of your Python script. Modify any other details as needed to fit your project. |
| 101 | + |
| 102 | + |
0 commit comments