find the price of the last closed position

 

Hello,

I have a very basic EA. It open a position and when condition is met it close it. I need a simple way to find the price of the last closed position. Some very basic way  (for a beginner in coding).

Thanks in advance! 

 

I forgot to mention. If is possible do not suggest OnTradeTransaction(); is too complicated for me; at least till this moment ;-)

 
tenlau:

I forgot to mention. If is possible do not suggest OnTradeTransaction(); is too complicated for me; at least till this moment ;-)

You can try this (in MQL5):

Print("Last deal price = "+DoubleToString(HistoryDealGetDouble(HistoryDealGetTicket(HistoryDealsTotal()-1),DEAL_PRICE),_Digits));
 
Malacarne:

You can try this (in MQL5):

Thanks for your suggestion. I try the code but it always returns: "Last deal price = 0.00000"
 

 Try with... 

//-------------------------- PRECIO CIERRE POSICION ANTERIOR -------------------------------
double precioCierrePosicAnt(string simb= NULL)
{
   int      ticket= 0, nTrans= 0, tipoTrans= 0;
   string   simbPosic= "";
   double   precioTrans= 0;
   datetime horaApertAct= PositionSelect(simb)? (datetime)PositionGetInteger(POSITION_TIME): TimeCurrent();
   if(HistorySelect(0, horaApertAct-1)
   {
      nTrans= HistoryDealsTotal();
      for(ticket= nTrans; ticket>=0; ticket--)
      {
         simbPosic= HistoryDealGetString(HistoryDealGetTicket(ticket),DEAL_SYMBOL);
         precioTrans= HistoryDealGetDouble(HistoryDealGetTicket(ticket),DEAL_PRICE);
         tipoTrans= HistoryDealGetInteger(ticket,DEAL_ENTRY)
         if(simbPosic==simb && tipoTrans==DEAL_ENTRY_OUT) break;
      }
   }
   return(precioTrans);
}
 
josemiguel1812:

 Try with... 

Thank you too. Your code returns precioTrans=0.0

Also I notice it takes about 10 seconds for execution. I believe is because of for loop. Maybe I was not so clear. I trade only one pair on one chart. I need the price of the last CLOSE deal.

The deal that close an open position.

Basically things works like this: Open one or more consecutive positions. If condition to close==true position/positions will be closed at market price.  I NEED THIS PRICE.

And the code again check for Open condition==true and so on....

 
tenlau:

Thank you too. Your code returns precioTrans=0.0

Also I notice it takes about 10 seconds for execution. I believe is because of for loop. Maybe I was not so clear. I trade only one pair on one chart. I need the price of the last CLOSE deal.

The deal that close an open position.

Basically things works like this: Open one or more consecutive positions. If condition to close==true position/positions will be closed at market price.  I NEED THIS PRICE.

And the code again check for Open condition==true and so on....

Hi Tenlau, try to first insert

HistorySelect(0, TimeCurrent());

 and then

Print("Last deal price = "+DoubleToString(HistoryDealGetDouble(HistoryDealGetTicket(HistoryDealsTotal()-1),DEAL_PRICE),_Digits));

Maybe this could solve the problem. 

 
Malacarne:

Hi Tenlau, try to first insert

 and then

Maybe this could solve the problem. 

Thanks Malacarne. This (   HistorySelect(0, TimeCurrent());   ) fix the problem
 
tenlau:

Thank you too. Your code returns precioTrans=0.0

Also I notice it takes about 10 seconds for execution. I believe is because of for loop. Maybe I was not so clear. I trade only one pair on one chart. I need the price of the last CLOSE deal.

The deal that close an open position.

Basically things works like this: Open one or more consecutive positions. If condition to close==true position/positions will be closed at market price.  I NEED THIS PRICE.

And the code again check for Open condition==true and so on....

Excuse me. I´ll remit the initial draft version.

The one that I have in my INCLUDE file operating is... 

//-------------------------- PRECIO CIERRE POSICIO ANTERIOR -------------------------------
double precioCierrePosicAnt(string simb= NULL)
{
   int      i, nTrans= 0;
   ulong    ticket= 0;
   ENUM_DEAL_ENTRY tipoTrans= DEAL_ENTRY_IN;
   string   simbPosic= "";
   double   precioTrans = 0;
   datetime horaApertAct= PositionSelect(simb)? (datetime)PositionGetInteger(POSITION_TIME): TimeCurrent();
   if(HistorySelect(0, horaApertAct-1))
   {
      nTrans= HistoryDealsTotal();
      for(i= nTrans-1; i>=0; i--)
      {
         ticket= HistoryDealGetTicket(i);
         simbPosic= HistoryDealGetString(ticket, DEAL_SYMBOL);
         precioTrans= HistoryDealGetDouble(ticket, DEAL_PRICE);
         tipoTrans= (ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticket, DEAL_ENTRY);
         if(simbPosic==simb && tipoTrans==DEAL_ENTRY_OUT) break;
      }
   }
   return(precioTrans);
}

Gives the price correctly in EA multicurrency and multi transaction positions.

 
josemiguel1812:

Excuse me. I´ll remit the initial draft version.

The one that I have in my INCLUDE file operating is... 

Gives the price correctly in EA multicurrency and multi transaction positions.

Hello josemiguel1812,

OK no problem. I put it in my EA and now it returns something. But what returns is penultimate deal price not ultimate.

Can you fix this ? I'm a beginner in coding. 

 
tenlau:

Hello josemiguel1812,

OK no problem. I put it in my EA and now it returns something. But what returns is penultimate deal price not ultimate.

Can you fix this ? I'm a beginner in coding. 

If there is open position gives the closing price of the previous one.

If there is no open position, gives the closing price of the latter.

In any case, and taking advantage to improve my code, if you use an counter, you can get the closing price of the position that interests you: from the end of history, the first closed, the second, the fourth ... 

//-------------------------- PRECIO CIERRE POSICION n ANTERIOR -------------------------------
double precioCierrePosicAnt(string simb= NULL, int nPosic= 1)
{
   int      i, cont= 1, nTrans= 0;
   ulong    ticket= 0;
   ENUM_DEAL_ENTRY tipoTrans= DEAL_ENTRY_IN;
   string   simbPosic= "";
   double   precioTrans = 0;
   datetime horaApertAct= PositionSelect(simb)? (datetime)PositionGetInteger(POSITION_TIME): TimeCurrent();
   bool entrar= HistorySelect(0, horaApertAct-1);
   if(entrar)
   {
      nTrans= HistoryDealsTotal();
      for(i= nTrans-1; i>=0; i--)
      {
         ticket= HistoryDealGetTicket(i);
         simbPosic= HistoryDealGetString(ticket, DEAL_SYMBOL);
         precioTrans= HistoryDealGetDouble(ticket, DEAL_PRICE);
         tipoTrans= (ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticket, DEAL_ENTRY);
         if(tipoTrans==DEAL_ENTRY_OUT)
         {
            if(cont==nPosic && simbPosic==simb) break;
            else cont++;
         }
      }
   }
   return(precioTrans);
}

There is a small error to run well in multi-currency ... Can you guess it?.

I leave that as an exercise