//+------------------------------------------------------------------+ //| CTrailingIntelligent.mqh | //| AnimateDread | //| https://tawarriors.com| //+------------------------------------------------------------------+ #property copyright "AnimateDread" #include "..\Trailing\TrailingATR.mqh" #include "..\Variables\ConfidenceBridge.mqh" //--- Confidence-adaptive ATR trailing band. The trail distance interpolates between a TIGHT floor and //--- a WIDE ceiling (as ATR multiples) based on how strongly the live AI signal still backs the OPEN //--- position's direction (g_LiveAISignedConfidence, refreshed every tick in //--- CExpertSignalAIBase::ScheduleTrainingIfNeeded()): //--- * confidence still firmly in the position's favor -> widen toward TRAIL_ATR_MAX_MULT, letting a //--- winner keep room to run; //--- * confidence weak, flat, or flipped against it -> tighten toward TRAIL_ATR_MIN_MULT, locking //--- in profit faster. //--- It reuses CTrailingATR's ATR indicator and freeze-level-aware AdjustStopLoss() verbatim; only the //--- per-check multiplier differs. Trailing is still strictly one-directional (SL only ever moves in the //--- favorable direction), so this can never widen a stop against the trade - see AdjustStopLoss(). #define TRAIL_ATR_MIN_MULT 1.0 // tightest trail (no/against confidence) #define TRAIL_ATR_MAX_MULT 3.0 // widest trail (full confidence in favor) //+------------------------------------------------------------------+ //| Class CTrailingIntelligent. | //+------------------------------------------------------------------+ class CTrailingIntelligent : public CTrailingATR { protected: //--- maps the live signed confidence to an ATR multiple for the given position direction, then //--- stashes it in the inherited m_multiplier so the base AdjustStopLoss() picks it up. void ApplyAdaptiveMultiplier(bool isLong); public: virtual bool CheckTrailingStopLong(CPositionInfo* position, double& sl, double& tp) override; virtual bool CheckTrailingStopShort(CPositionInfo* position, double& sl, double& tp) override; }; //+------------------------------------------------------------------+ //| Set m_multiplier from live directional confidence | //+------------------------------------------------------------------+ void CTrailingIntelligent::ApplyAdaptiveMultiplier(bool isLong) { //--- confidence component pulling in THIS position's favor, in [0,1] double favor = isLong ? MathMax(0.0, g_LiveAISignedConfidence) : MathMax(0.0, -g_LiveAISignedConfidence); favor = MathMin(1.0, favor); m_multiplier = TRAIL_ATR_MIN_MULT + (TRAIL_ATR_MAX_MULT - TRAIL_ATR_MIN_MULT) * favor; } //+------------------------------------------------------------------+ //| Trailing stop for a long position | //+------------------------------------------------------------------+ bool CTrailingIntelligent::CheckTrailingStopLong(CPositionInfo* position, double& sl, double& tp) { ApplyAdaptiveMultiplier(true); return CTrailingATR::CheckTrailingStopLong(position, sl, tp); } //+------------------------------------------------------------------+ //| Trailing stop for a short position | //+------------------------------------------------------------------+ bool CTrailingIntelligent::CheckTrailingStopShort(CPositionInfo* position, double& sl, double& tp) { ApplyAdaptiveMultiplier(false); return CTrailingATR::CheckTrailingStopShort(position, sl, tp); } //+------------------------------------------------------------------+