2016-05-29 102 views
-3

我想告訴某個特定的列值是否大於.77,但如果該值大於0,則此代碼返回1.有沒有什麼方法可以使用已知的函數(不是自定義的)做一個簡單的更大,然後檢查?檢查值是大於還是小於指定值

library(PerformanceAnalytics) 
library(quantmod) 
library(lattice) 
startDate <- '2010-01-01' # start of data 
endDate <- '2015-05-01' # end of data 
.blotter<-new.env() 
.strategy<-new.env() 


Sys.setenv(TZ="EST") # set time zone 
symbols<-c("GOOG") 
data<-getSymbols(symbols, from=startDate, to=endDate, index.class="POSIXct",env=NULL) 

library(quantstrat) 
initDate <- '2009-12-31' 
initEq <- 1e6 
currency("USD") 
stock(symbols, currency="USD", multiplier=1) 

rm.strat("multiAsset.bb1") # remove portfolio, account, orderbook if re-run 
initPortf(name="multiAsset.bb1", symbols, initDate=initDate) 
initAcct(name="multiAsset.bb1", portfolios="multiAsset.bb1",initDate=initDate, initEq=initEq) 
initOrders(portfolio="multiAsset.bb1", initDate=initDate) 

strategy("bbands", store=TRUE) 
#Indicators are applied before signals and rules, and the output of indicators may be used as inputs to construct signals or fire rules 

#mktdata is the time series object that holds the current symbols data during evaluation (pg 55) 
add.indicator("bbands", name = "BBands",arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'), label='bbInd') 

test <- applyIndicators("bbands", mktdata=data) 
head(test, 10) 
add.signal("bbands", name="sigThreshold", arguments=list(columns=c("pctB.bbInd",".77"),relationship="gt"),label="H.gt.UpperBand") 


test <- applySignals("bbands", mktdata=test) #DOESNT WORK 
head(test, 10) 

錯誤:

Error in match.names(column, colnames(data)) : 
    argument "column" is missing, with no default 
+7

請添加[再現的示例](HTTP://計算器.com/q/5963269/1217536)供人們使用。 – gung

+0

增加了一個例子,對不起,我認爲這只是一個語法錯誤,因此不需要完整的代碼示例 – Rilcon42

回答

0

我的解決辦法是改變:

add.signal("bbands", name="sigThreshold", arguments=list(columns=c("pctB.bbInd",".77"),relationship="gt"),label="H.gt.UpperBand") 

到:

add.signal("bbands", name="sigThreshold", arguments=list(threshold=.77,column="pctB.bbInd",relationship="gt"),label="H.gt.UpperBand") 
相關問題