How to Fetch Real-Time Stock Data from Paytm Money in Python

Do you know Paytm Money has a trading API for advanced traders who want to use custom algo-trading. To implement custom algorithm for trading, first we need to learn how to fetch real time data of a particular stock or symbol. In this article I will guide you how to fetch live stock data from Paytm Money API using python. At the end of this article you will be able to fetch live data for any stock from Paytm Money. And You will have a python script which will be very useful for you, and can be imported in other scripts.

What is Paytm Money?

Paytm Money is an online discount broker. It does not offer any tips or recommendations on stock trading. Paytm Money is SEBI registered Investment advisor, and offer advisory services in the form of investment packs. Paytm Money has informative blogs and announcements related to personal finance, capital markets, the latest feature updates, a product launch that create investor awareness and help them make informed and better decisions on their investments. Recently Paytm Money has launched its API for advanced traders. With this API we can fetch data from Paytm Money or place order as well.

What is real-time stock data and why do we care?

The “real-time stock data” is insightful stock market data generated from every transaction in Stock Market Exchanges. Real-time market data consists of latest bid & ask, last trade, volume, and overall order flow between active buyers and sellers. To implement custom strategy, first requirement is to have stock’s latest data in hand. With this data we can understand the flow of the market. And analyze the right time to enter into trade. So, it is extremally important to understand how to get real-time date before proceeding to implement any strategy.

Prerequisite

There is a bug in official Paytm Money API in function get_live_market_data. I have informed them for the same. Meanwhile you can use the same Python Package from my GitHub profile pyPMClient.

Python Code to fetch Real-Time Data from Paytm Money API

Python Code Explanation

  • [Line #1] First we are importing our authentication script and using refresh_token method and pm object for this script.
  • [Line #2] To manage tabular data we are using pandas library.
  • [Line #4] For this script we are creating a class module named StockData
  • [Line #5-9] We need a holder for our security master and also we are checking for valid access token here in this __init__ function.
  • [Line #11-12] We need Paytm Money security master to fetch security_id for our symbol and we are creating a table for this here.
  • [Line #14] This is the function we want most, it will fetch live data from Paytm Money, and we can set symbol & exchange in this function.
    • [Line #15-17] Whenever we run this function, first we check if we have a valid access token or not.
    • [Line #19-20] First time when we run this function, it will have to fetch security_master.csv from Paytm Money.
    • [Line #22] We are searching for our symbol in security master table for security_id.
    • [Line #24-25] Normally security master will have at most 2 rows for a symbol, one for NSE and another for BSE. If more than one row found in the table filter it with exchange.
    • [Line #27-29] If we don’t have any row in security master table for the symbol return None and do nothing.
    • [Line #31-33] Now extract the script_id/security_id for our symbol and exchange. Then send request to Paytm Money using get_live_market_data to fetch our live data then return. Here in line #33 first parameter can be ‘LTP’, ‘FULL’, ‘QUOTE’ and second parameter is a list of string.
  • [Line #36-40] Finally we can test our script and fetch real-time stock data from Paytm Money for any symbol from any exchange.

Conclusion

Finally if you have reached the end of this post (How to Fetch Real-Time Stock Data from Paytm Money in Python), as a result you should now be able to fetch the real-time stock market data from Paytm Money API using Python. Now you can use this file as an API in your program and import it where you want to add. If you want to learn more about trading, then stay tune to tutlogic.com or follow my channel on youtube.com/@tutlogic.

Scroll to Top