Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1740

 
Maxim Kuznetsov #:

don't tell people what to do.

You have to remember your data and work with it

Maxim, in that situation, you don't need to remember anything. It's much safer to just look at the previous closed position.

You don't need to do anything with remembering the data and restoring it after an abnormal situation. If the terminal is closed, the position data will be lost. Accordingly, when memorizing them, you should immediately consider the possibility of their quick and effective restoration.

(Memorizing an order in the comments would not be said to be a reliable solution - it means relying on the will of the broker/dealer).

And the interesting thing is that it's going to be reading this data from somewhere anyway. So wouldn't it be easier to read it straight from the source?

 
Sergey Zhukov #:

how do I force the compiler to compile for MT4 and not MT5?

#ifdef __MQL4__

#else

#endif
 
Artyom Trishkin #:

Maxim, in that situation, you don't need to memorise anything anywhere. It's much safer to just look at the past closed position.

You don't have to do anything with remembering the data and then restoring it after an emergency. If the terminal is closed, the position data will be lost. Accordingly, when memorizing them, you should immediately consider the possibility of their quick and effective restoration.

(Memorizing an order in the comments would not be said to be a reliable solution - it would mean relying on the will of the broker/dealer).

And the interesting thing is that it's going to be reading this data from somewhere anyway. So wouldn't it be easier to read it straight from the source?

and then follows "why OnTimer doesn't fit into couple of seconds or OnTick misses wildly" :-)

To restore state once at initialisation (either by reading from a file or browsing history/environment) and that's it... The last order together with its characteristics is stored in a variable and is always known

 
Maxim Kuznetsov #:

and then the "why OnTimer doesn't fit in a couple of seconds or OnTick misses wildly" theme follows :-)

When initializing, restore state once (either by reading from a file or looking through history/environment) and that's it... The last order together with its characteristics is stored in a variable and is always known

In order to know how the last position was closed, you need to find it. After all, while it is open, there is no way to record and remember how it was closed, because it is still open.

It is possible to store a ticket of the last opened position and use it to determine how it was closed, but ... When manipulating with a position, while it is open, its ticket can change. Accordingly, we must implement a functionality to rewrite the changed ticket. All this complicates the code.

I only suggested looking for the last closed position. And it combines the search of all positions when restarting the EA and read only one position's data when closing a new one.

After all, I hope everyone understand that we do not need to loop through all historical positions for this, but only the last one. I.e., we only remember one variable of the loop index - its current value, and we start a new loop (when the number of historical orders increases) from the value of this variable, not from zero. And the value of this variable is written in OnInit() at the start and restart of the Expert Advisor, as well as when closing a new position. I.e., this way we avoid the necessity of writing and reading position data and working with the drive.

I think this approach is the most profitable, because

1. You only need to remember the value of the loop index
2. Obtain data only at the necessary moment using a direct access to a closed order using its index
3. You don't need to create a read/write function
4. It's faster both to work with and to write.

And, yes, I agree that you need to know all your data - I do it myself. But it is desirable to know them beneficial to the program and yourself.

 
Ivan Butko #:

Thank you for the information!

If you have time, please answer how to fix:

On a history of N candles ago two differently directed targets are set: Close[N]+50*Point and Close[N]-50*Point.
Then there is a condition that the price should reach one of the targets (if (High[i]> Close[N]+50*Point) or (if (Low[i]<Close[N]-50*Point)
When the condition is met, the distance from Close[N] to High[i] is written into x[high]=High[i] array.
Then it is randomly thrown onto any of the graphs at any time.

And when checking via Print(x[high]), 1-2 out of ten values show less than 50! One has 12, the other 49. Although, it is strictly stated+50*Point. 8-9 are correct (over 50) and 1-2 are abnormal. It is not through the tester, but with a real chart I place an Expert Advisor (without any trading functions), but with the above-mentioned ones it works with the history and displays incorrect results.

The larger the value is, the less such errors occur. I thought it might be the spread that interferes, but... I don't think there is a spread on history in MT4

Solved.

 
Do I have to add RefreshRates() to OnTimer(), or does the timer update itself by default?
 
Ivan Butko #:
Do I have to add RefreshRates() to OnTimer(), or does the timer update itself by default?
It doesn't
 
Aliaksandr Hryshyn #:
Doesn't update

Thank you

 
Artyom Trishkin #:

To find out how the last position was closed, you have to find it. As long as it is open, there is no way to record and remember how it was closed - it is still open.

It is possible to store a ticket of the last opened position and use it to determine how it closed, but ... When manipulating with a position, while it is open, its ticket can change. Accordingly, we must implement a functionality to rewrite the changed ticket. All this complicates the code.

I only suggested looking for the last closed position. And it combines the search of all positions when restarting the EA and reads data of only one position when closing a new one.

After all, I hope everyone understand that we do not need to loop through all historical positions for this, but only the last one. I.e., we only remember one variable of the loop index - its current value, and we start a new loop (when the number of historical orders increases) from the value of this variable, not from zero. And the value of this variable is written in OnInit() at the start and restart of the Expert Advisor, as well as when closing a new position. I.e., this way we avoid the necessity of writing and reading position data and working with the drive.

I think this approach is the most profitable, because

1. You only need to remember the value of the loop index
2. Obtain data only at the necessary moment using a direct access to a closed order using its index
3. You don't need to create a read/write function
4. It's faster both to work with and to write.

And, yes, I agree that you need to know all your data - I do it myself. But knowing them is desirable for the benefit of the program and yourself.

I have not noticed that the ticket of a position changes, while it is open. It is usually equal to the ticket of the first order, which opened this position.

The last one closed - what if there is more than one EA trading on the symbol, plus manual trading?
 
JRandomTrader #:

I have not noticed that the ticket of a position changes while it is open. It is usually the same as the ticket of the first order which opened the position.

Last closed - but what if there is more than one EA trading on the symbol, plus manual trading?

A fill or partial close, and the ticket is different. We have to determine which ticket originated from which one, and rewrite the remembered one. However, it is not clear which platform we are talking about - both are slightly different, but you still need to keep track.

The "last closed one belonging to this EA" is what is meant.

Reason: