Dividing open positions into groups - page 9

 
Sergey Voytsekhovsky:

If I understand you correctly, this is the right way to go. As a more experienced comrade, please advise, for the purpose of catching newborn positions such a filter will work ??? Or maybe there is a better option ???

This is for Alexey - I don't work with OnTradeTransaction() for some reason.

 
Alexey Viktorov:

Are you thus suggesting that the other posts be pushed back? Let them go unmentioned? That's not right.

In my opinion, it is better to define a separate section of the forum in which each problem would be a separate topic. The number of topics is much less than the number of posts per topic. So despite the considerable number of topics, they won't get pushed back as far as individual issues and bug reports are pushed back now.

Well, you highlighted in red that you don't know. What was the problem? How did it manifest itself? And what was the new method?

As for reporting bugs: "right or wrong", but if you want to report it, you will. And if you don't want to, you don't want to. And to shut yourself off like "letting people talk about their own" is an excuse.

 
Sergey Voytsekhovsky:

If I understand you correctly, this is the right way to go. As a more experienced comrade, please advise, for the purpose of catching newborn positions such a filter will work ??? Or maybe there is a more correct option ???

In my opinion, this line

         if(trans.type != TRADE_TRANSACTION_ORDER_DELETE)

unnecessary. And in general, I am currently reconsidering OnTradeTransaction operation for the following variant

  if(trans.type == TRADE_TRANSACTION_HISTORY_ADD)

At the moment of adding of a deal we don't have a ticket of the order which generated the deal. But if we need a ticket for the order and its properties, as I now need the opening price of the order not to disable grid step because of slippage, then it is the best variant. imho.

In general, I am a supporter of writing for a specific task. In contrast, SB has a great deal of universality requiring additional variables which are sometimes absolutely unnecessary.

 
Artyom Trishkin:

Well, you highlighted in red that you didn't know. What was the problem? How was it manifesting itself? And what was the new method?

And about the error messages: "right or wrong", but if you want to report it, you will. And if you don't want to, you don't want to. And to shut yourself off like "letting people talk about theirs" is an excuse.

Somewhere on my old disks I still have the old terminals. If I can find them, I'll show you the old library and the new one. Then we'll have a substantive conversation. Otherwise, I remember you don't, so what's there to talk about? How to explain, to show what is not there now.
 
Alexey Viktorov:

In my opinion, this line

unnecessary. In general, I am currently reconsidering OnTradeTransaction operation for the following variant

At the moment of adding a trade there is no ticket of the order which generated the trade. But if you need a ticket of the order and its properties, as I now need the opening price of the order, so that the grid step is not knocked down due to slippage, then this is the best option. imho.

Generally, I am a supporter of developing for a particular task. And the UB has crammed in universality, which requires additional, sometimes absolutely unnecessary variables.

Extremely grateful for pointing in the right direction. Did it this way (below, seems to work):

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
//---
   if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
      {
         if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_IN) // открылась позиция
            {
               int SizeArrayPosition = ArraySize(ArrayPosition);
               ArrayResize(ArrayPosition,SizeArrayPosition+1);
               
               if(PositionSelectByTicket(trans.position)){
               ArrayPosition[SizeArrayPosition].ticket_pos = trans.position;
               ArrayPosition[SizeArrayPosition].open_price = trans.price;
               ArrayPosition[SizeArrayPosition].open_tyme  = (datetime)PositionGetInteger(POSITION_TIME);
               ArrayPosition[SizeArrayPosition].type       = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
               ArrayPosition[SizeArrayPosition].kod_clana  = 0;
               
               Print("\n"
               "Открылась новая позиция: ticket = ",ArrayPosition[SizeArrayPosition].ticket_pos);}}}
  }

Now I'm puzzling over how to catch tickers of positions which closed at Take Profit or by a direct order. We only need ticks from them to remove these positions from the main array of positions.

 
Sergey Voytsekhovsky:

Extremely grateful for the guidance in the right direction. I did so (below, seems to work):

Now I'm puzzling how to catch ticks of positions which closed at takeprofit or by direct order. We only need ticks from them to remove these positions from the main position array.

I would add a check for the symbol and magician.

    if(PositionSelectByTicket(trans.position) && PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == magick)
     {
      if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_IN)

Here is a fragment of my code where position closing is detected.

    if(!PositionSelectByTicket(trans.position))
     {
      HistorySelectByPosition(trans.position);
      ulong ordTicket = HistoryOrderGetTicket(0);
      if(HistoryOrderGetInteger(ordTicket, ORDER_MAGIC) == magick && HistoryOrderGetString(ordTicket, ORDER_SYMBOL) == _Symbol)
       {

We can also divide by reasons of closing from enumeration ENUM_DEAL_REASON

In another Expert Advisor

    if(!PositionSelectByTicket(trans.position))
     {
      if(trans.symbol == _Symbol && HistorySelectByPosition(HistoryDealGetInteger(trans.deal, DEAL_POSITION_ID)))
       {
        int dealTotal = HistoryDealsTotal();
        long posMagic = 0;
        ulong dealTicketIN = HistoryDealGetTicket(0);
        if(HistoryDealGetInteger(dealTicketIN, DEAL_ENTRY) == DEAL_ENTRY_IN)
          posMagic = HistoryDealGetInteger(dealTicketIN, DEAL_MAGIC);
        if(posMagic == magick)
         {
          ulong dealTicketOUT = HistoryDealGetTicket(1);
          if(HistoryDealGetInteger(dealTicketOUT, DEAL_ENTRY) == DEAL_ENTRY_OUT)
           {

In the first case I need an order, in the second case a deal is enough. In general, when I need something, I will write it.

 
Alexey Viktorov:

I would also add a check for symbol and magik

Here is a fragment of my code where position closing is detected.

We can also divide by reasons of closing from enumeration ENUM_DEAL_REASON

Here in another Expert Advisor

In first case I need order, in second case trade is enough. In general, I will write it when I need it.

Good evening. Your responsiveness and consideration do me great honour. Much obliged. As the youngsters say - respect and respect.

Thank you for your good advice. For one, they are not very much needed at the moment, my Expert Advisor has no and probably will not have any competitors because it is high-frequency, a lot of small positions. He is suffocating on his own, where else can he compete with others? Of course, if it comes to that, it will have one in the terminal and in the account. The second one is probably farfetched, I just don't know much yet, but maybe it will save resources a bit.

Your examples are good, I will definitely save them, but I have no ticket, no order, no deal. More precisely, I have hundreds of them and I don't know which one will close now. You suggested me to think that the fact of closing a position may be pulled out of my calculations and I will try to do it now.

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)

I will try to print everything that it prints in the flow. My Expert Advisor will be a position generator and it knows how to actively open positions and close them at the same time. The only thing is to find this position (the position ticket) in the array/structure and remove it. So it is like this.

And as the typewriter, I will try to use an example code from the reference, in the section about this function, it is called there:

//|OnTradeTransaction_Sample.mq5 |
//| Copyright 2018, MetaQuotes Software Corp.
//|https://www.mql5.com ||
//+------------------------------------------------------------------+
#property copyright"Copyright 2018, MetaQuotes Software Corp."
#property link"https://www.mql5.com"
#property version"1.00"
#property description"TradeTransaction event listener example"

 
Sergey Voytsekhovsky:

Good evening. Your responsiveness and attention do me honour. Much obliged. As the young people say, kudos and respect.

Thank you for your wise advice. I don't turn on Magik and the symbol almost consciously, firstly, because they are not very much needed right now, my Expert Advisor has no and probably will not have any competitors, because it has a high frequency, a huge number of minimum positions. He is suffocating on his own, where else can he compete with others? Of course, if it comes to that, it will have one in the terminal and in the account. The second one is probably farfetched, I just don't know very much yet, but maybe it will save resources a bit.

Your examples are good, I will definitely save them, but I have no ticket, no order, no deal. More precisely, I have hundreds of them and I don't know which one will close now. You suggested me to think that the fact of closing a position may be taken out of my calculations and I am trying to do it now.

I will try to print everything that it prints in the flow. My Expert Advisor will be a position generator and it knows how to actively open positions and close them at the same time. The only thing is to find this position (the position ticket) in the array/structure and remove it. So it is like this.

And as a typewriter, I'll try to use the example code from the reference, in the section about this function, it's called there:

It's always nice to share my little knowledge with someone who is figuring something out on his own from documentation on the basis of these tips. © I.A. Krylov. The Cuckoo praises the Cock for praising the Cuckoo. )))

If you are writing an EA solely for yourself, of course you can take some liberties. If you don't think it is necessary to check the symbol and the magician, then don't.

About the highlighted:

There's everything, the position ticket, the order ticket and the transaction ticket. And it's not even difficult to pull out a trade and / or order opening position. It's all in the MqlTradeTransaction structure


    if(!PositionSelectByTicket(trans.position)) // Если позицию выбрать не получилось, значит она закрыта
     {
      HistorySelectByPosition(trans.position); // Получим список ордеров и сделок относящихся к этой, конкретной позиции
      ulong ordTicket = HistoryOrderGetTicket(0); // Получили тикет ордера который породил сделку и позицию.
      if(HistoryOrderGetInteger(ordTicket, ORDER_MAGIC) == magick && HistoryOrderGetString(ordTicket, ORDER_SYMBOL) == _Symbol)
       {
 
Если позицию выбрать не получилось, значит она закрыта 
Ой-ли? 
 
Алексей Тарабанов:

Alexey, it all refers only to OnTradeTransaction event handling

An event has occurred, a trade that closes a position has been executed. The position is already gone at this moment. This can be confirmed by selecting the list of orders and deals (not present in the code) and reading

          ulong dealTicketOUT = HistoryDealGetTicket(1);
          if(HistoryDealGetInteger(dealTicketOUT, DEAL_ENTRY) == DEAL_ENTRY_OUT)
And note that we are talking about a hadge account where the position usually has only two orders and two trades.
Reason: