添加指示剂定量策略R

添加指示剂定量策略R,r,indicator,quantstrat,R,Indicator,Quantstrat,我想在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,

我想在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)
我喜欢使用“ifelse”函数

Rule1SMA,1,-1)}
add.indicator(strategy=strategyname,name=“SMA”,
参数=列表(x=引号(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可以用作卖出信号。

#camerongiles您的解决方案不仍然依赖间谍价格吗?与#Viitama一样,我们拥有外部数据,并且正在努力在quantstrat中使用这些数据。另请参见:
# 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")
   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")