在'['标记之前缺少模板参数

时间:2017-06-26 09:19:43

标签: rcpp

我是R和cpp的新人。这些是我用Rcpp的cpp代码。

#include <cmath>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]

NumericVector getPerformance(NumericVector accProfitSeries, NumericVector dateSeries, double capital, double riskFreeRate)
{
NumericVector retult(8);
int Length = accProfitSeries.length();
double strategyProfit = accProfitSeries[Length - 1];
double returnRate = log(strategyProfit / capital);
double tradingPeriod = (dateSeries[dateSeries.length() - 1] - dateSeries[0]) / 365;
double returnRateYear = returnRate / tradingPeriod;
double sharpeRatio;

for (int i = 0; i < 8; i++) {
    result[i] = 0;
}

NumericVector capitalLevel(Length), capitalReturn(Length);
double meanReturn, sumReturn, stdReturn;
double tmp;

for (int i = 0; i < Length; i++)
{

    capitalLevel[i] = accProfitSeries[i] + capital;

    capitalReturn[i] = log(capitalReturn[i] / capitalReturn[0]);

    sumReturn = sumReturn + capitalReturn[i];

}

meanReturn = sumReturn / Length;


for (int i = 0; i < Length; i++)
    tmp += pow(capitalReturn[i] - meanReturn, 2);
stdReturn = sqrt(tmp / Length);

sharpeRatio = (meanReturn - riskFreeRate) / stdReturn;


double maxCapital = 0, drawback = 0, maxDrawback = 0, maxDrawbackPercent = 0;
int drawbackPeriod = 0, maxDrawbackPeriod = 0;
for (int i = 0; i < Length; i++)
{
    maxCapital = max(NumericVector::create(maxCapital, capitalLevel[i]));
    drawback = capitalLevel[i] - maxCapital;
    maxDrawback = min(NumericVector::create(maxDrawback, drawback));

    maxDrawbackPercent = maxDrawback / (maxCapital - capital);
    if (drawback >= 0)
    {
        drawbackPeriod = 0;
    }
    else
    {
        drawbackPeriod++;
        maxDrawbackPeriod = max(NumericVector::create(maxDrawbackPeriod, drawbackPeriod));
    }
}
result[0] = returnRate;
result[1] = tradingPeriod;
result[2] = returnRateYear;
result[3] = sharpeRatio;
result[4] = maxDrawback;
result[5] = maxDrawbackPeriod;
result[6] = strategyProfit;
result[7] = maxDrawbackPercent;
return(result);
}

我收到了一条错误消息,其他消息类似:

getPerformance.cpp:17:9:错误:在'['标记之前缺少模板参数    result [i] = 0;

我该如何解决?

1 个答案:

答案 0 :(得分:2)

主要有两个问题:

  1. 正如@DirkEddelbuettel所指出的,您需要将retult(8)更改为result(8)以正确设置脚本中其他位置引用的变量。
  2. 您未能初始化多个double变量。
  3. 以下内容应该有效......

    #include <cmath>
    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    NumericVector getPerformance(NumericVector accProfitSeries, 
                                 NumericVector dateSeries,
                                 double capital, double riskFreeRate)
    {
        NumericVector result(8); // retult -> result
        int Length = accProfitSeries.length();
        double strategyProfit = accProfitSeries[Length - 1];
        double returnRate = log(strategyProfit / capital);
        double tradingPeriod = (dateSeries[dateSeries.length() - 1] - dateSeries[0]) / 365;
        double returnRateYear = returnRate / tradingPeriod;
        double sharpeRatio = 0.0;     // Initialize value
    
        for (int i = 0; i < 8; i++) {
            result[i] = 0;
        }
    
        NumericVector capitalLevel(Length), capitalReturn(Length);
    
        // Initialize values
        double meanReturn = 0.0, sumReturn = 0.0, stdReturn = 0.0;
        double tmp = 0.0;
    
        for (int i = 0; i < Length; i++) {
            capitalLevel[i] = accProfitSeries[i] + capital;
            capitalReturn[i] = log(capitalReturn[i] / capitalReturn[0]);
            sumReturn = sumReturn + capitalReturn[i];
        }
    
        meanReturn = sumReturn / Length;
    
        for (int i = 0; i < Length; i++)
            tmp += pow(capitalReturn[i] - meanReturn, 2);
    
        stdReturn = sqrt(tmp / Length);
        sharpeRatio = (meanReturn - riskFreeRate) / stdReturn;
    
        double maxCapital = 0, drawback = 0, maxDrawback = 0, maxDrawbackPercent = 0;
        int drawbackPeriod = 0, maxDrawbackPeriod = 0;
    
        for (int i = 0; i < Length; i++) {
            maxCapital = max(NumericVector::create(maxCapital, capitalLevel[i]));
            drawback = capitalLevel[i] - maxCapital;
            maxDrawback = min(NumericVector::create(maxDrawback, drawback));
    
            maxDrawbackPercent = maxDrawback / (maxCapital - capital);
    
            if (drawback >= 0) {
                drawbackPeriod = 0;
            } else {
                drawbackPeriod++;
                maxDrawbackPeriod = max(NumericVector::create(maxDrawbackPeriod, drawbackPeriod));
            }
        }
    
        result[0] = returnRate;
        result[1] = tradingPeriod;
        result[2] = returnRateYear;
        result[3] = sharpeRatio;
        result[4] = maxDrawback;
        result[5] = maxDrawbackPeriod;
        result[6] = strategyProfit;
        result[7] = maxDrawbackPercent;
        return(result);
    }