Retrieving History Positions

 

Hi people!


Since build 1525, Metatrader 5 allows listing positions history.

What are the functions related to retrieving them, as there is HistoryOrdersTotal(), HistoryDealsTotal(), but there is no HistoryPositionsTotal(), HistoryPositionGetTicket(), and HistorySelect() generates a list of orders and trades. I just want a history of positions.

Is there a function for it?

Many thanks!

 

Just read the doc about HistoryOrdersTotal(): " Prior to calling HistoryOrdersTotal(), first it is necessary to receive the history of deals and orders using the HistorySelect() or HistorySelectByPosition() function."

If you follow the link of HistorySeletc() you'll find an example which you can amend according to your priorities..

 
Arthur Albano:

Hi people!


Since build 1525, Metatrader 5 allows listing positions history.

What are the functions related to retrieving them, as there is HistoryOrdersTotal(), HistoryDealsTotal(), but there is no HistoryPositionsTotal(), HistoryPositionGetTicket(), and HistorySelect() generates a list of orders and trades. I just want a history of positions.

Is there a function for it?

Many thanks!

No direct function. You need to code it yourself. Probably there is already one available publicly somewhere.
 
Carl Schreiber:

Just read the doc about HistoryOrdersTotal(): " Prior to calling HistoryOrdersTotal(), first it is necessary to receive the history of deals and orders using the HistorySelect() or HistorySelectByPosition() function."

If you follow the link of HistorySeletc() you'll find an example which you can amend according to your priorities..

HistorySelectByPosition does not retrieve Positions, only retrieves Orders or Deals.

How can I retrieve a list of all Closed POSITIONS?

 
cfgmalaga:

HistorySelectByPosition does not retrieve Positions, only retrieves Orders or Deals.

How can I retrieve a list of all Closed POSITIONS?

A history positionInfo library, with methods similar to those in OrderInfo in std library. 

I assume you understand some basic OOP of MQL at least.

Files:
 
Arthur Albano:

Hi people!


Since build 1525, Metatrader 5 allows listing positions history.

What are the functions related to retrieving them, as there is HistoryOrdersTotal(), HistoryDealsTotal(), but there is no HistoryPositionsTotal(), HistoryPositionGetTicket(), and HistorySelect() generates a list of orders and trades. I just want a history of positions.

Is there a function for it?

Many thanks!

Please refer to

https://www.mql5.com/en/code/27683

see also

https://www.mql5.com/en/code/21434

CHistoryPositionInfo Class
CHistoryPositionInfo Class
  • www.mql5.com
Note: when using the methods  HistorySelect() and  SelectByIndex(),  the list of positions is ordered by time of closing  (not by the opening times). This means the history of...
 
Alain Verleyen #:
No direct function. You need to code it yourself. Probably there is already one available publicly somewhere.


the point of the question was for you to provide the guidance on where to 'find it publicly  available somewhere ', not very helpful

 

your answer is professional and explicative bliss, great!

 

Please @amrali, though a rudimentary question, i would like to ask if you could help me with a scirpt that helps me get the ticket of a trade i just took

e.g what i have tried


Ctrade trade;

//////////other parts of the code

// Function to get the ticket of the last trade

ulong GetLastTradeTicket(string symbol) {

    int totalDeals = HistoryDealsTotal();

    for (int i = totalDeals - 1; i >= 0; i--) {

        ulong dealTicket = HistoryDealGetTicket(i);

        string dealSymbol = HistoryDealGetString(i, DEAL_SYMBOL);

        if (dealTicket > 0 && StringCompare(dealSymbol, symbol) == 0) {

            // Found the last trade for the specified symbol

            return dealTicket;

        }

    }

    // No trade found for the specified symbol

    return 0;

}

trade.Sell(1, Symbol(), 0, NULL , NULL, 'sell trade');

// Get the ticket of the last trade

ulong lastTradeTicket = GetLastTradeTicket(_Symbol);

if (lastTradeTicket > 0) {

    Print("Ticket of the last trade: ", lastTradeTicket);

} else {

    Print("No trades in history.");

}



this always outputs 0, let me know whatever additional information you need

 
simonushie #:

Please @amrali, though a rudimentary question, i would like to ask if you could help me with a scirpt that helps me get the ticket of a trade i just took

e.g what i have tried




this always outputs 0, let me know whatever additional information you need

I want to go with your word "... ticket of a trade i just took ". This means that the trade is still running.


if the trade is still running below code might help


double GetLastDetails(ENUM_POSITION_TYPE positionType, int type = 0){
   double price = 0;
   for(int i = PositionsTotal()-1; i >= 0; i--)
        {
           ulong ticket = PositionGetTicket(i);
           PositionSelectByTicket(ticket);
           
           if(PositionGetInteger(POSITION_MAGIC) != MagicNumber || PositionGetInteger(POSITION_TYPE) != positionType || PositionGetString(POSITION_SYMBOL) != Symbol()) continue;
           
           if(PositionGetInteger(POSITION_TYPE) == positionType && PositionGetInteger(POSITION_MAGIC) == MagicNumber) {
              switch(type) {
                 case 0:
                   price = PositionGetDouble(POSITION_VOLUME);
                   break;
                 case 1:
                   price = PositionGetDouble(POSITION_PRICE_OPEN);
                   break;
                 case 2:
                   price = PositionGetDouble(POSITION_TP);
                   break;
                 case 3:
                    price = PositionGetDouble(POSITION_SL);
                    break;
                 case 4:
                    price = (double)PositionGetInteger(POSITION_TICKET);
                    break;
                 default:
                   break;
              }      
              break;
           }
        }
        return price;
}

This code can be used to get last volume(i.e. lotsize), last open price, last takeprofit, last stoploss, and then last ticket.

Try it and let me know if it work

 
Raphael Adetunji Olaiyapo #:

I want to go with your word "... ticket of a trade i just took ". This means that the trade is still running.


if the trade is still running below code might help


This code can be used to get last volume(i.e. lotsize), last open price, last takeprofit, last stoploss, and then last ticket.

Try it and let me know if it work

THANKS A LOT! my friend, i found a workaround for that, i stored my trades in a struct each time they were taken so that i can give them extra properties after being taken, i consequently also stored the ticket there so i could easily identify a trade...works for me now