Application Center - Maplesoft

App Preview:

Drawdown of Historical Stock Prices

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application




Drawdown of Historical Stock Prices

Introduction

The drawdown of a stock indicates how much time it's spent "underwater" - it's essentially the percentage drop of its price from a peak to a trough, with the drawdown resetting to zero if a previous high is reached.  The drawdown of a stock is a valuable risk measure and is employed by traders to gauge volatility.

 

This application

 

• 

   downloads historical stock prices from Yahoo Finance for a chosen ticker symbol,

• 

   defines a procedure that calculates the drawdown of the historical stock price

• 

   and plots the drawdown against the adjusted close price of the asset

 

By changing the ticker and the dates, you can examine drawdown of any stock between your chosen dates.

 

The application uses Maple 18's improved Internet connectivity; you can now download data from a URL into a matrix using ImportMatrix() .

restart

Ticker, Dates and Frequency

Download historical data for the S&P 500

ticker := "^GSPC"

startDay := "1"; startMonth := "1"; startYear := "1975"

endDay := "1"; endMonth := "1"; endYear := "2011"

frequency := "m"

Download Historical Stock Quotes

url := cat("http://ichart.finance.yahoo.com/table.csv?s=", ticker, "&a=", startMonth, "&b=", startDay, "&c=", startYear, "&d=", endMonth, "&e=", endDay, "&f=", endYear, "&g=",frequency,"&ignore=.csv")

Strip out header row

data := ImportMatrix(url)[2 .. (), () .. ()]

data := Vector(4, {(1) = ` 433 x 7 `*Matrix, (2) = `Data Type: `*anything, (3) = `Storage: `*rectangular, (4) = `Order: `*Fortran_order})

(3.1)

Note that the adjusted close price is the seventh column.

 

Reverse the matrix so it's in date ascending order.

data := convert(ListTools:-Reverse(convert(data, listlist)), Matrix)

nRows := LinearAlgebra:-RowDimension(data)

433

(3.2)

Calculate and Plot Drawdown

The algorithm is referenced from http://en.wikipedia.org/wiki/Drawdown_(economics)

DD is a vector that will be filled with the drawdown of the historical stock price

DD := Vector(nRows, datatype = float[8])

peak := -99999; for i to nRows do if data[i, 7] > peak then peak := data[i, 7] end if; DD[i] := (100*(peak-data[i, 7]))/peak end do

The maximum drawdown is

max(DD)

52.5558610499999972

(4.1)

p1 := Statistics[ColumnGraph](-DD, thickness = 0, color = gray, distance = 10^(-6), labels = ["Time Units from Start Date", "Drawdown (%)"], labeldirections = [horizontal, vertical], labelfont = [Arial], filled = true, style = patchnogrid, legend = "Drawdown (%)", legendstyle = [font = [Arial]], axesfont = [Arial], width = 1)

p2 := plots:-pointplot([seq([i, data[i, 7]], i = 1 .. nRows)], connect = true, color = "SteelBlue", thickness = 1, labels = ["Time Units from Start Date", "Adjusted Close"], labeldirections = [horizontal, vertical], labelfont = [Arial], legend = "Adjusted Close", legendstyle = [font = [Arial]], axesfont = [Arial])

plots:-dualaxisplot(p1, p2, size = [800, 400], tickmarks = [[seq(i = data[i, 1], i = 1 .. nRows, floor(.2*nRows))], decimalticks])

NULL