2016-10-25 4 views
0

quantstrat에 맞춤형 표시기를 추가하고 싶지만이 표시기는 가격 시리즈에서 계산되지 않습니다. 예 :표시기 추가 Quantstrat R

# 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") 

답변

1

는 내가 "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") 

이 수도권과 가진 열을 당신에게 줄 것 중 하나 당신이 구매 신갈 또는로 사용할 수 -1있는 수 1 판매 신호로 사용하십시오.