I have written a code in python to get option chain data through API, this code requires authorization code (manually i generate and update) which eventually create access token and gets the option chain data. I have put this in loop to run the code every 5 minutes automatically. however my authorization code gets expire after 3-4 minutes and my code break please help to resolve.
@Ajay_Rahangdale The authorization code is short-lived and can only be used once. However, you donât need to generate an access token every time. Once generated, the access token remains valid until 3:30 AM the next day, allowing you to use it throughout the day without needing to regenerate it.
Thanks Pradeep for your response, I did try that.
First I have got the access token with separate code (by using authorization code), then the same access code i have used in my main code. it still throw the same error -
401
Also, when i generate the access token with the help of authorization code it comes different everytime, is it expected?
Yes, the access token will be unique each time itâs generated. Could you please share your UCC so we can look into the 401 issue?
Hi Pradeep
Issue resolved and now access token is not expiring, in first attempt i had not got the correct access token. Thanks for your help.
@Pradeep_Jaiswar thanks for your help, i have one more query for you. I have now setup my system to fetch the live data however, this data is also late by a minute (approx), could you please suggest me any other way to get the live data faster than this.
@Ajay_Rahangdale, what API are you using to retrieve live data? Are you using web sockets?
https://api.upstox.com/v2/option/chain
This is the link I am using to fetch the live data now
It would be best if you used the websocket to collect real-time data. For WebSocket integration, please check out the following link: Market Data Feed | Upstox Developer API.
Additionally, we have provided pre-built SDKs for Python, Node.js, Java, and PHP:
@Ajay_Rahangdale I had faced a similar problem when fetching 1 min candles for equity stocks. I did some testing and research and discovered that, the data is delayed by 1 min 20 seconds when fetched from APIs.
Solution: I moved to websockets and the data is real-time. The journey to websockets can be a complex, compared to API calls, but it brings in great value if you are doing time sensitive trades.
Thanks Pradeep let me try this
@Amit_Kumar_Sharma Thanks much buddy, I will surely try this method to fetch data, appreciate your response with details
I am writing my code in visual studio and there was one of the installation i was trying to do for âUpstox_clientâ (pip upstox_client) and this is throwing error, is there any other name for this ?
From what you shared, it seems you are writing code in python. Here is the snapshot of my requirements.txt to get started
upstox-python-sdk
websockets
asyncio
protobuf
You can find more code here: Example Code | Upstox Developer API
Websocket sample code here (look at the bottom of the page) : Websocket Implementation | Upstox Developer API
kindly help I am stuck here
Protocol Buffers (Protobuf) Classes Generation
Generate the Protobuf classes in Python from .proto
file.
Before you can generate the Protobuf classes, you need to download the proto file and install the Protocol Buffers compiler (protoc).
To download the Protocol Buffers compiler, go to the Google Protocol Buffers GitHub repository 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.
For example, on a Unix-like system, you can add the directory to your PATH like this:
I have win32 system, downloaded the package and updated the path under environment variable as well, still it is not working
Hey @Ajay_Rahangdale
You can download the compiled protobuf python file (MarketDataFeed_pb2.py) from here: public-share/upstox-protobuf at main ¡ sharmaak/public-share ¡ GitHub . The corrsponding upstox src (MarketDataFeed.proto) is shared there as well.
This should get you unblocked while you figure out how to make it work on windows.
IMHO, python works best, with minimal effort, on Linux. I use Ubuntu machines for all my algo trading and analysis tooling. I tried working with Python3 on Windows 11, but out of sheer frustration ended up moving to Ubuntu within a few weeks. For coding I use PyCharm Community edition. Now my trading / dev laptop is a dual boot - with Windows on internal SSD and Linux running out of external SSD (sharing a pic of my hacky laptop for fun )
Disclaimer
The code and artifacts provided are for informational and educational purposes only. They are shared as-is, without any warranties or guarantees of accuracy, reliability, or suitability for any specific purpose. The author of this code assumes no responsibility or liability for any financial losses, damages, or legal actions that may arise from the use, interpretation, or implementation of this code or related resources.By using or relying on the provided code, you acknowledge that you are doing so at your own risk. Any decisions based on the outputs or recommendations of the code are solely your own responsibility, and you agree to indemnify and hold the author harmless from any consequences, financial or otherwise, resulting from its usage.
This disclaimer extends to any and all financial losses, missed opportunities, or unfavorable outcomes. If you have any concerns regarding the applicability of the code to your financial decisions, please consult with a qualified financial professional before proceeding.
Thanks Amit for suggestion i will surely elevate my setup from windows to Ubantu but for now i have resolved the error in windows and able to fetch the data. however, I am still looking how to fetch Option data, currently written the code to get OHLC data only.
I build my strategies around open interest data and few option greeks as well, kindly let me know if someone have idea on how to fetch option data through websocket API
sir mujhe nifty 50 ke all stock ke 5 mint time frem par
jab bhi koi stock high ya low breck kare to mujhe autometic mujhe alter ma msg aaye email par ya mobile par nitification mile aaisa ho sakta hai kya
ki is stock mai is time first candle ke high ko brek kiya hai ya low ko brek kiya hai