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 :)
◼️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
Pandas Installation using CMD
pip install pandas
Pandas DataReader Installation using CMD
pip install pandas-datareader
Plotly Installation using CMD
pip install plotly==5.3.1
◼️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
# Data Sources
from pandas_datareader import data,wb
# Convert date in right format
# Today's date as variable
from datetime import date
# 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())
◼️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)
◼️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👋
Top comments (0)