derivato da MrBaro75/Warrior_EA
- Added bulk read/write methods for feature caches in IFeaturesView and its implementations to optimize performance. - Introduced LabelCacheInvalidateAll method to manage label cache invalidation alongside feature cache. - Implemented PooledIndependentBars method in topology interfaces to account for additional independent observations. - Enhanced risk budget management with throttling for peak-equity updates to reduce unnecessary file operations. - Improved error handling and logging for ATR trailing stops to ensure better visibility of issues. - Updated alt-data handling to prevent unnecessary operations during testing and optimization phases.
198 righe
9,6 KiB
MQL5
198 righe
9,6 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| CTrailingATR.mqh |
|
|
//| AnimateDread |
|
|
//| https://tawarriors.com|
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "AnimateDread"
|
|
#include <Expert\ExpertTrailing.mqh>
|
|
#include "..\System\TradeChecks.mqh"
|
|
// wizard description start
|
|
//+----------------------------------------------------------------------+
|
|
//| Description of the class |
|
|
//| Title=Trailing Stop based on ATR Indicator |
|
|
//| Type=Trailing |
|
|
//| Name=ATR |
|
|
//| Class=CTrailingATR |
|
|
//| Page= |
|
|
//| Parameter=Multiplier,double,2, ATR Multiplier |
|
|
//| Parameter=Periods,int,14, ATR Periods |
|
|
//| Parameter=Shift,int,0, ATR Shift |
|
|
//+----------------------------------------------------------------------+
|
|
// wizard description end
|
|
//+------------------------------------------------------------------+
|
|
//| Class CTrailingATR. |
|
|
//| Purpose: Class of trailing stops based on ATR * Multiplier. |
|
|
//| Derives from class CExpertTrailing. |
|
|
//+------------------------------------------------------------------+
|
|
class CTrailingATR : public CExpertTrailing
|
|
{
|
|
protected:
|
|
CiATR m_ATR; // ATR indicator
|
|
//--- input parameters
|
|
double m_multiplier; // Configurable multiple for ATR
|
|
int m_periods; // Configurable periods for ATR
|
|
int m_shift; // Configurable shift for ATR
|
|
//--- Print-once-until-resolved: a dead/cold ATR made CheckTrailingStop() return false with
|
|
//--- nothing logged, which reads identically to "the stop simply hasn't moved yet" - trailing
|
|
//--- silently stops working. Reset the moment a good read comes back so a LATER outage logs again.
|
|
bool m_atrDeadWarned;
|
|
|
|
public:
|
|
CTrailingATR(void);
|
|
~CTrailingATR(void);
|
|
//--- methods of initialization of protected data
|
|
void Multiplier(double multiplier) { m_multiplier = multiplier; }
|
|
void Periods(int periods) { m_periods = periods; }
|
|
void Shift(int shift) { m_shift = shift; }
|
|
|
|
virtual bool InitIndicators(CIndicators* indicators);
|
|
virtual bool ValidationSettings();
|
|
virtual bool CheckTrailingStopLong(CPositionInfo* position, double& sl, double& tp);
|
|
virtual bool CheckTrailingStopShort(CPositionInfo* position, double& sl, double& tp);
|
|
protected:
|
|
bool AdjustStopLoss(double& sl, double currentPrice, double atrValue, bool isLong);
|
|
bool CheckTrailingStop(CPositionInfo* position, double& sl, double& tp, bool isLong);
|
|
};
|
|
//+------------------------------------------------------------------+
|
|
//| Constructor |
|
|
//+------------------------------------------------------------------+
|
|
void CTrailingATR::CTrailingATR(void) :
|
|
m_multiplier(2),
|
|
m_periods(14),
|
|
m_shift(0),
|
|
m_atrDeadWarned(false)
|
|
{
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Destructor |
|
|
//+------------------------------------------------------------------+
|
|
void CTrailingATR::~CTrailingATR(void)
|
|
{
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Validation settings protected data. |
|
|
//+------------------------------------------------------------------+
|
|
bool CTrailingATR::ValidationSettings()
|
|
{
|
|
if(!CExpertTrailing::ValidationSettings())
|
|
return (false);
|
|
// Check multiplier
|
|
if(m_multiplier <= 0.0 || m_multiplier > 100)
|
|
{
|
|
printf(__FUNCTION__ + ": multiplier must be greater than 0 and lesser than 100");
|
|
return (false);
|
|
}
|
|
// Check ATR Periods
|
|
if(m_periods <= 0 || m_periods > 200)
|
|
{
|
|
printf(__FUNCTION__ + ": ATR Periods must be greater than 0 and lesser than 200");
|
|
return (false);
|
|
}
|
|
// Check ATR shift
|
|
if(m_shift < 0 || m_shift > 200)
|
|
{
|
|
printf(__FUNCTION__ + ": ATR shift must be 0-200");
|
|
return (false);
|
|
}
|
|
//--- ok
|
|
return (true);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Checking for input parameters and setting protected data. |
|
|
//+------------------------------------------------------------------+
|
|
bool CTrailingATR::InitIndicators(CIndicators* indicators)
|
|
{
|
|
if(indicators == NULL)
|
|
return (false);
|
|
// Add ATR indicator to the collection
|
|
if(!indicators.Add(GetPointer(m_ATR)))
|
|
{
|
|
printf(__FUNCTION__ + ": error adding object");
|
|
return (false);
|
|
}
|
|
// Initialize ATR indicator
|
|
if(!m_ATR.Create(m_symbol.Name(), m_period, m_periods))
|
|
{
|
|
return (false);
|
|
}
|
|
//--- ok
|
|
return (true);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Common logic for adjusting SL considering freeze level |
|
|
//+------------------------------------------------------------------+
|
|
bool CTrailingATR::AdjustStopLoss(double& sl, double currentPrice, double atrValue, bool isLong)
|
|
{
|
|
// Brokers often set SYMBOL_TRADE_STOPS_LEVEL independently of (and sometimes larger than)
|
|
// SYMBOL_TRADE_FREEZE_LEVEL - validating only against the freeze level let a computed new_sl pass
|
|
// here yet still be inside the broker's minimum-stop-distance zone, so the eventual
|
|
// PositionModify() outside this function would get rejected with nothing logged here to explain it.
|
|
// TCMinStopDistance() is the shared max(stops, freeze) rule from System\TradeChecks.mqh (article
|
|
// 2555 #6/#7); it also floors the stops level at the current spread, which brokers that publish a
|
|
// 0 SYMBOL_TRADE_STOPS_LEVEL and enforce a floating spread-derived limit instead require.
|
|
double minDistance = TCMinStopDistance(m_symbol.Name());
|
|
int digits = m_symbol.Digits(); // Get the number of digits after the decimal for the instrument
|
|
// Calculate new SL based on position type (Long or Short)
|
|
double new_sl = isLong ? NormalizeDouble(currentPrice - atrValue * m_multiplier, digits)
|
|
: NormalizeDouble(currentPrice + atrValue * m_multiplier, digits);
|
|
// Calculate the level beyond which SL cannot be set due to freeze/stops level
|
|
double level = isLong ? currentPrice - minDistance : currentPrice + minDistance;
|
|
// Check if new SL is in the correct direction and respects the freeze level
|
|
bool isSlValid = isLong ? (new_sl > sl && new_sl < level) : (new_sl < sl && new_sl > level);
|
|
if(isSlValid)
|
|
{
|
|
sl = new_sl;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Checking trailing stop and/or profit for long position. |
|
|
//+------------------------------------------------------------------+
|
|
bool CTrailingATR::CheckTrailingStopLong(CPositionInfo* position, double& sl, double& tp)
|
|
{
|
|
return CheckTrailingStop(position, sl, tp, true);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Checking trailing stop and/or profit for short position. |
|
|
//+------------------------------------------------------------------+
|
|
bool CTrailingATR::CheckTrailingStopShort(CPositionInfo* position, double& sl, double& tp)
|
|
{
|
|
return CheckTrailingStop(position, sl, tp, false);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Shared body of both directions. A long trails off the Bid, a |
|
|
//| short off the Ask; everything else is identical. |
|
|
//+------------------------------------------------------------------+
|
|
bool CTrailingATR::CheckTrailingStop(CPositionInfo* position, double& sl, double& tp, bool isLong)
|
|
{
|
|
sl = EMPTY_VALUE;
|
|
tp = EMPTY_VALUE;
|
|
if(position == NULL)
|
|
return false;
|
|
double new_sl = position.StopLoss();
|
|
double price = isLong ? m_symbol.Bid() : m_symbol.Ask();
|
|
double atr = m_ATR.Main(m_shift);
|
|
//--- A cold/dead ATR reads EMPTY_VALUE == DBL_MAX, a real MathIsValidNumber()-passing number, not
|
|
//--- a recognisable failure - AdjustStopLoss would compute against it and its own validity tests
|
|
//--- would fail, returning false with NOTHING logged. That reads identically to "the stop simply
|
|
//--- has not moved yet", so trailing can silently stop working for as long as the handle stays bad.
|
|
if(atr == EMPTY_VALUE || atr <= 0.0 || !MathIsValidNumber(atr))
|
|
{
|
|
if(!m_atrDeadWarned)
|
|
{
|
|
m_atrDeadWarned = true;
|
|
PrintFormat("%s: ATR trailing STALLED for %s - m_ATR.Main(%d)=%.10g (needs a valid > 0"
|
|
" value). Stops will not trail until this indicator recovers.",
|
|
__FUNCTION__, (position != NULL ? position.Symbol() : "?"), m_shift, atr);
|
|
}
|
|
return false;
|
|
}
|
|
m_atrDeadWarned = false;
|
|
//--- AdjustStopLoss honours the freeze level; only publish sl when it actually moved
|
|
if(!AdjustStopLoss(new_sl, price, atr, isLong))
|
|
return false;
|
|
sl = new_sl;
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|