Question

I have a stock market data set with the following two fields:

  • date
  • closing_price

I would like to apply DWT on this data set and plot a graph using Matlab. Can some one please guide me how to do it..

Thank you

EDIT

here is an example set of data

Date,Open,High,Low,Close,Volume

24-Oct-03,29.12,29.2,28.93,29.16,3069600

23-Oct-03,29.5,29.69,29.34,29.55,3414200

22-Oct-03,29.95,29.95,29.49,29.65,6659700

It is CSV.

I need to plot a DWT graph where x axis is date and y axis is any one or all of the variables.

Was it helpful?

Solution

Computing DWT

This is actually straightforward. Use dwt to obtain the approximation and detail coefficients. For instance, a DWT using a daubechies 4-tap wavelet would be:

[cA, cD] = dwt(X, 'db4')

where X is your data.

If each sample in X has several fields, and you want to apply the DWT just on a certain field, say X.closing_price, add square brackets:

[cA, cD] = dwt([X.closing_price], 'db4')

Note that this solution assumes that the data samples are taken at constant time intervals.
To plot the data, you need to prepare another vector, which corresponds to the day number:

t = 1:2:length(X);

The x-axis values skips every other day because the approximation and detail coefficients vectors each have half of the samples of X.


Demo code

The following code generates random data and puts it into an array of structs, each element having two fields: date and closing_price:

%# Generate some random data
C = cell(31, 2);
C(:, 1) = arrayfun(@(z)[num2str(z), '-Oct-03'], 1:length(C), 'Un', 0);
C(:, 2) = num2cell(100 * randn(1, length(C)));
X = cell2struct(C, {'date', 'closing_price'}, 2);

Now, to business:

%# Apply DWT
[cA, cD] = dwt([X.closing_price], 'db4');

%# Prepare x-axis values
t = 1:2:length(X);

%# Plot result with respect to date
figure
subplot(2, 1, 1), plot(t, cA)
title('Approximation coefficients'), xlabel('day'), ylabel('C_A')
subplot(2, 1, 2), plot(t, cD)
title('Detail coefficients'), xlabel('day'), ylabel('C_D')

This is what you should get:

Expected result

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top