Copy code Cpp //================================================== // XAUUSD FINAL ABSOLUTE EA (v1.01) // Timeframe: M1 execution / M5 context // Energy Collapse Detector fully integrated //================================================== #property strict #property version "1.01" #property description "XAUUSD Forced-Move EA with Energy Collapse Detector" // -------- Inputs (قفل) -------- input double LotSize = 0.15; input int MaxTradesPerDay = 6; input double MaxSpread = 0.5; input double KillSpread = 0.8; // -------- Globals -------- int tradesToday = 0; bool inTrade = false; double entryPrice = 0; double maxProfit = 0; datetime lastTradeTime = 0; // -------- Utility -------- bool IsSessionOK() { datetime t = TimeCurrent(); int h = TimeHour(t), m = TimeMinute(t); // London 10:10–17:00 | New York 15:10–22:00 (UTC تقریبی) if((h==10 && m>=10) || (h>10 && h<17)) return true; if((h==15 && m>=10) || (h>15 && h<22)) return true; return false; } double Spread() { return (SymbolInfoDouble(_Symbol,SYMBOL_ASK) - SymbolInfoDouble(_Symbol,SYMBOL_BID)); } // -------- Safety Kill -------- bool SafetyOK() { if(Spread() > KillSpread) return false; return true; } // -------- Impulse Detector (6–8$) -------- bool ImpulseValid() { MqlRates r[5]; if(CopyRates(_Symbol, PERIOD_M1, 1, 5, r) < 5) return false; double move = MathAbs(r[0].high - r[4].low); if(move < 6.0 || move > 8.0) return false; int dir = (r[0].close > r[0].open) ? 1 : -1; for(int i=0;i<3;i++) { if((r[i].close - r[i].open)*dir <= 0) return false; } return true; } // -------- Entry -------- void TryEntry() { if(!IsSessionOK()) return; if(tradesToday >= MaxTradesPerDay) return; if(Spread() > MaxSpread) return; if(!ImpulseValid()) return; double price = SymbolInfoDouble(_Symbol,SYMBOL_ASK); MqlTradeRequest req; MqlTradeResult res; ZeroMemory(req); req.action = TRADE_ACTION_DEAL; req.symbol = _Symbol; req.volume = LotSize; req.type = ORDER_TYPE_BUY; req.price = price; req.deviation = 30; if(OrderSend(req,res) && res.retcode==10009) { inTrade = true; entryPrice = price; maxProfit = 0; tradesToday++; lastTradeTime = TimeCurrent(); } } // -------- Exit Engine + Energy Collapse -------- void ManageTrade() { if(!PositionSelect(_Symbol)) { inTrade=false; return; } double price = SymbolInfoDouble(_Symbol,SYMBOL_BID); double profit = (price - entryPrice); if(profit > maxProfit) maxProfit = profit; // Emergency exits if(!SafetyOK() || profit <= -1.5) { PositionClose(_Symbol); inTrade=false; return; } // Profit lock logic if(maxProfit >= 2.0) { if(profit <= 1.2) { PositionClose(_Symbol); inTrade=false; return; } } // Final target if(profit >= 4.0) { PositionClose(_Symbol); inTrade=false; return; } // --------------------------- // Energy Collapse Detector // --------------------------- MqlRates last2[3]; if(CopyRates(_Symbol, PERIOD_M1, 0, 3, last2) == 3) { double r1 = last2[1].high - last2[1].low; double r2 = last2[0].high - last2[0].low; bool newHighLow = (last2[1].high > entryPrice || last2[1].low < entryPrice) || (last2[0].high > entryPrice || last2[0].low < entryPrice); // 2 کندل پشت سر هم با Range < 0.4$ و بدون High/Low جدید if(r1<0.4 && r2<0.4 && !newHighLow) { PositionClose(_Symbol); inTrade=false; return; } // بعد از +2$ سود قفل‌شده if(maxProfit >= 2.0 && !newHighLow) { PositionClose(_Symbol); inTrade=false; return; } } } // -------- OnTick -------- void OnTick() { static datetime lastBar=0; datetime curBar = iTime(_Symbol, PERIOD_M1, 0); if(inTrade) { ManageTrade(); return; } if(curBar != lastBar) { lastBar = curBar; TryEntry(); } } // -------- Reset Daily -------- void OnTimer() { static int lastDay=-1; int d = TimeDay(TimeCurrent()); if(d!=lastDay) { tradesToday=0; lastDay=d; } } int OnInit() { EventSetTimer(60); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); }