-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_script.R
101 lines (90 loc) · 2.93 KB
/
sample_script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Install and activate these 4 packages in RStudio
lapply(c("quantmod",
"PortfolioAnalytics",
"timeSeries",
"fBasics"
),
require,
character.only = TRUE
)
# Then, select tickers of stocks you want to get data of
tickers <- c("1801.HK",
"6618.HK",
"1093.HK",
"1177.HK",
"2269.HK"
)
# Set up any start date you need
start_date <- "2017-10-17"
# Here you can set up time period that you would like to analyse
portfolioPrices <- NULL
for (Ticker in tickers)
portfolioPrices <- cbind(portfolioPrices,
getSymbols(Ticker,
src = "yahoo",
auto.assign=FALSE
)[,4]
)
portfolioPrices <- portfolioPrices[apply(portfolioPrices,
1,
function(x) all(!is.na(x))
),
]
colnames(portfolioPrices) <- tickers
portfolioReturns <- ROC(portfolioPrices,
type = "discrete")
portfolioReturns <-as.timeSeries(portfolioPrices)
# Get first values of stock prices
head(portfolioReturns)
# Of course you want to get returns
lrtn=diff(log(portfolioReturns)
)
lrtn <- lrtn[-1,]
# Check values
head(lrtn)
# To calculate basic stats in one matrix
stats1 <- t(apply(lrtn,
2,
function(lrtn) c(mean(lrtn),
quantile(lrtn,
na.rm = T,
probs = c(0.25)
),
median(lrtn),
quantile(lrtn,
na.rm = T,
probs = c(0.75)
),
sd(lrtn),
skewness(lrtn),
kurtosis(lrtn)
)
)
)
colnames(stats1) <- c("Mean",
"25%",
"50%",
"75%",
"SD",
"Skewness",
"Kurtosis")
stats1
# To calculate Sharpe Ratio
t(SharpeRatio(lrtn,
Rf = 0.05,
p = 0.95
)
)
# If you would like to know correlation between stocks
cor(lrtn)
# Boxplot
boxPlot(lrtn,
main = "Hong Kong Stocks",
title = FALSE,
xlab = "Source: Yahoo Finance",
ylab = "Returns"
)
# For those who need returns' histogram with normal distribution
pdf("myfile.pdf")
histPlot(lrtn)
dev.off()