2014년 3월 30일 일요일

계절성 분석

library(tseries)
library(quadprog)
getwd()
setwd('c:/rdata')

###here kospi data
dat <- read.csv('hite.csv', header=TRUE)

odat<-dat[c(1:nrow(dat)),]

attach(odat)

x<-odat[order(일자),]

x$newday <- as.Date(x$일자, "%Y-%m-%d")

price <- as.numeric(gsub( ",", "", as.vector(x$종가)))


par(mfrow=c(3,1))

par(mar=c(3,3,1,2))

plot(price, type='l')

time<-c(1:30)

lm1 <-lm(price[1:30]~time)

err <- price[1:30] - (lm1$coeff[2]*time+lm1$coeff[1])


time<-c(31:40)

lm1 <-lm(price[31:40]~time)

err <- c(err, (price[31:40] - (lm1$coeff[2]*time+lm1$coeff[1])))

time<-c(41:50)

lm1 <-lm(price[41:50]~time)

err <- c(err, (price[41:50] - (lm1$coeff[2]*time+lm1$coeff[1])))


plot(x$newday[1:50], err, type='l', col='red',
    panel.first=grid(col = "gray", lty = "dotted"), pch=20)
points(x$newday[1:50], err, pch = 20, col = "red")
axis.Date(side = 1, x$newday[1:50])

2014년 3월 28일 금요일

베타중립포트폴리오

library(tseries)
library(quadprog)
getwd()
setwd('c:/rdata')

###here kospi data
dat <- read.csv('port3.csv', header=TRUE)

odat<-dat[c(1:nrow(dat)),]

attach(odat)

x<-odat[order(일자),]

ret <- 0
ret2<-0
ret3<-0


kospi <- as.numeric(as.vector(x$코스피))
kosdaq <- as.numeric(as.vector(x$코스닥))


beta <- 0
beta2 <-0

price <- as.numeric(as.vector(x$sk하이닉스))
for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]
ret2[i] <- (kospi[i]-kospi[1])/kospi[1]
ret3[i] <- (kosdaq[i]-kosdaq[1])/kosdaq[1]
}

beta[1] <- cov(ret, ret2)/sd(ret2)^2
beta2[1] <- cov(ret, ret3)/sd(ret3)^2



price <- as.numeric(as.vector(x$하이트진로))
for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]
}


beta[2] <- cov(ret, ret2)/sd(ret2)^2
beta2[2] <- cov(ret, ret3)/sd(ret3)^2


price <- as.numeric(as.vector(x$lg전자))
for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]
}


beta[3] <- cov(ret, ret2)/sd(ret2)^2
beta2[3] <- cov(ret, ret3)/sd(ret3)^2

price <- as.numeric(gsub( ",", "", as.vector(x$삼성중공업)))



for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]
}



beta[4] <- cov(ret, ret2)/sd(ret2)^2
beta2[4] <- cov(ret, ret3)/sd(ret3)^2

price <- as.numeric(as.vector(x$삼성전기))


for (i in 1:length(price)) {
ret[i] <- (price[i]-price[1])/price[1]
}

beta[5] <- cov(ret, ret2)/sd(ret2)^2
beta2[5] <- cov(ret, ret3)/sd(ret3)^2



####### 베타중립 포트폴리오 구성 몬테카를로 시뮬레이션 이용 ##########

objbeta <- 0
nwsum <- 1
w<-0
nw <-0

d <- ncol(x)

n<-5

for (r in 1:1000){
w <- runif(n, 1, 100)

sw <- w/sum(w)

for (i in 1:n){
wsum <- sw[i]*beta[i]
}

if (abs(wsum) < nwsum) {
nwsum <- wsum
nw <- sw
cat("objective function", nwsum, sep="\n")
}
}

names = c('sk하이닉스', '하이트진로', 'lg전자', '삼성중공업', '삼성전기')

names
cat("optimized value of beta is:", nwsum, "under weights of stocks as following", sep="\n")
names
round(nw,2)



####

plot(x=beta, y=beta2, xlim=c(-3,3), ylim=c(-3,3), pch=20, col='red',
 panel.first=grid(col = "gray", lty = "dotted"),

xlab="beta with kospi", ylab="beta with kosdaq")

text(x=beta, y=beta2, labels = names, adj=c(0,1))

####






2014년 3월 22일 토요일

5초 후 자동으로 사라짐

<!DOCTYPE html>

<html>

<title>Web Page Design</title>

<head>

<style type="text/css">


#box {  
    height: 300px;  
    position: absolute;
    top: 0;
    left: 0;  
    width: 500px;
    opacity: 0;  
    transition: opacity 3s ease;
}

</style>

<body onload="document.getElementById('box').style.opacity='1'">
<div id="box"></div>



<script>
    ni = document.getElementById('box');

  var newdiv = document.createElement('div');

   newdiv.setAttribute('id','box2');

  newdiv.innerHTML = 'This is test';
  ni.appendChild(newdiv);

var myVar=setTimeout(function(){Notifr()},5000);

function Notifr()
{
    document.getElementById('box2').remove();
}


</script>


</body>
</html>

css 서서히 나타나는 효과

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<style type="text/css">
body{
opacity:0;
-moz-transition: opacity 2s;
-webkit-transition: opacity 2s; /* Safari */
}

</style>
</head>
<body onload="document.body.style.opacity='1'">
this string is to test
</body>
</html>


-------------------
 div id를 이용


<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<style type="text/css">

#box {   
    height: 300px;   
    position: absolute;
    top: 0;
    left: 0;   
    width: 500px;
    opacity: 0;   
    transition: opacity 3s ease;
}
</style>
<body onload="document.getElementById('box').style.opacity='1'">
<div id="box">
       This is test</div>
</body>
</html>

2014년 3월 16일 일요일

prediction of kospi with arma(2,2)

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(log(x$Close)), order=c(2,0,2))

sim <- diff(log(abs(x$Close)))[99]
sim <- c(sim, diff(log(abs(x$Close)))[98])
err <- rnorm(30, 0, sd(diff(log(x$Close[length(x$Close)-10:length(x$Close)]))))

upcount = 1
downcount = 1

ye <-x$Close[100]

td<-x$Date[100]
group<-rep(i, length(ye))

pred<-data.frame(td, ye, group)

// 시뮬레이션 회수
n<-100

for (i in 1:n){

for (t in 3:100) {

    sim[t] <- fit$coef[1]*sim[t-1] + fit$coef[2]*sim[t-2]
        +fit$coef[3]*err[t-1]
        +fit$coef[4]*err[t-2]

}


err <- rnorm(100, 0, 15)

// 20일간 주가를 예측함
for (j in 2:20) {

td[j] <- x$Date[length(x$Date)] + j

ye[j] <- exp(sim[j-1])*ye[j-1]+err[j-1]

pred<- rbind(pred, data.frame(td=td[j], ye=ye[j], group=i))

}


if (ye[20] > x$Close[100]){
        upcount = upcount+1}
else downcount = downcount +1

}

// 상승확률과 하락확률
cat("Probabilty of upward is", upcount/n)
cat("Probabilty of downward is", downcount/n)


library(ggplot2)
ggplot(pred, aes(x=td, y=ye, colour=group, group=group))+geom_line()




2014년 3월 13일 목요일

arma(2,2)

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(log(x$Close)), order=c(2,0,2))

#fore <- predict(fit, n.ahead=10, doplot=F)

sim <- diff(log(abs(x$Close)))[99]
sim <- c(sim, diff(log(abs(x$Close)))[98])
err <- rnorm(30, 0, sd(diff(log(x$Close[length(x$Close)-10:length(x$Close)]))))


for (t in 3:30) {

    sim[t] <- fit$coef[1]*sim[t-1] + fit$coef[2]*sim[t-2]
        +fit$coef[3]*err[t-1]
        +fit$coef[4]*err[t-2]

}

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', xlim=c(x$newday[1],x$newday[length(x$newday)]+20),

    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

err <- rnorm(30, 0, sd(diff(x$Close[length(x$Close)-50:length(x$Close)])))

for (j in 2:30) {

td[j] <- x$newday[length(x$newday)] + j

ye[j] <- exp(sim[j])*ye[j-1]+err[j-1]

}

td<-as.Date(td)

ye

points(td, ye, col='red', pch=20, type='b')

##########################################################

library(forecast)

afit <- auto.arima(x$Close)

fore <- forecast(afit, h=15)

plot(fore)