MQL5: last position open price function becomes slow

Omega J Msigwa  

hello folks ,

i have this function that i need to get the last position open price so that i can use it to create grid positions to my EA but the problem is that this function becomes very slow to show values when i print out to see if everything is okay ,and not only on printing the function does not print out values quickly but also on trading that it leads to a problem that causes my grid EA to open multiple trades at the same entry, which is something i do not want...this is my Function 

double lastPosOpenPrice(){
  double lastPrice = -1;
   for(int i = PositionsTotal() - 1; i >= 0; i--)
      if(m_position.SelectByIndex(i))
         if(m_position.Symbol() == Symbol() && m_position.Magic() == magicNumber )
           {
            lastPrice = m_position.PriceOpen();
           }
   return(lastPrice);
  }


See the image to understand more of what i am talking about


las position open price error

As u can See on the image it has opened the first condition well but the last position open price function was slow then when the next grid condition happened the EA bombarded me with trades as you can see on the image.

If there is any solution to this function orther adjustments to the EA i would like to get your ideas ... thank you 

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
Vladimir Karputov  
Omega J Msigwa :

hello folks ,

i have this function that i need to get the last position open price so that i can use it to create grid positions to my EA but the problem is that this function becomes very slow to show values when i print out to see if everything is okay ,and not only on printing the function does not print out values quickly but also on trading that it leads to a problem that causes my grid EA to open multiple trades at the same entry, which is something i do not want...this is my Function 

If there is any solution to this function orther adjustments to the EA i would like to get your ideas ... thank you

What does "slow" mean? How did you determine that this code is slow?


Add:

Note: Your code iterates over ALL positions. There is no guarantee that the code will return the "open price" values for the LAST position.

To find the last position, you need to compare the "open time" of the position.


Add Add: 

Forum on trading, automated trading systems and testing trading strategies

Latest Position type

Vladimir Karputov, 2020.10.29 15:03

It is necessary to take into account the position time and store it in a variable. Implementation example: Last position type:

Vladimir Karputov  
Omega J Msigwa :


If your code opens many positions - one by one without a pause - this is your fault: you wrote bad code. You must correct the howling code - since MQL5 functions work very, very fast

Omega J Msigwa  
Vladimir Karputov:

What does "slow" mean? How did you determine that this code is slow?


Add:

Note: Your code iterates over ALL positions. There is no guarantee that the code will return the "open price" values for the LAST position.

To find the last position, you need to compare the "open time" of the position.

i saw that it updates the value after several 2 up to 3 positions so i thought that is slow. To compare "open time" ..is it like this...??

double lastPosOpenPrice()
  {
   datetime lasttime = 0;
   double lastPrice = -1;
   for(int i = PositionsTotal() - 1; i >= 0; i--)
      if(m_position.SelectByIndex(i))
         if(m_position.Symbol() == Symbol() && m_position.Magic() == magicNumber && m_position.Time() > lasttime)
           {
            lasttime = m_position.Time();
            lastPrice = m_position.PriceOpen();
           }
   return(lastPrice);
  }
//+-----
Vladimir Karputov  
Omega J Msigwa:

i saw that it updates the value after several 2 up to 3 positions so i thought that is slow. To compare "open time" ..is it like this...??

Forum on trading, automated trading systems and testing trading strategies

Latest Position type

Vladimir Karputov, 2020.10.29 15:03

It is necessary to take into account the position time and store it in a variable. Implementation example: Last position type:

Omega J Msigwa  
Vladimir Karputov:

If your code opens many positions - one by one without a pause - this is your fault: you wrote bad code. You must correct the howling code - since MQL5 functions work very, very fast

you are right i'm trying to improve my mql5 coding will you please at least give me direction on what should i do to improve that and to make my code better.. especially to this EA .It opens trades very fast with no pause 

Vladimir Karputov  
Omega J Msigwa :

you are right i'm trying to improve my mql5 coding will you please at least give me direction on what should i do to improve that and to make my code better.. especially to this EA .It opens trades very fast with no pause 

Enter control of opening positions in your code.

Omega J Msigwa  
Vladimir Karputov:

Enter control of opening positions in your code.

void createGrid(double buygap, double sellgap)
  {
   int bb=0, ss=0, tp = distance * 2;
   double ask,bid; 
   ask = SymbolInfoDouble(m_symbol.Name(), SYMBOL_ASK);
   bid = SymbolInfoDouble(m_symbol.Name(), SYMBOL_BID);
   if(PosExists() == false){
      m_trade.Buy(lotsize, Symbol(), ask, 0, ask + tp * _Point, "Simple Grid");
     }
   if(PosExists() == true)
    {
      if(bid >= buygap) {
         m_trade.Buy(lotsize, Symbol(), ask, 0, ask + tp * _Point, "Simple Grid");
        } else if(ask <= sellgap) {
         m_trade.Sell(lotsize, Symbol(), bid, 0, bid - tp * _Point, "Simple Grid");
       }
     } //---
  }
Vladimir Karputov  
Omega J Msigwa :

I cannot read your code, so I applied a styler (this way it is more convenient for me to see variables and loops)

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void createGrid(double buygap, double sellgap)
  {
   int bb=0, ss=0, tp = distance * 2;
   double ask,bid;
   ask = SymbolInfoDouble(m_symbol.Name(), SYMBOL_ASK);
   bid = SymbolInfoDouble(m_symbol.Name(), SYMBOL_BID);
   if(PosExists() == false)
     {
      m_trade.Buy(lotsize, Symbol(), ask, 0, ask + tp * _Point, "Simple Grid");
     }
   if(PosExists() == true)
     {
      if(bid >= buygap)
        {
         m_trade.Buy(lotsize, Symbol(), ask, 0, ask + tp * _Point, "Simple Grid");
        }
      else
         if(ask <= sellgap)
           {
            m_trade.Sell(lotsize, Symbol(), bid, 0, bid - tp * _Point, "Simple Grid");
           }
     } //---
  }
Reason: