• No se han encontrado resultados

RESPUESTA AL REQUERIMIENTO DE INFORMACIÓN ADICIONAL

In document 02 DE MARZO DE 2009 REQUERIMIENTOS. (página 96-100)

As mentioned at the beginning of the chapter, MQL4 has predefined arrays that hold the open, close, high, low, time and volume data for each bar on the chart. In MQL5, we will have to create and fill those arrays ourselves. There are several Copy...() functions we can use to retrieve bar data. Let's begin with CopyRates().

CopyRates() and the MqlRates Structure

MqlRates is a structure that contains variables that hold price, time, volume and spread information for each bar. It is used as an array type. Here is the structure definition for MqlRates from the MQL5 Reference:

struct MqlRates {

datetime time; // Period start time double open; // Open price

double high; // The highest price of the period double low; // The lowest price of the period double close; // Close price

long tick_volume; // Tick volume int spread; // Spread long real_volume; // Trade volume };

To use the MqlRates structure, we declare an array object of type MqlRates and pass the object to the CopyRates() function to fill the array with data. There are several variants of the CopyRates() function, but we will only concern ourselves with the first one, which requires us to specify a start position and the number of bars of data to copy:

int CopyRates(

string symbol_name, // symbol name ENUM_TIMEFRAMES timeframe, // period

int start_pos, // start position int count, // data count to copy MqlRates rates_array[] // target array to copy );

The symbol_name parameter is the chart symbol to use, timeframe is the chart period to use, start_pos is the index of the starting bar (0 is the most recent bar), count is the number of bars to copy into the array, and rates_array[] is our MqlRates object.

Let's demonstrate how to use MqlRates and CopyRates() to copy bar data into an array object. We will typically only need data for the most recent bars – three is a good number.

MqlRates bar[];

ArraySetAsSeries(bar,true);

CopyRates(_Symbol,_Period,0,3,bar); double open = bar[1].open;

double close = bar[1].close;

First we declare the array object bar[], using MqlRates as the type. Next, we use the ArraySetAsSeries() function to index the bar[] array for a price series. For a series array, the most recent element is indexed at 0. As we increase the array index, we move back in time. A non-series array would be the opposite of this. It is important to always use the ArraySetAsSeries() function to set an array as series before you copy price or indicator data to it.

Next, we call the CopyRates() function. We use the current chart symbol (_Symbol) and period (_Period). We start copying at index 0, or the most recent bar. We will copy 3 bars worth of data. The last parameter is the name of our bar[] array, which will hold our bar data.

To access the bar data, we reference the bar[] array with the appropriate index inside the brackets. We then use the dot (.) operator to access the member variables of the array object. For example, to retrieve the close price of the last bar, we use bar[1].close.

We can simplify this a bit by creating a class that will retrieve the bar data for us. We will name the class CBars. This class, and the other functions that we'll create in this chapter, will go in a new include file named Price.mqh, located in the \MQL5\Include\Mql5Book\ folder. Here is the class declaration for CBars:

class CBars {

public:

MqlRates bar[]; CBars(void);

void Update(string pSymbol, ENUM_TIMEFRAMES pPeriod); };

All of the members of the CBars class will be public. The bar[] array will hold our price data. The CBars(void) function is our class constructor, a function that runs automatically every time an object is created. Here is the constructor for the CBars class:

CBars::CBars(void) {

ArraySetAsSeries(bar,true); }

The CBars class constructor sets the bar[] array as a series array. This is done automatically anytime we create an object based on the class. The Update() function calls the CopyRates() function and fills the bar[] array with data. Here is the function declaration for CBars::Update():

#define MAX_BARS 100

void CBars::Update(string pSymbol,ENUM_TIMEFRAMES pPeriod) {

CopyRates(pSymbol,pPeriod,0,MAX_BARS,bar); }

At least once in the OnTick() event handler of our program, we will call the Update() function to fill the bar[] array with the most recent data. The MAX_BARS constant defines the number of bars worth of data to copy whenever we call the Update() function. We've set it to 100 bars.

Now we'll need to add a few user-friendly functions to retrieve the prices. Here is a function that will retrieve the close price for the indicated bar:

double CBars::Close(int pShift=0) {

return(bar[pShift].close); }

The function simply retrieves the value of bar[].close for the specified index value. The default value for the pShift parameter is 0, so if no parameter is passed to the function, it will retrieve the price for the current bar. We have several more functions to retrieve the high, low, open, time and volume information. Here is the complete CBars function declaration with all of the necessary functions:

class CBars {

public:

MqlRates bar[]; CBars(void);

void Update(string pSymbol, ENUM_TIMEFRAMES pPeriod); double Close(int pShift);

double High(int pShift); double Low(int pShift); double Open(int pShift); datetime Time(int pShift); long TickVolume(int pShift); long Volume(int pShift); };

Note that our function names correspond to each of the variables in the MqlRates structure. Let's

demonstrate how we can use the CBars class in an expert advisor. First we'll need to create an object based on the CBars class. Then the Update() function will need to be called before we can access the price data. Finally, we use either the price retrieval functions or the bar[] array to retrieve the prices:

// Include directives

#include <Mql5Book/Price.mqh> CBars Price;

// OnTick() event handler Price.Update(_Symbol,_Period); double close = Price.Close();

double alsoClose = Price.bar[0].close; // Same value as close variable

We include the Price.mqh file and create an object named Price, based on our CBars class. In the OnTick() event handler, before we access any price data, we call the Price.Update() function to retrieve the bar data for the current chart symbol and period.

Next, we illustrate two ways of accessing the price data. The easiest way is to use the price retrieval functions that we defined in the CBars class. The close variable contains the current bar's close price, and is retrieved using the Price.Close() function. The alsoClose variable also contains the current bar's close price, and is retrieved using the bar[] array with the close member variable.

The file \MQL5\Experts\Mql5Book\Simple Expert Advisor with Functions.mq5 has been updated to use the CBars function to access the close price.

In document 02 DE MARZO DE 2009 REQUERIMIENTOS. (página 96-100)