I've just started to try create Lanzcos filtering method in trend filtering. My code is applied for S&P500 in python as below.
I see that signal is strange with original trend. Please help to review or other way for Lanzcos filtering?
figure of Lanczos filtering
Thanks
Dodo
import datetime
from pandas.io.data import DataReader
import numpy as np
from oceans import lanc
import matplotlib.pyplot as plt
# filter trend of S&P500
symbol = '^GSPC'
price = DataReader(symbol, "yahoo", datetime.datetime(2000,1,1), datetime.datetime(2011,1,1))
bars = np.log(price)
#lanzcos filtering
freq = 1./65 # Hours
window_size = 96+1+96
pad = np.zeros(window_size) * np.NaN
wt = lanc(window_size, freq)
res = np.convolve(wt, bars['Close'], mode='same')
bars['low_lanc'] = res
bars['high_lanc'] = bars['Close'] - bars['low_lanc']
#plot
fig, (ax0, ax2) = plt.subplots(nrows=2, figsize=(7, 4), sharex=True, sharey=True)
x = bars.index.to_pydatetime()
ax0.plot(x, bars['Close'], label='original')
ax0.legend(loc='upper center', bbox_to_anchor=(0.6, .4), ncol=3, fancybox=True, shadow=True, numpoints=1)
ax2.plot(x, bars['low_lanc'], label='lanc low', linewidth=2., alpha=0.5)
leg = ax2.legend(loc='upper center', bbox_to_anchor=(0.6, .4), ncol=3, fancybox=True, shadow=True, numpoints=1)
plt.ylim([6, 8])
plt.show()