library(quantmod)
library(fGarch)
URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KS11"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:100),]
attach(odat)
x<-odat[order(Date),]
fit <- arima(diff(x$Close), order=c(1,0,2))
err <- rnorm(30, 0, sd(diff(x$Close)))
sim <- diff(x$Close)[99]
for (t in 2:30) {
sim[t] <- fit$coef[1]*sim[t-1] + fit$coef[2]*err[t-1]*0.5
+ fit$coef[3]*err[t-2]*0.5
}
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
x$newday <- as.Date(x$Date, "%m/%d/%Y")
plot(x$newday, x$Close, type='l', col='red',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
points(x$newday, x$Close, pch = 20, col = "red")
ye <- x$Close[100]
td<-x$newday[length(x$newday)]+1
for (j in 2:30) {
td[j] <- x$newday[length(x$newday)] + j
ye[j] <- sim[j-1]+ye[j-1]
}
td<-as.Date(td)
points(td, ye, col='red', pch=20, type='b')
plot(diff(x$Close), type='l', col=heat.colors(32)[15],panel.first=grid(col = "gray", lty = "dotted"))
##########################################################
library(forecast)
fit <- auto.arima(x$Close)
fore <- forecast(fit, h=10)
plot(fore)
ye
2014년 3월 4일 화요일
2014년 3월 2일 일요일
코스닥 주기 68일
URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KQ11"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:170),]
attach(odat)
x<-odat[order(Date),]
ret <- 0
for (i in 1:length(x$Close)) {
ret[i] <- (x$Close[i]-x$Close[1])/x$Close[1]*100
}
par(mfrow=c(2,1))
par(mar=c(3,3,1,2))
sample.t<-seq(1:length(ret))
sample.v<- ret
plot(sample.t, sample.v, type='l')
lines(sample.t, sample.v, lwd=2, col=heat.colors(32)[15])
## FFT stuff goes in here
##
x<- sample.v
per <- abs(fft(x-mean(x)))^2/length(x) # Mod() and abs() same here
# if you want a nice picture:
freq = (1:length(x)-1)/length(x)
plot(freq, per, xlim=c(0,.2), type='o', lwd=2, col=heat.colors(32)[15])
text(freq, per, round(freq,digits=3), cex=0.6, pos=4, col=heat.colors(32)[15])
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:170),]
attach(odat)
x<-odat[order(Date),]
ret <- 0
for (i in 1:length(x$Close)) {
ret[i] <- (x$Close[i]-x$Close[1])/x$Close[1]*100
}
par(mfrow=c(2,1))
par(mar=c(3,3,1,2))
sample.t<-seq(1:length(ret))
sample.v<- ret
plot(sample.t, sample.v, type='l')
lines(sample.t, sample.v, lwd=2, col=heat.colors(32)[15])
## FFT stuff goes in here
##
x<- sample.v
per <- abs(fft(x-mean(x)))^2/length(x) # Mod() and abs() same here
# if you want a nice picture:
freq = (1:length(x)-1)/length(x)
plot(freq, per, xlim=c(0,.2), type='o', lwd=2, col=heat.colors(32)[15])
text(freq, per, round(freq,digits=3), cex=0.6, pos=4, col=heat.colors(32)[15])
2014년 3월 1일 토요일
2014년 2월 24일 월요일
kosdaq arma
library(quantmod)
library(fGarch)
URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KQ11"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:100),]
attach(odat)
x<-odat[order(Date),]
#kospiGarch <- garchFit(~arma(1, 2) + garch(1, 1), data=as.ts(x$Close))
fit <- arima(diff(log(x$Close)), order=c(1,0,1))
#fore <- predict(fit, n.ahead=10, doplot=F)
err <- rnorm(30, 0, sd(diff(log(x$Close))))
sim <- diff(log(abs(x$Close)))[99]
for (t in 2:30) {
sim[t] <- fit$coef[1]*sim[t-1] + fit$coef[2]*err[t-1]
}
dev.new()
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
x$newday <- as.Date(x$Date, "%m/%d/%Y")
plot(x$newday, x$Close, type='l', col='red',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
points(x$newday, x$Close, pch = 20, col = "red")
ye <- x$Close[100]
td<-x$newday[length(x$newday)]+1
for (j in 2:30) {
td[j] <- x$newday[length(x$newday)] + j
ye[j] <- exp(sim[j-1])*ye[j-1]
}
td<-as.Date(td)
ye
points(td, ye, col='red', pch=20, type='b')
##########################################################
library(forecast)
fit <- arima(x$Close, order=c(1,1,10))
fore <- forecast(fit, h=15)
plot(fore)
fore
library(fGarch)
URL <- "http://ichart.finance.yahoo.com/table.csv?s=^KQ11"
dat <- read.csv(URL)
dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:100),]
attach(odat)
x<-odat[order(Date),]
#kospiGarch <- garchFit(~arma(1, 2) + garch(1, 1), data=as.ts(x$Close))
fit <- arima(diff(log(x$Close)), order=c(1,0,1))
#fore <- predict(fit, n.ahead=10, doplot=F)
err <- rnorm(30, 0, sd(diff(log(x$Close))))
sim <- diff(log(abs(x$Close)))[99]
for (t in 2:30) {
sim[t] <- fit$coef[1]*sim[t-1] + fit$coef[2]*err[t-1]
}
dev.new()
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
x$newday <- as.Date(x$Date, "%m/%d/%Y")
plot(x$newday, x$Close, type='l', col='red',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
points(x$newday, x$Close, pch = 20, col = "red")
ye <- x$Close[100]
td<-x$newday[length(x$newday)]+1
for (j in 2:30) {
td[j] <- x$newday[length(x$newday)] + j
ye[j] <- exp(sim[j-1])*ye[j-1]
}
td<-as.Date(td)
ye
points(td, ye, col='red', pch=20, type='b')
##########################################################
library(forecast)
fit <- arima(x$Close, order=c(1,1,10))
fore <- forecast(fit, h=15)
plot(fore)
fore
변동성 garch(1)
library(tseries)
library(quadprog)
getwd()
setwd('c:/rdata')
###here kospi data
dat <- read.csv('port.csv', header=TRUE)
#dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:nrow(dat)),]
apply(na.omit(odat),2,max)
attach(odat)
x<-odat[order(일자),]
apply(na.omit(x),2,max)
price <- as.numeric(x$안랩)
ret <- 0
for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]*100
}
chg <-0
for (i in 2:length(price)) {
chg[i] <- (price[i]-price[i-1])/price[i-1]*100
}
#garch
mod.fit<-garch(x = ret - mean(ret), order = c(0,1), trace = TRUE) #Fit ARCH(1)
summary(mod.fit) #Summarizes fit
names(mod.fit)
mod.fit$coef
# plot(mod.fit)
coef(mod.fit) # see above and compare with coefficient alpha0 and alpha1 caculated by arch model
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
plot(mod.fit$fitted.values[,1], type='l', xaxt='n',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
title(main = "변동성", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
plot(chg, type='l', xaxt='n'
)
points(chg, pch = 20, col = "red")
title(main = "일등락율%", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
plot(ret, type='l', xaxt='n'
)
points(ret, pch = 20, col = "red")
title(main = "수익률%", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
library(quadprog)
getwd()
setwd('c:/rdata')
###here kospi data
dat <- read.csv('port.csv', header=TRUE)
#dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:nrow(dat)),]
apply(na.omit(odat),2,max)
attach(odat)
x<-odat[order(일자),]
apply(na.omit(x),2,max)
price <- as.numeric(x$안랩)
ret <- 0
for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]*100
}
chg <-0
for (i in 2:length(price)) {
chg[i] <- (price[i]-price[i-1])/price[i-1]*100
}
#garch
mod.fit<-garch(x = ret - mean(ret), order = c(0,1), trace = TRUE) #Fit ARCH(1)
summary(mod.fit) #Summarizes fit
names(mod.fit)
mod.fit$coef
# plot(mod.fit)
coef(mod.fit) # see above and compare with coefficient alpha0 and alpha1 caculated by arch model
par(mfrow=c(3,1))
par(mar=c(3,3,1,2))
plot(mod.fit$fitted.values[,1], type='l', xaxt='n',
panel.first=grid(col = "gray", lty = "dotted"), pch=20)
title(main = "변동성", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
plot(chg, type='l', xaxt='n'
)
points(chg, pch = 20, col = "red")
title(main = "일등락율%", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
plot(ret, type='l', xaxt='n'
)
points(ret, pch = 20, col = "red")
title(main = "수익률%", sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE)
2014년 2월 23일 일요일
최소분산포트폴리오
# Copyright 2013 LBB. All rights reserved.
# Author: Gray Fox
# Description: PortOpt-3Fund.R
# Optimize 3-Fund Portfolio
# Date: 07-March-2013
## source and library statements
library(tseries)
getwd()
setwd('c:/rdata')
## CHANGE THE PATH TO YOUR LOCAL DIRECTORY
source("portfolio_noshorts.r")
options(digits=4, width=70)
## constants
shorts=FALSE
library(tseries)
library(quadprog)
getwd()
setwd('c:/rdata')
###here kospi data
dat <- read.csv('port.csv', header=TRUE)
#dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:nrow(dat)),]
attach(odat)
x<-odat[order(Date),]
#x<-odat
price1 <- as.numeric(x$로만손)
price2 <- as.numeric(x$KB금융)
price3 <- as.numeric(x$동서)
#price4 <- as.numeric(x$동서)
ret1 <- 0
ret2 <- 0
ret3 <- 0
#ret4 <- 0
for (i in 1:length(price1)) {
ret1[i] <- (price1[i]-price1[1])/price1[1]
ret2[i] <- (price2[i]-price2[1])/price2[1]
ret3[i] <- (price3[i]-price3[1])/price3[1]
#ret4[i] <- (price3[i]-price3[1])/price3[1]
}
Returns.z <- cbind(ret1, ret2, ret3)
## constants
shorts=FALSE
r.free.a <- 0.025 # T-bill Rate
r.free <- r.free.a/12
start_date <- "2013-11-13"
end_date <- "2014-02-21"
#Prices.z <- merge(price1, price2, price3)
colnames(Returns.z) = c("로만손", "KB금융", "동서")
# Plot 1. Monthly cc returns vs time
plot(Returns.z, plot.type="single", lty=1:3, col=1:3, lwd=2)
legend(x="bottomleft", legend=colnames(Returns.z), lty=1:3, col=1:3, lwd=2)
abline(h=0)
title("Monthly cc returns")
## Plot 2. Pairwise scatterplot
#pairs(coredata(Returns.z), col="blue", pch=16)
## Compute estimates of CER model parameters
muhat.vals <- apply(Returns.z, 2, mean)
sigma2hat.vals <- apply(Returns.z, 2, var)
sigmahat.vals <- apply(Returns.z, 2, sd)
cov.mat <- var(Returns.z)
cor.mat <- cor(Returns.z)
###############################################################################
## GMVP
gmin.port <- globalMin.portfolio(muhat.vals, cov.mat, shorts=FALSE)
summary(gmin.port)
gmin.SR = (12*gmin.port$er - r.free.a) / (sqrt(12)*gmin.port$sd)
print("GMVP Sharpe Ratio"); print(gmin.SR)
## Plot 3. GMVP weights
plot(gmin.port, col="blue")
## Tangency portfolio
tan.port <- tangency.portfolio(muhat.vals, cov.mat, risk.free=r.free, shorts=shorts)
summary(tan.port)
tp.SR.m = (tan.port$er - r.free) / (tan.port$sd)
tp.SR = (12*tan.port$er - r.free.a) / (sqrt(12)*tan.port$sd)
print("TP Sharpe Ratio"); print(tp.SR)
## Plot 4. Tangency portfolio weights
plot(tan.port, col="blue")
## Efficient Frontier
ef <- efficient.frontier(muhat.vals, cov.mat, nport=21, alpha.min=-10, alpha.max=10, shorts=shorts)
## Plot 6. Efficient Frontier allowing short sales
plot(ef, plot.assets=T, col="blue", pch=16, cex=1)
points(tan.port$sd, tan.port$er, col="green", pch=16, cex=1)
text(tan.port$sd, tan.port$er, labels="TP", pos=2)
abline(a=r.free, b=tp.SR.m, col="green", lwd=2) ## CAL
print("Done")
# Author: Gray Fox
# Description: PortOpt-3Fund.R
# Optimize 3-Fund Portfolio
# Date: 07-March-2013
## source and library statements
library(tseries)
getwd()
setwd('c:/rdata')
## CHANGE THE PATH TO YOUR LOCAL DIRECTORY
source("portfolio_noshorts.r")
options(digits=4, width=70)
## constants
shorts=FALSE
library(tseries)
library(quadprog)
getwd()
setwd('c:/rdata')
###here kospi data
dat <- read.csv('port.csv', header=TRUE)
#dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
odat<-dat[c(1:nrow(dat)),]
attach(odat)
x<-odat[order(Date),]
#x<-odat
price1 <- as.numeric(x$로만손)
price2 <- as.numeric(x$KB금융)
price3 <- as.numeric(x$동서)
#price4 <- as.numeric(x$동서)
ret1 <- 0
ret2 <- 0
ret3 <- 0
#ret4 <- 0
for (i in 1:length(price1)) {
ret1[i] <- (price1[i]-price1[1])/price1[1]
ret2[i] <- (price2[i]-price2[1])/price2[1]
ret3[i] <- (price3[i]-price3[1])/price3[1]
#ret4[i] <- (price3[i]-price3[1])/price3[1]
}
Returns.z <- cbind(ret1, ret2, ret3)
## constants
shorts=FALSE
r.free.a <- 0.025 # T-bill Rate
r.free <- r.free.a/12
start_date <- "2013-11-13"
end_date <- "2014-02-21"
#Prices.z <- merge(price1, price2, price3)
colnames(Returns.z) = c("로만손", "KB금융", "동서")
# Plot 1. Monthly cc returns vs time
plot(Returns.z, plot.type="single", lty=1:3, col=1:3, lwd=2)
legend(x="bottomleft", legend=colnames(Returns.z), lty=1:3, col=1:3, lwd=2)
abline(h=0)
title("Monthly cc returns")
## Plot 2. Pairwise scatterplot
#pairs(coredata(Returns.z), col="blue", pch=16)
## Compute estimates of CER model parameters
muhat.vals <- apply(Returns.z, 2, mean)
sigma2hat.vals <- apply(Returns.z, 2, var)
sigmahat.vals <- apply(Returns.z, 2, sd)
cov.mat <- var(Returns.z)
cor.mat <- cor(Returns.z)
###############################################################################
## GMVP
gmin.port <- globalMin.portfolio(muhat.vals, cov.mat, shorts=FALSE)
summary(gmin.port)
gmin.SR = (12*gmin.port$er - r.free.a) / (sqrt(12)*gmin.port$sd)
print("GMVP Sharpe Ratio"); print(gmin.SR)
## Plot 3. GMVP weights
plot(gmin.port, col="blue")
## Tangency portfolio
tan.port <- tangency.portfolio(muhat.vals, cov.mat, risk.free=r.free, shorts=shorts)
summary(tan.port)
tp.SR.m = (tan.port$er - r.free) / (tan.port$sd)
tp.SR = (12*tan.port$er - r.free.a) / (sqrt(12)*tan.port$sd)
print("TP Sharpe Ratio"); print(tp.SR)
## Plot 4. Tangency portfolio weights
plot(tan.port, col="blue")
## Efficient Frontier
ef <- efficient.frontier(muhat.vals, cov.mat, nport=21, alpha.min=-10, alpha.max=10, shorts=shorts)
## Plot 6. Efficient Frontier allowing short sales
plot(ef, plot.assets=T, col="blue", pch=16, cex=1)
points(tan.port$sd, tan.port$er, col="green", pch=16, cex=1)
text(tan.port$sd, tan.port$er, labels="TP", pos=2)
abline(a=r.free, b=tp.SR.m, col="green", lwd=2) ## CAL
print("Done")
2014년 2월 7일 금요일
git 사용법
git bash를 연다
사용자 이름 및 이메일주소 등록
git --global username
git --global email
// git 초기화
git init
// 저장소에 파일 추가
git add *.c
git add README
사용자 이름 및 이메일주소 등록
git --global username
git --global email
// git 초기화
git init
// 저장소에 파일 추가
git add *.c
git add README
피드 구독하기:
글 (Atom)




