SJS*_*013 9 r trading quantmod
我正在构建一个交易策略,我被困在两个关键领域.当使用Stoch和MACD时quantmod,我试图创建一个信号,当慢速随机交叉快速随机(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
Run Code Online (Sandbox Code Playgroud)
更新:我在这个答案diff之后使用 a 修复了所有令人讨厌的循环。
这就是我解决这个问题的方法。您正在计算具有所需关系的所有位置。您只希望满足交易信号的第一个头寸尽快对其采取行动。
我会像这样设置布林线信号:
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
Run Code Online (Sandbox Code Playgroud)
我会创建这样的随机信号:
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
Run Code Online (Sandbox Code Playgroud)
一旦计算出差异,您想要找到第一个交叉,其中一个高于另一个,因此您需要考虑第i和i-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
Run Code Online (Sandbox Code Playgroud)
现在我们合并它们并计算组合信号:
all <- merge(x$sig,y$sig,z$sig)
all[is.na(all)] <- 0
Run Code Online (Sandbox Code Playgroud)
如果是我,我宁愿有信号的总和,因为它会告诉你每个信号的可信度。如果你有 3,那就很强,但 1 或 2 就没那么强。所以我会把总和作为组合信号。
all <- cbind(all,rowSums(all))
Run Code Online (Sandbox Code Playgroud)
现在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
Run Code Online (Sandbox Code Playgroud)
这些卖出信号只会带来短暂的下跌,然后图表就会飙升。当然这一切都取决于库存等。
你还会遇到这样的情况:
2014-07-07 -1 0 1 0
2014-07-08 0 -1 0 -1
2014-07-09 0 0 -1 -1
Run Code Online (Sandbox Code Playgroud)
有些指标比其他指标更快或更慢。这将是我的方法,但您应该进行广泛的测试,并确定您是否认为这些交易是可行的交易,以及扣除佣金和持有期限后您是否能从这些交易中赚到钱。
| 归档时间: |
|
| 查看次数: |
2088 次 |
| 最近记录: |