Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@riqbal-k
riqbal-k / d1
Created October 11, 2022 16:29
data<-read.csv("FTSESF.csv")
head(data)
str(data)
getwd() # get working directory
setwd('') # set working directory
install.packages("PerformanceAnalytics")
library (PerformanceAnalytics) # load the package
ms<-read.csv("Microsoft.csv", header=TRUE) #load Microsoft daily prices
head(ms)
sp<-read.csv("SP.csv", header=TRUE) # load S and P 500 daily prices
head(sp)
class(ms)
class(sp)
@riqbal-k
riqbal-k / r2
Created October 16, 2022 13:14
require(zoo)
#Microsoft
ms$Dates <- as.Date(ms$Dates, "%d/%m/%Y")
class(ms$Dates)
ms.z = zoo(x=ms$Close, order.by=ms$Dates)
#S and P 500
sp$Dates <- as.Date(sp$Dates, "%d/%m/%Y")
class(sp$Dates)
sp.z = zoo(x=sp$Close, order.by=sp$Dates)
@riqbal-k
riqbal-k / r3
Last active October 16, 2022 13:19
ms.monthly<-to.monthly(ms.z,OHLC = FALSE)
head(ms.monthly)
sp.monthly<-to.monthly(sp.z,OHLC = FALSE)
head(sp.monthly)
@riqbal-k
riqbal-k / r4
Created October 16, 2022 13:27
#combine Microsoft and SP daily data
ms.sp.dp<-merge(ms.z, sp.z)
head(ms.sp.dp)
#combine Microsoft and sP monthly data
ms.sp.dm<-merge(ms.monthly, sp.monthly)
head(ms.sp.dm)
@riqbal-k
riqbal-k / r5
Created October 16, 2022 13:33
#Microsoft montly and daily log returns
ms.monthly.r<-Return.calculate(ms.monthly, method = "log")
ms.daily.r<-Return.calculate(ms.z, method = "log")
head(ms.monthly.r)
head(ms.daily.r)
#S and P montly and daily log returns
sp.monthly.r<-Return.calculate(sp.monthly, method = "log")
sp.daily.r<-Return.calculate(sp.z, method = "log")
head(sp.monthly.r)
head(sp.daily.r)
@riqbal-k
riqbal-k / r6
Created October 16, 2022 13:35
ms.monthly.r<-ms.monthly.r[-1]
ms.daily.r<-ms.daily.r[-1]
sp.monthly.r<-sp.monthly.r[-1]
sp.daily.r<-sp.daily.r[-1]
ms.sp.dp.r<-ms.sp.dp.r[-1]
ms.sp.dm.r<-ms.sp.dm.r[-1]
@riqbal-k
riqbal-k / r7
Created October 16, 2022 13:48
#Daily prices line plot for Microsoft
plot.zoo(ms.z, main = "Microsoft Daily Prices", xlab =
"Time", ylab = "Daily Prices", lwd = 2, col = "blue")
#Monthly prices line plot for Microsoft
plot.zoo(ms.monthly, main = "Microsoft Monthly Prices Plot",xlab =
"Time", ylab = "Monthly Prices", lwd = 2, col = "red")
@riqbal-k
riqbal-k / r8
Last active October 16, 2022 14:20
#plot for merged daily data
plot.zoo(ms.sp.dp, main = "Microsoft and S&P Daily Prices Plot", lwd = 2, col = "red")