To do this, you need to write your module for Trailing and connect it (instead of TrailingFixedPips).
Articles: MQL5 Wizard: How to Create a Module of Trailing of Open Positions

- www.mql5.com
Ok, i did exactly like the article told me, but now i am having errors when i use the module:
HG 0 02:42:05.581 Test (WIN$,M5) 2019.01.01 00:00:00 CExpertBase::SetPriceSeries: changing of timeseries is forbidden KF 0 02:42:05.581 Test (WIN$,M5) 2019.01.01 00:00:00 CExpertBase::SetOtherSeries: changing of timeseries is forbidden OG 0 02:42:05.581 Test (WIN$,M5) 2019.01.01 00:00:00 CExpertBase::InitIndicators: parameters of setting are not checked OG 0 02:42:05.581 Test (WIN$,M5) 2019.01.01 00:00:00 CExpert::InitIndicators: error initialization indicators of trailing object GD 0 02:42:05.581 Test (WIN$,M5) 2019.01.01 00:00:00 OnInit: error initializing indicators RN 2 02:42:05.581 Tester tester stopped because OnInit returns non-zero code
This is the code of the module, i copied from the article since download is available:
//+------------------------------------------------------------------+ //| SampleTrailing.mqh | //| Copyright 2010, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2010, MetaQuotes Software Corp." #property link "https://www.mql5.com" //+------------------------------------------------------------------+ //| include files | //+------------------------------------------------------------------+ #include <Expert\ExpertTrailing.mqh> // wizard description start //+------------------------------------------------------------------+ //| Description of the class | //| Title=Moving a position to a lossless level | //| Type=Trailing | //| Name=BreakEven | //| Class=CSampleTrailing | //| Page= | //| Parameter=Profit,int,20 | //| Parameter=StopLevel,int,0 | //+------------------------------------------------------------------+ // wizard description end //+------------------------------------------------------------------+ //| Class CSampleTrailing. | //| Purpose: Class for trailing of open positions | //| by moving Stop order to a lossless level. | //| Is derived from the CExpertTrailing class. | //+------------------------------------------------------------------+ class CSampleTrailing : public CExpertTrailing { protected: int m_profit; // threshold level of profit int m_stop_level; // lossless level public: CSampleTrailing(); //--- method of setting adjustable parameters void Profit(int value) { m_profit=value; } void StopLevel(int value) { m_stop_level=value; } //--- method of validation of adjustable settings virtual bool ValidationSettings(); //--- methods of generation of position modification signals virtual bool CheckTrailingStopLong(CPositionInfo* position,double& sl,double& tp); virtual bool CheckTrailingStopShort(CPositionInfo* position,double& sl,double& tp); }; //+------------------------------------------------------------------+ //| Constructor CSampleTrailing. | //| INPUT: no. | //| OUTPUT: no. | //| REMARK: no. | //+------------------------------------------------------------------+ void CSampleTrailing::CSampleTrailing() { //--- setting default values m_profit =20; m_stop_level=0; } //+------------------------------------------------------------------+ //| Check of adjustable parameters. | //| INPUT: no. | //| OUTPUT: true if the parameters are correct, false if not. | //| REMARK: no. | //+------------------------------------------------------------------+ bool CSampleTrailing::ValidationSettings() { //--- what if the Init has not been called? if(m_symbol==NULL) return(false); //--- check of parameters if((m_profit-m_stop_level)*m_adjusted_point<=m_symbol.StopsLevel()*m_symbol.Point() && m_profit!=0.0) { printf(__FUNCTION__+": threshold level of profit must be greater than the level of setting stop orders"); return(false); } //--- ok return(true); } //+------------------------------------------------------------------+ //| Check for modification of stop orders of a long position. | //| INPUT: position - pointer to a position object, | //| sl - link for a new price of stop loss order, | //| tp - link for a new price of take profit order. | //| OUTPUT: true if condition is satisfied, false if not. | //| REMARK: no. | //+------------------------------------------------------------------+ bool CSampleTrailing::CheckTrailingStopLong(CPositionInfo* position,double& sl,double& tp) { //--- check of pointer if(position==NULL) return(false); //--- check of parameters if(m_profit==0.0) return(false); //--- already in a lossless zone? double open=position.PriceOpen(); if(position.StopLoss()>=open) return(false); //--- check of profit sl=EMPTY_VALUE; tp=EMPTY_VALUE; if(m_symbol.Bid()-open>m_profit*m_adjusted_point) sl=m_symbol.NormalizePrice(open+m_stop_level*m_adjusted_point); //--- return(sl!=EMPTY_VALUE); } //+------------------------------------------------------------------+ //| Check for modification of stop orders of a short position. | //| INPUT: position - pointer to a position object, | //| sl - link for a new price of stop loss order, | //| tp - link for a new take profit order. | //| OUTPUT: true if condition is satisfied, false if not. | //| REMARK: no. | //+------------------------------------------------------------------+ bool CSampleTrailing::CheckTrailingStopShort(CPositionInfo* position,double& sl,double& tp) { //--- check of pointer if(position==NULL) return(false); //--- check of parameters if(m_profit==0.0) return(false); //--- already in a lossless zone? double open=position.PriceOpen(); if(position.StopLoss()<=open) return(false); //--- check of profit sl=EMPTY_VALUE; tp=EMPTY_VALUE; if(open-m_symbol.Ask()>m_profit*m_adjusted_point) sl=m_symbol.NormalizePrice(open-m_stop_level*m_adjusted_point); //--- return(sl!=EMPTY_VALUE); } //+------------------------------------------------------------------+
I am sorry, i think i misunderstood things, i believe the word in english would be "breakeven", i need that everytime i reach 40 points of gain, my stop order gets moved to a lossless level and stay there.
I am really thankful for your patience
I am sorry, i think i misunderstood things, i believe the word in english would be "breakeven", i need that everytime i reach 40 points of gain, my stop order gets moved to a lossless level and stay there.
I am really thankful for your patience
Trailing Stop (min distance from price to Stop Loss, in pips)
Set this value 'Trailing Stop' to '40', and 'Trailing Step' to '1'.it works now, but it will only work if i remove this line of code. I need this line, otherwise the EA will never wait for a trade to be fully complete, it will keep ending positions and starting others before it reaches loss or gain.
Anyway, thank you very much for your help.
void OnTick() { if (PositionsTotal()==0) ExtExpert.OnTick(); }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I`ve been trying to create an EA but i am having trouble with the Trailing Stop, i have tried everything and it wont work, when i compile everything is fine, no errors, but when i simulate the Trailing Stop does nothing. I would like that everytime i am having 40points gain, my stop loss would go to my entry point.