DEV Community

Cover image for How to fetch Stock Data using PANDAS DATAREADER in Python?
Mr.Shah
Mr.Shah

Posted on

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? ```python

For Indian NSE Stocks use .NS after Stock Name

Example:

data.DataReader("INFY.NS", 'yahoo', startdate, enddate)

data.DataReader("SPY", 'yahoo', startdate, enddate)

![Yahoo Finance Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b812oixngrb2qf0di558.jpg)
<hr>

### ◼️Importing Stock Data from Google Finance
```python


# 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 ```python

Forex Data

Conversion from GBP to USD

data.DataReader("GBP/USD",'av-forex',startdate,enddate,api_key='YourAPIKEY')

![AlphaVantage Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vaiy4n0vxectjyshawrs.jpg)

<h6>Hope you this Post was helpful to you 😁🤘</h6>
See you soon in next Post till then GoodBye👋
Enter fullscreen mode Exit fullscreen mode

Top comments (0)