Never mind.... I think I had auto trading disabled. In any case, any comments for optimization on the code?
Never mind.... I think I had auto trading disabled. In any case, any comments for optimization on the code?
You've attached the compiled ex5
wow!!!!!!!!
Im still trying to figure out why it won't close the position. I still need to clean up the objects on the deinit(). But the EA log shows it works very nicely. Now if only it would close the position when it says it successfully closes the position. I don't get it. The first if ends up !(true) && !(true) so my EA log shows me the last else printf. But the
(CTrade) m_trade.ResultPrice()
is showing up 0.0 and the Trades window showsthe price happily dancing above and below my pretty HLine trailing stop. What am I doing wrong? I may end up abondoning the CTrade for now and using OrderClose() just to make it work. I will see how that works, but it would be very nice and clean looking if someone could point out where I am going wrong with
(CTrade) m_trade.PositionClose(_Symbol, 5);
From this article https://www.mql5.com/en/articles/91 I saw how they closed the position in if branches like
if(posinf.Select(_Symbol)) { if(posinf.Type()==POSITION_TYPE_BUY) { // lot=lot*2; trade.PositionClose(_Symbol,3); } }
So all I did was just do them both in a single if() to check if it wasn't successful. To uhm, try again before Accepting failure. This is the section of code I have under question, but if anyone cares to help, fell free to look at the source attached in the above post.
if( !m_position.Select(_Symbol) && !m_trade.PositionClose(_Symbol, 5) ){ printf("Failed to Close POS:%i at PRICE:%f for %s",m_position.Idintifier(),m_position.PriceCurrent(),m_trade.ResultRetcodeDescription()); printf("Reattempting to Close POS:%i at PRICE:%f",m_position.Idintifier(),m_position.PriceCurrent()); if(m_position.Select(_Symbol) && m_trade.PositionClose(_Symbol, 5) ){ printf("Successfully Closed POS:%i at PRICE:%f",m_position.Idintifier(),m_trade.ResultPrice()); } else{ printf("Failed to Close POS:%i at PRICE:%f for %s",m_position.Idintifier(),m_trade.ResultPrice(),m_trade.ResultRetcodeDescription()); } } else{ printf("Successfully Closed POS:%i at PRICE:%f",m_position.Idintifier(),m_trade.ResultPrice()); }
- 2010.06.21
- Dmitry
- www.mql5.com
Ok. So I changed the logic. Now instead of SELECT()&&CLOSE() I will go with || because if either of them fail I will need to retry from scratch.
Update:: Ok the EA works as expected now. It closes the position. I finally understand why as well, which it was the logic that was messing it up. When I used the && (and) operator, the m_position.Select() was returning true, thus when inverted, !(m_position.Select(_Symbol)), it would return false, and SINCE IT WAS FALSE, THERE WAS NO NEED TO EVALUATE THE OTHER HALF OF THE && which is why m_trade.PositionClose(_Symbol, 5) was not even being executed and m_trade.ResultPrice() was null or 0.0!!!!
This is the new code with the HLine object cleanup on DeInit() and the proper logic. I'm still working on having the EA close upon Successfully closing the open position. I am going to try a call to deinit() after the printf. Any pointers?
Ok. So I changed the logic. Now instead of SELECT()&&CLOSE() I will go with || because if either of them fail I will need to retry from scratch.
Update:: Ok the EA works as expected now. It closes the position. I finally understand why as well, which it was the logic that was messing it up. When I used the && (and) operator, the m_position.Select() was returning true, thus when inverted, !(m_position.Select(_Symbol)), it would return false, and SINCE IT WAS FALSE, THERE WAS NO NEED TO EVALUATE THE OTHER HALF OF THE && which is why m_trade.PositionClose(_Symbol, 5) was not even being executed and m_trade.ResultPrice() was null or 0.0!!!!
This is the new code with the HLine object cleanup on DeInit() and the proper logic. I'm still working on having the EA close upon Successfully closing the open position. I am going to try a call to deinit() after the printf. Any pointers?
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have a trailing stoploss EA that I use to just close positions. The code I have drops the SL price along with other info in the EA log. The issue I have is with the code I use to close the order. For some reason it makes it to the expressions as if the CPositionInfo::Select() and the Ctrade::PositionClose() both return true but the position doesn't get closed. What am I doing wrong?