添加指标 Quantstrat R
Adding Indicator Quantstrat R
我想在 quantstrat 中添加自定义指标,但该指标不是根据价格序列计算的。例如:
# Get SPY from Yahoo Finance
getSymbols("SPY", from = "2016-01-01", to = "2016-01-31", src = "yahoo", adjust = TRUE)
SPY <- SPY[,1:4]
#Create Indicator
set.seed(123)
indicator <- sample(seq(from = 0, to = 100, by = 5), size = nrow(SPY), replace = TRUE)
如何将该指标添加到我的策略中并从中生成信号?我只找到了添加指标的基本符号,但是是否可以添加已经计算好的指标?
# Add a 5-day simple moving average indicator to your strategy
add.indicator(strategy = strategy.st,
# Add the SMA function
name = "SMA",
# Create a lookback period
arguments = list(x = quote(Cl(mktdata)), n = 5),
# Label your indicator SMA5
label = "SMA5")
我喜欢使用"ifelse"功能
Rule1<-function(price,SMA,...)
{ifelse(price>SMA,1,-1)}
add.indicator(strategy=strategyname,name="SMA",
arguments=list(x=quote(mktdata$Close),n=5),label="SMA40")
add.indicator(strategyname, name="Rule1", arguments=list(price = quote(mktdata$Close), SMA=quote(mktdata$SMA.SMA5)), label="Rule1Signal")
这将为您提供 SMA 和一列,其中包含可用作买入信号的 1 或可用作卖出信号的 -1。
我想在 quantstrat 中添加自定义指标,但该指标不是根据价格序列计算的。例如:
# Get SPY from Yahoo Finance
getSymbols("SPY", from = "2016-01-01", to = "2016-01-31", src = "yahoo", adjust = TRUE)
SPY <- SPY[,1:4]
#Create Indicator
set.seed(123)
indicator <- sample(seq(from = 0, to = 100, by = 5), size = nrow(SPY), replace = TRUE)
如何将该指标添加到我的策略中并从中生成信号?我只找到了添加指标的基本符号,但是是否可以添加已经计算好的指标?
# Add a 5-day simple moving average indicator to your strategy
add.indicator(strategy = strategy.st,
# Add the SMA function
name = "SMA",
# Create a lookback period
arguments = list(x = quote(Cl(mktdata)), n = 5),
# Label your indicator SMA5
label = "SMA5")
我喜欢使用"ifelse"功能
Rule1<-function(price,SMA,...)
{ifelse(price>SMA,1,-1)}
add.indicator(strategy=strategyname,name="SMA",
arguments=list(x=quote(mktdata$Close),n=5),label="SMA40")
add.indicator(strategyname, name="Rule1", arguments=list(price = quote(mktdata$Close), SMA=quote(mktdata$SMA.SMA5)), label="Rule1Signal")
这将为您提供 SMA 和一列,其中包含可用作买入信号的 1 或可用作卖出信号的 -1。