Fourie analysis for stock time series data④

How to calculate fourie analysis for stock time series data④ 〜Detrend〜

Hello, Good morning, good evening and good night^^ This is Data scientist Hiroki🐶

Winter is coming☃️ Yes let’s celebrate the holiday🎂

When you watching the stock chart, have you ever have the experience that you want to remove the trend component from the stock data?

https://jp.mathworks.com/help/matlab/ref/detrend.html

You can do it just 1 line of Matlab code. It’s so useful for understanding overall periodicity.

Today’s blog is…

  • how to detrend by just one line
Let me introduce the code!! Here it is..
D = detrend(Y);

Yes, just input the original data into Y and you’ll get the detrended data as D.
So easy, you’ll love it!!💖

Example

Let me give you an example by using the code with the real stock data. At first, Let’s download the real stock data from yahoo finance. Let’s check how to do it from here.

Matlab Code

Here is entire Matlab code. It’s so simple and easy and cool!

%download stock data from yahoo finance
data = hist_stock_data('09112010','09112021','^N225');

%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
D = detrend(Y);


%make the graph
plot(t,Y)
hold on
plot(t,D)
hold on
plot(t,y-D,":k")
legend("Input Data","Detrended Data","Trend","Location","northwest")
ylabel('株価 [Yen]')

Here is the output graph,

You can see the trend easily so that the data’s trend component is removed from original data.😉

At last, let me introduce the related YouTube channel. It is about detrending and machine-language. ^^

Have a happy holiday🍵🌟 Hiroki🐶