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)





2014년 3월 12일 수요일

ar2kospi

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,0))

sim <- diff(log(abs(x$Close)))[98]

sim <- c(sim, diff(log(abs(x$Close)))[99])

for (t in 3:20) {

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

}


dev.new()

par(mfrow=c(3,1))

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

plot(x$Close, type='l', xlim=c(0, 120), xaxt='n',

    panel.first=grid(col = "gray", lty = "dotted"), pch=20)

t <- c(101:120)

points(x$Close, pch = 20, col = "red")

ye <- x$Close[100]

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

for (j in 2:20) {

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

}

ye

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

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

library(forecast)

fit <- auto.arima(x$Close[1:80])
#library(dataframes2xls)
#write.xls(forecast(fit, h=20), "c:/mydata.xls")

write.table(x, "c:/kospi.txt")

plot(forecast(fit, h=20))




2014년 3월 7일 금요일

[perl] 이미지가로 500px 작으면 삭제

use strict;
#use warnings;
use Cwd;

use File::Find;
use Image::Size;

if (@ARGV <1){
print "Useage: $0 [string to search] [file type]\n";
exit 1;
}
 opendir (DIR, $ARGV[0]) or die $!;
   
    my @list;
chdir($ARGV[0]);   
  while (my $file = readdir(DIR)) {

   my ($width, $height ) = imgsize($file);
       
   #print $width;
    if ($width < 500) {
    push(@list,$file);
    }

    }
   
unlink @list;

2014년 3월 4일 화요일

arma(1,1) kospi

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월 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])



2014년 3월 1일 토요일

qone8 악성코드 제거

파이어폭스 주소창에
about:config

명령으로 고급환경설정으로 들어감

qone8 검색 후
초기화