#1.7 VISUALIZATION OF FINANCIAL DATA
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
da=read.table("d-mmm-0111.txt",header=T) 
mmm=da[,2] 
hist(mmm,nclass=30) 

d1=density(mmm) 
range(mmm) 
## [1] -0.089569  0.098784
x=seq(-.1,.1,.001) 
# The next command creates normal density
y1=dnorm(x,mean(mmm),stdev(mmm))
plot(d1$x,d1$y,xlab="rtn",ylab="density",type="l")
lines(x,y1,lty=2)
# ohlc plot
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
## 
##     time<-
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## 
## Attaching package: 'TTR'
## The following object is masked from 'package:fBasics':
## 
##     volatility
## Version 0.4-0 included new data defaults. See ?getSymbols.

getSymbols("AAPL",from="2011-01-03",to="2011-06-30")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "AAPL"
X=AAPL[,1:4] 
xx=cbind(as.numeric(X[,1]),as.numeric(X[,2]),as.numeric(X[,3]),
           as.numeric(X[,4]))
source("ohlc.R") 
ohlc_plot(xx,xl="days",yl="price",title="Apple Stock")

# Moving average plot
source("ma.R") 
getSymbols("AAPL",from="2010-01-02",to="2011-12-08")
## [1] "AAPL"
x1=as.numeric(AAPL$AAPL.Close) 
ma(x1,21)

# Bivariate and Scatter plots
da=read.table("m-ibmsp-2611.txt",header=T)
head(da)
##       data       ibm        sp
## 1 19260130 -0.010381  0.022472
## 2 19260227 -0.024476 -0.043956
## 3 19260331 -0.115591 -0.059113
## 4 19260430  0.089783  0.022688
## 5 19260528  0.036932  0.007679
## 6 19260630  0.068493  0.043184
ibm=log(da$ibm+1) 
sp=log(da$sp+1)
tdx=c(1:nrow(da))/12+1926 
par(mfcol=c(2,1))
plot(tdx,ibm,xlab='year',ylab='lrtn',type='l')
title(main="(a) IBM returns")
plot(tdx,sp,xlab='year',ylab='lrtn',type='l')

title(main="(b) SP index")

cor(ibm,sp) 
## [1] 0.6409642
m1=lm(ibm~ sp) 
summary(m1)
## 
## Call:
## lm(formula = ibm ~ sp)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.31956 -0.03122 -0.00108  0.03116  0.24061 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.007768   0.001672   4.645 3.84e-06 ***
## sp          0.806685   0.030144  26.761  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.05348 on 1027 degrees of freedom
## Multiple R-squared:  0.4108, Adjusted R-squared:  0.4103 
## F-statistic: 716.1 on 1 and 1027 DF,  p-value: < 2.2e-16
plot(sp,ibm,cex=0.8) 
abline(0.008,.807)