Beta version of the online book on MQL4 programming - by Sergey Kovalev (SK.) - page 3

 
Climber:
I have tried Meta Trader for the first time a week ago and found out that it is much more convenient than my previous terminals (Rumus and Forex Trader). About two years ago, when I got to know about Forex and opened my first demo account, I wondered how to automate everything. Then I opened the terminal and saw the message Autotrading in the mail section. And here I am))) Intensely reading the book, and I hope that soon I will write my first Expert Advisor for my developed strategy. I had absolutely nothing to do with programming before, it was even hard to imagine that I would ever study programming))) As I was reading, questions arose, but they also disappeared as I went along. The question appeared, I looked ahead and was satisfied, because I saw the answer after a couple of chapters and went back to read everything in order. I was especially attracted by the possibility to test the strategy on historical data and adjust the strategy as a result.
Anyway, thanks a lot for the book.


It is excellent. You are a new MT user. And you know practically nothing about programming at the moment.

Allow me to suggest that you report at least two or three times a month how things are going (and more often if you wish). That would be extremely interesting.

In the meantime, if you allow me, I have a question. You say that a question has arisen which is answered in the following sections. I've tried to put it together in a way that avoids that situation. Could you please clarify what question you are referring to?

 
SK. писал (а):
Climber:
A week ago I installed and tried Meta Trader for the first time. It turned out that this terminal is much more convenient than my previous ones (Rumus and Forex Trader). About two years ago, when I got to know about Forex and opened my first demo account, I wondered how to automate everything. Then I opened the terminal and saw the message Autotrading in the mail section. And here I am))) Intensely reading the book, and I hope that soon I will write my first Expert Advisor for my developed strategy. I had absolutely nothing to do with programming before, it was even hard to imagine that I would ever study programming))) As I was reading, questions arose, but they also disappeared as I went along. The question came up, I looked ahead and was satisfied, as I saw the answer after a couple of chapters and went back to read everything in order. I was especially attracted by the possibility to test the strategy on historical data and to correct it as a result.
Anyway, thanks a lot for the book.


It is excellent. You are a new MT user. And you know practically nothing about programming at the moment.

May I offer you: to let me know at least 2 or 3 times a month how you are doing (or more often, if you wish). It would be very interesting.

In the meantime, if you allow me, I have a question. You say that a question has arisen which is answered in the following sections. I've tried to put it together in a way that avoids that situation. Could you please clarify which question you are referring to?

I once used Sign of Misery to make my work easier, where you can write a programme without knowing any programming. You just had to specify the actions in the order they were performed (I mainly used it as an automator, although there are much more possibilities). Let's say I had a lot of values in 4 columns in Excel (electrical survey data from geophysics, every 20 cm). For further processing of this data I needed to take data only from meters (i.e. from 0, 1, 2, 3, 4 ....). I made this program to automate the actions, by commands to emulate the keys and change the mouse coordinates. I compiled it into the ehe. But it was just a digression. The program had to make labels and set the conditions for transition to these labels, ie to make a cycle of operations, with the condition to exit this cycle. When I came to the question of building the EA I had some idea about the general view of the code structure. But when I started reading the book, I was a bit confused by the structure of program construction and then by function int start () which is repeated at each tick and if at the time of its execution I found out the price of bid, it is written into a variable and is stored in it throughout its execution, then how will I know when the price has changed and the program can perform any other actions based on the new price? I thought that the price in my program will be updated by some kind of loop code, constantly asking for it. I also had another question: when you call a function and its execution, I imagined that the next function will be executed, but it turned out that control returns to the function, following the one that initially called the function. But this fact pleased me in particular, because it became clear to me (because I thought, what about other actions, which followed this function, are they skipped in this way?) Now I have a question like this: I understood that when executing init function, I will call a function for calculating a suitable opening price for my order, and also for calculating a lot size based on balance data on my account; after init is finished, execution of start function will start, which will contain a code for "waiting" for The necessary price of the transaction, and as soon as it will come, the order opening will be executed (take profit is not assumed to put because if there will be a jump which will jump over value ТР, it will not trigger, and the jump this potentially favourably than value ТР, therefore I want to form the order to close the order at last known price when the current price either is equal, or < , or > specified.or >); then the most mysterious thing for me is where to write the code of waiting for the order "close" price and return to the very beginning, i.e. again calculate the settled price and use the order "close". i.e., I have to calculate the price to be set for the opening, wait for this price, etc. I looked ahead into the sections; I think I saw something in the titles, which will probably answer my question. As I am finishing reading the Opening and Setting Orders section. I started reading it yesterday morning.
I will let you know the progress of course, as in my situation and without communication)). I hope for a constructive feedback.
Thank you for your time and attention.
 

Please break up the following messages into paragraphs, as it is difficult to understand what is being written.

Climber:
But having started reading the book, I was a bit confused by structure of program's construction and then by int start () function, which is repeated at every tick and if at the time of its execution I know the price of bid, then it is written to a variable and is stored in it throughout the execution time, then how will I know when the price has changed and the program can perform any other actions based on the new price? I thought that the price in my program would be updated by some loop code, constantly asking for it.

You are right to say that for the program to be able to work in real time, it is necessary to be informed periodically about the fact of a new price and the price itself.

In fact, there are 2 methods:

1st way is to run in endless loop some function and from it periodically request price.
But this method has disadvantages:
- If queries are made often, the result is a heavy load on the communication channel and a waste of PC resources;
- If you make queries infrequently, you may miss a new price between queries.

The 2-nd method (accepted as a basic one in MQ technology) is quite different in that the launch of the necessary code belongs to the terminal. The terminal receives information from the server, understands that a new tick has come (the fact of a new price and the price itself) and based on this information, the terminal starts() function. All this is described in the Program in MQL4 section.

Climber:
Now I have a question: I realized that when executing init function I'll call function of calculating opened price of my order, and also lot size calculation, based on balance data on my account; after init execution execution will start start start function start...

Incorrect. init() and start() are special functions. They are called by the client terminal according to their own properties (or more simply, according to the rules, conditions under which they are called for execution). This is described in the Special Functions section. The init() function is executed once, when the program is loaded in the window. And start() is called on each tick. This is very convenient. A tick came and brought a new price = the terminal launches start() that is executed until it is executed. All calculations must be inserted into it - calculation of the number of lots, conditions of opening/closing orders and everything else.

Climber:
Then the most puzzling thing for me is where to write the code expecting the "close" price of the order and return to the beginning, i.e. again, calculating the opening price, waiting for this price, etc.. I looked ahead into the sections; I think I saw something in the titles, which will probably answer my question. I will enjoy reading the Opening and Setting Orders section.

Judging by your questions, you do not need to write programs and you do not need to read what you are now reading. In this situation, it is strongly recommended to start reading the book again. Consistently and without getting ahead of yourself. And as you go along, type all of the presented examples into ME and execute them on your PC. And do not proceed to the next section until every letter of the code is fully understood. Look up the concepts of Tick, Control, Function, Special Function, Operator, and generally check the Dictionary of Terms from time to time.

Climber:
I will certainly report the progress as in my position and without communication)) I hope for a constructive feedback.
Thank you for your feedback.

I hope you will take the advice correctly and follow it.

 
SK. wrote (a):
<br / translate="no">

Climber:
Then the most puzzling thing for me is where to write the code waiting for the "close" price and return to the beginning, i.e. again counting the opening price, waiting for this price, etc. I looked ahead into the sections; I think I saw something in the titles, which will probably answer my question. I am still reading the Opening and Setting Orders section.

Judging by the questions you have, you don't need to write programs yet and you don't need to read what you're reading now either.

Ugh, I think I've got the structure of the expert.
Quickly scribbled it down in the vdp, here you go:

Init

Start

I set the maximum number of open orders = 1;

Check how many orders are open;

if 1 is open, then I call the function to close the order,

If no orders are opened, then I call the function "calculate price and number of lots".

--------------------------

Necessary to calculate a favourable buy and sell price and to calculate the number of lots;

--------------------------

querying the current price;

if the current price >= bargain price to sell,

then open a sell order equal to the number of lots already determined;

if not, compare the current price with the winning bid price;

if the current price is <= Bid price,

respectively open a Buy order;

--------------------------

Call function to close the order;

return

deinit


Got that right?
 
Climber:
Got it right?

Well, right on the whole :) Congratulations to you. It's always nice to understand something else.

Still, take my advice: go back to the beginning and go from the beginning without skipping or skipping over, methodically executing on your PC all the codes suggested in the tutorial.

 
Parabellum:

There is an interesting ROC indicator in the book - the rate of price change indicator(https://book.mql4.com/ru/samples/iroc). I understand that this book is a textbook and the author is not obliged to give semi- or even quarter-grain source code, but the idea of the indicator is very good, and I've tweaked it a bit to make the lag even weaker.

By the way, the text of indicator published on the page itself has two misprints in the code and therefore it is uncompilable.
      Line_4[i]=(Line_1[i]+Line_2[i]+Line_3[i])3;// Суммарный массив
      //-------------------------------------------------------- 17 --

      Line_5[i]= Sum(Aver_Bars+1); // Индик. массив сглаженной линии
Two times division character is missing (disappeared during creation of html file?).
The indicator file linked in the text is normal.
 
SK. писал (а):

Well, right on the whole :) Congratulations to you. It's always nice to understand something else.

Still, follow my advice: you need to go back to the beginning and go from the beginning without skipping and skipping, methodically executing on your PC all the codes suggested in the tutorial.


Yes, I agree, it is very important to see and feel any result of what you read, that is when it is fixed in the long term memory, all the more under the influence of emotional content, more than one association appears:) By the way, I also wanted to emphasize that in order to stimulate interest, it is necessary to go back or use those chapters that are most important, then the interest itself will lead to the goal and expand the circle of knowledge. The most difficult thing in our situation is to understand what is interesting as we go along, to work out the order that will be most effective, as we see the effectiveness of everything we own. I would relate the operation of associative memory to guided memory, where a distributed area exists as long as there is at least one link(association) to it, the more links, the more likely the area will not be released, thus ensuring its use and subsequent usefulness. It is therefore necessary to constantly replenish the stock of associations in order to consolidate what you have read.

P.S.: Sorry if I went too far:)

 
timbo: By the way, the indicator text published on the page itself has two typos in the code, and therefore is uncompilable.
      Line_4[i]=(Line_1[i]+Line_2[i]+Line_3[i])3;// Суммарный массив
      //-------------------------------------------------------- 17 --

      Line_5[i]= Sum(Aver_Bars+1); // Индик. массив сглаженной линии
The division symbol is missing twice (disappeared when creating the html file?).
Indicator file linked in the text is normal.


Yes, there is such a thing. In source codes no errors detected yet, but text codes are screwed up in some places for some reason. We will sort it out.
 
xnsnet:

P.S.: Sorry if I overreacted:)

:) Yeah, a little bit there)))
 
There is also a small note about the book. It is better to use real-life examples, I mean not on cats, but on money or things that are relevant. For example, when I got to the loop operators, the example with sheep distracts you a little, you start to think about how it can be used in our conditions, and this distracts from the process of perception.

I'm already having a hard time with loops (I'll come back to these sections more than once), and then there are those sheep.

Just my opinion, if considered a specific example of trading (almost raiding:)), okay, trading, then if the reader meets an example similar to his idea (which he does not know how to implement), he will with great interest consider this example and think how to modify and customize it to implement his idea (I think you know what I mean).

I had such an example, I was interested in how to open an order for 1/3 of the balance. You can't imagine what kind of mathematical operations were performed in my head in order to convert 1/3 in dollars, so that I got the sum in lots (0.1 or 3.5 lots...). And how happy I was when I saw an example ofopenbuy.mq4, which opens a Buy order worth 35% of available funds, with some specified values of stop orders. I looked it through and through, looked into each line, what, why, and where. I looked at the standard functions used in it (MathFloor, MarketInfo. ....).
Reason: