السلام عليكم تم تصحيح الكود :
//+------------------------------------------------------------------+
//| Shark_Bat_Harmonic_MT5_Backtest_Ready.mq5 |
//+------------------------------------------------------------------+
#property strict
// ================== INPUTS ==================
input double InpLotSize = 0.1;
input long InpMagic = 777999;
input int ZZ_Depth = 12;
input int ZZ_Dev = 5;
input int ZZ_Back = 3;
// ================== GLOBALS ==================
int zzHandle = INVALID_HANDLE;
double zz[];
datetime lastBarTime = 0;
//+------------------------------------------------------------------+
int OnInit()
{
zzHandle = iCustom(_Symbol, _Period, "Examples\\ZigZag", ZZ_Depth, ZZ_Dev, ZZ_Back);
if(zzHandle == INVALID_HANDLE)
{
Print("❌ فشل تحميل المؤشر! تأكد من وجوده في MQL5/Indicators/Examples");
return INIT_FAILED;
}
ArraySetAsSeries(zz, true);
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(zzHandle != INVALID_HANDLE)
IndicatorRelease(zzHandle);
}
//+------------------------------------------------------------------+
void OnTick()
{
datetime barTime = iTime(_Symbol, _Period, 0);
if(barTime == 0 || barTime == lastBarTime)
return;
lastBarTime = barTime;
if(CopyBuffer(zzHandle, 0, 0, 500, zz) <= 0)
return;
double tmpP[5];
datetime tmpT[5];
int c = 0;
// استخراج 5 نقاط ZigZag مؤكدة، مع تجاهل آخر شمعتين
for(int i = 2; i < 500 && c < 5; i++)
{
if(zz[i] != 0.0)
{
tmpP[c] = zz[i];
tmpT[c] = iTime(_Symbol, _Period, i);
c++;
}
}
if(c < 5)
return;
// ترتيب النقاط من الأقدم إلى الأحدث: X A B C D
double p[5];
datetime t[5];
for(int i = 0; i < 5; i++)
{
p[i] = tmpP[4 - i];
t[i] = tmpT[4 - i];
}
double X = p[0];
double A = p[1];
double B = p[2];
double C = p[3];
double D = p[4]; // غير مستخدمة حاليًا، لكن احتفظنا بها للهيكل
double XA = MathAbs(A - X);
double AB = MathAbs(B - A);
double BC = MathAbs(C - B);
if(XA <= 0.0 || AB <= 0.0)
return;
double rAB = AB / XA;
double rBC = BC / AB;
if(HasAnyOrderOrPosition())
return;
bool bullish = (X > A);
// --- BAT ---
if(rAB >= 0.38 && rAB <= 0.60)
{
double entry = bullish ? (X - XA * 0.886)

X + XA * 0.886);
double sl = X;
double risk = MathAbs(entry - sl);
double tp = bullish ? (entry + risk)

entry - risk);
if((bullish && C > B) || (!bullish && C < B))
{
PlaceOrder(
bullish ? ORDER_TYPE_BUY_LIMIT : ORDER_TYPE_SELL_LIMIT,
entry, sl, tp,
"Bat 1:1",
t, p
);
}
}
// --- SHARK ---
if(rBC >= 1.13 && rBC <= 1.60)
{
double entry = bullish ? (X - XA * 1.13)

X + XA * 1.13);
double sl = bullish ? (X - XA * 1.27)

X + XA * 1.27);
double risk = MathAbs(entry - sl);
double tp = bullish ? (entry + risk)

entry - risk);
PlaceOrder(
bullish ? ORDER_TYPE_BUY_LIMIT : ORDER_TYPE_SELL_LIMIT,
entry, sl, tp,
"Shark 1:1",
t, p
);
}
}
//+------------------------------------------------------------------+
void PlaceOrder(ENUM_ORDER_TYPE type,
double price,
double sl,
double tp,
string comm,
const datetime &t[],
const double &p[])
{
MqlTradeRequest req;
MqlTradeResult res;
ZeroMemory(req);
ZeroMemory(res);
req.action = TRADE_ACTION_PENDING;
req.symbol = _Symbol;
req.volume = InpLotSize;
req.type = type;
req.price = NormalizeDouble(price, _Digits);
req.sl = NormalizeDouble(sl, _Digits);
req.tp = NormalizeDouble(tp, _Digits);
req.magic = (ulong)InpMagic;
req.comment = comm;
req.type_filling = GetFillingMode();
req.type_time = ORDER_TIME_GTC;
if(!OrderSend(req, res))
{
Print("❌ خطأ في التنفيذ: ", GetLastError());
return;
}
if(res.retcode != TRADE_RETCODE_DONE && res.retcode != TRADE_RETCODE_PLACED)
{
Print("❌ فشل إرسال الأمر. Retcode=", res.retcode, " | ", res.comment);
return;
}
DrawPattern(t, p, clrDeepSkyBlue, comm);
}
//+------------------------------------------------------------------+
ENUM_ORDER_TYPE_FILLING GetFillingMode()
{
long filling = SymbolInfoInteger(_Symbol, SYMBOL_FILLING_MODE);
if((filling & SYMBOL_FILLING_FOK) != 0)
return ORDER_FILLING_FOK;
if((filling & SYMBOL_FILLING_IOC) != 0)
return ORDER_FILLING_IOC;
return ORDER_FILLING_RETURN;
}
//+------------------------------------------------------------------+
void DrawPattern(const datetime &t[], const double &p[], color clr, string name)
{
for(int i = 0; i < 4; i++)
{
string ln = "H_" + name + "_" + IntegerToString

;
ObjectDelete(0, ln);
ObjectCreate(0, ln, OBJ_TREND, 0, t[i], p[i], t[i + 1], p[i + 1]);
ObjectSetInteger(0, ln, OBJPROP_COLOR, clr);
ObjectSetInteger(0, ln, OBJPROP_RAY_RIGHT, false);
}
}
//+------------------------------------------------------------------+
bool HasAnyOrderOrPosition()
{
// فحص الصفقات المفتوحة
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket

;
if(ticket > 0 && PositionSelectByTicket(ticket))
{
if((long)PositionGetInteger(POSITION_MAGIC) == InpMagic &&
PositionGetString(POSITION_SYMBOL) == _Symbol)
{
return true;
}
}
}
// فحص الأوامر المعلقة
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
ulong ticket = OrderGetTicket

;
if(ticket > 0 && OrderSelect(ticket))
{
if((long)OrderGetInteger(ORDER_MAGIC) == InpMagic &&
OrderGetString(ORDER_SYMBOL) == _Symbol)
{
return true;
}
}
}
return false;
}
//+------------------------------------------------------------------+