Go through all positions by timer and compare TimeCurrent()-OrderOpenTime()>=300
After all, all tickets and opening times are already present in standard arrays.
We enumerate all positions by timer and compare TimeCurrent()-OrderOpenTime()>=300
What are those standard arrays?
Can you elaborate on them?
- docs.mql4.com
How can I use these functions to close positions individually and not all at once after 5 minutes?
There is the first open position, we wrote time and ticket
in the variable, then the second position is opened, we see it in the loop as the last one and write time and ticket
in the variable, but all these actions are overwritten and it turns out that data is stored only from the last position
How can I use these functions to close positions individually and not all at once after 5 minutes?
There is the first open position, we recorded it in the variable time and ticket
then the second position opened, we see it in the loop as the last one and record it in the variable time and ticket
but all these actions are overwritten and it turns out that data is stored only from the last position
The condition of selecting only one order out of those to be closed not earlier than 5 minutes after opening is not quite clear.
Therefore, I do not know what to answer.
If there is a condition, we should start with it.
At the moment, all of them have been virtually selected and will be closed in a pile, one after the other.
The very first open order has this: TimeCurrent()-OrderOpenTime() will be the largest, for example, its OrderTicket().
You can go through all of your market orders in the following loop
int i; for (i=OrdersTotal()-1; i>=0; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { ............ } }
mql4
For example, 5 minutes have passed after the order was opened and this order must be closed.
Have a look at Kim Igor's functions.
Here, at a glance, the function returns the number of seconds after the last position was opened.
//+----------------------------------------------------------------------------+ //| Автор : Ким Игорь В. aka KimIV, http://www.kimiv.ru | //+----------------------------------------------------------------------------+ //| Версия : 19.02.2008 | //| Описание : Возвращает количество секунд после открытия последней позиций. | //+----------------------------------------------------------------------------+ //| Параметры: | //| sy - наименование инструмента ("" - любой символ, | //| NULL - текущий символ) | //| op - операция (-1 - любая позиция) | //| mn - MagicNumber (-1 - любой магик) | //+----------------------------------------------------------------------------+ datetime SecondsAfterOpenLastPos(string sy="", int op=-1, int mn=-1) { datetime t=0; int i, k=OrdersTotal(); if (sy=="0") sy=Symbol(); for (i=0; i<k; i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol()==sy || sy=="") { if (OrderType()==OP_BUY || OrderType()==OP_SELL) { if (op<0 || OrderType()==op) { if (mn<0 || OrderMagicNumber()==mn) { if (t<OrderOpenTime()) t=OrderOpenTime(); } } } } } } return(TimeCurrent()-t); }
Revise it to suit your task. Add to it the function that closes one position.
It is not quite clear whether it is one order that has to be closed at least 5 minutes after opening.
Let me explain in a simpler way:
There is the 1st order in the market, then the 2nd, 3rd and so on.
The time of the first order has expired and we should close it, then the 2nd order is the first in the series, the time has expired and we should close it, etc.
In fact, we should choose the oldest order to close if it is >=5*60
But to select it out of a series to be closed by the ticket, we should somehow define the smallest value of the ticket in the open orders and determine the time of existence.
Have a look at Kim Igor's functions.
Here, at a glance, the function returns the number of seconds after the last position was opened.
It does not fit. Interested in the very first position in the market.
I'll make it simple:
There is the 1st order in the market, then the 2nd order, the 3rd one and so on.
The time of the first order has expired, then the 2nd order is the first in the series, time has expired, it should be closed, etc.
In fact, we should choose the oldest order to close if it is >=5*60
But to select it from a series to be closed by the ticket, we should somehow define the smallest value of the ticket in the open orders and determine the time of existence.
It won't fit. Interested in the very first position on the market.
What's the problem?
- Find the oldest order opened by the Expert Advisor. It has the smallest OrderOpenTime().
- Compare how much time has passed from the moment of opening of this order to the current time. If it is equal to or greater than the specified time, close it.
int nOlderTicket = -1; datetime dtOlderOrderTime = D'3000.12.30'; for (int i = OrdersTotal() - 1; i >= 0; --i) { if (!OrderSelect(i, SELECT_BY_POS)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != i_nMagicNumber) continue; if (nOlderTicket < 0 || OrderOpenTime() < dtOlderOrderTime) { nOlderTicket = OrderTicket(); dtOlderOrderTime = OrderOpenTime(); } } if (TimeCurrent() - dtOlderOrderTime >= время в секундах) { // Закрыть ордер nOlderTicket }
I'll make it simple:
The 1st order is in the market, then the 2nd, 3rd and so on.
The time of the first order has expired and we should close it, then the 2nd order is the first in the series, time has expired and we should close it, etc.
In fact, we should choose the oldest order to close if it is >=5*60
But to select it from a series to be closed by the ticket, we should somehow define the smallest value of the ticket in the open orders and determine the time of existence.
It won't fit. You are interested in the very first position in the market.
In terms of time of existence, you can select both the very first open order and the most recent one. In this case we remember the ticket and the number of seconds of existence. For example
int i, DeltaTimeOpen, prevDeltaMax, prevDeltaMin, TicketFirst, TicketLast, DeltaTimeClose; //--------------- prevDeltaMax=0; prevDeltaMin=9999999999999; TicketFirst=0; TicketLast=0; DeltaTimeClose = 5*60; for (i=OrdersTotal()-1; i>=0; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { DeltaTimeOpen = TimeCurrent()-OrderOpenTime();//возраст ордера в секундах if(DeltaTimeOpen>=DeltaTimeClose) { if(DeltaTimeOpen>prevDeltaMax) { TicketLast=OrderTicket();//последний prevDeltaMax=DeltaTimeOpen;//возраст } if(DeltaTimeOpen<prevDeltaMin) { TicketFirst=OrderTicket(); //первый prevDeltaMin=DeltaTimeOpen;//возраст } } } } if(TicketFirst>0) { //ну и пошло-поехало... }
The easiest way is to enter in the comment field the time when the order should be closed.
Then you only need to remember the closest time to close (and even then, it is optimization).
When it is time for the "revision", on a timer or whatever you want, loop through the open orders and close those whose lifetime exceeds the time specified in the comment
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
mql4
For example, 5 minutes have passed after the order was opened, and this order should close.
The code is too cumbersome to memorize the ticket to close and the opening time to calculate the minutes for each order separately.
There may be 1,2,3-10 positions, buy and sell at the same time.
Can you suggest a function to store this data in an array to further compare and close on the required ticket?