Discussion of article "MQL5 Wizard: How to Create a Module of Trailing of Open Positions" - page 2

 

Thank you for your great and helpful article,

May I kindly ask you bring an example how could use this class in writing an expert adviser, my mean is that using this class in EA template not EA generate..

Thank you again,.. 

 

Good afternoon. The module does not work.

The log shows that the level of threshold profit must be greater than the level of setting orders.

In the constructor I initialise variables that are not empty and are not equal to zero. Why do I get an error?

 //+------------------------------------------------------------------+
//|TrailingFixedPips.mqh |
//| Copyright 2009-2013, MetaQuotes Software Corp. | |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#include <Expert\ExpertTrailing.mqh>
// wizard description start
//+----------------------------------------------------------------------+
//| Description of the class|
//| Title=Trailing with floating Stop Loss (Fox104) | |
//| Type=Trailing|
//| Name=FixedPips|
//| Class=CTrailingFixedPips|
//| Page=|
//| Parameter=StopLevel,int,30,Stop Loss trailing level (in points) | |
//| Parameter=ProfitLevel,int,50,Take Profit trailing level (in points) | |
//+----------------------------------------------------------------------+
// wizard description end
//+------------------------------------------------------------------+
//| Class CTrailingFixedPips.|
//| Purpose: Class of trailing stops with fixed stop level in pips. || Purpose: Class of trailing stops with fixed stop level in pips. ||
//| Derives from class CExpertTrailing.|
//+------------------------------------------------------------------+
class CTrailingFixedPips : public CExpertTrailing
  {
protected:
   //--- input parameters
   int               m_stop_level;
   int               m_profit_level;

public:
                     CTrailingFixedPips(void);
                    ~CTrailingFixedPips(void);
   //--- methods of initialisation of protected data
   void              StopLevel(int stop_level)     { m_stop_level=stop_level;     }
   void              ProfitLevel(int profit_level) { m_profit_level=profit_level; }
   virtual bool      ValidationSettings(void);
   //---
   virtual bool      CheckTrailingStopLong(CPositionInfo *position,double &sl,double &tp);
   virtual bool      CheckTrailingStopShort(CPositionInfo *position,double &sl,double &tp);
  };
//+------------------------------------------------------------------+
//| Constructor|
//+------------------------------------------------------------------+
void CTrailingFixedPips::CTrailingFixedPips(void)
  {
            m_stop_level = 30;
            m_profit_level = 50;
  }
//+------------------------------------------------------------------+
//| Destructor|
//+------------------------------------------------------------------+
CTrailingFixedPips::~CTrailingFixedPips(void)
  {
  }
//+------------------------------------------------------------------+
//|| Validation settings protected data. ||
//+------------------------------------------------------------------+
bool CTrailingFixedPips::ValidationSettings(void)
  {
   if(!CExpertTrailing::ValidationSettings())
      return(false);
//--- initial data checks
   if((m_profit_level-m_stop_level)*m_adjusted_point<=m_symbol.StopsLevel()*m_symbol.Point() && m_profit_level!=0.0)
     {
      printf(__FUNCTION__+": the threshold profit level must be greater than the order setting level");
      return(false);
     }
//--- ok
   return(true);
  }
//+------------------------------------------------------------------+
//| Checking trailing stop and/or profit for long position. ||
//+------------------------------------------------------------------+
bool CTrailingFixedPips::CheckTrailingStopLong(CPositionInfo *position,double &sl,double &tp)
  {
//--- check
   if(position==NULL)
      return(false);
   if(m_stop_level==0)
      return(false);
//--- parameter check 
   if(m_profit_level==0.0)  return(false);
//--- already at breakeven?
   double open=position.PriceOpen();
   if(position.StopLoss()>=open) return(false);
//--- profit check
   sl=EMPTY_VALUE;
   tp=EMPTY_VALUE;
   if(m_symbol.Bid()-open>m_profit_level*m_adjusted_point)
      sl=m_symbol.NormalizePrice(open+m_stop_level*m_adjusted_point);
//---
   return(sl!=EMPTY_VALUE);
  }
//+------------------------------------------------------------------+
//| Checking trailing stop and/or profit for short position. ||
//+------------------------------------------------------------------+
bool CTrailingFixedPips::CheckTrailingStopShort(CPositionInfo *position,double &sl,double &tp)
  {
//--- check
   if(position==NULL)
      return(false);
   if(m_stop_level==0)
      return(false);
//--- parameter check
   if(m_profit_level==0.0)  return(false);
//--- already at breakeven?
   double open=position.PriceOpen();
   if(position.StopLoss()<=open) return(false);
//--- profit check
   sl=EMPTY_VALUE;
   tp=EMPTY_VALUE;
   if(open-m_symbol.Ask()>m_profit_level*m_adjusted_point)
      sl=m_symbol.NormalizePrice(open-m_stop_level*m_adjusted_point);
//---
   return(sl!=EMPTY_VALUE);
  }
//+------------------------------------------------------------------+

Автоматический трейдинг и тестирование торговых стратегий
Автоматический трейдинг и тестирование торговых стратегий
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
 
Fox104:

Good afternoon. The module does not work.

The log shows that the level of threshold profit must be greater than the level of setting orders.

In the constructor I initialise variables that are not empty and are not equal to zero. Why do I get an error?

Please format the code in your message correctly: Insert the code correctly in the forum. (Your message has already been corrected).
 
Andy:

When using the module specified in the article, errors are generated in the Expert Advisor log while loading the Expert Advisor to the chart:

2017.09.06 00:28:29.873 1 (EURUSD,M5) OnInit: error initializing indicators

2017.09.06 00:31:20.256 1 (EURUSD,M5) CExpertBase::SetPriceSeries: changing of timeseries is forbidden

2017.09.06 00:31:20.256 1 (EURUSD,M5) CExpertBase::SetOtherSeries: changing of timeseries is forbidden

2017.09.06 00:31:20.256 1 (EURUSD,M5) CExpertBase::InitIndicators: parameters of setting are not checked

2017.09.06 00:31:20.256 1 (EURUSD,M5) CExpert::InitIndicators: error initialisation indicators of trailing object

2017.09.06 00:31:20.256 1 (EURUSD,M5) OnInit: error initializing indicators

Maybe it is out of date, where to get a new one or what to fix in this?

Make a change in the trailing module "sampleTrailing.mqh":

//+------------------------------------------------------------------+
//| Check the setting parameters.|
//| INPUT: no.|
//| OUTPUT: true if the settings are correct, otherwise false. || OUTPUT: true if the settings are correct, otherwise false.
//| REMARK: No.|
//+------------------------------------------------------------------+
bool CSampleTrailing::ValidationSettings()
  {
   if(!CExpertTrailing::ValidationSettings())
      return(false);
//--- in case the Init method was not called
   if(m_symbol==NULL) return(false);
//--- parameter check
   if((m_profit-m_stop_level)*m_adjusted_point<=m_symbol.StopsLevel()*m_symbol.Point() && m_profit!=0.0)
     {
      printf(__FUNCTION__+": the threshold profit level must be greater than the order setting level");
      return(false);
     }
//--- ok
   return(true);
  }
 
Andy:

When using the module specified in the article, errors are generated in the Expert Advisor's log while loading the Expert Advisor to a chart:


Perhaps it is outdated, where to get a new one or what to fix in this?

Thanks for the message, the article has been corrected

 
Andy:

When opening a short position and setting the usual stop loss (not trailing) to 0 in the parameters, the trailing stop is not set to breakeven, but if you slightly move the usual stop and set it at least -1 pip, everything works. In longing everything works normally. In checking the condition that there is already a breakeven (the stop is less than the price), initially 0 in the value of the stop and it does not let it go further.


That's right. It is the specified Stop Loss that is the trigger for THIS trailing module to work at breakeven. That is, it is assumed that the position MUST have a stop loss initially.


Added:

the purpose of any article is to show and familiarise with approaches to solve some problem. Then the user himself should develop his own code based on the knowledge gained.

 
Andy:

It should be supplemented so that if there is no stop, the trailing is also - fill the variable with at least the opening value.

(I got a push notification for 2 of your messages, but one of them is missing - deleted)?


If you need to change something - this is not the problem of this article. Here you can do it yourself, based on the knowledge you have gained. That is, if you want to compose a Technical Assignment and think that the article will be rewritten under it - it is not so.

As I see your actions: rewrite the module (a little change) and use.


Added: For the last time, please use the "reply" button. Otherwise I will not reply.

 
Andy:

Okay, I'll write it myself. Have you deleted one of your posts (tell me if you have, so I won't have to look for it)?


You can open a new topic, something like"Module of open positions maintenance to breakeven" - I think such a topic will be useful.

So yes, I was editing my post and clicked on "delete" by mistake - that is in fact I wrote TWO posts and deleted one. Therefore the push arrived correctly.