我试图在quantstrat
中将几个EMA和RSI组合在一起。最终,我的目标是生成一些交易策略的图表和表现。不幸的是,我似乎陷入混合指标并不断收到错误消息(如下所述)。这是代码:
### Add Indicators
nRSI <- 21
buyThresh <- 50
sellThresh <- 50
#Indicator for EMA long medium short
nEMAL<- 80
nEMAM<- 21
nEMAS<- 13
nEMAF<- 5
add.indicator(strategy.st, name="RSI",
arguments=list(price=quote(Cl(mktdata)), n=nRSI),
label="rsi")
add.indicator(strategy.st, name="EMA",
arguments=list(x=quote(Cl(mktdata)), n=nEMAL),
label="EMAL")
add.indicator(strategy.st, name="EMA",
arguments=list(x=quote(Cl(mktdata)), n=nEMAM),
label="EMAM")
add.indicator(strategy.st, name="EMA",
arguments=list(x=quote(Cl(mktdata)), n=nEMAS),
label="EMAS")
add.indicator(strategy.st, name="EMA",
arguments=list(x=quote(Cl(mktdata)), n=nEMAF),
label="EMAF")
#customsig <- function(data) {
sig <- data[, "EMA.EMAF"] > data[, "EMA.EMAS"] & data[, "EMA.EMAF"] > data[, "EMA.EMAM"] & data[, "rsi"] >50 & data[, "EMA.EMAM"] > data[, "EMA.EMAL"] & data[, "EMA.EMAS"] > data[, "EMA.EMAL"]
colnames(sig) <- "upSig"
sig
}
#downsig <- function(data) {
sig <- data[, "EMA.EMAF"] < data[, "EMA.EMAS"] & data[, "EMA.EMAF"] < data[, "EMA.EMAM"] & data[, "rsi"] <50 & data[, "EMA.EMAM"] < data[, "EMA.EMAL"] & data[, "EMA.EMAS"] < data[, "EMA.EMAL"]
colnames(sig) <- "downSig"
sig
}
### Add Signal- Enter
add.signal(strategy.st, name="customsig",
arguments=list(data = quote(mktdata)),
label = "entersig")
add.signal(strategy.st, name="downsig",
arguments=list(data = quote(mktdata)),
label = "downsig.exitsig")
### Add rule - Enter
add.rule(strategy.st,
name='ruleSignal',
arguments = list(sigcol="entersig",
sigval=TRUE,
orderqty=1000,
ordertype='market',
orderside='long',
threshold=NULL),
type='enter',
path.dep=TRUE)
### Add rule- Exit
add.rule(strategy.st,
name='ruleSignal',
arguments = list(sigcol="downsig.exitsig",
sigval=TRUE,
orderqty=1000,
ordertype='market',
orderside='long',
pricemethod='market',
replace=FALSE),
type='exit',
path.dep=TRUE)
start_t<-Sys.time()
out<-try(applyStrategy(strategy=strategy.st,
portfolios=portfolio.st))
updatePortf(portfolio.st)
updateAcct(portfolio.st)
updateEndEq(account.st)
for(symbol in symbols) {
chart.Posn(
Portfolio=portfolio.st,
Symbol=symbol,
log=TRUE)
}
我得到的错误是
Error in applyRules(portfolio = portfolio, symbol = symbol, strategy = strategy, : mktdata does not contain 'sigcol': entersig
我如何完全适用规则?
答案 0 :(得分:2)
您尚未正确识别信号的列名称。看看这个,你会看到你的信号栏没有你期望的标签:
a = dir('C:\example\Desktop\imagefolder\*.png');
numberofImages = length(a);
%sizeofimage?
matrix = zeros(numberofImages, sizeofimage);
以下代码在进行3次更正后有效。由于您的示例不可重复,我必须在您的上面添加一些额外的代码:
var date1 = "2018-04-19T20:23:01.804Z";
var date2 = "2018-12-31T23:59:34.527Z"; //New Year's Eve
var dateDifference;
function getDifference(dateA, dateB){
var dateInt1 = new Date(date1);
var dateInt2 = new Date(date2);
if(dateInt1<dateInt2){
dateDifference = Math.round((dateInt2 - dateInt1)/86400000);
}else{
dateDifference = Math.round((dateInt1 - dateInt2)/86400000);
}
}
getDifference(date1, date2);
console.log(dateDifference);