How To - Avoid Error 29, Variable used without having been initialized

How can I avoid, hide or ignore Error 29 when I choose tools, Verify Syntax?
My Global Variable is defined and initialized in the parent formula, then referenced in an included formula
When verifying the syntax for the included formula, the error is identified.

//Name: FormulaOne
Global myVariable;
myVariable = 0;
#Include FormulaTwo
//Name: FormulaTwo
myVariable = MyVariable + IIF(Buy,1,0);

It’s not a good idea to hide or ignore errors because usually there is a good reason why such notifications are displayed :wink:

You probably get this error because you declare/initialize " myVariable" only in the first code. For that reason it is not recognized in the second one. If you declare “myVariable” in the second code and then include this formula in the first one, you should not have errors. The formula which is included should work fine on its own.

I suppose you misunderstand what global variable in AmiBroker means. A quote from:

The global keyword declares global variable inside user-defined function. Global variable is the variable that is visible/accessible inside the function AND outside the function (at global formula level)

… but (as I understand) only in the formula where it has been declared (or in another code if the formula with such declaration has been included).

Besides, if you want to share some variables between different formulas, you can use Static Variables for that: StaticVarSet() , StaticVarGet() , StaticVarSetText() , StaticVarGetText()

Hope this helps.

Modifying FormulaTwo as shown below prevents error 29 during the Verify Syntax. When the calling program (FormulaOne) defines the global variable and its value, the value is carried into the included formula. Note: due to the way Amibroker handles variable scoping, the Global declaration is optional. Further, since the included code is simply a shortcut for inserting actual formula, MyVariable is recognized regardless of whether it is local or global. Lastly, as Milosz indicates in his response below, Global keyword is most valuable when used with user defined functions that need to change a global variable’s value.

//Name: FormulaTwo if (typeof(MyVariable) == "undefined") {myVariable = 0;} myVariable = MyVariable + IIF(Buy,1,0);

// Oh My GOD !@#*&@#
if (typeof(MyVariable) == "undefined") {myVariable = 0;}

Don’t do that!

The variables should be always initialized in the formula file where they are used.
Initialization writes initial value.

Just do this:

myVariable = 0;

at the top of your code. UNCONDITIONALLY.

The other file may write to already initialized variable again, but the concept of checking for “undefined” is completely SICK.

Using variable without writing initial value to it is equivalent to saying “oh well get me some random garbage, I will add something to that garbage and will see what happens”.

BTW: there is SetOption("RequireDeclarations", True ); that can be used to force declarations, but declaration and initialization are different things. Lack of initialization is user coding mistake and it should be FIXED, not circumvented.

Why or why users come up with so many bad ideas???


Tomasz, Thank you for the response. I understand the concern regarding variable initialization and I agree with you. I provided the initial code snippets for illustration purposes only to seek a fix for a specific error 29. Perhaps it was out of context and possibly misunderstood.

In my case, I have a written a standalone formula (e.g. The formula (indicator) has it’s own set of parameters and works independently. Using dynamic variables I can repeatedly include this formula (indicator) in a Parent formula. Here are the code snippets to illustrate this programming technique. I’m always open to a more constructive approach. Thanks Again!

[code] //

//1. Fast Moving Average
Instance = 1; Periods_Init1 = 50;
#include “C:\program Files\AmiBroker\Formulas\Custom\Sandbox\”;

//2. Slow Moving Average
Instance = 2; Periods_Init2 = 200;
#include “C:\program Files\AmiBroker\Formulas\Custom\Sandbox\”;

_SECTION_END(); [/code]

[code]//Name: MvgAvg
if (typeof(Instance) == “undefined”) {Instance = 1;} //Instance should be defined in the calling formula.
if (VarGetText(“Periods_Init” + instance) == “” ) {VarSet(“Periods_Init”+Instance, 50);}

VarSet(“Periods_Init”+Instance,Param(“Periods”, VarGet(“Periods_Init”+Instance), 1, 200, 1, 0 ));
Plot(MA(Close, VarGet(“Periods_Init” + Instance)), “MA”+Instance, colorGreen, styleLine, Null, Null, 0, zOrder = 1);


It’s a pity you barely noticed my reply in which I suggested two solutions to your problem (Error 29 during the Verify Syntax). At first I thought you hadn’t read my post at all, but you mentioned my name in your reply. You mentioned my name in the third person (Milosz indicates…) so you were clearly not replying to me, but probably expecting another response from someone else - probably a more experienced person than me. What is most important, you didn’t draw any conclusions from my post.

It is not true. You can check it in 10 seconds. If your statement was true, you wouldn’t get Error 29 (Variable used without having been initialized) in Formula Two. As I wrote in my reply - you misunderstand what global/local variables in AmiBroker mean and what are their applications. You can read about it (in details) in the link which I have already provided.[quote=“SwingTradeMonkey, post:3, topic:996”]
Further, since the included code is simply a shortcut for inserting actual formula, MyVariable is recognized regardless of whether it is local or global.
How can you write that your variable is recognized if you get Error 29 (Variable used without having been initialized). It’s completely illogical. :flushed:

First you inquire about the reason why you get an error during the code checking. Then when you receive a reply, you don’t bother checking it, instead you reply claiming, that you are right. No comment … :flushed:

  1. If you initialized your “Instance” variable in the second code (which is included) you wouldn’t get Error 29. The formula which is included should work fine on its own. I wrote about - you didn’t try it. When Tomasz wrote about it - you agreed with him and thanked twice for his response.

  2. If you used Static Variables instead of declaring “global variables” you wouldn’t get Error 29. Static variables are used to share some variables between different formulas - not “global variables”. I wrote about it - you didn’t try it.

If you implemented any of these solutions, your formulas would be working fine (but the most important thing is - you really don’t need such complex construction). Instead you have found “a workaround” and implemented your “solution” which Tomasz described as - I quote: “Oh My GOD … completely SICK”. :flushed:

I am far from being the most experienced user here, but I have a couple of years of AFL coding experience (so I am more experienced than you), and I have to say I haven’t seen many examples of such " triumph of form over content" coding samples as you provided in your last reply. Something that consists of 16 lines of code and is divided into 2 AFLs, can be written in one AFL in 2-3 simple lines of code:

if (condition1) Plot (...); if (condition2) Plot (...);
… without the need of using global/local declarations, dynamic variables, static variables, including other codes or checks like this: if (typeof(Instance) == "undefined"), etc.

Besides, I noticed, that you use such notation:

Brackets delimit code blocks.You don’t need curly brackets in this conditional statement if the statement which follows condition fits into 1 line (to the semicolon).

Such situations make me re-thing the way I participate in this forum. I offer my help to someone I don’t know, devote my time to make a post (English is not my native language, so it takes me much more time to compose a detailed and properly written post than it would take a native speaker) I believe I provide some useful information and in return I am treated as if I didn’t exist. You didn’t check the things I wrote about and you replied to my post - not to me - but to someone else, mentioning me in the third person as if I was not worth replying directly. Maybe I take it to personal, but I am used to showing my appreciation to anybody who offered me any kind of help - even it was of no use to me. Because it is not the first time I tried to help you and I didn’t get any response from you, and because I can see, my suggestions are useless to you, be sure I won’t bother you again …


@Milosz I am sure that everyone, including me, greatly appreciate all your posts, so pretty please with sugar on top - don’t get discouraged. All your posts provided tremendous value to community so even if one person does not exactly follow the advice, it does not mean that others don’t.


I agree with Tomasz, Thank you Milosz.

1 Like

@Tomasz, @Anthony - thank you for the kind words.

Milosz and to anyone else this may concern:

I emphatically apologize that I did not respond appropriately, or sufficiently acknowledge Milosz’s response to my Error 29 question, or to anyone’s other post for that matter. I truly appreciate the time it takes to read and respond to a request.

Milosz, I did carefully read your post and the links you graciously provided. In fact Milosz, your post provided me with the elements that clued me into a solution that fit my needs.

Regarding verbose code, I try to post sandbox code that includes comments and that will actually work.

I look forward to everyone’s future responses.

I’m glad to hear it Gary. Thanks for the reply. :+1: