This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
Download the Nasdaq dataset on the book's website. (http://ipython-books.github.io)
The data has been obtained here: http://finance.yahoo.com/q/hp?s=%5EIXIC&a=00&b=1&c=1990&d=00&e=1&f=2014&g=d
import numpy as np
import scipy as sp
import scipy.signal as sg
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
2 We load the Nasdaq data with Pandas.
nasdaq_df = pd.read_csv('data/nasdaq.csv')
nasdaq_df.head()
date = pd.to_datetime(nasdaq_df['Date'])
nasdaq = nasdaq_df['Close']
plt.figure(figsize=(6,4));
plt.plot_date(date, nasdaq, '-');
# We get a triangular window with 60 samples.
h = sg.get_window('triang', 60)
# We convolve the signal with this window.
fil = sg.convolve(nasdaq, h/h.sum())
plt.figure(figsize=(6,4));
# We plot the original signal...
plt.plot_date(date, nasdaq, '-', lw=1);
# ... and the filtered signal.
plt.plot_date(date, fil[:len(nasdaq)], '-');
filtfilt
method allows us to apply a filter forward and backward in order to avoid phase delays.plt.figure(figsize=(6,4));
plt.plot_date(date, nasdaq, '-', lw=1);
# We create a 4-th order Butterworth low-pass filter.
b, a = sg.butter(4, 2./365)
# We apply this filter to the signal.
plt.plot_date(date, sg.filtfilt(b, a, nasdaq), '-');
plt.figure(figsize=(6,4));
plt.plot_date(date, nasdaq, '-', lw=1);
b, a = sg.butter(4, 2*5./365, btype='high')
plt.plot_date(date, sg.filtfilt(b, a, nasdaq), '-', lw=.5);
The fast variations around 2000 correspond to the dot-com bubble burst, reflecting the high market volatility and the fast fluctuations of the stock market indices at that time. (http://en.wikipedia.org/wiki/Dot-com_bubble)
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).