Files
2018-06-20 15:35:55 +02:00

48 lines
4.1 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//+------------------------------------------------------------------+
//| TrailingMoving.mqh |
//| Copyright 2016, Vasiliy Sokolov. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Vasiliy Sokolov."
#property link "http://www.mql5.com"
#include "Trailing.mqh"
#include "..\Indicators\MovingAverage.mqh"
//+------------------------------------------------------------------+
//| Trailing stop based on MovingAverage. Sets a stop loss of    |
//| a position equal to the MA level                          |
//+------------------------------------------------------------------+
class CTrailingMoving : public CTrailing
{
public:
virtual bool Modify(void);
CIndMovingAverage* Moving;
virtual CTrailing* Copy(void);
};
//+------------------------------------------------------------------+
//| Sets the stop-loss of a position equal to the MA level |
//+------------------------------------------------------------------+
bool CTrailingMoving::Modify(void)
{
if(CheckPointer(Moving) == POINTER_INVALID)
return false;
double value = Moving.OutValue(1);
if(m_position.Direction() == POSITION_TYPE_BUY &&
value > m_position.CurrentPrice())
m_position.CloseAtMarket();
else if(m_position.Direction() == POSITION_TYPE_SELL &&
value < m_position.CurrentPrice())
m_position.CloseAtMarket();
else if(m_position.StopLossValue() != value)
return m_position.StopLossValue(value);
return false;
}
//+------------------------------------------------------------------+
//| Returns an exact copy of the CTrailingMoving instance            |
//+------------------------------------------------------------------+
CTrailing* CTrailingMoving::Copy(void)
{
CTrailingMoving* mov = new CTrailingMoving();
mov.Moving = Moving;
return mov;
}