25.10.2016 Views

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

An Introduction to MQL<br />

DefaultValFunc(TicketNum,UsePrice,UseNumber);<br />

Again, Comment uses the default value of NULL. But, if we want to specify a value for Comment,<br />

regardless of whether or not we want to use the default value for Number, we have to specify a value<br />

for Number as well:<br />

DefaultValFunc(TicketNum,UsePrice,0,"Comment String");<br />

In this example, we used 0 as the value for Number, which is the same as the default value, and a<br />

string constant as the value for Comment. Remember that when you're dealing with multiple<br />

arguments that have default values, you can only omit arguments if you want to use the default<br />

values for all of the remaining arguments!<br />

Variable Scope<br />

The scope of a variable determines which functions it is available to, and how long it stays in<br />

memory. In MQL, scope can be local or global. A local variable can also be static.<br />

A local variable is one that is declared inside a function. Local variables are only available inside the<br />

function it is declared in. The variable is initialized every time the function runs. Once the function<br />

exits, the variable and its data are cleared from memory.<br />

An exception to this would be a static local variable. Static variables remain in memory even after the<br />

function exits. When the function is run again, the variable is not reinitialized, but instead retains it's<br />

previous value.<br />

A static variable is declared <strong>by</strong> typing static in front of the variable declaration. Here's an example<br />

of a static variable declaration:<br />

static int MyStaticVar;<br />

If a static variable needs to be made available to more than one function, use a global variable<br />

instead. In this case you do not need to declare the variable as static.<br />

A global variable is one that is available to all functions inside a program. As long as the program is<br />

running, the value of the global variable is maintained. Global variables are declared outside of a<br />

function, generally at the top of the source code file.<br />

13

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!