R Labs 6

Technical Analysis

R Example 6.1: The R commands below retrieve the historical quotes of Apple (AAPL) from Yahoo, and charts the price history from June, 2008 to April, 2009,  using a candlestick representation for each daily tick, with positive candles in green up.col='green' and negative candles in blue  dn.col='blue'; two moving averages  MA(50) in blue and MA(10) in black, Bollinger bands on a 20 day moving average with bands at 2 standard deviations, and RSI indicator are also drawn.

##Technical analysis with quantmod
##Chart a stock with MA(50), Bollinger, Volume and RSI
chartSeries(AAPL, subset='2008-06::2009-04',theme=chartTheme('white',up.col='green',dn.col='red'),TA=c(addBBands(n=20,sd=2,),addSMA(n=50,col="blue"),addSMA(n=10,col="black"),addRSI(n=14)))
#up.col, dn.col= color of up (down) candle,
Fundamental Analysis

R Example 6.2: Use quantmod methods to retrieve financial statements of Apple Inc. from Yahoo Finance.  A backup AAPL.f can be obtained here  (NB: In the textbook we use the default source which is Google Finance, but as of 2014 it does not connects correctly).

##getFinancials retrives financials from src (default google)
##returns six individual matrices organized in a list of class ‘financials’:
# IS a list containing (Q)uarterly and (A)nnual Income Statements
# BS a list containing (Q)uarterly and (A)nnual Balance Sheets
# CF a list containing (Q)uarterly and (A)nnual Cash Flow Statements
getFinancials('AAPL',src="yahoo")  #returns AAPL.f to "env"
viewFin(AAPL.f, "CF", "A") #view Annual Cash Flows
viewFin(AAPL.f, "BS", "A", "2010/2011")

applBS = viewFin(AAPL.f, "BS","A")
colnames(applBS) ##give four columns labeled by the date (yyyy-mm-dd) the statement was issued
rownames(applBS) ##give 42 rows labelled with financial indicators

applBS["Total Current Assets","2012-09-29"] ##returns the value of Total Current Assets in that year

##Compute some liquidity measures:
CurrentRat=applBS["Total Current Assets","2012-09-29"]/applBS["Total Current Liabilities","2012-09-29"]