2009年3月28日土曜日

GMMAのコード

Rでの適当コード。
ベタ書きの手抜きし過ぎー

# GUPPY MULTIPLE MOVING AVERAGES
#
# These are two groups of exponential moving averages.
# The short term group is a 3, 5, 8, 10, 12 and 15 day moving averages.
# This is a proxy for the behaviour of short term traders and speculators in the market.
#
# The long term group is made up of 30, 35, 40, 45, 50 and 60 day moving averages.
# This is a proxy for the long term investors in the market.
#
# The relationship within each of these groups tells us when there is agreement on value - when they are
# close together - and when there is disagreement on value - when they are well spaced apart.
#
# The relationship between the two groups tells the trader about the strength of the market action.
# A change in price direction that is well supported by both short and long term investors
# signals a strong trading opportunity. The crossover of the two groups of moving averages is not as
# important as the relationship between them.
# When both groups compress at the same time it alerts the trader to increased price volatility
# and the potential for good trading opportunities.

GMMA <- function(xxx) {
nn <- length(xxx)
attributes(xxx)$tsp <- c(1, nn, 1)

# short
yt03 <- yt05 <- yt08 <- yt10 <- yt12 <- yt15 <- c()
yt30 <- yt35 <- yt40 <- yt45 <- yt50 <- yt60 <- c()

yt03_2 <- yt05_2 <- yt08_2 <- yt10_2 <- yt12_2 <- yt15_2 <- c()
yt30_2 <- yt35_2 <- yt40_2 <- yt45_2 <- yt50_2 <- yt60_2 <- c()

a03 <- 1 - 2 / (3 + 1)
a05 <- 1 - 2 / (5 + 1)
a08 <- 1 - 2 / (8 + 1)
a10 <- 1 - 2 / (10 + 1)
a12 <- 1 - 2 / (12 + 1)
a15 <- 1 - 2 / (15 + 1)

a30 <- 1 - 2 / (30 + 1)
a35 <- 1 - 2 / (35 + 1)
a40 <- 1 - 2 / (40 + 1)
a45 <- 1 - 2 / (45 + 1)
a50 <- 1 - 2 / (50 + 1)
a60 <- 1 - 2 / (60 + 1)

for (ii in 1:nn) {
yt03[ii] <- xxx[ii] * a03^(nn - ii)
yt05[ii] <- xxx[ii] * a05^(nn - ii)
yt08[ii] <- xxx[ii] * a08^(nn - ii)
yt10[ii] <- xxx[ii] * a10^(nn - ii)
yt12[ii] <- xxx[ii] * a12^(nn - ii)
yt15[ii] <- xxx[ii] * a15^(nn - ii)

yt30[ii] <- xxx[ii] * a30^(nn - ii)
yt35[ii] <- xxx[ii] * a35^(nn - ii)
yt40[ii] <- xxx[ii] * a40^(nn - ii)
yt45[ii] <- xxx[ii] * a45^(nn - ii)
yt50[ii] <- xxx[ii] * a50^(nn - ii)
yt60[ii] <- xxx[ii] * a60^(nn - ii)
}

# short group, 3, 5, 8, 10, 12, 15
# long group, 30, 35, 40, 45, 50, 60
for (ii in 1:nn) {
yt03_2[ii] <- (1 - a03) * (sum(yt03[seq(1,ii)]) / a03^(nn - ii))
yt05_2[ii] <- (1 - a05) * (sum(yt05[seq(1,ii)]) / a05^(nn - ii))
yt08_2[ii] <- (1 - a08) * (sum(yt08[seq(1,ii)]) / a08^(nn - ii))
yt10_2[ii] <- (1 - a10) * (sum(yt10[seq(1,ii)]) / a10^(nn - ii))
yt12_2[ii] <- (1 - a12) * (sum(yt12[seq(1,ii)]) / a12^(nn - ii))
yt15_2[ii] <- (1 - a15) * (sum(yt15[seq(1,ii)]) / a15^(nn - ii))

yt30_2[ii] <- (1 - a30) * (sum(yt30[seq(1,ii)]) / a30^(nn - ii))
yt35_2[ii] <- (1 - a35) * (sum(yt35[seq(1,ii)]) / a35^(nn - ii))
yt40_2[ii] <- (1 - a40) * (sum(yt40[seq(1,ii)]) / a40^(nn - ii))
yt45_2[ii] <- (1 - a45) * (sum(yt45[seq(1,ii)]) / a45^(nn - ii))
yt50_2[ii] <- (1 - a50) * (sum(yt50[seq(1,ii)]) / a50^(nn - ii))
yt60_2[ii] <- (1 - a60) * (sum(yt60[seq(1,ii)]) / a60^(nn - ii))
}

M1 <- max(xxx)
M2 <- min(xxx)

# origianl
plot(xxx, xlim = c(1, nn), ylim = c(M2, M1))

# red dasshed
par(new=T, col=2)
plot(ts(yt03_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=2)
plot(ts(yt05_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=2)
plot(ts(yt08_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=2)
plot(ts(yt10_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=2)
plot(ts(yt12_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=2)
plot(ts(yt15_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")

# blue dotted
par(new=T, col=4)
plot(ts(yt30_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=4)
plot(ts(yt35_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=4)
plot(ts(yt40_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=4)
plot(ts(yt45_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=4)
plot(ts(yt50_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")
par(new=T, col=4)
plot(ts(yt60_2), xlim = c(1, nn), ylim = c(M2, M1), ylab = "")

title("GMMA(EMA) - (Short(red), Long(blue))")
}

0 件のコメント:

コメントを投稿