[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 564

 
rigonich:


The problem is as follows: since you don't have OrderSelect() in this function, it seems to be used somewhere in the loop with a search of orders. If this loop organizes the search from zero order to the last one, then after the modification of the first order, or if any order has been already buried on this tick, whenOrderSelect() is next called, the order order order order sequence changes and the function may select an order that has already been modified. Therefore, we should check if the value of the order StopLoss is equal to the one we send to the OrderModify() function


No, the problem of changing the position order number when one of them is closed has been solved, I have checked more than once thinking there might be an error there, but everything is OK there, the required order is selected and the stoploss is calculated correctly for it, but the order does not want to be modified...
 

delf:

The last order must be closed in any direction and the next order must open in the other direction (when the conditions are met, respectively).

Stricter option

#define OrderBuy  0
#define OrderSell 1
int NewOrder;
int RezultatSend; 

int start()
{
... 
... 

if(OrdersTotal()==0)
{
if(NewOrder==OrderBuy)
   {
   if(/*Условие для Buy*/)
      { RezultatSend=OrderSend(/*...buy*/);     //Ставим Buy
        if(RezultatSend!=-1)NewOrder=OrderSell; //Если ставка прошла
      }
   }
 else
   {
   if(/*Условие для Sell*/)
      { RezultatSend=OrderSend(/*...*/);       //Ставим Sell
        if(RezultatSend!=-1)NewOrder=OrderBuy; //Если ставка прошла
      }
   }
}

...
...

}  
 

abeiks:

For some time I have been trying to rework the buy close block to close only the last two buy positions, but it fails. Can you tell how to redo the block ?

It is easier and more convenient to make a function (block) to close one last position. And run it twice. In a loop, or just in a row.
 
abeiks:

For some time I have been trying to rework the buy close block to close only the last two buy positions, but it fails. Can you tell how to redo the block ?

Some time ago I gave you a solution algorithm to find the last positions. And then what to do with them is IMPORTANT: count, delete, modify STOPs. If you are not able to write code at least "in the image and likeness", it's not yours (programming)...
 
rigonich:



Thank you!

 
peshihod:
It's easier, and more convenient, to make a function (block) to close one last position. And run it twice. In a loop, or just in a row.

Thank you, it's a good option I'll try too.

 
delf:

I must have phrased it wrong :(

There is an algorithm for opening orders.

I need, that at any variant of closing of the last order, the following was opened in other direction (when the conditions accordingly appear).

All works, but if I have closed by a stop loss, for example, for sell, and the conditions to open a sell in this direction, the opening continues. What we need is to wait for conditions to buy.

That is, we need to check the last closed order, so it would not coincide with the opening one (buy-sell-buy-sell alternation). Only one order or waiting for a signal is in operation.

I understand that we should probably use OrdersHistoryTotal(), but how would it look in a working EA?

Thank you.


This is probably the case somewhere. But if there are a lot of orders in the history it is better to use your own order book. https://www.mql5.com/ru/articles/1404https://www.mql5.com/ru/articles/1390
//+------------------------------------------------------------------+
//|                                                         0000.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
void CloseOrder(string signal)
   {
   int      i,Ticket=-1,error; 
   double   Price;
   datetime time=0;
   for (i=0;i<OrdersHistoryTotal();i++)  
      {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY));
         {
         error=GetLastError();Print("Ошибка OrderSelect() = ", error);
      }
      if(OrderSymbol()==Symbol()&&(OrderMagicNumber()==magic
      ||OrderClosePrice()==OrderStopLoss())
         {
         if(OrderCloseTime()>time)
            {
            time=OrderCloseTime();Ticket=OrderTicket();
         }
      }
   }
   if(!OrderSelect(Ticket,SELECT_BY_TICKET,MODE_HISTORY))
      {
      error=GetLastError();Print("Ошибка OrderSelect() = ",error);
   }
   if(OrderType()==OP_BUY&&signal=="CloseBUY"||OrderType()==OP_SELL&&signal=="CloseSELL")return;
   if(signal=="CloseBUY")Price=NormalizeDouble(Bid,Digits);
   if(signal=="CloseSELL")Price=NormalizeDouble(Ask,Digits);
   for (i=0;i<OrdersTotal();i++)  
      {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES));
         {
         error=GetLastError();Print("Ошибка OrderSelect() = ",error);
      }
      if(OrderSymbol()==Symbol()&&(OrderMagicNumber()==magic)
         {
         if(OrderType()==OP_BUY&&signal=="CloseBUY"||OrderType()==OP_SELL||signal=="CloseSELL")
            {
            Ticket=OrderClose(OrderTicket(),OrderLots(),Price,0,CLR_NONE);
         }
      }
      if(Ticket<0)
         {
         error=GetLastError();Print("Ошибка = ",error);
      }
   }
   return;
}
       
 
peshihod:

A stricter option

Thanks so much to everyone who responded, result achieved, just what I needed.
 
delf:

I must have phrased it wrong :(

There is an algorithm for opening orders.

I need, that at any variant of closing of the last order, the following was opened in other direction (when the conditions accordingly appear).

All works, but if I have closed by a stop loss, for example, for sell, and the conditions to open a sell in this direction, the opening continues. What we need is to wait for conditions to buy.

That is, we need to check the last closed order, so it would not coincide with the opening one (buy-sell-buy-sell alternation). Only one order or waiting for a signal is in operation.

I understand that we should probably use OrdersHistoryTotal(), but how would it look in a working EA?

Thank you.

If you want to learn to program, you can either look through the code of others (this is what CodaBase is for), or proceed from the algorithm of the solution of the problem. Asking to write code for you is the most hopeless way to learn. More often than not you will get a poorly coded solution to your problem, something like (allegorically) your neighbour calling you at the door every minute and asking "what's your name" - this is about unnecessary (superfluous) calculations.

The algorithm for solving your problem may be as follows. First, you need to make sure you do not have "your" orders. This problem, if it isn't for the tester (where we can limit the solution to the condition if (OrdersTotal() == 0), is solved by searches of orders using filters for matching OrderMagicNumber() and Symbol(). Then you check the condition (according to your strategy) to open the order. If there is a signal, then using the search of OrdersHistoryTotal() (usually it is the last one in the list), calculate the OrderType() of the last closed order. If the signal to open the order and the type of the last closed order do not coincide, open the order based on the signal.

This approach involves minimum calculations and greatly reduces the time of the Expert Advisor optimization in the tester.

 
TarasBY:
Some time ago I gave you the algorithm for solving the search of the last positions. And then what to do with them does not matter: count, delete, modify STOP. If you are not able to write code at least "in the image and likeness", it's not yours (programming)...


Yes, I agree, programming is not my thing, so I am learning and asking questions here. I tried to add position closing to your algorithm but it failed (all buy positions were closing) so I decided to approach the problem from another angle and see the solution of closing the last two buy positions on a ready-made closing block that was provided and offered to me.

I would be very grateful if you could show me how to close the last two buy positions. I will understand where I have made a mistake.

double profits_buy_2pos()
{
    int ordercount = 0, OpenOrders = OrdersTotal();
    double pr_buy = 0;
    for (int cnt = OpenOrders - 1; cnt >= 0; cnt--)   // scan all orders and positions. ..
    {
        if (!OrderSelect (cnt, SELECT_BY_POS)) return (0.0);
        if (OrderSymbol() != Symbol()) continue;
        if (OrderMagicNumber() != magic) continue;
        if (OrderType() != OP_BUY) continue;
        pr_buy += OrderProfit();
        ordercount++;
        if (ordercount == 2) break;
    } 
    Print ("::::::::::::::::::::::::: pr_buy2 = ", pr_buy);   
    Print ("::::::::::::::::::::::::: ordercount = ", ordercount);    
    return (pr_buy);   
}

Reason: