Alpha Vantage offers free Stocks APIs that allows you to fetch stocks data , forex, commodity, cryptocurrency, etc in real time as well as historical data. In this tutorial you will learn how to do Insertion of Stock Market Data from Alpha Vantage API to MongoDB database.
Steps to Insert Stock Market Data from API to MongoDB
Step 1 : Import required libraries
Import all the necessary libraries that you will use in this example. You can import the required libraries using the import statement.
import pandas as pd
import pymongo
import requests
Step 2: Download the Data
After importing the required libraries I will download the stocks data as CSV file from the official Alpha vantage URL. To use the URL you have to use their API key. After that you will convert it to the pandas dataframe.
api_url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&outputsize=
full&datatype=csv&apikey=your_api_key"
df = pd.read_csv(api_url)
Step 3: Change the data to dictionary
Before inserting the stocks data to Mongodb you have to first convert the dataframe to dictionary. To do this you will use the dataframe.to_dict(orient=”records”). Here you will use the orient as records. If you do this you will able to convert the dataframe like below.
[{'timestamp': '2024-01-12', 'open': 162.97, 'high': 165.98, 'low': 162.355, 'close': 165.8, 'volume': 4958261},
{'timestamp': '2024-01-11', 'open': 161.02, 'high': 162.23, 'low': 160.29, 'close': 162.16, 'volume': 3778395},
{'timestamp': '2024-01-10', 'open': 160.28, 'high': 161.34, 'low': 159.74, 'close': 161.23, 'volume': 2967852}]
Step 4: Connect the MongoDB database
Now you will open the connection to the database using the pymongo.MongoClient() function.
client = pymongo.MongoClient("mongodb://localhost:27017")
db = client["stocks_data"]
collection = db["IBM"]
Here I am creating the “stocks_data” database and IBM collection where the time series of the IBM stock will be stored.
Step 5: Insert the data to MongoDB
Now the last step is to insert the stocks data to MongoDB. To to do I will first use the requests module to check the status of the response to make sure that data should be properly inserted to the database. After that I will use the collection.insert_many() function to insert into the database.
response = requests.get(api_url)
if response.status_code == 200:
collection.insert_many(data)
else:
print(f"Failed to retrieve data. Status code: {response.status_code}")
Full Code
import pandas as pd
import pymongo
import requests
api_url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&outputsize=full&datatype=csv&apikey=your_api_key"
df = pd.read_csv(api_url)
data = df.to_dict(orient="records")
client = pymongo.MongoClient("mongodb://localhost:27017")
db = client["stocks_data"]
collection = db["IBM"]
# Make a GET request to the API
response = requests.get(api_url)
if response.status_code == 200:
collection.insert_many(data)
else:
print(f"Failed to retrieve data. Status code: {response.status_code}"
You can check whether the data has been stored or not through fetching the stocks data from the MongoDB database.
cursor = collection.find()
db_data = list(cursor)
# Convert data to a DataFrame
df = pd.DataFrame(data)
print(df)
Output
Thats all for now. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.