• No se han encontrado resultados

Atribuciones y deberes de los miembros de Junta Directiva

Capítulo XIII: Disposiciones finales

Artículo 17: Atribuciones y deberes de los miembros de Junta Directiva

Sometimes you may need to create multiple functions that perform essentially the same task. Each of these functions will have different input parameters, but the end result is the same. In MQL4, it would be necessary to give these functions different identifiers. MQL5 introduces function overloading, which allows you to have multiple functions with the same name.

Each identically-named function must have different parameters, either in number or in type. Let's

demonstrate by using two trailing stop functions that we'll create later in this book. Both functions have the same name, and do basically the same thing. The difference is that the first function has an integer parameter named pTrailPoints, and the second has a double parameter named pTrailPrice:

bool TrailingStop(string pSymbol, int pTrailPoints, int pMinProfit = 0, int pStep = 10); bool TrailingStop(string pSymbol, double pTrailPrice, int pMinProfit = 0, int pStep = 10); The int parameter in the first function, pTrailPoints, accepts a trailing stop value in points. This is used to calculate a trailing stop price, relative to the current Bid or Ask price. The double parameter in the second function, pTrailPrice, accepts a price to be used as the trailing stop price.

By having two identically-named functions with different parameters, we have some flexibility as to how to administer the trailing stop, depending on the trading system. Since both functions share the same name, the programmer does not have to remember two (or more) different function names. In the end, this simply makes life easier for the programmer.

The compiler will know which function to use based on its unique parameter signature. The first function has a string parameter, followed by three integer parameters. The second has a string parameter, followed by a double parameter and two integer parameters. Here's how we would call the first variant of the function:

// Input variables

input int TrailingPoints = 500; // OnTick() event handler

TrailingStop(_Symbol,TrailingPoints);

An int input variable named TrailingPoints allows the user to set a trailing stop in points. This value is used as the second parameter in the TrailingStop() function call. Because the TrailingPoints variable is of type int, the compiler knows to use the first variant of the function. Since we are using the default values for the pMinProfit and pStep parameters, we have omitted them from the function call.

And here's how we would call the second variant of the function: // Input variables

input int TrailingPoints = 500; // OnTick() event handler

double trailingPrice = SymbolInfoDouble(_Symbol,SYMBOL_ASK) (TrailingPoints * _Point);– TrailingStop(_Symbol,trailingPrice);

The local double variable trailingPrice will contain a price to use as the trailing stop. Since the second parameter of the TrailingStop() function call is of type double, the compiler knows to use the second variant of the function.

Chapter 6 - Object-oriented Programming

One of the most exciting new features in MQL5 is the addition of object-oriented programming. Object- oriented programming (OOP for short) encourages code reuse and hides unnecessary implementation details from the user. This allows a much more flexible and compact style of programming.

The concepts of object-oriented programming are abstract in nature and often confounded by technical jargon. They can be difficult for the new programmer to grasp. But once you learn the fundamentals of OOP, you'll find them to be incredibly useful.

Object-oriented programming is based around the concepts of classes and objects. A class is a collection of variables and functions that perform a set of related tasks. The variables and functions contained inside a class are referred to as the members of a class.

A class is like a blueprint for an object. Take a car, for example. A car has a steering wheel, a gear shifter, a turn signal, headlights and so on. A class for a car object would contain all of the variables describing the car's state (speed, gear, whether the headlights are on and off), and all of the functions to perform a specific task (accelerating or decelerating, shifting gears, turning the headlights on and off, etc.)

An object is created using the class as a template. The class describes the car, while the object is the car itself. Each object has a unique name, similar to how each car has a unique vehicle identification number. You can create as many objects as necessary, just like a manufacturer can build many different cars of the same model. The variables of an object are distinct from the variables of other objects, just like how different cars are going different speeds on the highway.

For example, in an expert advisor you may have several indicators. For a moving average cross, you will have at least two moving average indicators. Each moving average will have a different period setting, and may have different calculation mode and price settings.

A moving average indicator can be represented by a class. The moving average indicator class contains all of the variables and functions necessary to create the indicator and retrieve the current indicator value during program execution. Using this class, we create one or more objects, each of which will have their own identifier, settings and indicator values.

We don't have to worry about creating a dynamic series array to hold the indicator values, nor do we need to think about initializing the indicator, storing the indicator handle and copying the indicator values from the buffers to the array. All of these details are handled in the class implementation. All we need to do is create an object, and use the class functions to carry out these tasks.

Classes

Classes are declared on the global scope, just like functions. A class can be placed inside your program or inside an include file. A class declaration uses the class keyword, followed by a unique identifier. The members of the class are placed inside the brackets, sorted by access keywords. The closing bracket of a class declaration is terminated with a semicolon.

Here's an example of a class declaration for an indicator object: class CIndicator { protected: int handle; double main[]; public:

double Main(int pShift=0); void Release();

CIndicator(); };

The name of the class is CIndicator. It has three public members and two protected members. Notice that every function and variable declaration inside the class declaration is terminated with a semicolon. The closing bracket of the class declaration itself is terminated with a semicolon as well. The public members of the CIndicator class include the Main() function, the Release() function, and a default constructor with the same name as our class. The protected members include the handle variable and the main[] array.

We'll discuss the CIndicator class in more detail in Chapter 17, so don't worry if you don't understand how it works just yet. In this chapter, we will be using the CIndicator class as an example to explain the concepts of object-oriented programming.

Documento similar