Dividing open positions into groups - page 6

 
Added a new function, now the advisor can change the caste of a group of positions to another set one.
//+------------------------------------------------------------------+
//Перевод отобранных по тикетам позиций в указанную касту
//array_base[][observations] основная база, в которой нужно провести изменения
//array_change[]             массив с тикетами позиций, касту которых нужно изменить
//array_change[]             номер касты, к которой нужно приписать позиции
//+------------------------------------------------------------------+
int Transfer_Positions_Specified_Caste_by_Tickets(int &array_base[][observations], 
                                                  int &array_change[], 
                                                  int new_caste){
int size_change = ArraySize (array_change);
int size_base   = ArrayRange(array_base,0);

   for(int i=0; i<size_change; i++){
      int ticket_change = array_change[i];
      for(int a=0; a<size_base; a++){
         if(array_base[a][0] == ticket_change ||
            array_base[a][1] == ticket_change){
            array_base[a][2]  = new_caste;
            break;}}}    
   ArrayFree(array_change);
       
return(size_change);
}
//+------------------------------------------------------------------+
 

Good evening. I'd like to hear from the pros.

Please advise which functions are best to use when the following task needs to be done:

I need to open several positions (from the market, without orders) and immediately place their tickets into an array.

The difficulty is getting these tickets, I haven't found any functions that open a position AND return it with a ticket (like in MT4).

Just open transactions in the loop, parameters, etc. - no questions, but to get a ticket just open a position, NOT HAVE!

Please advise plz.

Somehow it goes like this:

Ticket = Function.Open Position(Buy, 0.01, ......);

 
Sergey Voytsekhovsky:

opening a position AND returning a ticket (as in MT4).

.................... but to get a ticket of just opened position, NOT!!!

Please advise.

When I come to mind, I can only request a ticket of the last open position in the history. But somehow it seems to me clumsy, too ambiguous, there is no direct direct connection between the order in code and open position. For sure there are more correct methods, who has ideas or maybe enough experience, share please?

 
Sergey Voytsekhovsky:

The only thing that comes to mind is to request a ticket for the last open position in the history. But somehow it seems to me a bit messy, too ambiguous, there is no direct connection between the order in the code and the open position. For sure there are more correct methods, who has ideas or maybe have enough experience, share please?

I have described all this in articles. For mql5 and mql4 everything is done there. You can take an idea and make a function.

However. You have a transaction in which the position identifier is written. Read about the deals and their properties in the documentation.

 
Sergey Voytsekhovsky:

Good evening. I'd like to hear from the pros.

Please advise which functions are best to use when the following task needs to be done:

I need to open several positions (from the market, without orders) and immediately place their tickets into an array.

The difficulty is getting these tickets, I haven't found any functions that open a position AND return it with a ticket (like in MT4).

Just open transactions in the loop, parameters, etc. - no questions, but to get a ticket just open a position, NOT HAVE!

Please advise plz.

Somehow it goes like this:

Ticket = Function.Open Position(Buy, 0.01, ......);

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
 {
  if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
   {
    ulong ticket = trans.position; // тикет позиции
    if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_IN) // открылась позиция
      {
      }
    if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT)// закрылась позиция
     {
      if(HistoryDealGetInteger(trans.deal, DEAL_REASON) == DEAL_REASON_SL)
      if(HistoryDealGetInteger(trans.deal, DEAL_REASON) == DEAL_REASON_TP)
     }
   }
 }
 
Artyom Trishkin:

I have described it all in the articles. For mql5 and mql4 everything is done there. You can take the idea and make the function.

However. You have a deal in which a position identifier is written. Read about the deals and their properties in the documentation.

Thank you very much, I am going to investigate.

 
Alexey Viktorov:

Great, that's something to do now :-). I'll figure it out and be sure to post the result, maybe it'll help someone else, too.

 
Sergey Voytsekhovsky:

Great, that's something to do now :-). I'll figure it out and be sure to post the result, maybe it'll help someone else, too.

There are very few people who do not know. And those who don't know should be able to do it after reading this thread. If they can't, it's not the king's business... to poke around.

 
Artyom Trishkin:

I have described it all in the articles. For mql5 and mql4 everything is done there. You can take the idea and make the function.

Thanks, in which of your articles exactly? You have so many of them.....

 

Good afternoon.

Again, I'm appealing to the gurus of this forum.

I have written a small function. It works, but very slowly. I understand that the algorithm is extremely inefficient. I read it here:

https://www.mql5.com/ru/articles/567

that there'sa function to add to the end of an array.

I don't understand how to use it, please help me to get my imagination back on track, I'll colour code the area of interest.

//+------------------------------------------------------------------+
//| Заполняем структуру своих позиций
//+------------------------------------------------------------------+
int Fill_Position_Structure(int kodclana)
{
   int n = 0;
   if(!Refresh_Rates())return(false);
   int SizeTotalPosition = PositionsTotal();
   int SizeArrayPosition = ArraySize(ArrayPosition);
//---Запись новых позиций в структуру        
   for(int i=0; i < SizeTotalPosition; i++)
      {//---Из списка открытых позиций выбираю очередную, предполагаю что она НОВАЯ, определяю ее ВремяЖизни, 
       //----проверяю есть ли она в массиве позиций, если есть, значит СТАРАЯ, обновляю ВремяЖизни, считаю, выхожу. 
         bool  new_pos  = true;
         ulong ticket   = PositionGetTicket(i);
         int   lifetime = DatetimeToInteger(TimeCurrent() - m_position.Time());
         SizeArrayPosition = ArraySize(ArrayPosition);
         for(int y = 0; y < SizeArrayPosition; y++)
            {
               if(ArrayPosition[y].ticket_pos == ticket)
                  {
                     ArrayPosition[y].lifetime_candle = lifetime/60*_Period;
                     ArrayPosition[y].lifetime_sek    = lifetime;
                     ArrayPosition[y].profit          = m_position.Profit() + m_position.Commission() + m_position.Swap();    
                     new_pos = false;
                     n++;
                     break;
                  }
            }
         if(new_pos)
            {//---Если НОВАЯ, забиваю позицию в массив со всеми параметрами
               ArrayResize(ArrayPosition, SizeArrayPosition+1, 1);
               ArrayPosition[SizeArrayPosition].lifetime_candle = lifetime/60*_Period;
               ArrayPosition[SizeArrayPosition].lifetime_sek    = lifetime;
               ArrayPosition[SizeArrayPosition].ticket_pos      = ticket;
               ArrayPosition[SizeArrayPosition].kod_clana       = kodclana;
               ArrayPosition[SizeArrayPosition].profit          = m_position.Profit() + m_position.Commission() + m_position.Swap();    
               ArrayPosition[SizeArrayPosition].type            = m_position.PositionType();
               n++;
            }
      }   
//---Удаление закрытых позиций из структуры  
      SizeArrayPosition = ArraySize(ArrayPosition);
      SizeTotalPosition = PositionsTotal();
      
      for(int z = 0; z < SizeArrayPosition; z++)
         {
            ulong ticket_cl   = ArrayPosition[z].ticket_pos;
            bool  cl_position = true;
            
            for(int i = 0; i < SizeTotalPosition; i++)
               {
                  if(ticket_cl == PositionGetTicket(i))
                     {
                        cl_position = false;
                        n++;
                        break;
                     }
                }
            if(cl_position)
               {
               ArrayRemove(ArrayPosition,z,1);
               Sleep(SLEEPTIME*slp);
               SizeArrayPosition = ArraySize(ArrayPosition);
               }
         }
return(n);
}
Reason: