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
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: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); } //+-----
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
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:Thanks it works
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
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"); } } //--- }
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"); } } //--- }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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
See the image to understand more of what i am talking about
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