Error 29. Variable <name> used without having been initialized

I am getting this error message though as far as I can see I have initialized both variables.

I appreciate that this topic is covered here
http://www.amibroker.com/guide/errors/29.html

and in various places in the forum such as
https://forum.amibroker.com/t/how-to-avoid-error-29-variable-used-without-having-been-initialized/996

However I cannot work this one out for myself after playing for some time, I'm sure it's a simple error for a simple person :nerd_face:

_SECTION_BEGIN("ENTRY & EXIT");
//=================================================================================
//=================================================================================
LowValuePlus20  = LLV(L, 50) * PCRiseLow;            
HighValueLess20 = HHV(H, 50) * PCFallHigh;             

PCRiseLow = 1.2;	
PCFallHigh = 0.8; 

Buy  = Cross(C, LowValuePlus20); 				 				
Sell = Cross(HighValueLess20, C);
//=================================================================================
_SECTION_END();

@Random_Entry, in your code you should declare your variables BEFORE using them.

_SECTION_BEGIN("ENTRY & EXIT");
//=================================================================================
//=================================================================================
PCRiseLow = 1.2; // moved up - before using it	
PCFallHigh = 0.8; // moved up - before using it

LowValuePlus20  = LLV(L, 50) * PCRiseLow;            
HighValueLess20 = HHV(H, 50) * PCFallHigh;             

// PCRiseLow = 1.2; // line moved up otherwise you'll get error 29 since it was used before its declaration
// PCFallHigh = 0.8; // line moved up otherwise you'll get error 29 since it was used before its declaration

Buy  = Cross(C, LowValuePlus20); 				 				
Sell = Cross(HighValueLess20, C);
//=================================================================================
_SECTION_END();
1 Like

@beppe thanks you very much I would never have guessed that.

Hopefully other users will find this post and it will help them also.

In the last month that I have started to code and use amibroker, I was assuming that the order in which code is written has no effect on the outcome. Obviously this assumption was wrong.

I now assume that it is good practice to list ALL arrays before formulas.

Are there any other general good practice rules for the precedence of formula components?

1 Like

@Random_Entry, I will start reading the introduction to the AFL Language.
About precedence, it is crucial to understand the rules about operators.
Be sure to carefully review and understand what is explained in the Operator precedence and the parentheses (in the above document, halfway down).

AFL is more similar to C than other programming languages (there is no hoisting like in JavaScript).

A lot of essential information is also available in this thread. Study it carefully.

And when you run in trouble here are the tools and steps to debug your code.

3 Likes

Something important to remember about AFL is that the training documentation assumes you already have a working knowledge of computer programming concepts. Defining variables before using them would be taught in a high school programming course or freshman year in college, so this would not be mentioned in the AB docs.

A fantastic resource that will help you tremendously is CS50's Introduction to Computer Science, a free Harvard course you can take.

1 Like

Actually you don't really need to declare variables in AFL, but you just need to assign a value. The message is not about declaration but about variable having NO VALUE assigned in first place. Obviously you have to assign a value to the variable before you can read it, otherwise what value would you be reading? Random leftover from nobody knows where?

I don't know how one would assume that order of statements does not matter.
Obviously you do NOT need programming course to know that the outcome of sequence of events is different when you do it in different order.

Program is executed like you read sentences in the story or cooking recipe - sequentially.

Thanks for the links @beppe very useful.

I listened to the better system trader podcast with andreas unger, he recommended a book by a trader called beppe, did you write your own trading book?

Thanks @PeterD the link you provided looks like an awsome resource, I have enrolled :slight_smile:

@Tomasz I am a child learning to walk

I listened to the better system trader podcast with andreas unger, he recommended a book by a trader called beppe, did you write your own trading book?

@Random_Entry, no. I know Andrea since he is Italian like me, but I never wrote a book!

In the webpage of his interview you can find the books cited. When I read them, I found Stridsman's books worthwhile, but they target another platform, and I do not think they will benefit a beginner.

At your level, I suggest sticking to Amibroker using the valuable resources that I listed in this previous post..

I particularly recommend learning how to create, save, and reuse code snippets.
Start using the existing ones, and over time, increase your collection. When properly managed, they are at once a time saver and a way to ensure code consistency.
In this forum, in the KB and documentation, there are many examples of code samples that you can save/modify as snippets.

1 Like

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