在 R 中创建交易信号

Creating Trading Signals in R

我正在构建一个交易策略,但被困在两个关键领域。在 quantmod 中使用 Stoch 和 MACD 时,我试图在慢速随机指标越过快速随机指标 (1) 时创建一个信号,反之亦然 (-1),并在两者之间持平 (0)。 MACD 除了列名 MACD 和 Signal 之外,代码是相同的。最后,当所有三个信号都等于 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

这个怎么样

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

更新:我在 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

有些指标比其他指标快或慢。这将是我的方法,但你应该进行广泛的测试并确定你是否认为这些将是可操作的交易,以及你是否会通过减去佣金和持有期限来赚钱。