How to fetch Stock Data using PANDAS DATAREADER in Python?

shahstavan

Mr.Shah

Posted on October 12, 2021

How to fetch Stock Data using PANDAS DATAREADER in Python?

You can perform marvelous and creative things in Python and, it's a vast community. The more you go deeper, things start becoming fascinating.

Today in this article we are going to fetch Stock Data in Python using PANDAS DATAREADER from various Sources.
So, it's going to be amazing :)

Python

◼️Installing Raw Packages

  • First you need to install raw packages in Python like NumPy, Pandas, Plotly and Pandas Datareader.

NumPy Installation using CMD

pip install numpy
Enter fullscreen mode Exit fullscreen mode

Pandas Installation using CMD

pip install pandas
Enter fullscreen mode Exit fullscreen mode

Pandas DataReader Installation using CMD

pip install pandas-datareader
Enter fullscreen mode Exit fullscreen mode

Plotly Installation using CMD

pip install plotly==5.3.1
Enter fullscreen mode Exit fullscreen mode

◼️After Installing Raw Packages let's import it in Python File

  • Create a Python Notebook File [.ipynb extension after file name]
# Raw Packages
import numpy as np
import pandas as pd
import plotly.graph_objs as go
Enter fullscreen mode Exit fullscreen mode
# Data Sources
from pandas_datareader import data,wb
Enter fullscreen mode Exit fullscreen mode
# Convert date in right format
# Today's date as variable
from datetime import date
Enter fullscreen mode Exit fullscreen mode
# Start Date means from which date we want to start fetching Stock Data?
# End Date means till which date we want to fetch Stock Data?
startdate = pd.to_datetime('2018-07-15')
enddate = pd.to_datetime(date.today())
Enter fullscreen mode Exit fullscreen mode

◼️Importing Stock Data from Yahoo

  • Pandas Datareader plays a important role in it. How? See, DataReader consider four Parameters - Ticker Name[Stock Name],Source name[Yahoo],Start Date, End Date.
  • Source Name actually means from where you want to fetch Data?
# For Indian NSE Stocks use .NS after Stock Name
# Example:
# data.DataReader("INFY.NS", 'yahoo', startdate, enddate)
data.DataReader("SPY", 'yahoo', startdate, enddate)
Enter fullscreen mode Exit fullscreen mode

Yahoo Finance Output


◼️Importing Stock Data from Google Finance

# Just Changing our Source name in Second Parameter
# data.DataReader("INFY.NS", 'yahoo [Changing to stooq for Google Finance]', startdate, enddate)
data.DataReader("AAPL", 'stooq', startdate, enddate)
Enter fullscreen mode Exit fullscreen mode

Google Finance Output

◼️Importing Stock Data from AlphaVantage

  • For using this you need to register your account on AlphaVantage and get your API key
# Forex Data
# Conversion from GBP to USD
data.DataReader("GBP/USD",'av-forex',startdate,enddate,api_key='YourAPIKEY')
Enter fullscreen mode Exit fullscreen mode

AlphaVantage Output

Hope you this Post was helpful to you 😁🤘

See you soon in next Post till then GoodBye👋

💖 💪 🙅 🚩
shahstavan
Mr.Shah

Posted on October 12, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related