Download MetaTrader 5

CTrade Closing Position

To add comments, please log in or register
forlorn_beast
24
forlorn_beast  

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?

Files:
forlorn_beast
24
forlorn_beast  

Never mind.... I think I had auto trading disabled. In any case, any comments for optimization on the code?



Paul
784
Paul  
forlorn_beast:

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

forlorn_beast
24
forlorn_beast  
Sorry. By the way, it isn't working even with auto trading enabled. It dumps in the EA log the ResultPrice() at 0.0. Any Ideas what is wrong with it?
Files:
Nikolaos Pantzos
12605
Nikolaos Pantzos  

wow!!!!!!!!

forlorn_beast
24
forlorn_beast  

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());
}
An Example of a Trading System Based on a Heiken-Ashi Indicator
  • 2010.06.21
  • Dmitry
  • www.mql5.com
In this article we look into the question of using a Heiken-Ashi indicator in trading. Based on this indicator, a simple trading system is considered and an MQL5 Expert Advisor is written. Trading operations are implemented on the bases of classes of the Standard class library. The testing results of the reviewed trading strategy, are based on the history, and obtained using the built-in MetaTrader 5 strategy tester, are provided in the article.
forlorn_beast
24
forlorn_beast  

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? 

Files:
forlorn_beast
24
forlorn_beast  
forlorn_beast:

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? 

Just for the record, the proper way of having an EA close itself is through the use of RemoveExpert() which you can read about here https://www.mql5.com/en/docs/common/expertremove So I will continue to work on this EA. Thank you to anyone who bothered to look at it.
Documentation on MQL5: Common Functions / ExpertRemove
  • www.mql5.com
Common Functions / ExpertRemove - Documentation on MQL5
To add comments, please log in or register