创建交易信号在R



我正在构建一个交易策略,我在两个关键领域卡住了。当在quantmod中使用Stoch和MACD时,我试图创建一个信号,当慢随机穿过快速随机(1)时,反之亦然(-1),并且在(0)之间时平坦。MACD代码是相同的,除了列名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

更新:在此答案之后,我使用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

一旦你计算了差值,你想要找到第一个交叉点,其中一个比另一个高,所以你需要考虑i th和i-1 th的位置。此外,如果您处于超买或超卖区域(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

某些指标比其他指标快或慢。这将是我的方法,但你应该做广泛的测试,并确定你是否认为这些交易将是可操作的,如果你能从中赚钱减去佣金和持有期限。

这个怎么样

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

最新更新