Deb
Deb

Reputation: 335

How to get only Adjusted Close Price from Yahoo Finance library

I am using the Yahoo Finance Library in Python to pull data of a stock.

import yahoo_finance
ticker = 'GLD'
begdate = '2014-11-11'
enddate = '2016-11-11'
data = yahoo_finance.Share('GLD')
data1 = data.get_historical(begdate,enddate)
gld_df = pd.DataFrame(data1)
date_df = (list(gld_df["Date"]))
adj_close_df = list(gld_df["Adj_Close"])
print(adj_close_df) 

plt.plot(adj_close_df,date_df)

I would like to plot this Adjusted Close Price on Y-Axis and the corresponding Dates on the X Axis, but my above code is giving an error when I try to do that.

I am using Python 3.x, Anaconda

Upvotes: 0

Views: 6676

Answers (2)

Deb
Deb

Reputation: 335

I got it.

import yahoo_finance
from pylab import *
import numpy as np
import scipy.signal as sc
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt

ticker = 'GLD'
begdate = '2014-11-11'
enddate = '2016-11-11'
data = yahoo_finance.Share('GLD')
data1 = data.get_historical(begdate,enddate)
gld_df = pd.DataFrame(data1)
date_df = pd.to_datetime((list(gld_df["Date"])))
adj_close_df = list(gld_df["Adj_Close"])
plt.plot(date_df,adj_close_df)

Upvotes: 1

Axel Juraske
Axel Juraske

Reputation: 196

You could generate the list as below:

l = [ x['Close'] for x in data1]

And the plot:

import matplotlib.pyplot as plt
plt.plot(l)
plt.show()

Upvotes: 2

Related Questions