Download MetaTrader 5

find the price of the last closed position

To add comments, please log in or register
Publish the program in the Market and sell it to millions of traders!
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.02.09 18:57 

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! 

Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.02.09 19:26  

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

Rodrigo Malacarne
Moderator
9903
Rodrigo Malacarne 2014.02.09 23:36  
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));
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.02.10 09:06  
Malacarne:

You can try this (in MQL5):

Thanks for your suggestion. I try the code but it always returns: "Last deal price = 0.00000"
Jose Miguel Soriano
4963
Jose Miguel Soriano 2014.02.10 10:14  

 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);
}
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.02.10 11:18  
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....

Rodrigo Malacarne
Moderator
9903
Rodrigo Malacarne 2014.02.10 12:35  
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. 

Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.02.10 14:58  
Malacarne:

Hi Tenlau, try to first insert

 and then

Maybe this could solve the problem. 

Thanks Malacarne. This (   HistorySelect(0, TimeCurrent());   ) fix the problem
Jose Miguel Soriano
4963
Jose Miguel Soriano 2014.02.10 20:57  
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.

Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.02.11 07:44  
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. 

Jose Miguel Soriano
4963
Jose Miguel Soriano 2014.02.11 09:25  
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 

12
To add comments, please log in or register