For loop : run previous three day issue and get error 10 message

Hi everyone,

I am new to AmiBroker, just start around three weeks. I write below code to create a buy signal and it doesn't work and error message is error 10.

I further check it works for i-1 and i-2 but once deduct greater than three, all are not working.

RL30=0;
RL30=LinearReg( close, 30 );

for (i = 1; i < BarCount-1; i++){
	if(i>1){
		
		If(
                  RL30[i]-RL30[i-3]>0 
                   ) {
			Buy[i]=1;
		 

But when I change to below , everything goes well.
Question One:Anyone can help to explain?

RL30=0;
RL30=LinearReg( close, 30 );
Ref3_RL30 = Ref( RL30,-3);

for (i = 1; i < BarCount-1; i++){
	if(i>1){
		
		If(
                  RL30[i]-Ref3_RL30[i]>0 
                   ) {
			Buy[i]=1;

Question Two: I understand the beginning index is different (0 vs 1), I mean will it impact the result and which one you recommend?
for (i = 0; i < BarCount; i++) and
for (i = 1; i < BarCount-1; i++) ?

Thanks a lot.

If you have indices like this [i-3] then you should start your loop with i = 3, because without that your index will be negative.

for( i = 3; i < BarCount; i++ )
{
  ....
}

@Jodie I guess welcome to the forum though it appears to have been one year since you joined. May I suggest that instead of just posting your incorrect code it would be more helpful to properly explain your objectives.

I don't know if I understand your question but my interpretation is that you do not need a loop at all. (But perhaps I am completely misunderstanding your question and what you are really trying to learn is how to properly use loops?)

RL30 = LinearReg( Close, 30 );
Buy = RL30 > Ref(RL30, -3);

Some useful and necessary posts to read,

Perhaps an example of using an Exploration to understand what your variables are calculating,

// one method of debugging your code
Filter = 1;
AddColumn(RL30, "RL30");
AddColumn(Buy, "Buy", 1.0, colorDefault, IIf(Buy, colorLime, colorDefault));
AddColumn(Ref(RL30, -3), "Ref(RL30, -3)");

image

As you can see this code will produce a state Buy signal and if that is not your intention then you need to review state vs pulse (or impulse) signals. Found many places in this forum. But if all you were interested in was the proper coding of a loop then please ignore my post.

Again welcome to AmiBroker and good luck!

3 Likes

thanks for the reply.

Thanks for your reply.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.