Categories
Forex Videos

Forex Trading Algorithms Part 4 Elements Of Computer Languages For EA Design!

Trading Algorithms – The Elements of a Computer Language – Part II

 

A computer program is a combination of data structures and a set of instructions or commands in the form of functions that process the data structures to construct a solution or solutions.

 

Control flow tools

To efficiently process information, a high-level programming language owns specific instructions to do mathematical operations, check for conditions, and control data flow.

 

The if Statement:

The if statement is the most basic flow-control instruction. It allows us to check for conditions or the validity of a statement.

for example, 

if x > 0 checks for the variable x being higher than zero. If it is zero or negative, it will deliver a False value. If over zero, it will provide a True condition.

The if statement, combined with the else statement, handles the flow of the information. If/else is mostly similar in all languages. ( Example taken from docs.python.org 

Iterators

Iterators are used to move through the components or a data structure, such as lists or arrays. There are several ways to iterate, some language-specific, but most are present in all languages.

 

 The for statement

The for statement is used to do an orderly iteration on an array or list. In C++, it has the following structure:

for (initialization; condition; increase) . Initialization is the starting point; condition defines the endpoint, and increase sets the step.

CPP example, source cplusplus.com

Python’s for is more versatile and simple. To loop through a list is straightforward (taken from docs.python.org):

But we can use the range() function to do a similar C++ for (taken from docs.python.org):

The While statement

The while statement creates a computer loop that is exited only after a certain condition is met:

For example, the above while loop appends the Fibonacci numbers up to n, storing them in the fibo list. The loop breaks only when a is bigger than n.

 

Function definition

In a computer app, the code repeats itself most of the time, sometimes the values may be different, but the basic computational structure is the same. To organize these computational structures, all computer languages have functions. 

In C++ a funtion is defined with the following structure:

<out type> function name (<type> parameter1, …. <type> parameter n){

body

}

The out type is the output type of the function. It can be an integer, a floating-point, or any other data structure, pointer, or no output at all.

The parameters are inputs to the function but can be used to modify an external structure as well.

In Python, the definition is simpler. 

def function_name ( parameter1…parameter n):

body

If the function returns a value or data structure, it is delivered through a return statement.

The following example shows the fib function, which computes the Fibonacci numbers up to the input parameter. The results of the Fibonacci computations are stored in the fibo list, which, after exiting the while loop, is returned. The variable res is assigned the output of the fib function and printed. Please note that the last two statements are not part of the fib function.

The last introductory article on high-level languages will talk about classes, objects, and object-oriented programming.

Once we have completed this basic wrap-up on programming language features, we’ll start studying trading-focused algorithms in the coming videos.