Читать книгу Expert advisor for MT4 for one evening - Evgeny Zhdan - Страница 7

NOW WE GET THE INDICATORS” DATA

Оглавление

Open your MetaTrader4 and press F4 on the keyboard, or left click here:

In the popped up code editor, click New (Create), then the Expert Advisor (template), then Next, in the Name field after Experts\ add MyFirstEA – this will be the name of your first Expert Advisor. You’ll get Experts\MyFirstEA. We do not need the Autor and link fields for this test advisor. Click the Next button. The Event Handles of the Expert Advisor window should pop up. There is nothing to set here and just click Next. The Tester event handless of the Expert Advisor window will pop up, once more, do not select anything there and click Finish. Now we get a working area where our trading bot will soon be born.


In the comments on the image below, we indicated which blocks are responsible for what.


To know the price values of indicators, we need to assign the global variables of the double type for the top and bottom lines of the Envelopes indicator. Let’s call them enveUP and enveDW. You can call them yourself. The same should be done to get the price value of the ZZ indicator. Let’s call this variable ZZ. Why the global variables, of all of them? In order for these values to be requested anywhere in the program (namely, in the advisor). The fact is that we will request the indicator values not with each incoming tick, but once for each candle. This will significantly improve performance because the terminal would not need to perform the same operation for each tick. If we enclose the request for our indicators in curly braces, while assigning their values NOT for the global variables, then these values will be visible only within those curly braces. And outside of them, we will get an error. I’ll try to describe the specifics in the picture below.


Copy this code into your editor:

//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +

//| MyFirstEA.mq4 |

//| Copyright 2017, |

//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +

#property copyright “Copyright 2017”

#property link””

#property version “1.00”

#property strict

//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +

double enveUP, enveDW, ZZ;

datetime open;

//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +

int OnInit ()

{

return (INIT_SUCCEEDED);

}

void OnDeinit (const int reason)

{

}

void OnTick ()

{

if (Open [0]!= open)

{

enveUP = iEnvelopes (NULL,0,13,MODE_SMA,10,PRICE_CLOSE,0.2,MODE_UPPER,1);

enveDW = iEnvelopes (NULL,0,13,MODE_SMA,10,PRICE_CLOSE,0.2,MODE_LOWER,1);

ZZ = iCustom (Symbol (),0,“ZigZag”, 0,1);

if (enveUP> 0 && enveDW> 0 && ZZ> 0) open = Open [0];

}

}

Let’s examine what each line means.

In global variables, except the variables for indicator values, we assigned a variable of datetime type with the open name. Now it set to 0.

IMPORTANT: Position the cursor on the word datetime and press the F1 on the keyboard – the HELP window will pop up with a description of what the datetime type is. You can do it for all built-in commands!

if (Open [0]!= open): If the Zero Candle Open Time IS NOT EQUAL open (i.e., it is set to zero), the code in curly braces will be executed. The Open [0] command means the Zero Candle Open Time (i.e., the time of the current, not yet closed candle). Again, position the cursor on Open and press F1 – read about this command.

EnveUP = iEnvelopes (NULL,0,13,MODE_SMA,10,PRICE_CLOSE,0,2,MODE_UPPER,1); – click on iEnvelopes and see what data should be indicated and in which order:

double iEnvelopes (

– string symbol, // the symbol name

– int timeframe, // timeframe

– int ma_period, // period

– int ma_method, // the averaging method

– int ma_shift, // the average shift

– int applied_price, // the price type

– double deviation, // the deviation (as %)

– int mode, // the line index

– int shift // shift

);

In our code, we did not foresee the option of changing the data of the Envelopes indicator. Let’s fix it. We need to reassign the Period and Deviation, expressed as percentage, as external parameters.

In the advisor’s code, write in front of the global variables:

input int period = 14;

input double deviation = 0.10.

The input command makes the variable visible in the properties of the Expert Advisor so the user can change it.

Expert advisor for MT4 for one evening

Подняться наверх