Questions from Beginners MQL5 MT5 MetaTrader 5 - page 897

 
Seems clear, I guess it is. Thanks again!
 
Alexey Viktorov:
You can.

How?

 
Andy:

How?

The little bugger's already told me everything.

 
Alexey Kozitsyn:

The position identifier will not change. Use it to search for transactions.

Thank you. After clearing, the function does not return the average price of the trades as intended, but the price of the reopened position. Where is the error, please tell me.

double Aver_Pr_sell_nett()
  {
   double total_price_multiply_volume_sell   = 0.0;
   double total_volume_sell                  = 0.0;
   double net_price_sell_=0.0;
   string symb=_Symbol;
   int    total       =0;  // 

   for(int i=0; i<PositionsTotal(); i++)
     {
      ulong pt=PositionGetTicket(i);
      long ID=PositionGetInteger(POSITION_IDENTIFIER);
      if(PositionGetString(POSITION_SYMBOL)==symb && HistorySelect(PositionGetInteger(POSITION_TIME),TimeCurrent()+60))
        {
         //--- Получим количество сделок в полученном списке
         total=HistoryDealsTotal();
         //--- Пройдем по всем сделкам в полученном списке
         for(int i=0; i<total; i++)
           {
            ulong dt = HistoryDealGetTicket(i);
            long did = HistoryDealGetInteger(dt, DEAL_POSITION_ID);
            ENUM_DEAL_ENTRY in_out=HistoryDealGetInteger(dt,DEAL_ENTRY);
            if(did==pt && in_out==DEAL_ENTRY_IN)
              {
               if(m_position.PositionType()==POSITION_TYPE_SELL)
                 {
                  total_price_multiply_volume_sell+=PositionGetDouble(POSITION_PRICE_OPEN)*PositionGetDouble(POSITION_VOLUME);
                  total_volume_sell+=PositionGetDouble(POSITION_VOLUME);
                  if(total_price_multiply_volume_sell!=0 && total_volume_sell!=0)
                    {
                     net_price_sell_=total_price_multiply_volume_sell/total_volume_sell;
                     
                    };
                 }
              }
           }
        }
     }
//---
   return(net_price_sell_);
  }
 
Sile Si:

Thank you. After clearing, the function does not return the average price of the trades as intended, but the price of the reopened position. Where is the error, please tell me.

If you are working with a position, work with the position. If you are working on a trade, work on the trade. The deal is the price of the deal, not the price of the position.

 
Alexey Kozitsyn:

If you are working with a position, work with a position. If you are working on a trade, work on the trade. The deal shows the price of the trade, not the price of the position.

Didn't I select the trades involved in the opening of the position?

 
Sile Si:

Didn't I select the trades involved in the opening of the position?

The algorithm is simple: you select a position, by position ID, all its trades. You select for work only those deals that form the volume of the position, i.e. you exclude clearing deals.

After choosing a position, you receive its deals, and then you request a price for the position! And you need the price of each specific transaction.

 
Alexey Kozitsyn:

And you need the price of each specific transaction.

Sorry, I don't understand how to select a trade instead of a position, show me)

 
Sile Si:

Sorry, I don't understand how to select a trade instead of a position, show me)

ulong dt = HistoryDealGetTicket(i);
 
Alexey Kozitsyn:

Okay, that's what I do.

ulong dt = HistoryDealGetTicket(i);
long did = HistoryDealGetInteger(dt, DEAL_POSITION_ID);

Then I compare the transaction id with the position ticker, and I think I've selected the transaction,

but it returns the price of the position. Why?

Reason: