Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Technical Analysis Examples

Overview

To illustrate some of the technical analysis functions, this section uses the IBM® stock price data contained in the supplied file ibm9599.dat. First create a financial time series object from the data using ascii2fts:

ibm = ascii2fts('ibm9599.dat', 1, 3, 2);

The time series data contains the open, close, high, and low prices, and the volume traded on each day. The time series dates start on January 3, 1995, and end on April 1, 1999, with some values missing for weekday holidays; weekend dates are not included.

Moving Average Convergence/Divergence (MACD)

Moving Average Convergence/Divergence (MACD) is an oscillator function used by technical analysts to spot overbought and oversold conditions. Use the IBM® stock price data contained in the supplied file ibm9599.dat. First, create a financial time series object from the data using ascii2fts. Look at the portion of the time series covering the 3-month period between October 1, 1995 and December 31, 1995. At the same time fill any missing values due to holidays within the time period specified:

ibm = ascii2fts('ibm9599.dat', 1, 3, 2);
part_ibm = fillts(ibm('10/01/95::12/31/95'));

Calculate the MACD, which when plotted produces two lines; the first line is the MACD line itself and the second is the nine-period moving average line:

macd_ibm = macd(part_ibm);

When you call macd without giving it a second input argument to specify a particular data series name, it searches for a closing price series named Close (in all combinations of letter cases).

Plot the MACD lines and the High-Low plot of the IBM stock prices in two separate plots in one window.

subplot(2, 1, 1);
plot(macd_ibm);
title('MACD of IBM Close Stock Prices, 10/01/95-12/31/95');
datetick('x', 'mm/dd/yy');
subplot(2, 1, 2);
highlow(part_ibm);
title('IBM Stock Prices, 10/01/95-12/31/95');
datetick('x', 'mm/dd/yy')

Williams %R

Williams %R is an indicator that measures overbought and oversold levels. The function willpctr is from the stochastics category. All the technical analysis functions can accept a different name for a required data series. If, for example, a function needs the high, low, and closing price series but your time series object does not have the data series names exactly as High, Low, and Close, you can specify the correct names as follows:

wpr = willpctr(tsobj,14,'HighName','Hi','LowName','Lo','CloseName','Closing').

The function willpctr now assumes that your high price series is named Hi, low price series is named Lo, and closing price series is named Closing. Use the IBM® stock price data contained in the supplied file ibm9599.dat. First, create a financial time series object from the data using ascii2fts. Look at the portion of the time series covering the 3-month period between October 1, 1995 and December 31, 1995. At the same time fill any missing values due to holidays within the time period specified:

ibm = ascii2fts('ibm9599.dat', 1, 3, 2);
part_ibm = fillts(ibm('10/01/95::12/31/95'));

Since the time series object part_ibm has its data series names identical to the required names, name adjustments are not needed. The input argument to the function is only the name of the time series object itself.

Calculate and plot the Williams %R indicator for IBM stock along with the price range using these commands:

wpctr_ibm = willpctr(part_ibm);
subplot(2, 1, 1);
plot(wpctr_ibm);
title('Williams %R of IBM stock, 10/01/95-12/31/95');
datetick('x', 'mm/dd/yy');
hold on;
plot(wpctr_ibm.dates, -80*ones(1, length(wpctr_ibm)),...  
'color', [0.5 0 0], 'linewidth', 2)
plot(wpctr_ibm.dates, -20*ones(1, length(wpctr_ibm)),... 
'color', [0 0.5 0], 'linewidth', 2)
subplot(2, 1, 2);
highlow(part_ibm);
title('IBM Stock Prices, 10/01/95-12/31/95');
datetick('x', 'mm/dd/yy');

The top plot has the Williams %R line plus two lines at -20% and -80%. The bottom plot is the High-Low plot of the IBM stock price for the corresponding time period.

Relative Strength Index (RSI)

The Relative Strength Index (RSI) is a momentum indicator that measures an equity's price relative to itself and its past performance. The function name is rsindex. The rsindex function needs a series that contains the closing price of a stock. The default period length for the RSI calculation is 14 periods. This length can be changed by providing a second input argument to the function. First, create a financial time series object from the data using ascii2fts. Look at the portion of the time series covering the 3-month period between October 1, 1995 and December 31, 1995. At the same time fill any missing values due to holidays within the time period specified:

ibm = ascii2fts('ibm9599.dat', 1, 3, 2);
part_ibm = fillts(ibm('10/01/95::12/31/95'));

Calculate and plot the RSI for IBM® stock along with the price range using these commands:

rsi_ibm = rsindex(part_ibm);
subplot(2, 1, 1);
plot(rsi_ibm);
title('RSI of IBM stock, 10/01/95-12/31/95');
datetick('x', 'mm/dd/yy');
hold on;
wpctr_ibm = willpctr(part_ibm);
plot(rsi_ibm.dates, 30*ones(1, length(wpctr_ibm)),... 
'color', [0.5 0 0], 'linewidth', 2)
plot(rsi_ibm.dates, 70*ones(1, length(wpctr_ibm)),...  
'color',[0 0.5 0], 'linewidth', 2)
subplot(2, 1, 2);
highlow(part_ibm);
title('IBM Stock Prices, 10/01/95-12/31/95');
datetick('x', 'mm/dd/yy');

On-Balance Volume (OBV)

On-Balance Volume (OBV) relates volume to price change. The function onbalvol requires you to have the closing price (Close) series and the volume traded (Volume) series. First, create a financial time series object from the data using ascii2fts. Look at the portion of the time series covering the 3-month period between October 1, 1995 and December 31, 1995. At the same time fill any missing values due to holidays within the time period specified:

ibm = ascii2fts('ibm9599.dat', 1, 3, 2);
part_ibm = fillts(ibm('10/01/95::12/31/95'));

Calculate and plot the OBV for IBM® stock along with the price range using these commands:

obv_ibm = onbalvol(part_ibm);
subplot(2, 1, 1);
plot(obv_ibm);
title('On-Balance Volume of IBM Stock, 10/01/95-12/31/95');
datetick('x', 'mm/dd/yy');
subplot(2, 1, 2);
highlow(part_ibm);
title('IBM Stock Prices, 10/01/95-12/31/95');
datetick('x', 'mm/dd/yy');

See Also

| | | | | | | | | | | | | | | | | | | | | | | |

Related Topics

Was this topic helpful?