You are on page 1of 23

BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

LAB No: 1 Descriptive Statistics by importing stocks into R


Program:
> getSymbols("SBIN.NS",source='yahoo')
‘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] "SBIN.NS"
Warning message:
SBIN.NS contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
> View(SBIN.NS)
> summary(SBIN.NS)
Index SBIN.NS.Open SBIN.NS.High SBIN.NS.Low SBIN.NS.Close
Min. :2007-01-02 Min. : 87.26 Min. : 87.72 Min. : 84.74 Min. : 86.2
1st Qu.:2010-09-27 1st Qu.:186.80 1st Qu.:189.51 1st Qu.:183.28 1st Qu.:186.2
Median :2014-06-12 Median :233.22 Median :235.95 Median :230.10 Median :232.7
Mean :2014-06-17 Mean :238.14 Mean :241.51 Mean :234.33 Mean :237.8
3rd Qu.:2018-03-09 3rd Qu.:279.44 3rd Qu.:282.98 3rd Qu.:275.68 3rd Qu.:279.3
Max. :2021-12-03 Max. :534.45 Max. :542.30 Max. :528.05 Max. :530.5
NA's :7 NA's :7 NA's :7 NA's :7
SBIN.NS.Volume SBIN.NS.Adjusted
Min. : 0 Min. : 73.14
1st Qu.: 13812500 1st Qu.:174.52

1
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

Median : 19319414 Median :218.31


Mean : 24703292 Mean :225.78
3rd Qu.: 28799337 3rd Qu.:271.65
Max. :262677081 Max. :530.45
NA's :7 NA's :7

> library(PerformanceAnalytics)
> ar<-annualReturn(SBIN.NS$SBIN.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
> ar
yearly.returns
2007-12-31 0.89154802
2008-12-31 -0.42393032
2009-12-31 0.76055234
2010-12-31 0.23926844
2011-12-30 -0.42421495
2012-12-31 0.47339492
2013-12-31 -0.25948442
2014-12-31 0.76535532
2015-12-31 -0.28026297
2016-12-30 0.11472488
2017-12-29 0.23860910
2018-12-31 -0.04517586
2019-12-31 0.12791486
2020-12-31 -0.17617974
2021-12-03 0.72085824

2
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

> var(ar,na.rm='F')
yearly.returns
yearly.returns 0.2064887
> sd(ar,na.rm='F')
[1] 0.4544102
> range(ar,na.rm='F')
[1] -0.424215 0.891548
> quantile(ar,na.rm='F')
0% 25% 50% 75%
-0.4242150 -0.2178321 0.1279149 0.5971266
100%
0.8915480

3
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

LAB No: 2 Time Series Decomposition


Program:
> data("AirPassengers")
> d=decompose(AirPassengers)
> plot(decompose(AirPassengers))

> plot(d$trend)

4
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

LAB No: 3 Calculation of Returns


Program:
> library(quantmod)
> getSymbols('INFY.NS',src='yahoo')
‘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] "INFY.NS"
Warning message:
INFY.NS contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
> dr=dailyReturn(INFY.NS$INFY.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data

5
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

> View(dr)

> mr=monthlyReturn(INFY.NS$INFY.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
> View(mr)

> yr=annualReturn(INFY.NS$INFY.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data

6
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

> View(yr)

> all=allReturns(INFY.NS$INFY.NS.Close)
Warning messages:
1: In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
2: In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
3: In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
4: In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
5: In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data

> View(all)

7
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

8
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

LAB No: 4 Using Technical Indicators to plot the chart


Program:
> library(quantmod)
> getSymbols('TCS.NS',src='yahoo')
‘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] "TCS.NS"
Warning message:
TCS.NS contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
> tcs=TCS.NS
> chartSeries(tcs,subset='2021-08-01/',theme='white')

9
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

> chartSeries(tcs,subset='2021-08-01/',theme='white',TA='addSMA(n=40)')

> chartSeries(tcs,subset='2021-08-01/',theme='white',TA=c(addSMA(n=45),addVo()))

> chartSeries(tcs,subset='2021-08-01/',theme='white',TA="addRSI(n=20)")

10
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

11
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

LAB No: 5 Candle stick charts with various graphical properties in R


Program:
> library(quantmod)
> getSymbols('WIPRO.NS',src='yahoo')
[1] "WIPRO.NS"
Warning message:
WIPRO.NS contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
> wip=WIPRO.NS
> chartSeries(wip)

> chartSeries(wip,theme='white')

> chartSeries(wip,theme='white',subset='2020::')

12
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

> chartSeries(wip,theme='white',subset='::2008')

> chartSeries(wip,theme='white',subset='2021-04-01/',up.col='blue',do.col='green')

13
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

LAB No: 6 Creating a portfolio and analysing risk and return as per
Markowitz model
14
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

Program:
> library(IntroCompFinR)
> library(PerformanceAnalytics)
> library(quantmod)
> basketsymbols=c('TCS.NS','INFY.NS','WIPRO.NS')
> getSymbols()
Warning message:
In getSymbols() : no Symbols specified
> getSymbols(basketsymbols,src='yahoo')
[1] "TCS.NS" "INFY.NS" "WIPRO.NS"
Warning messages:
1: TCS.NS contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
2: INFY.NS contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
3: WIPRO.NS contains missing values. Some functions will not work if objects contain
missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc
to remove or replace them.
> tcs=yearlyReturn(TCS.NS$TCS.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
> inf=yearlyReturn(INFY.NS$INFY.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
> wipro=yearlyReturn(WIPRO.NS$WIPRO.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :

15
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

missing values removed from data


> returns=c(0.2716,0.3820,0.6588)
> assetnames=c("TCS","INFOSYS","WIPRO")
> names(returns)=assetnames
> returns
TCS INFOSYS WIPRO
0.2716 0.3820 0.6588
>
covmat=matrix(c(CoVariance(tcs,tcs),CoVariance(tcs,inf),CoVariance(tcs,wipro),CoVariance(i
nf,tcs),CoVariance(inf,inf),CoVariance(inf,wipro),CoVariance(wipro,tcs),CoVariance(wipro,inf
),CoVariance(wipro,wipro)),nrow=3,ncol=3,byrow = T)
> covmat
[,1] [,2] [,3]
[1,] 0.3331735 0.2198708 0.2940903
[2,] 0.2198708 0.1741399 0.2136661
[3,] 0.2940903 0.2136661 0.2999242
> dimnames(covmat)=list(assetnames,assetnames)
> covmat
TCS INFOSYS WIPRO
TCS 0.3331735 0.2198708 0.2940903
INFOSYS 0.2198708 0.1741399 0.2136661
WIPRO 0.2940903 0.2136661 0.2999242
> w=rep(1,3)/3
> portfolio=getPortfolio(er=returns,cov.mat=covmat,weights=w)
> portfolio
Call:
getPortfolio(er = returns, cov.mat = covmat, weights = w)

Portfolio expected return: 0.4374667


Portfolio standard deviation: 0.5013861

16
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

Portfolio weights:
TCS INFOSYS WIPRO
0.3333 0.3333 0.3333

LAB No: 7 Creating a portfolio and analysing risk and return as per
Sharpe model

17
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

Program:
>basketsymbols=c('GODREJIND.BO','WIPRO.BO','BHARTIARTL.BO')
> getSymbols(basketsymbols,src='yahoo')
‘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] "GODREJIND.BO" "WIPRO.BO" "BHARTIARTL.BO"


Warning messages:
1: GODREJIND.BO contains missing values. Some functions will not work if objects contain
missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc
to remove or replace them.
2: WIPRO.BO contains missing values. Some functions will not work if objects contain
missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc
to remove or replace them.
3: BHARTIARTL.BO contains missing values. Some functions will not work if objects contain
missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc
to remove or replace them.
> View(GODREJIND.BO)
> View(WIPRO.BO)
> View(BHARTIARTL.BO)
> godrej=GODREJIND.BO[,4]
> View(godrej)
> wipro=WIPRO.BO[,4]
> View(wipro)
> bhartiair=BHARTIARTL.BO[,4]

18
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

> View(bhartiair)
> getSymbols('^BSESN',src='yahoo')
[1] "^BSESN"
Warning message:
^BSESN contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
> View(BSESN)
> bse=BSESN[,4]
> View(bse)
> #Regression for Godrej,bse
> fit1=lm(godrej~bse)
> fit1

Call:
lm(formula = godrej ~ bse)

Coefficients:
(Intercept) bse
37.0240 0.0114

> #godrej=37.0240+0.0114(bse)
> #when increases by 10
> godrejj=37.0240+0.0114*10
> godrejj
[1] 37.138
> #Regression for Wipro,bse
> fit2=lm(wipro~bse)
> fit2

19
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

Call:
lm(formula = wipro ~ bse)

Coefficients:
(Intercept) bse
-24.076887 0.008635

> #wipro=-24.076887+0.008635(wipro)
> #when increased by 10
> wiproo=-24.076887+0.008635*10
> wiproo
[1] -23.99054
> #Regression for Bhartiair,bse
> fit3=lm(bhartiair~bse)
> fit3

Call:
lm(formula = bhartiair ~ bse)

Coefficients:
(Intercept) bse
2.211e+02 5.318e-03

> #bharti=2.211e+02 +5.318e-03(bse)


> bharti=(2.211e+02)+(5.318e-03)*10
> bharti
[1] 221.1532
> #portfolio return=sigma(invidual return*individual weights)
> portfolioreturn=(godrejj*0.40)+(wiproo*0.20)+(bharti*0.40)

20
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

> portfolioreturn
[1] 98.51836
> #Portfolio return consisting of Godrej, Wipro and Bhartiairtel is 98.51836

LAB No: 8 Creating a portfolio and analysing risk and return as per
CAPM model
Program:

21
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

> library(quantmod)
> basketsymbols=c('SBIN.NS','TCS.NS')
> getSymbols(basketsymbols,src='yahoo')
[1] "SBIN.NS" "TCS.NS"
Warning messages:
1: SBIN.NS contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
2: TCS.NS contains missing values. Some functions will not work if objects contain missing
values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to
remove or replace them.
ar=annualReturn(SBIN.NS$SBIN.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
> ar2=annualReturn(TCS.NS$TCS.NS.Close)
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
missing values removed from data
> View(ar)
> View(ar2)
> data=as.xts(merge(ar,ar2))
> View(data)
> class(data)
[1] "xts" "zoo"
> summary(lm(data$yearly.returns~data$yearly.returns.1))

Call:
lm(formula = data$yearly.returns ~ data$yearly.returns.1)

Residuals:

22
BUSINESS ANALYTICS IN FINANCE 2001510088- AFZAL KHAN

Min 1Q Median 3Q Max


-0.54428 -0.37719 0.00428 0.21717 0.80587

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.1172 0.1275 0.919 0.375
data$yearly.returns.1 0.2298 0.2060 1.115 0.285

Residual standard error: 0.4437 on 13 degrees of freedom


Multiple R-squared: 0.08735, Adjusted R-squared: 0.01715
F-statistic: 1.244 on 1 and 13 DF, p-value: 0.2848

> CAPM.beta(Ra=data$yearly.returns,Rb=data$yearly.returns.1)
[1] 0.2297738
> CAPM.alpha(Ra=data$yearly.returns,Rb=data$yearly.returns.1)
[1] 0.117164

23

You might also like