Rustam Bikbulatov: Hi All. Please tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
At the moment it is closing the most recently opened ones. You want it to close the first ones first!
for( int i = 0; i < OrdersTotal(); i++ ) { if( /*Функция закрытия. Если вернула истину*/ ) i--; }
Try it this way. Instead of comments, you should use the function to close the order. I have not checked its functionality. And here, I think that we will have to call the function that calculates the amount of orders each time, since the number of orders will decrease.
Rustam Bikbulatov: Hi All. Can you tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
At the moment it is closing the most recently opened ones. I want it to close the first ones first!
//+------------------------------------------------------------------+ //| Закрыть все позиции начиная с первых | //+------------------------------------------------------------------+
double CloseOrders()
{ for(int i=0; i<OrdersTotal(); ) { if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
if (OrderMagicNumber() != mn) break;
int type = OrderType();
bool result = false;
switch(type) { //Close opened long positions caseOP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, CloseColor ); break;
//Close opened short positions caseOP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, CloseColor ); break; }
The option I have suggested is generally incongruous for closure.
If you are talking about closing from old to new, then you are closing the grid. Having some experience with grids, all I can say for sure is that you should not choose by the age of the position, but by its lot, and you should close from a larger lot to a smaller one. If the grid is single lot, then there is no difference in the order of closing. Use ready-made functions to close, and do not invent something that has already been invented)
The option I have suggested is generally incongruous for closure.
If you are talking about closing from old to new, then you are closing the grid. Having some experience with grids, all I can say for sure is that you should not choose by the age of the position, but by its lot, and you should close from a larger lot to a smaller one. If the grid is single lot, then there is no difference in the order of closing. Use ready-made functions for closing, and do not invent something that has already been invented)
Gentlemen Developers! Good day to all. I am interested in the question of creating a template for an Expert Advisor (script) when creating it. Can this be edited somewhere and how is it done?
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hi All. Can you tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
Hi All. Please tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
{
if( /*Функция закрытия. Если вернула истину*/ )
i--;
}
Try it this way. Instead of comments, you should use the function to close the order. I have not checked its functionality. And here, I think that we will have to call the function that calculates the amount of orders each time, since the number of orders will decrease.
You may also try such a variant:
/*Функция закрытия ордера*/
it closes in an awkward way)
not correct, but for now (up to some build) it works: on successful closing of i--
Hi All. Can you tell me how to change this code for(int i=OrdersTotal()-1;i>=0;i--){
//| Закрыть все позиции начиная с первых |
//+------------------------------------------------------------------+
double CloseOrders()
{
for(int i=0; i<OrdersTotal(); )
{
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
break;
if (OrderMagicNumber() != mn)
break;
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, CloseColor );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, CloseColor );
break;
}
if(result == false)
{
Print("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
i++;
}
}
return (True);
}
// End
It closes in an awkward way)