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++) ?
@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?)
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)");
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.