Hi everybody. I am newbie and just code a small strategy and backtest it ( the code and backtest result in images attached). If someone can help me explain:

1. Why my backtest just have Long position though my code have both long and short entry
2. What problem with my test ( entry time and exit time is the same, profit always be zero).
Thank a lots.
1 Like

@JamesIDFS there are no images attached to your message, but this appears to be a repeat of the question that I answered for you yesterday. Please do not post duplicate topics.

1 Like

Dear @mradtke, sorry for my mistake post. I 've fixed this problem above already

``````Alpha1=(EMA(C,2)+1);
Alpha2=(EMA(C,4)+1);
for(i=1;i<BarCount-1;i++)
{
s1 = 1.5*Alpha1[i];
s2 =1.5*Alpha2[i];
}
printf("Alpha1 = %g\n",Alpha1);
printf("Alpha2 = %g\n",Alpha2);
printf("s1 = %g\n",s1);
printf("s2 = %g\n",s2);

``````

Just simple code to practice AFL for novice. But I "felt" it is wrong at some points,
because I am trying to use Alpha[i] in loops and alpha is defined with EMA function.
I am trying to calculate s1 = 1.5* EMA for each bar in loops. But when I see the result in different bars with the same Alpha1 and Alpha2, the s1 and s2 is different. I re- read how AFL array work but still do not figure out???

As a novice you really should avoid using loops. In 90% of cases you simply do not need loops. Loops are for advanced things (e.g. creating custom stops not being covered by ApplyStop etc).

In your case you just need to do this (without any loop)

``````// Alpha1/2 are arrays
Alpha1=(EMA(C,2)+1);
Alpha2=(EMA(C,4)+1);

// s1/2 become arrays too
s1 = 1.5*Alpha1;
s2 = 1.5*Alpha2;

printf("Alpha1 = %g\n",Alpha1);
printf("Alpha2 = %g\n",Alpha2);
printf("s1 = %g\n",s1);
printf("s2 = %g\n",s2);
``````

Now, why is your loop not creating expected result (not getting s1 and s2 arrays). In your loop you assign `1.5*Alpha...` to a variable being type number. If you trace s1 and s2 inside loop then the values of those two variables will change (because of iteration through Alpha arrays) but calling s1 and s2 later outside loop will get you only the values of last iteration (where `i < Barcount-1` in your loop statement) because s1 and s2 are not arrays but remain type number (single number but not array of numbers).

So to create arrays of s1 and s2 in loop you would have to use subscript operator.

``````Alpha1=(EMA(C,2)+1);
Alpha2=(EMA(C,4)+1);

s1 = s2 = Null;
for(i=0;i<BarCount;i++)
{
// now s1 and s2 become arrays
// as we assign Alpha* array elements
// to s1/s2 array elements
s1[i] = 1.5*Alpha1[i];
s2[i] = 1.5*Alpha2[i];
}

printf("Alpha1 = %g\n",Alpha1);
printf("Alpha2 = %g\n",Alpha2);
printf("s1 = %g\n",s1);
printf("s2 = %g\n",s2);
``````

But again prefer array processing in the first place (as shown in first code of this post).