Posted By: Nater
JMA Indicator Translation - C++ - 01/04/14 16:27
All,
I have attached my initial efforts to try and convert an indicator created in C++ called JMA. It makes it difficult to troubleshoot the issue, because the "Crash in script" error has been challenge to track down. The best I can tell is that the issue is related to the "continue" mechanism located at the bottom of the main loop(s).
I have attached my initial efforts to try and convert an indicator created in C++ called JMA. It makes it difficult to troubleshoot the issue, because the "Crash in script" error has been challenge to track down. The best I can tell is that the issue is related to the "continue" mechanism located at the bottom of the main loop(s).
Code:
#include <default.c>
int IntPortion (double);
/*=================================================================================================================================*/
var JMA(var* Data, int Period, int Phase)
{
Verbose = 30;
double list[128];
double ring1[128];
double ring2[11];
double buffer[62];
double PrevJMAValueBuffer = 0;
double Cur_fC0Buffer = 0; double Prev_fC0Buffer = 0;
double Cur_fA8Buffer = 0; double Prev_fA8Buffer = 0;
double Cur_fC8Buffer = 0; double Prev_fC8Buffer = 0;
int limitValue = 63; int startValue = 64;
int loopParam = 0; int loopCriteria = 0;
int cycleLimit = 0; int highLimit = 0;
int counterA = 0; int counterB = 0;
int s58 = 0; int s60 = 0;
int s40 = 0; int s38 = 0;
int s68 = 0;
double cycleDelta = 0; double lowDValue = 0;
double highDValue = 0; double absValue = 0;
double paramA = 0; double paramB = 0;
double phaseParam = 0; double logParam = 0;
double JMAValue = 0; double seriesA = 0;
double sValue = 0; double sqrtParam = 0;
double lengthDivider = 0; double NoEvalValue = 0;
bool MoreThanThirtyFlag = true;
int intPart;
int leftInt;
int rightPart;
int upShift;
int dnShift;
double sqrtDivider;
double dValue;
double powerValue;
double squareValue;
double JMATempValue;
double lengthParam;
//Updated Items for Zorro
long e;
int i;
int j;
var* Output;
{
phaseParam = Phase/100.0 + 1.5;
if (Phase < -100) phaseParam = 0.5;
if (Phase > 250) phaseParam = 4.0;
for (i=0; i<62; i++) buffer[i] = 0.0;
for (i=0; i<11; i++) ring2[i] = 0.0;
for (i=0; i<128; i++) ring1[i] = 0.0;
for (i=0; i<128; i++) list[i] = 0.0;
for (i=0; i<=limitValue; i++) list[i] = -1000000.0;
for (i=startValue; i<128; i++) list[i] = 1000000.0;
lengthParam = (Period - 1)/2;
logParam = log (sqrt(lengthParam)/log(2.0));
if ((logParam + 2.0) < 0) logParam = 0;
else logParam = logParam + 2.0;
sqrtParam = logParam * sqrt(lengthParam);
lengthParam = lengthParam * 0.9;
lengthDivider = lengthParam / (lengthParam + 2.0);
}
for (e=0; e<NumBars; e++)
{
if (NoEvalValue == 0.0) NoEvalValue = Data[e];
seriesA = Data[e];
if (loopParam < 61)
{ loopParam++; buffer[loopParam] = seriesA; }
if (loopParam > 30)
{
if (MoreThanThirtyFlag == true)
{ int diffFlag = 0;
MoreThanThirtyFlag = false;
for (i=1; i<=29; i++)
{ if (buffer[i+1] != buffer[i]) diffFlag = 1; }
highLimit = diffFlag * 30;
if (highLimit == 0) paramB = seriesA;
else paramB = buffer[1];
paramA = paramB;
if (highLimit > 29) highLimit = 29; }
else highLimit = 0;
///////////////////////////////////////////////////////////////////
for (i=highLimit; i>=0; i--)
{
if (i == 0) sValue = seriesA;
else sValue = buffer[31-i];
if ((abs(sValue - paramA)) > (abs(sValue - paramB))) absValue = abs(sValue - paramA); // was "fabs" function
else absValue = abs(sValue - paramB);
dValue = absValue + 0.0000000001;
if (counterA <= 1) counterA = 127;
else counterA--;
if (counterB <= 1) counterB = 10;
else counterB--;
if (cycleLimit < 128) cycleLimit++;
cycleDelta += (dValue - ring2[counterB]);
ring2[counterB] = dValue;
if (cycleLimit > 10) highDValue = cycleDelta;
else highDValue = cycleDelta / cycleLimit;
if (cycleLimit > 127)
{ dValue = ring1[counterA];
ring1[counterA] = highDValue;
s68 = 64; s58 = s68;
while (s68 > 1)
{ if (list [s58] < dValue)
{ s68 = s68 / 2.0; s58 += s68; }
else { if (list [s58] <= dValue) s68 = 1;
else { s68 = s68 / 2.0; s58 -= s68; } } } }
else { ring1 [counterA] = highDValue;
if ((limitValue + startValue) > 127) { startValue--; s58 = startValue; }
else { limitValue++; s58 = limitValue; }
if (limitValue > 96) s38 = 96;
else s38 = limitValue;
if (startValue < 32) s40 = 32;
else s40 = startValue; }
s68 = 64;
s60 = s68;
while (s68 > 1)
{ if (list[s60] >= highDValue)
{ if (list[s60-1] <= highDValue) s68 = 1;
else { s68 = s68 / 2.0; s60 -= s68; } }
else { s68 = s68 / 2.0; s60 += s68; }
if(s60 == 127) if(highDValue > list[127]) s60 = 128; }
if (cycleLimit > 127)
{ if (s58 >= s60)
{ if ((s38 + 1) > s60) if ((s40 - 1) < s60) lowDValue += highDValue;
else { if (s40 > s60) if ((s40 - 1) < s58) lowDValue += list[s40-1]; } }
else { if (s40 >= s60)
{ if ((s38 + 1) < s60) if ((s38 + 1) > s58) lowDValue += list[s38+1]; }
else { if ((s38 + 2) > s60) lowDValue += highDValue;
else { if ((s38 + 1) < s60) if ((s38 + 1) > s58) lowDValue += list[s38+1]; } } }
if (s58 > s60)
{ if ((s40 - 1) < s58) if ((s38 + 1) > s58) lowDValue -= list[s58];
else { if (s38 < s58) if ((s38 + 1) > s60) lowDValue -= list[s38]; } }
else { if ((s38 + 1) > s58) if ((s40 - 1) < s58) lowDValue -= list[s58];
else { if (s40 > s58) if (s40 < s60) lowDValue -= list[s40]; } } }
if (s58 <= s60)
{ if (s58 >= s60) list[s60] = highDValue;
else { for (j=s58+1; j<=(s60-1); j++)
{ list[j-1] = list[j]; }
list[s60-1] = highDValue; } }
else { for (j=s58-1; j>=s60; j--)
{ list[j+1] = list[j]; }
list[s60] = highDValue; }
if (cycleLimit <= 127)
{ lowDValue = 0;
for (j=s40; j<=s38; j++) { lowDValue += list[j]; } }
if ((loopCriteria + 1) > 31) loopCriteria = 31;
else loopCriteria++;
sqrtDivider = sqrtParam / (sqrtParam + 1.0);
if (loopCriteria <= 30)
{ if ((sValue - paramA) > 0) paramA = sValue;
else paramA = sValue - ((sValue - paramA) * sqrtDivider);
if ((sValue - paramB) < 0) paramB = sValue;
else paramB = sValue - ((sValue - paramB) * sqrtDivider);
JMATempValue = seriesA;
if (loopCriteria == 30)
{ Cur_fC0Buffer = seriesA;
if (ceil(sqrtParam) >= 1) intPart = ceil(sqrtParam);
else intPart = 1;
leftInt = IntPortion (intPart);
if (floor(sqrtParam) >= 1) intPart = floor(sqrtParam);
else intPart = 1;
rightPart = IntPortion (intPart);
if (leftInt == rightPart) dValue = 1.0;
else { dValue = (sqrtParam - rightPart) / (leftInt - rightPart); }
if (rightPart <= 29) upShift = rightPart;
else upShift = 29;
if (leftInt <= 29) dnShift = leftInt;
else dnShift = 29;
Cur_fA8Buffer = ((seriesA - buffer [loopParam-upShift]) * (1 - dValue) / rightPart) +
((seriesA - buffer[loopParam-dnShift]) * dValue / leftInt); } }
else { dValue = lowDValue / (s38 - s40 + 1);
if ((logParam - 2.0) >= 0.5) powerValue = logParam - 2.0;
else powerValue = 0.5;
if (logParam >= pow(absValue/dValue, powerValue)) dValue = pow((absValue/dValue),powerValue);
else dValue = logParam;
if (dValue < 1) dValue = 1;
powerValue = pow(sqrtDivider,sqrt(dValue));
if ((sValue - paramA) > 0) paramA = sValue;
else { paramA = sValue - ((sValue - paramA) * powerValue); }
if ((sValue - paramB) < 0) paramB = sValue;
else paramB = sValue - ((sValue - paramB) * powerValue); }
}
//////////////////////////////////////////////////////////////////////////////////
if (loopCriteria > 30)
{ JMATempValue = PrevJMAValueBuffer;
powerValue = pow(lengthDivider,dValue);
squareValue = pow(powerValue,2);
Cur_fC0Buffer = ((1 - powerValue) * seriesA) + (powerValue * Prev_fC0Buffer);
Cur_fC8Buffer = ((seriesA - Cur_fC0Buffer) * (1 - lengthDivider)) + (lengthDivider * Prev_fC8Buffer);
Cur_fA8Buffer = (phaseParam * Cur_fC8Buffer + Cur_fC0Buffer - JMATempValue) *
(powerValue * (-2.0) + squareValue + 1) + (squareValue * Prev_fA8Buffer);
JMATempValue += Cur_fA8Buffer; }
JMAValue = JMATempValue;
PrevJMAValueBuffer = JMAValue;
Prev_fC0Buffer = Cur_fC0Buffer;
Prev_fC8Buffer = Cur_fC8Buffer;
Prev_fA8Buffer = Cur_fA8Buffer;
if (e <= (Period + 50)) continue;
else { Output[e] = JMAValue; continue; }
}
if (e <= (Period + 50)) continue;
else { Output[e] = NoEvalValue; continue; }
}
return;
}
//
/*==================================================================================================================================*/
int IntPortion (double param)
{
double Result = 0.0;
if (param > 0) Result = floor (param);
if (param < 0) Result = ceil (param);
return ((int)Result);
}
/*==================================================================================================================================*/
//int _matherr (exception* E)
//{
// if (lstrcmp((E->name),"log") == 0)
// {
// if ((E->type) == UNDERFLOW) (E->retval) = DBL_MIN;
// if ((E->type) == SING) (E->retval) = DBL_MIN;
// return 1;
// }
// if (lstrcmp((E->name),"exp") == 0)
// {
// if ((E->type) == OVERFLOW) (E->retval) = DBL_MAX;
// if ((E->type) == UNDERFLOW) (E->retval) = DBL_MIN;
// return 1;
// }
// if (lstrcmp((E->name),"pow") == 0)
// {
// if ((E->type) == OVERFLOW) (E->retval) = DBL_MAX;
// if ((E->type) == UNDERFLOW) (E->retval) = DBL_MIN;
// return 1;
// }
// if (lstrcmp((E->name),"sqrt") == 0)
// {
// if ((E->type) == OVERFLOW) (E->retval) = DBL_MAX;
// if ((E->type) == UNDERFLOW) (E->retval) = DBL_MIN;
// if ((E->type) == DOMAIN) (E->retval) = sqrt (-(E->arg1));
// return 1;
// }
// return 0;
//}
/*==================================================================================================================================*/