Self-learning the MQL5 language from scratch - page 65

 
MrBrooklin:

Hello, Valery! Thank you very much, your version "...if positions with our magik and at our symbol are equal to zero, we check the time and open a position..." turned out to be the easiest and most effective. I added it to the condition of checking time && PositionTotal()==0 and only one position with the needed amount is opened!

Sincerely, Vladimir.


Unfortunately, MKL doesn't have command, which returns the number of positions on the symbol. TotalPosition returns all positions on the account on all symbols. Therefore, to get positions on the symbol you need to search all positions and compare the symbol position on ours.

Also. The logic in the headfirst time, open it and check the presence of our position, trilling is not always optimal. Sometimes it is better to check first if the position is opened with our wizard and if it is, we check closing time, or else we trawl it, or else we check opening time.

The objective is to reduce the number of EA actions.

 
Valeriy Yastremskiy:

Unfortunately, there is no command in MKL that returns the number of positions on a symbol. TotalPositions returns all positions on all symbols. Therefore, to get positions on a symbol you need to search all positions and compare the symbol position to ours.

Also. The logic in the headfirst time, open it and check the presence of our position, trilling is not always optimal. Sometimes it is better to check first if the position is opened with our wizard and if it is, we check closing time, or else we trawl it, or else we check opening time.

The objective is to reduce the number of EA actions.

All is clear, Valery! I am trying to implement your tips.

Sincerely, Vladimir.

 
Valeriy Yastremskiy:

If positions with our Magik and on our symbol are zero, we check the time and open a position, if it is 1, we don't open a position, we check the closing time and trawl, if it is more than 1, we alert and don't work. We may use piggyback or if we want to check it with iff.

Hello, Valery! I tried to write the code without the position enumeration. I wrote it, checked it, it all works.

   if(PositionSelect(Symbol())==false && PositionGetInteger(POSITION_MAGIC)!=Magic_Number
   && time_current.hour==time_open.hour && time_current.min>=time_open.min && time_current.min<time_open1.min)
      OpenBUY();
Sincerely, Vladimir.
 
MrBrooklin:

Hello Valery! I tried to write the code without the position enumeration. I wrote it, checked it, it all works.

Regards, Vladimir.

It's OK for a start, but only for a start. The conditions are too strict. If there are no positions on our symbol and no positions with our magik on all symbols then it seems normal. In real life you may have 2 windows open on one symbol and another may have a position. You can simply use Magik to account for example like trishkin, first 3 digits - symbol code and the next 2 - script/advisor code. And the first step is to code manually, or generate a magik by adding lines depending on the window symbol and script code and then convert the line into a number.

The classic way is to search for all orders or positions.

But this is too much for the beginning.

That is why it is ok to start with.

 
Valeriy Yastremskiy:

It's OK to start with, but only to start with. The conditions are too strict. If there are no positions on our symbol and no positions with our magik on all instruments then it seems normal. In real life you may have 2 windows open on one symbol, and another may have a position. You can simply use Magik to account for example like trishkin, first 3 digits - symbol code and the next 2 - script/advisor code. And the first step is to code manually, or generate a magik by adding lines depending on the window symbol and script code and then convert the line into a number.

The classic way is to search for all orders or positions.

But this is too much for the beginning.

That is why it is OK to start with.

Thank you, Valeriy! Searching through all the positions is one of the next steps in the self-study that I will surely do.

Regards, Vladimir.

 
Valeriy Yastremskiy:

For some reason all orders or positions are considered a classic overrun.

Alas, this is a tradition in the MQL-community, all EAs are developed taking into account a sudden loss of connection and/or power outage of a PC.

on one hand this is a good methodology, but on the other hand this imposes great restrictions on the EA code writing style - EA only works with order masters. For example, if you need to write a martingale, EA will search the history for the last order with its masters and look at the profit/loss and decide whether to increase the new lot or not

... in general since the creation of MT - everyone is waiting for the internet to disappear and the strategy itself is not so important anymore ))

 
Igor Makanu:

Alas, this is a tradition in the MQL-community, all EAs are developed with a sudden loss of connection and/or PC power outage in mind.

On one hand this is a good methodology, but on the other hand it imposes great restrictions on the EA code writing style - EA only works with order masters. For example, if you need to write a martingale, EA will search the history for the last order with its masters and look at the profit/loss and decide whether to increase the new lot or not

... in general since the creation of MT - everyone is waiting for the Internet to disappear, and the strategy itself is no longer so important ))

Hello Igor, thank you for sharing this very useful information.

Sincerely, Vladimir.

 
Igor Makanu:

Alas, this is a tradition in the MQL-community, all EAs are developed with a sudden loss of connection and/or PC power outage in mind.

On one hand this is a good methodology, but on the other hand it imposes great restrictions on the EA code writing style - EA only works with order masters. For example, if you need to write a martingale, EA will search the history for the last order with its masters and look at the profit/loss and decide whether to increase the new lot or not

... In general since the creation of MT - everyone is waiting for the internet to disappear, and the strategy itself is not so important anymore ))

Protection against fire, floods and foolishness should always be!) Usually I limit myself to stops in case of loss of connection with DT.

 

Good day and good mood everyone!

I'm continuing studying the MQL5 programming language. I've started a detailed study of the for loop operator, which according to the MQL5 Reference, and I quote:

The for loop operator

Executes the operator until the expression being checked becomes false. The expression is checked before each iteration


I go to the description of the for loop operator and there I read that:

Оператор for состоит из трех выражений и выполняемого оператора:

for(выражение1; выражение2; выражение3) 
   оператор;

Выражение1 описывает инициализацию цикла. Выражение2 - проверка условия завершения цикла.
Если оно истинно, то выполняется оператор тела цикла for. Все повторяется, пока выражение2 не станет ложным. 
Если оно ложно, цикл заканчивается и управление передается следующему оператору. 
ВыражениеЗ вычисляется после каждой итерации.

Operator executes an operator? OK. I take and run the for loop operator to try all the open positions and then type in the if statement with the conditions I need:

   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      if(PositionSelect(Symbol())==false && PositionGetInteger(POSITION_MAGIC)!=Magic_Number
         && time_current.hour==time_open.hour && time_current.min>=time_open.min && time_current.min<time_open1.min)
         OpenBUY();
     }

and then nothing happens. The Buy position is not opened. How do I understand this? I must be doing something wrong, or I do not understand the term of the for statement correctly at all?

Dear Expert! Please advise me, but not in words, not in a corrected code, otherwise I'll never learn the MQL5 programming language.

Sincerely, Vladimir.
 
MrBrooklin:

Good day and good mood everyone!

I'm continuing studying the MQL5 programming language. I've started a detailed study of the for loop operator, which according to the MQL5 Reference, and I quote:

The for loop operator

Executes the operator until the expression being checked becomes false. The expression is checked before each iteration


I go to the description of the for loop operator and there I read that:

Operator executes an operator? OK. I take and run the for loop operator to try all the open positions and then type in the if statement with the conditions I need:

and nothing happens. The Buy position is not opened. How do I understand this? I must be doing something wrong or I do not understand the purpose of the for loop operator correctly at all?

Dear specialists! I'm asking you to give me some feedback, but not in words, otherwise I'll never learn MQL5 programming language.

Sincerely, Vladimir.

The loop operator and operators in the loop body. It is not quite a classical Russian language.

Everything is good, of course, but where is the iterator i in the loop body? And in order to get the position character and its magic symbol, it must be selected first. The position description is a structure (in MQL5 everything is a structure, orders, time, and deals), and it is filled out through selection, by the iterator that is not a position ticket, but a position number. And we should remember that the position description structure is always the last choice.

Reason: