In [1]:
%matplotlib inline
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
import pandas as pd
import pandas_datareader.data as web
In [2]:
style.use("ggplot")
Let's specify the time window we are interested in:
In [3]:
start = dt.datetime(2000,1,1)
end = dt.datetime(2016,12,31)
In [4]:
df = web.DataReader('TSLA', 'yahoo', start, end)
df.head()
Out[4]:
In [1]:
df.to_csv('data/tsla.csv')
This file can be read from disk to a pandas dataframe:
In [6]:
df = pd.read_csv('data/tsla.csv', parse_dates=True, index_col=0)
df.head()
Out[6]:
The extra parameter are necessary to parse correctly the dates as the index of the dataframe. One can plot the dataframe with a simple command:
In [7]:
df.plot()
# plt.show()
Out[7]:
To plot specific columns:
In [8]:
df['Adj Close'].plot()
# plt.show()
Out[8]:
In [9]:
# Let's create a 100-moving-average-window data column
df['100ma'] = df['Adj Close'].rolling(window=100, min_periods=0).mean()
# df.dropna(inplace=True) # Without min_periods, the first 100 rows will be NaN for obvious reasons
df.head()
Out[9]:
To be able to make more involved plots, we'll have to unleash the power of matplotlib!
In [10]:
# 6 rows, 1 column, starts at (0,0)
ax1 = plt.subplot2grid((6,1), (0,0), rowspan=5, colspan=1)
# 6 rows, 1 column, starts at (5,0), shares x-axis with ax1
ax2 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1)
ax1.plot(df.index, df['Adj Close'])
ax1.plot(df.index, df['100ma'])
ax2.bar(df.index, df['Volume'])
# plt.close()
Out[10]:
In [11]:
# We want open-high-low-close data every 10 days
df_ohlc = df['Adj Close'].resample('10D').ohlc()
# Then we want the volume. Sum() conserves the same total volume
df_volume = df['Volume'].resample('10D').sum()
df_ohlc.head()
Out[11]:
To use matplotlib's candlestick plots we need to have the date as a normal column.
In [12]:
df_ohlc.reset_index(inplace=True)
df_ohlc['Date'] = df_ohlc['Date'].map(mdates.date2num)
df_ohlc.head()
Out[12]:
In [13]:
%config InlineBackend.figure_format='retina'
# 6 rows, 1 column, starts at (0,0)
ax1 = plt.subplot2grid((6,1), (0,0), rowspan=5, colspan=1)
# 6 rows, 1 column, starts at (5,0), shares x-axis with ax1
ax2 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1)
ax1.xaxis_date()
candlestick_ohlc(ax1, df_ohlc.values, width=2, colorup='g', colordown='r')
ax2.fill_between(df_volume.index.map(mdates.date2num), df_volume.values, 0)
# plt.show()
Out[13]: