在R中创建交易信号

时间:2015-05-21 04:45:31

标签: r quantmod trading

我构建了一个交易策略,并且陷入了两个关键领域。当在quantmod中使用Stoch和MACD时,我试图在慢速随机交叉快速随机(1)时创建一个信号,反之亦然(-1),并在(0)之间平缓。除了列名MACD和Signal之外,MACD代码是相同的。最后,当所有三个信号等于1,-1,0时,我试图合并三个信号以创建主信号。

library(quantmod)

####################
## BOLINGER BANDS ##
####################

getSymbols("SPY", src="yahoo", from="2013-01-01", to="2015-05-01")
x <- na.omit(merge(SPY, BBands(Cl(SPY))))

x$sig <- NA

# Flat where Close crossed the mavg
x$sig[c(FALSE, diff(sign(Cl(x) - x$mavg), na.pad=FALSE) != 0)] <- 0
x$sig[Cl(x) > x$up] <- -1 # short when Close is above up
x$sig[Cl(x) < x$dn] <- 1 # long when Close is below dn
x$sig[1] <- 0 # flat on the first day
x$sig[nrow(x)] <- 0 # flat on the last day

# Fill in the signal for other times
x$sig <- na.locf(x$sig) # wherever sig is NA, copy previous value to next row

# Now Lag your signal to reflect that you can't trade on the same bar that 
# your signal fires
x$sig <- Lag(x$sig)
x$sig[1] <- 0 # replace NA with zero position on first row

####################
### STOCHASTICS ####
####################

y <- na.omit(merge(SPY, stoch(Cl(SPY))))

y$sig <- NA

# Flat where  between crosses. Not sure how to write 
#y$sig[c(FALSE, diff(sign(y$slowD == y$fastD), na.pad=FALSE !=0)] <- 0
y$sig[y$fastD > y$slowD] <- -1 # short when Close is above up
y$sig[y$fastD < y$slowD] <- 1 # long when Close is below dn
y$sig[1] <- 0 # flat on the first day
y$sig[nrow(x)] <- 0 # flat on the last day

# Fill in the signal for other times
y$sig <- na.locf(y$sig) # wherever sig is NA, copy previous value to next row

# Now Lag your signal to reflect that you can't trade on the same bar that 
# your signal fires
y$sig <- Lag(y$sig)
y$sig[1] <- 0 

####################
###### MACD ########
####################

z <- na.omit(merge(SPY, MACD(Cl(SPY))))

z$sig <- NA

# Flat where  between crosses. Not sure how to write 
z$sig[c(FALSE, diff(sign(z$signal == z$macd), na.pad=FALSE) != 1)] <- 1
z$sig[z$signal > z$macd] <- -1 # short when Close is above up
z$sig[z$signal < z$macd] <- 1 # long when Close is below dn
z$sig[1] <- 0 # flat on the first day
z$sig[nrow(z)] <- 0 # flat on the last day

# Fill in the signal for other times
z$sig <- na.locf(z$sig) # wherever sig is NA, copy previous value to next row

# Now Lag your signal to reflect that you can't trade on the same bar that 
# your signal fires
z$sig <- Lag(z$sig)
z$sig[1] <- 0 

# Merge xyz by date and create new signal when all three conditions are met

2 个答案:

答案 0 :(得分:4)

更新:我在this answer之后使用diff修复了所有讨厌的循环。

这就是我如何解决这个问题。您正在计算具有所需关系的所有位置。您只希望第一个满足交易信号的头寸尽快对其采取行动。

我会像这样建立布林带信号:

price.over.up <- Cl(x) > x$up
price.under.dn <- Cl(x) < x$dn

x$sig <- rep(0,nrow(x))
#sell which price breaks top band
x$sig[which(diff(price.over.up)==1] <- -1
#buy when price breaks bottom band
x$sig[which(diff(price.under.dn)==1)] <- 1

x$sig <- Lag(x$sig)
x$sig[1] <- 0

我会像这样创建随机信号:

fast.over.slow <- y$fastD > y$slowD
y$sig <- rep(0,nrow(y))
y$sig[which(diff(fast.over.slow) == 1 & y$slowD < 0.2)] <- 1
y$sig[which(diff(fast.over.slow) == -1 & y$slowD > 0.8)] <- -1
y$sig <- Lag(y$sig)
y$sig[1] <- 0

计算差异后,您希望找到第一个交叉点,其中一个交叉点高于另一个交叉点,因此您需要考虑ii-1个位置。如果您处于超买或超卖区域(0.8或0.2),信号也会更强。

同样对于MACD:

mac.over.signal <- z$macd > z$signal
z$sig <- rep(0,nrow(z))
z$sig[diff(mac.over.signal) == 1] <- 1
z$sig[diff(mac.over.signal) == -1] <- -1
z$sig <- Lag(z$sig)
z$sig[1] <- 0 

现在我们将它们合并并计算组合信号:

all <- merge(x$sig,y$sig,z$sig)
all[is.na(all)] <- 0

如果是我,我宁愿得到一些信号,因为它会告诉你每个信号的值得信任。如果你有3,那是强壮但1或2不强。所以我会将总和作为组合信号。

all <- cbind(all,rowSums(all))

现在all是包含所有信号的矩阵,最后一列是组合信号强度。

还要考虑一下这可能不会给你一个好的信号。使用此图表的方法,我得到的最强信号是-2,我只得到5次。有点奇怪,因为图表直线上涨,但没有强劲的买入。

> all[which(all[,4] == -2),]
           sig sig.1 sig.2 ..2
2013-04-16   0    -1    -1  -2
2013-08-07   0    -1    -1  -2
2013-11-08   0    -1    -1  -2
2014-04-07   0    -1    -1  -2
2014-06-24   0    -1    -1  -2

这些卖出信号只会给出一个短暂的下行空间,然后图表就会更高。当然这一切都取决于股票等。

你也会遇到这样的情况:

2014-07-07  -1     0     1   0
2014-07-08   0    -1     0  -1
2014-07-09   0     0    -1  -1

某些指标比其他指标更快或更慢。这将是我的方法,但你应该进行广泛的测试,并确定你是否认为这些将是可操作的交易,如果你将任何钱作用于他们减去佣金和持有时间。

答案 1 :(得分:1)

这个怎么样

master.signal <- rep(NA, nrow(x))   # init to all NA's or whatever you like
master.signal[x$sig == 1 & y$sig == 1 & z$sig == 1] <- 1
master.signal[x$sig == -1 & y$sig == -1 & z$sig == -1] <- -1
master.signal[x$sig == 0 & y$sig == 0 & z$sig == 0] <- 0