Dividing open positions into groups - page 10

 

I'm putting it out there for the public to judge. Although I haven't attached any checks yet, but I think they should 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)// Открылась позиция
            {
               if(PositionSelectByTicket(trans.position))
                  {
                     int SizeArrayPosition = ArraySize(ArrayPosition);
                     ArrayResize(ArrayPosition,SizeArrayPosition+1);
                        
                        ArrayPosition[SizeArrayPosition].kod_clana                      = 0;
                        ArrayPosition[SizeArrayPosition].open_price                     = trans.price;
                        ArrayPosition[SizeArrayPosition].ticket_position                = trans.position;
                        ArrayPosition[SizeArrayPosition].identi_position                = PositionGetInteger(POSITION_IDENTIFIER);
                        ArrayPosition[SizeArrayPosition].open_tyme            = (datetime)PositionGetInteger(POSITION_TIME);
                        ArrayPosition[SizeArrayPosition].type       = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
                        
                     Print("\n"
                     "Открылась новая позиция: ticket = ",ArrayPosition[SizeArrayPosition].ticket_position);
                  }
            }
         if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == (DEAL_ENTRY_OUT || DEAL_ENTRY_OUT_BY))// Закрылась позиция
            {
               int SizeArrayDealOut = ArraySize(ArrayDealOut);
               ArrayResize(ArrayDealOut,SizeArrayDealOut+1);
               ArrayDealOut[SizeArrayDealOut].ticket_position_out = trans.position;
               ArrayDealOut[SizeArrayDealOut].identi_position_out = HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);
            }          
      }
  }

And somewhere in OnTick:DelitClosePositionFromArrayPosition();

//+------------------------------------------------------------------+
//| Удаляем закрытые позиции из массива структуры
//+------------------------------------------------------------------+
int DelitClosePositionFromArrayPosition()
{
   int n = 0;
   if(!Refresh_Rates())return(false);
//---Удаление закрытых позиций из структуры  
   int SizeArrayDealOut  = ArraySize(ArrayDealOut);
   int SizeArrayPosition = ArraySize(ArrayPosition);
   if(SizeArrayDealOut > 0)
      {
         for(int i = SizeArrayDealOut+1; i > 0; i--)
            {
               for(int y = SizeArrayPosition+1; y > 0; y--)
                  {
                     if(ArrayPosition[y].identi_position == ArrayDealOut[i].identi_position_out)
                        {
                           if(ArrayRemove(ArrayPosition,y,1) && ArrayRemove(ArrayDealOut,i,1))
                              {
                                 SizeArrayDealOut  = ArraySize(ArrayDealOut);
                                 SizeArrayPosition = ArraySize(ArrayPosition);
                                 n++;
                                 break;
                              }
                        }
                  }
            }
      }            
return(n);
}
 

Good afternoon, and once again I'm begging. Second day I can't understand why this piece of code doesn't work

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
//---
   if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
      {
         Print("Зашла сделка ########## ",trans.position," ###########");
         
         if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_IN)// Открылась позиция
            {
               if(PositionSelectByTicket(trans.position))
                  {
                     int SizeArrayPosition = ArraySize(ArrayPosition);
                     ArrayResize(ArrayPosition,SizeArrayPosition+1);
                        
                        ArrayPosition[SizeArrayPosition].kod_clana                         = 0;
                        ArrayPosition[SizeArrayPosition].open_price                        = trans.price;
                        ArrayPosition[SizeArrayPosition].ticket_position                   = trans.position;
                        ArrayPosition[SizeArrayPosition].identi_position                   = PositionGetInteger(POSITION_IDENTIFIER);
                        ArrayPosition[SizeArrayPosition].open_tyme               = (datetime)PositionGetInteger(POSITION_TIME);
                        ArrayPosition[SizeArrayPosition].type_position = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
                        ArrayPosition[SizeArrayPosition].StopLoss                          = trans.price_sl;
                        ArrayPosition[SizeArrayPosition].TakeProfit                        = trans.price_tp;
                        
                       
                        Print("Открыта позиция +++ ",trans.position," +++ записана в структуру ArrayPosition ++++++++++++++");        

                  }
            }
         if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) != DEAL_ENTRY_IN)
            {
               Print("Закрыта позиция --- ",trans.position," --- записана в структуру ArrayDealOut ------------");
            }
      }
  }

Print "Deal entered...." goes fine, then there should be a split, some deals"Open position ...." are printed, but all other deals (these are deals closing position on TakeProfit) are not printed.

I do not understand what is the problem. I will mark the lost trades in colour below.

2020.04.12 11:27:33.103 Core 1  2018.01.02 18:54:00   CTrade::OrderSend: market buy 0.01 EURUSD tp: 1.20621 [done at 1.20574]
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:54:00   Зашла сделка ########## 1042 ###########
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:54:00   Открыта позиция +++ 1042 +++ записана в структуру ArrayPosition ++++++++++++++
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:54:00   Зашла сделка ########## 1043 ###########
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:54:00   Открыта позиция +++ 1043 +++ записана в структуру ArrayPosition ++++++++++++++
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:55:43   take profit triggered #1039  sell 0.01 EURUSD 1.20579 tp: 1.20532 [#1044  buy 0.01 EURUSD at 1.20532]
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:55:43   deal #1044  buy 0.01 EURUSD at 1.20527 done (based on order #1044)
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:55:43   deal performed [#1044  buy 0.01 EURUSD at 1.20527]
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:55:43   order performed buy 0.01 at 1.20527 [#1044  buy 0.01 EURUSD at 1.20532]
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:55:43   Зашла сделка ########## 1039 ###########
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:56:30   take profit triggered #1042  sell 0.01 EURUSD 1.20571 tp: 1.20523 [#1045  buy 0.01 EURUSD at 1.20523]
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:56:30   deal #1045  buy 0.01 EURUSD at 1.20523 done (based on order #1045)
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:56:30   deal performed [#1045  buy 0.01 EURUSD at 1.20523]
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:56:30   order performed buy 0.01 at 1.20523 [#1045  buy 0.01 EURUSD at 1.20523]
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:56:30   Зашла сделка ########## 1042 ###########
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:57:00   На символе EURUSD открылся новый бар в 2018.01.02 18:57
2020.04.12 11:27:33.103 Core 1  2018.01.02 18:57:00   market sell 0.01 EURUSD tp: 1.20458 (1.20507 / 1.20509 / 1.20507)

So it's like SOS !!!

 
Alexey Viktorov:

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. You can confirm it by selecting a list of orders and deals (there is no such a list in the code) and reading

And note that we are talking about a hadge account where the position usually has only two orders and two trades.

Good afternoon, if it's not too much trouble for you, you have accurately described everything in this post. My two posts above, I am trying to get the result I need - to commit to close a position. I still cannot do it.

Please advise me, where I have a mistake or defect ?????

 
And this way there is no distinction at all, so it turns out that closing a position on TakeProfit also fits underDEAL_ENTRY_IN???
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
//---
   if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
      {
         Print("Зашла сделка ########## ",trans.position," ###########");
         
            if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_IN)// Открылась позиция
               {
                  Print("Открыта позиция +++ ",trans.position," +++ записана в структуру ArrayPosition ++++++++++++++");        
               }
            if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) != DEAL_ENTRY_IN)
               {
                  Print("Закрыта позиция --- ",trans.position," --- записана в структуру ArrayDealOut ------------");
               }
      }
  }


2020.04.12 12:10:27.368 Core 1  2018.01.03 04:58:00   CTrade::OrderSend: modify position #1252  EURUSD (sl: 0.00000, tp: 0.00000) [done]
2020.04.12 12:10:27.368 Core 1  2018.01.03 04:59:38   take profit triggered #2540  sell 0.01 EURUSD 1.20473 tp: 1.20425 [#2676  buy 0.01 EURUSD at 1.20425]
2020.04.12 12:10:27.368 Core 1  2018.01.03 04:59:38   deal #2676  buy 0.01 EURUSD at 1.20425 done (based on order #2676)
2020.04.12 12:10:27.368 Core 1  2018.01.03 04:59:38   deal performed [#2676  buy 0.01 EURUSD at 1.20425]
2020.04.12 12:10:27.368 Core 1  2018.01.03 04:59:38   order performed buy 0.01 at 1.20425 [#2676  buy 0.01 EURUSD at 1.20425]
2020.04.12 12:10:27.368 Core 1  2018.01.03 04:59:38   Зашла сделка ########## 2540 ###########
2020.04.12 12:10:27.368 Core 1  2018.01.03 04:59:38   Открыта позиция +++ 2540 +++ записана в структуру ArrayPosition ++++++++++++++
2020.04.12 12:10:27.368 Core 1  2018.01.03 04:59:40   take profit triggered #2473  sell 0.01 EURUSD 1.20472 tp: 1.20424 [#2677  buy 0.01 EURUSD at 1.20424]

 
Sergey Voytsekhovsky:
And this way there is no separation at all, it turns out that closing of a position at TakeProfit also fits under DEAL_ENTRY_IN?

It seems to me that some changes have been made to the handling of the OnTradeTransaction event.

Try to replace TRADE_TRANSACTION_DEAL_ADD with TRADE_TRANSACTION_HISTORY_ADD

 
Alexey Viktorov:

It seems to me that some changes have been made to OnTradeTransaction event handling.

Try replacing TRADE_TRANSACTION_DEAL_ADD with TRADE_TRANSACTION_HISTORY_ADD

Doesn't help. Thank you for your feedback.

 
Alexey Viktorov:

It seems to me that some changes were made to OnTradeTransaction event handling.

Try to replace TRADE_TRANSACTION_DEAL_ADD with TRADE_TRANSACTION_HISTORY_ADD

Transactions opening positions are printed, i.e. this selection works. But it is not clear how to select trades - closing positions, even though they are visible in the list of terminal transactions (a couple/three lines higher always).

 

I'll try it from the beginning, because my head is already a mess. As the movie classic said - "Who builds like this.....????".

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

   if(trans.type == TRADE_TRANSACTION_HISTORY_ADD)
      {
         Print("Торговая транзакция = Добавление сделки в историю ######### Тикет сделки # ",trans.deal," ###########");
  
      }
  }

There isa trade transaction, but there is no trade ticket yet. Am I getting this right?

2020.04.12 14:14:19.287 Core 1  2018.01.02 13:15:00   deal performed [#405  buy 0.01 EURUSD at 1.20646]
2020.04.12 14:14:19.287 Core 1  2018.01.02 13:15:00   order performed buy 0.01 at 1.20646 [#405  buy 0.01 EURUSD at 1.20646]
2020.04.12 14:14:19.287 Core 1  2018.01.02 13:15:00   CTrade::OrderSend: market buy 0.01 EURUSD tp: 1.20694 [done at 1.20646]
2020.04.12 14:14:19.287 Core 1  2018.01.02 13:15:00   Торговая транзакция = Добавление сделки в историю #########  Тикет сделки # 0 ###########
2020.04.12 14:14:19.287 Core 1  2018.01.02 13:15:00   Торговая транзакция = Добавление сделки в историю #########  Тикет сделки # 0 ###########
2020.04.12 14:14:19.287 Core 1  2018.01.02 13:16:02   take profit triggered #394  sell 0.01 EURUSD 1.20675 tp: 1.20627 [#406  buy 0.01 EURUSD at 1.20627]
2020.04.12 14:14:19.287 Core 1  2018.01.02 13:16:02   deal #406  buy 0.01 EURUSD at 1.20627 done (based on order #406)
2020.04.12 14:14:19.287 Core 1  2018.01.02 13:16:02   deal performed [#406  buy 0.01 EURUSD at 1.20627]
 
Sergey Voytsekhovsky:

Transactions opening positions are printed, i.e. this selection works. But how to select transactions - closing positions - is not clear, even though they are visible in the list of transactions of the terminal (a couple / three lines higher always).

Try to pass in the debugger by pressing the blue button.


pre-set breakpoints and check where the values are and what values are expected. OK, I don't seem to see any errors, so I have to check, hence write open positions, adjust stops so that I don't have to wait long to close...

 
Sergey Voytsekhovsky:

I'll try it from the beginning, because my head is already in a mess. As the movie classic said - "Who builds like this.....????".

There is a trade transaction, but there is no trade ticket yet. Am I reading this right?

I don't understand anything at all. Here's a piece of code from my running EA

/*********************TradeTransaction function**********************/
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
 {
  if(trans.type == TRADE_TRANSACTION_HISTORY_ADD)
   {
    if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_IN)
     {
      if(PositionSelectByTicket(trans.position && PositionGetString(POSITION_SYMBOL) == _Symbol) && PositionGetInteger(POSITION_MAGIC) == magick)
       {
        if(HistorySelectByPosition(PositionGetInteger(POSITION_IDENTIFIER)))
         {
          double priceGrid = HistoryOrderGetDouble(HistoryOrderGetTicket(0), ORDER_PRICE_OPEN);
          if(priceGrid == 0)
            DebugBreak();

And it never stopped on DebugBreak(); so everything works out without any problems.

Apparently you have an order being added to history and you're waiting for a trade ticket.