© Evgeny Zhdan, 2018
ISBN 978-5-4493-1046-0
Created with Ridero smart publishing system
The FOREX market, like the securities exchange, attracts more and more people by the day. It’s par for the course, everyone wants to make money from the thin air. But, it’s not all that simple.
The notion that the traders who make profit amount to no more than 5% is widely known. The life earnings of remaining 95% get divided between the first 5% and the brokers/dealing centers.
To join with the successful traders, you need to have a clear strategy and ironclad nerves. In principle, there are many profitable trading strategies and methods. The main problem lies in the psychology of the trader. As a rule, almost all traders start with a steady success. A soon after, they have to deal with a gradual or sudden collapse.
The fact is that when they’re starting to trade, the traders fulfill the conditions of their trading strategy. After a series of successful transactions, the latter relaxes, secure in his conviction that he has hit the jackpot and guaranteed himself a comfortable life. Here comes the excessive confidence in actions and the trader begins to deviate from the trading strategy. The transactions begin to be opened not by the system, but “on a hunch”. While swimming in euphoria from a series of successfully closed orders, the trader increases the trading lot. And not a long after this one is also being sent to those 95% of traders, who’re losing money.
You can solve the problem of the psychological side of trading with its automation – the use of a trading expert (adviser, trade bot), which will work with the account of the trader without the intervention of the human – the owner of the account.
The trading bot is devoid of emotions and is able to monotonously execute its algorithm with the arrival of each new price value. Of course, sometimes a trader will have to prohibit its actions, for example, during extremely important instances of financial and political news, when volatility increases exponentially. The example of such events in the recent past can be British Exit – the Brexit – a campaign of supporters of Britain’s withdrawal from the EU, the election of US and French presidents, an accident at the Fukushima-1 nuclear power plant, which provoked a collapse of the Japanese national currency, etc. You get the idea.
In this book, we will learn how to make the trading bots for the most common and most convenient MetaTrader4 trading terminal by MetaQuotes. To be more precise, in this book we will step-by-step create an Expert Advisor, completely ready for “use”. Naturally, I do not promise you the profitability of the final product, it is only important for us to learn how to make them.
After studying this book, you will be able to bring to life your boldest trading ideas by yourself without resorting to the services of mql coders. Also, you will be able to earn money by programming customized advisers.
Perhaps, half-way through this book, you will deviate from it and make your own upgrades to the adviser we are creating. This is as it should be. Let’s get going.
Types of data
The trading expert manipulates data. It works with incoming prices, price values of indicators, keeps counting the open orders, writes to the trading terminal log.
In mql4, the following data types exist:
Basic data types:
– integers (char, short, int, long, uchar, ushort, uint, ulong)
– logicals (bool)
– literals (ushort)
– strings (string)
– floating-point numbers (double, float)
– colors (color)
– date and time (datetime)
– enumerations (enum)
Complex data types:
– structures;
– classes.
At first, you will not need 70% of the above. Let’s consider only those we will need for the development of our trading expert.
– Int type – integers, i.e., 1, 2, 5, 100, 1425…
– Double type – numbers with a fractional part (with a decimal point): 1.0254, 0.0547…
– Bool type – it only has 2 values – true and false.
– String type – string variables, i.e. words: “word”, “four-word sentence”…
Variables are letter symbols that contain values of some type. Variables are like kegs with something inside.
The bool type is the same – for example, the variable bool b = true means that the keg with the name b contains true inside.
Before creating a variable for later use, you must assign its type, so that the MetaEditor’s compiler (the one where we will create our bot) knew what will be stored inside this variable. The variable names can not begin with a digit.
You can only assign a variable once. Later we will examine where they can be assigned and how it affects our subsequent actions.
The if-else conditional operators can be used left and right. If – means “if” clause, else – “if not, then”.
For example:
if (x <y) // If the content of the x keg is less than the content of the y keg
{
Do something here, for example, open an order. Or close another order, do anything you like!
} else // And if x is not less than y, do the task put in braces below
{
Do something here.
}
the use of the else operator is not must-have, it all depends on the specific task.
Two slashes – // – mean comments in the advisor code, right after the symbol. When you compile your Expert Advisor (turn your code into machine code, understandable for your computer), the comments will be ignored. If possible, you should write the comments for yourself, so as not to forget what has been done and why.
The blocks of comments are done like this:
/* this
is the commentary
block */
Anything put between the /* and */ symbols is also ignored by the compiler.
Cycles
There are for and while cycles in mql4. The for is used more often, but while is also popular.
for (int i=0; i <100; i++)
{
count something 100 times.
}
int i = 0 – assign a variable to work within this cycle; i <100 – the cycle runs 100 times, from 0 to 99; i ++ (increment) means that with each run (iteration) of the cycle, the variable i will be incremented by 1.
bool x = false; // assign value false for a variable x of bool type
while (x==false) // while x is false. Two equal symbols – == – mean comparison
{
/*
some conditions will come true here.
As soon as x is true, the cycle will stop.
*/
//for example
x = true;// set x to true after the first run
//and thus the cycle stops
}
In the process of writing an adviser, we will use both of these cycles, and you will puzzle them out easily.