我正在创建一个简单的函数来计算相对强弱指数( RSI
)。我按照本网站http://stockcharts.com/school/doku.php?st=stochastic+rsi&id=chart_school:technical_indicators:relative_strength_index_rsi
对于这些示例值,代码完美运行。但对于MetaTrader 4的任何外汇数据,MetaTrader的预期价值是不同的。
这是我正在使用的简化代码:
double CalculateRSISimple(std::vector<ROWDATA>& aData, int nPos, int RSIPeriod)
{
double dTotalGain = 0;
double dTotalLoss = 0;
double dRS = 0;
double dRSI = 0;
int nOffset = 0;
int nPrevOffset = 0;
for (int n = RSIPeriod-1; n >= 0; n--)
{
nOffset = nPos - n;
nPrevOffset = nOffset - 1;
double dDiff = aData[nOffset].dClose - aData[nPrevOffset].dClose;
if (dDiff > 0)
{
dTotalGain += dDiff;
}
else
{
dTotalLoss -= dDiff;
}
}
double dAveGain = dTotalGain / double(RSIPeriod);
double dAveLoss = dTotalLoss / double(RSIPeriod);
dRS = dAveGain / dAveLoss;
dRSI = 100.0 - (100.0 / (1 + dRS));
return dRSI;
}
它与此示例数据完美配合:
void TestRSI_1()
{
// TEST
std::vector<ROWDATA> vRowData;
ROWDATA rowTemp;
rowTemp.dClose = 44.338900;
vRowData.push_back(rowTemp);
rowTemp.dClose = 44.090200;
vRowData.push_back(rowTemp);
rowTemp.dClose = 44.149700;
vRowData.push_back(rowTemp);
rowTemp.dClose = 43.612400;
vRowData.push_back(rowTemp);
rowTemp.dClose = 44.327800;
vRowData.push_back(rowTemp);
rowTemp.dClose = 44.826400;
vRowData.push_back(rowTemp);
rowTemp.dClose = 45.095500;
vRowData.push_back(rowTemp);
rowTemp.dClose = 45.424500;
vRowData.push_back(rowTemp);
rowTemp.dClose = 45.843300;
vRowData.push_back(rowTemp);
rowTemp.dClose = 46.082600;
vRowData.push_back(rowTemp);
rowTemp.dClose = 45.893100;
vRowData.push_back(rowTemp);
rowTemp.dClose = 46.032800;
vRowData.push_back(rowTemp);
rowTemp.dClose = 45.614000;
vRowData.push_back(rowTemp);
rowTemp.dClose = 46.282000;
vRowData.push_back(rowTemp);
rowTemp.dClose = 46.282000;
vRowData.push_back(rowTemp);
double dRSI = CalculateRSISimple(vRowData, 14, 14);
if (fabs(dRSI - 70.5328) < 0.0001)
{
printf("... Test Passsed %f. Expected %f\n", dRSI, 70.5328);
}
else
{
printf("... Test Failed %f. Expected %f\n", dRSI, 70.5328);
}
}
但是当我尝试使用外汇数据时,结果与MetaTrader 4的预期完全不同:
void TestRSI_3()
{
// TEST
std::vector<ROWDATA> vRowData;
ROWDATA rowTemp;
rowTemp.dClose = 1.11919; // 0:00
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11932;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11879;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11847;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11867;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11845;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11836;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11817;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11826;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11823;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11842;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11859;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11874;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11876;
vRowData.push_back(rowTemp);
rowTemp.dClose = 1.11915;
vRowData.push_back(rowTemp);
double dRSI = CalculateRSISimple(vRowData, 14, 14);
double dExpected = 58.4792;
if (fabs(dRSI - dExpected) < 0.0001)
{
printf("... Test Passsed %f. Expected %f\n", dRSI, dExpected);
}
else
{
printf("... Test Failed %f. Expected %f\n", dRSI, dExpected);
}
}
这是输出:
Test Passsed 70.532789. Expected 70.532800
Test Failed 49.264706. Expected 58.479200
我想知道MetaTrader4是否以不同的方式计算 RSI
?
有什么想法吗?
答案 0 :(得分:-1)
找到解决方案。我忘记了系列中的下一个元素。