Python script to fetch Real-Time Stock/Index/Forex data !!| Daily Python #10
This article is a tutorial on how to fetch Stock/Index data using Python and World Trading Data API.
This article is a part of Daily Python challenge that I have taken up for myself. I will be writing short python articles daily.
First, let’s create an account on https://www.worldtradingdata.com/. This has a free subscription plan for real-time requests with fully free access to historical data. There is a paid subscription as well if you want to request real-time data more often.
After creating the account, you will get an API token on the dashboard of the platform. This token can be used to make requests to the API.
Requirements:
- Python 3.0
- Pip
Install the following packages:
- requests — Library to making HTTP requests.
- pprint — Library for printing structures of data in a formatted way.
pip install requests pprint
Let’s import the requests library
import requests
As the response of the requests from the API will be a JSON object, let’s import PrettyPrinter, library to beautify the JSON output.
from pprint import PrettyPrinter
pp = PrettyPrinter()
The authentication for this API is done per request, that is, we don’t need to create a login and then stream the data. The API token is required for every request. Store the API Token in a variable to be used in the future.
api_token = 'YOUR_API_TOKEN'
Let’s start by fetching stock data
#Fetch Stock Data
url = 'https://api.worldtradingdata.com/api/v1/stock'
params = {
'symbol':'GOOGL',
'api_token':api_token,
}
response = requests.request('GET',url,params=params)
pp.pprint(response.json())
The params dictionary contains the required parameters by the API to fetch Stock data. Symbol — of the stock and the API Token are required in every request for fetching Stock data.
Similarly, we can fetch Mutual Fund Data of the previous day
#Fetch Mutual Fund Data
url = 'https://api.worldtradingdata.com/api/v1/mutualfund'
params = {
'symbol': 'AAAAX,AAAGX',
'api_token':api_token,
}
response = requests.request('GET', url, params=params)
pp.pprint(response.json())
Let’s fetch Intraday Stock Data
#Fetch Intraday Stock Data
url = 'https://intraday.worldtradingdata.com/api/v1/intraday'
params = {
'symbol': 'GOOGL',
'api_token': api_token,
'interval': '1',
'range': '1'
}
response = requests.request('GET', url, params=params)
pp.pprint(response.json())
Now, let’s try fetching the Real-Time Foreign Exchange Data (Forex) with Base currency as Indian Rupee (INR)
#Fetch Real-Time Forex Data
url = 'https://api.worldtradingdata.com/api/v1/forex'
params = {
'base': 'INR',
'api_token': api_token
}
response = requests.request('GET', url, params=params)
print(response.json())
Finally, let’s fetch the FREE content — Historical Market Data: From 08 Jan 2020 to 14 Jan 2020
#Fetch Historical Market Data
url = 'https://api.worldtradingdata.com/api/v1/history'
params = {
'symbol': 'GOOGL',
'api_token': api_token,
'date_from':'2020-01-08',
'date_to':'2020-01-14'
}
response = requests.request('GET', url, params=params)
pp.pprint(response.json())
I hope this article was helpful, do leave some claps if you liked it.