Application Center - Maplesoft

App Preview:

Downloading Stock Prices and Plotting Returns Distributions

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

Learn about Maple
Download Application




Downloading Stock Prices and Plotting Returns Distributions

Introduction

This application

 

• 

   downloads historical stock prices from Yahoo Finance,

• 

   calculates the returns,

• 

   plots the distribution of the returns in a histogram,

• 

   and overlays a normal distribution with the same mean and standard deviation as the historical data.

 

with(LinearAlgebra); with(Statistics); with(plots)

Ticker, Dates and Frequency

ticker := "XOM"

startDay := "25"; startMonth := "07"; startYear := "2010"

endDay := "25"; endMonth := "02"; endYear := "2014"

frequency := "d"

Download Historical Stock Quotes and Calculate Returns

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")

data := ImportMatrix(url)

nRows := RowDimension(data)-2

returns := Vector(nRows, proc (i) options operator, arrow; evalf(ln(data[i+2, 7]/data[i+1, 7])) end proc)

p1 := Histogram(returns, frequencyscale = relative, color = "SteelBlue", axes = boxed, gridlines)

Plot Histogram and Overlay Normal Distribution

av := Mean(returns)

HFloat(-6.285999253433331e-4)

stdev := StandardDeviation(returns)

HFloat(0.011409250478207239)

n := Normal(av, stdev)

p2 := DensityPlot(Normal(av, stdev), range = min(returns) .. max(returns))

display(p1, p2)

``