datascientist110
datascientist110

Reputation: 75

How to get Historical Financial Data for Data Analysis in Python?

I need to get Stock data using pandas DataReader for the following banks:

    Bank of America
    CitiGroup
    Goldman Sachs
    JPMorgan Chase
    Morgan Stanley
    Wells Fargo

How to get the stock data from Jan 1st 2006 to Jan 1st 2016 for each of these banks.

I have tried...

import numpy as np
import pandas as pd
from pandas_datareader import data, wb
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import requests
import io
%matplotlib inline

import datetime
start = datetime.datetime(2006,1,1)
end = datetime.datetime(2016,1,1)

# Bank of America
BAC = data.DataReader("BAC",'ff', start, end)

Upvotes: 2

Views: 8496

Answers (4)

Anwarvic
Anwarvic

Reputation: 13022

2021 Answer

In my opinion, there is no need for subscirbing to IEX to get a free API token or installing another package. The Yahoo API is working fine:

# Bank of America
BAC = data.DataReader("BAC", 'yahoo', start, end)

# CitiGroup
C = data.DataReader("C", 'yahoo', start, end)

# Goldman Sachs
GS = data.DataReader("GS", 'yahoo', start, end)

# JPMorgan Chase
JPM = data.DataReader("JPM", 'yahoo', start, end)

# Morgan Stanley
MS = data.DataReader("MS", 'yahoo', start, end)

# Wells Fargo
WFC = data.DataReader("WFC", 'yahoo', start, end)

Upvotes: 3

alvarobartt
alvarobartt

Reputation: 463

Another solution you can try is investpy which is a Python package for historical data extraction from diverse financial products from all over the world from Investing.com. It has no limitations, no API keys needed and it is completely free since it is an open-source project.

Here I present you a piece of code in order to retrieve stock historical data from the past 9 years of the stocks you asked for above:

import investpy

stock_symbols = ['BAC', 'C', 'GS', 'JPM', 'MS', 'WFC']

for stock_symbol in stock_symbols:
    df = investpy.get_stock_historical_data(stock=symbol,
                                            country='united states',
                                            from_date='01/01/2010',
                                            to_date='01/01/2019')
    print(df.head())

Hope this helped you! And I also encourage you to use investpy!


Note that investpy stock data retrieval functions take as input parameter both the stock symbol and the country from where the specified stock is. So on, as your input was the stock names, you will need to search over investpy data in order to retrieve the symbol of the stock names you introduced, that could be done as it follows:

import investpy

stocks = ['Bank of America', 'CitiGroup', 'Goldman Sachs', 'JPMorgan', 'Morgan Stanley', 'Wells Fargo&Co']

for stock in stocks:
    print(investpy.search_stocks(by='name', value=stock))

The code above will print all the stocks found that matched the introduced name fully or partially.

Upvotes: 2

Suyash
Suyash

Reputation: 51

Following is the method I find very reliable unlike the rest, I only use this to analyze and this has never failed me. (Also the range of dates for data available is very expansive as in the example below)

from datetime import datetime
start = datetime(2006, 2, 9)
end = datetime(2018, 5, 24)
#Bank Of America
BAC = data.DataReader('BAC', 'yahoo', start, end)

Upvotes: 0

llompalles
llompalles

Reputation: 3186

Your problem is with the source which you are using to retrieve the data with Datareader. It doesn't seems that 'ff' corresponds to any accepted API.

I've tried this and it works:

import pandas_datareader.data as web
from datetime import datetime

start = datetime(2016, 9, 1)
end   = datetime(2018, 9, 1)

f = web.DataReader('BAC', 'iex', start, end)
print(f)

Also, take a look at the pandas-datareader official doccumentation, there are plenty of examples.

Upvotes: 2

Related Questions