HELP: This function should return the highest price between the last two closed trades. - page 2

 

Didn't read the other answers, the solution may be out there.

If I understood correctly: there're two closed trades. You want to get the closing time of the last trade and the opening time of the second to last trade and get the highest price in the mean time.


This code should work (not fully tested and may not be the best solution):


void OnStart()
{
    HistorySelect(0, TimeCurrent());
    int total_deals = HistoryDealsTotal();

    if(total_deals < 4) return; // each trade is a combination of an in-entry type deal and an out-entry type deal. if there're less than 4 deals, there's certainly not 2 trades, so we do not continue

    int last_closed_deal_ticket = 0;
    int sec_to_last_closed_ticket = 0;
    int sec_to_last_order_ticket = 0;

    bool break_loop = false;

    for(int i = total_deals-1; i >= 0; --i)
    {
        int tmp_ticket = HistoryDealGetTicket(i);
        ENUM_DEAL_ENTRY entry_type = HistoryDealGetInteger(tmp_ticket, DEAL_ENTRY);

        if(entry_type == DEAL_ENTRY_OUT)
        {
            if(last_closed_deal_ticket==0)
            {
                last_closed_deal_ticket = tmp_ticket; // gets the deal that closed the last trade 
            }
            else
            {
                int position_id = HistoryDealGetInteger(tmp_ticket, DEAL_POSITION_ID); // second to last trade position id
                for(int n = i-1; n >= 0; --i) // loops in advance to get the second to last trade entry deal; since we already got the DEAL_ENTRY_OUT, the next ticket will be the DEAL_ENTRY_IN (assuming new deals do not increase your position, so there's no DEAL_ENTRY_IN_OUT in the middle)
                {
                    int tmp_ticket_2 = HistoryDealGetTicket(n);
                    int tmp_position_id = HistoryDealGetInteger(tmp_ticket_2, DEAL_POSITION_ID);

                    if(tmp_position_id == position_id)
                    {
                        sec_to_last_closed_ticket = tmp_ticket_2; // if the DEAL_ENTRY_IN is found, we store it's ticket and break the loops
                        break_loop = true;
                        break;
                    }
                }
            }
        }

        if(break_loop)
        {
            break;
        }
    }

    datetime last_deal_time = HistoryDealGetInteger(last_closed_deal_ticket, DEAL_TIME); // get the time of the closing deal of the last trade
    datetime sec_to_last_time = HistoryDealGetInteger(sec_to_last_closed_ticket, DEAL_TIME); // get the time of the opening deal of the second to last trade

    int last_deal_bar = iBarShift(_Symbol, PERIOD_CURRENT, last_deal_time); // gets the bar, from the current bar, of the closing deal of the last trade
    int sec_to_last_bar = iBarShift(_Symbol, PERIOD_CURRENT, sec_to_last_time); // gets the bar, from the current bar, of the opening deal of the second to last trade

    int highest_bar_index = iHighest(_Symbol, PERIOD_CURRENT, MODE_HIGH, sec_to_last_bar-last_deal_bar, last_deal_bar); // gets the highest bar in between the bars fetched before

    double highest_price = iHigh(_Symbol, PERIOD_CURRENT, highest_bar_index);
    Print("Highest price in the period = ", highest_price);
}


 

 
Emanuel Cavalcante Amorim Filho #:

Didn't read the other answers, the solution may be out there.

If I understood correctly: there're two closed trades. You want to get the closing time of the last trade and the opening time of the second to last trade and get the highest price in the mean time.


This code should work (not fully tested and may not be the best solution):



 

Thank you. I will test this and see

 
Tobias Johannes Zimmer #:
https://www.mql5.com/en/docs/basis/function/functionoverload

Can you create a test where you open and close three or so positions with some bars in between in order to HistorySelect them and get all their deal tickets in an array first? Then I would make the function that gets the history deals the first goal and keep the function separate from the rest so that once it works you don't change its functionality by accident.

You see it gets complex quickly and if you require somebody to walk you through every step then it would be nice if you pay them their hard work. Else you can pay someone on the freelance section to do it and then look at the code.

Other than that I would separate the functionality of 
=>Selecting all the deals/storing the tickets with the desired symbol and magic in an array

=>then you check for the closing trades and store them in another array (so you have one global array for the gross tickets and one for the net tickets which you process with the functions)

=>then you pick the latest two closing trades and get the bars with iBarShift 

=>Then you find out how to use iHigh/Low to search for the high in between these two bars.

So I think you should build one function for each of the steps and call them one by one from the main program.

And you will need to use the debugger, otherwise there is no chance to make it.

Thanks for the advice

 
45Jumia #:

Thanks for the advice

The guy after me posted some decent code, he was more motivated than me.
 
45Jumia: This function should return the highest price between the last two closed trades,

You don't want “the last two closed trades.” You want the last two on the current chart. Go through the list and find them.

double p1,p2; datetime t1=0, t2=0;
for(…) if(
   select …; 
&& filter by MN 
&& filter by symbol
){
   datetime t=…; double p=…;                 // Get the next order.
   if(t >= t2){                              // Newer than the second
      if(t >= t1){ t2=t1; p2=p1; t1=t; p1=p; // Newer than the first
      else{        t2=t;  p2=p;
   }   // for
}
return MathMax(p1,p2);

Do not assume history has only closed orders.
          OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017)

Do not assume history is ordered by date, it's not.
          Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
          Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

 
William Roeder #:

You don't want “the last two closed trades.” You want the last two on the current chart. Go through the list and find them.

Do not assume history has only closed orders.
          OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017)

Do not assume history is ordered by date, it's not.
          Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
          Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

That's some new insights I didn't know about. I will definitely look into that. Thanks.