Fourie analysis for stock time series data⑤ 〜periodicity〜
Hello, Good morning, good evening and good night^^ This is Data scientist Hiroki🐶
It is said that the stock price of toysrus goes up in christmas season🧸, and the stock price of coca cola goes up in summer season🥤
Is it true? Let’s see with the data. in this blog, you can get the technique how to find the periodicity from the stock price data.
in this blow output is below graph.
Technically,
- To create degital filter which you can get a particular frequency band based on periodogram
- To create the time line that consists only of the particular frequency band
Let’s dive in to the Matlab code^^
Matlab Code
Here is the entire Matlab code
%download the stock data from yahoo finance.
data = hist_stock_data('24102002','24112022',append('6752.T'));
%store the data
y=data.Close;
t=data.Date;
%fill the missing data if it's available
[Y,TF] = fillmissing(y,'linear','SamplePoints',t);
%detrend of the data
T=(1:length(Y))';
p= polyfit(T,Y,1);
YYfit= polyval(p,T);
dYY=detrend(Y);
%make the graph
figure
plot(t,Y)
hold on
plot(t,YYfit)
hold on
plot(t,dYY)
%define one year as 245 business day.
Fs=245;
%conduct the fourie transformation and output the power spectrum density and frequency.
[pxx,f] = periodogram(dYY,rectwin(length(y)),length(y),Fs);
figure
%create graph
plot(f,10*log10(pxx))
xlabel('Cycles/Year')
ylabel('dB / (Cycles/Year)')
title('Periodogram')
xlim([0 3])
ylim([10 80])
%to filter the 0.06~0.5[Cycles/Year] band pass filter
figure
bandpass(dYY,[0.06 0.5],Fs);
xlabel('Cycles/Year')
xlim([0 10])
Here is the first graph.
this yellow graph is detrend graph from blue chart. to subtract the trend from 20 years data make you easier for finding 2~3 years trend.
Next graph is periodogram. you can see the peak value.
this time, I pick up 3 peaks😉
Since these 3 peaks are in from 0.098 to 0.44[Cycles/Year], I creat the band pass filter from 0.06 to 0.5[Cycles/Year] which is including above 3 peaks. and I filt the band pass filter to the original time line data.
this is not just for stock data but for so many data like physics, engineering, electric, and so on. let’s use above code for a lot of another example^^
see ya
Hiroki🐶