Discussion of article "Universal Expert Advisor: A Custom Trailing Stop (Part 6)"

 

New article Universal Expert Advisor: A Custom Trailing Stop (Part 6) has been published:

The sixth part of the article about the universal Expert Advisor describes the use of the trailing stop feature. The article will guide you through how to create a custom trailing stop module using unified rules, as well as how to add it to the trading engine so that it would automatically manage positions.

A Trailing Stop is a kind of algorithm, and its only task is to move a stop-loss order to a certain price level in order to protect a position from excessive loss. Trailing stop algorithms are external in relation to the trading engine. This means that their presence, as well as their absence in no way should affect the functionality of CStrategy. In addition, the trailing algorithms require the configuration of certain parameters. For example, for a classic trailing stop we need to specify the distance in pips between the reached extreme price values and the stop-loss level, which we want to be managed. Therefore, in addition to the algorithms themselves, we need to store their operation parameters somewhere.

 


Author: Vasiliy Sokolov

 
Now everything works - just cool...all at once.... thank you very much...I knew that until the new version becomes stable...there will be errors...)))))
 
Hi. Great work, thank you very much! Tell me, are you planning to implement in the engine the approaches for trading on MOEX, described by you in the article https://www.mql5.com/en/articles/1683? Specifically, I am interested in analysing market liquidity and based on it entering with a specified deviation (max. slippage).
Как обезопасить себя и своего эксперта при торговле на Московской бирже
Как обезопасить себя и своего эксперта при торговле на Московской бирже
  • 2015.06.18
  • Vasiliy Sokolov
  • www.mql5.com
В статье подробно описываются методы работы, призванные обеспечить безопасность совершения торговых операций на биржевых и малоликвидных рынках, на примере срочной секции Московской биржи. Статья является логическим продолжением статьи "Основы биржевого ценообразования на примере срочной секции Московской биржи", в которой даны теоретические основы биржевой торговли, но носит более практический характер.
 
igorbel:
Hi. Great work, thank you very much! Tell me, are you planning to implement in the engine the approaches for trading on MOEX, described by you in the article https://www.mql5.com/en/articles/1683? Specifically interested in analysing market liquidity and based on it entering with a specified deviation (max. slippage).
The proposal is interesting. Let's see.
 
//+------------------------------------------------------------------+
//| Closes the current market position by setting the closing |
//| comment equal to comment|
//+------------------------------------------------------------------+
bool CPosition::CloseAtMarket(string comment="")
  {
   if(!IsActive())
      return false;
   m_trade.PositionModify(m_id, 0.0, 0.0);
   ENUM_ACCOUNT_MARGIN_MODE mode=(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);
   if(mode != ACCOUNT_MARGIN_MODE_RETAIL_HEDGING)
      return m_trade.PositionClose(m_symbol);
   return m_trade.PositionClose(m_id);
  }

Can you tell me why m_trade.PositionModify(m_id, 0.0, 0.0) in this function? I have Invalid Stops errors in the log because of this operation.

 
igorbel:

Can you tell me why m_trade.PositionModify(m_id, 0.0, 0.0) in this function? I have Invalid Stops errors in the log because of this operation.

These are rudiments of the old version. Thanks for pointing it out, it will be fixed.
 

I didn't change anything, but when compiling any of the trailing modules, including the base class, I get errors:

'CTrailing' - declaration without type  PositionMT5.mqh 48      4
'Trailing' - undeclared identifier      PositionMT5.mqh 73      20
'Trailing' - object pointer expected    PositionMT5.mqh 73      20
'Trailing' - object pointer expected    PositionMT5.mqh 74      14
 
bool CTrailingClassic::Modify(void)
  {

   if(CheckPointer(m_position)==POINTER_INVALID)
     {
      string text="Invalid position for current trailing-stop. Set position with 'SetPosition' method";
      CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
      Log.AddMessage(msg);
      return false;
     }
   if(m_diff_extremum<=0.0)
     {
      string text="Set points trailing-stop with 'SetDiffExtremum' method";
      CMessage *msg=new CMessage(MESSAGE_WARNING,__FUNCTION__,text);
      Log.AddMessage(msg);
      return false;
     }
   double extremum=FindExtremum(m_position);
   if(extremum == 0.0)return false;
   double n_sl = 0.0;
   if(m_position.Direction()==POSITION_TYPE_BUY)
      n_sl=extremum-m_diff_extremum;
   else
      n_sl=extremum+m_diff_extremum;
   if(n_sl!=m_position.StopLossValue())
      return m_position.StopLossValue(n_sl);
   return false;
  }

It wouldn't hurt to check that the new sl is below the current price for a long position and above the current price for a short position.

 

Hello. Thanks for the article.

How can I adjust the lot size for each trade? I see it buys one lot only


Thanks in advance

 

Dear Mr Sokolov,

Very interesting article, but unfortunately I couldn't install a single one of your codes without tons of compiller errors. I tried all 9 parts but in vain.

Hence my question: Is there a specific approach to install your code?

Thank You

 
Brevity is the soul of wit.
Acceptance, understanding, correct interpretation, there are hardly any more. Unfortunately