[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 858

 

It's a simple question, but I can't think of anything.

There are 4 open positions at a particular point in time. I am trying to close all of them in the following way:

for (int i=0;i<OrdersTotal();i++)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
        if(OrderType()==OP_BUY)
           OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
        if(OrderType()==OP_SELL)
           OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
      }
  }

But they are closed not all, but through one. In fact they have the following numbers: 2,4,6,7. So 2nd and 6th positions are closed.

What is the trick?

 
usver:

It's a simple question, but I can't think of anything.

There are 4 open positions at a particular point in time. I am trying to close all of them in the following way:

But they are closed not all, but through one. In fact they have the following numbers: 2,4,6,7. So 2nd and 6th positions are closed.

What is the catch?


The trick is that after the position is closed, the numbering changes in the OrderSelect(i,SELECT_BY_POS,MODE_TRADES) and simultaneously not all can be closed

SZY: I was very helped by fts I. Kim, I have changed a little - removed extra brackets and pulled in one line:

//+------------------------------------------------------------------+
void CloseAllFirstProfit() {
   int i, k=OrdersTotal();
   for (i=k-1; i>=0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) if (OrderType()==OP_BUY || OrderType()==OP_SELL) if (OrderProfit()+OrderSwap()>0) ClosePosBySelect();
   k=OrdersTotal();
   for (i=k-1; i>=0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) if (OrderType()==OP_BUY || OrderType()==OP_SELL) ClosePosBySelect();
}
//+------------------------------------------------------------------+
void ClosePosBySelect() {
   if (OrderType()==OP_BUY)  OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 10, CLR_NONE);
   if (OrderType()==OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 10, CLR_NONE);
}
//+------------------------------------------------------------------+

search the forum - you'll find the originals

 
IgorM:


is that after closing a position, the numbering changes in OrderSelect(i,SELECT_BY_POS,MODE_TRADES) and all cannot be closed simultaneously

SZY: I was very helped by fts I. Kim, I have changed a little - removed extra brackets and pulled in one line:

search through the forum - you will find the original


Thanks for the tip and example. I will look into it.

On the way I will ask another question.

The task is as follows: I need to determine what order type triggered by a stop loss was closed, and to compare a future order closed by a stop loss with a previous one (i.e. compare their types). Having looked through the documentation, I could not find anything related to the processing of the last event.

At this point, I implemented this operation as a closing of an order when it reaches a set boundary (Bid-OrderOpenPrice() >=TP*Point). That enabled me to store in a variable the type of the last closed order and compare it with the one just closed ( if(type==OrderType()) ). Everything works fine during testing, but I would still like to implement it using stop losses.

I tried to select a closed order from the history using OrderSelect() and the MODE_HISTORY argument. Since there are no conditions to close orders, then a change in the OrdersTotal() variable may be considered as a trigger of a stop loss. But I ran into a problem: I cannot find the number of the last closed order (since their number is constantly changing), or I just haven't figured out how to do it.

The question is: How to implement it using stop-losses? Maybe there are easier ways? Well, if the direction of thought is correct, then how do we find the last closed order?

 
IgorM:


the numbering is changed in OrderSelect(i,SELECT_BY_POS,MODE_TRADES) after closing a position and all positions cannot be closed simultaneously

SZS: I was very helped by I. Kim's functions, I changed them a little - I removed the extra brackets and pulled them into a single line:

search through the forum - you will find the original

You've said enough... all in one pile.
https://docs.mql4.com/ru/trading/OrderSelect
 
usver:

It's a simple question, but I can't think of anything.

There are 4 open positions at a particular point in time. I am trying to close all of them in the following way:

But they are closed not all, but through one. In fact they have the following numbers: 2,4,6,7. So 2nd and 6th positions are closed.

What is the problem?

Are you talking about numbers in order? Or tickets?

 
abolk:

Are you talking about numbers in order? Or tickets?


I am already confused myself. They are tickets, but I gave them to make the situation clear. They close one by one.
 

Hooray I got it working!!!


Victor thank you very much, I had a mistake, I didn't write the name of the indicator correctly.

 
abolk:
What you just said... all in one pile
https://docs.mql4.com/ru/trading/OrderSelect


Try to change/remove takeaways or stops in a group of orders - the task is simple, but I have encountered that, when order selection was done using OrderSelect(i,SELECT_BY_POS,MODE_TRADES), the order numbering changed right in the loop body and I managed to modify an already modified (without takeaway) order several times - error #1, which is why I suggested that wever had a similar problem

Please, advise - how to make the indicator recalculate at the close of several bars, for example: I want the indicator to be recalculated every 5 new bars

 
IgorM:


Please advise - how to make an indicator recalculate at the close of several bars, for example: I want the indicator to be recalculated every 5 new bars

You know about programming.

This is a logical task and you do not have to be very smart to solve it. Think with your head.

There are different cases. Where exactly it should be re-rinked, on what TF, how the re-rinking should occur, etc.. How to answer your question.

Note the time of the bar and after five bars reset this time and note it again. At the time of zeroing time you can put a condition to recalculate. Function iTime(NULL,0,0) will help. The rest is all logic.

 
IgorM:


Please advise - how to make an indicator recalculated at the close of several bars, for example: I want the indicator to be recalculated every 5 new bars


The method is not the most reliable, probably, but it will work just to try the idea (for example, a draft).

Use the Bars variable and keep track of its change. Increased by 5, remembered, etc.

Reason: