2017-06-26 20 views
0

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); 
} 

私は、エラーメッセージの1つを得た、他の人が似ています

getPerformance.cpp:9:17エラー:不足しているテンプレート引数 '[' トークン 結果の前に[I] = 0;

どうすれば解決できますか?

+0

スタート: 'NumericVectorのretultは(8);'あなたは ''結果(8)をしたいです。 –

答えて

2

主に、あなたは二つの問題があります。@DirkEddelbuettelで指摘したように

  1. を、あなたは適切な設定にresult(8)に別の場所にスクリプト内で参照変数をretult(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); 
} 
関連する問題