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

 
This function should return the highest price between the last two closed trades, i.e. from the open of the second last trade to the close of the very last trade. I don't know where I am going wrong. Help me out.
//Returns highest price between the last two closed trades
 double Highest_Price(){
  
    int count            = 0;
    double dealpricein   = 0;
    double dealpriceout  = 0;
    double highestprice  = 0;
    
     if(CountOpenPositions()<=0){   //if no open positions
     
    HistorySelect(0,TimeCurrent());
    if(HistoryDealsTotal()>0){
      
      for(int i = HistoryDealsTotal()-1; i>=0 && count <2; i--){
        ulong dealTicket = HistoryDealGetTicket(i);
        
        if(dealTicket>0){
        if(HistoryDealSelect(dealTicket)){
         if(HistoryDealGetInteger(dealTicket,DEAL_ENTRY)== DEAL_ENTRY_IN){
         if(HistoryDealGetString(dealTicket,DEAL_SYMBOL)==_Symbol){
         if(HistoryDealGetInteger(dealTicket,DEAL_MAGIC)==eaMagic){
           count++;
           
           if(count==1){ //second last deal entry in
             
             dealpricein = HistoryDealGetDouble(dealTicket,DEAL_PRICE);
                       }
         }
         }
         }
         if(HistoryDealGetInteger(dealTicket,DEAL_ENTRY)== DEAL_ENTRY_OUT){
         if(HistoryDealGetString(dealTicket,DEAL_SYMBOL)==_Symbol){
         if(HistoryDealGetInteger(dealTicket,DEAL_MAGIC)==eaMagic){
           count++;
           
           if(count==0){ //the very last deal entry out
             
             dealpriceout = HistoryDealGetDouble(dealTicket,DEAL_PRICE);
           
           }
           
         }    
         }
         }
         
         }
         }
        
        }
      
      }
    }
    
     
   highestprice = iHighest(_Symbol,PERIOD_CURRENT,MODE_HIGH,{dealpricein,dealpriceout},0);
   return highestprice;
    
  }
 
45Jumia:
This function should return the highest price between the last two closed trades, i.e. from the open of the second last trade to the close of the very last trade. I don't know where I am going wrong. Help me out.

Hi,

You can save the results of each trading position into an array with type double.

After that, you sort the numbers in the array, and take the highest or lowest number according to your needs.

Regards.

 
Yohana Parmi #:

Hi,

You can save the results of each trading position into an array with type double.

After that, you sort the numbers in the array, and take the highest or lowest number according to your needs.

Regards.

Hello. Thanks for the feedback. 

The highest price returned should include all the prices during when the two trades were open.  So if the two trades lasted for 3 hours, (from the open of the second last trade to the close of the last trade), the highest price during these three hours is what I want.

Thanks.

 
iHighest is not the solution... How about if trade enters and exits in the midst of bar?
 
When asking where you went wrong I would always answer: stop working with highly nested functions like you did, instead divide the functionality in multiple subfunctions, then you can check and correct them modularly. Because right now you are searching for a needle in a haystack.
 
Yashar Seyyedin #:
iHighest is not the solution... How about if trade enters and exits in the midst of bar?

Well, about that, what do you think is the best solution?

 
Tobias Johannes Zimmer #:
When asking where you went wrong I would always answer: stop working with highly nested functions like you did, instead divide the functionality in multiple subfunctions, then you can check and correct them modularly. Because right now you are searching for a needle in a haystack.

Okay. Let's assume then that I can get the ENTRY price of the second last closed trade and the EXIT price of the very last trade comfortably, in different sub-functions. How do you propose I should write up the function that returns the highest price formed from the entry price of the second last closed trade to the exit price of the last trade? I think that's where my main problem is.

 
45Jumia #:

Well, about that, what do you think is the best solution?

CopyTicks is the solution that is not simple to handle.
 
Yashar Seyyedin #:
CopyTicks is the solution that is not simple to handle.

thanks. any examples if you mind not?

 
45Jumia #:

thanks. any examples if you mind not?

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

Calculate unrealized profit(s) at a specific time in history
Calculate unrealized profit(s) at a specific time in history
  • www.mql5.com
This is a script to print all open trades and their PnLs at a specific time in history.
 
45Jumia #:

Okay. Let's assume then that I can get the ENTRY price of the second last closed trade and the EXIT price of the very last trade comfortably, in different sub-functions. How do you propose I should write up the function that returns the highest price formed from the entry price of the second last closed trade to the exit price of the last trade? I think that's where my main problem is.

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.
Reason: