Skip to content

Instantly share code, notes, and snippets.

@muhannadadel
Created January 17, 2026 02:20
Show Gist options
  • Select an option

  • Save muhannadadel/e7f7fb190f31e6cf71e9dc16efc82f2a to your computer and use it in GitHub Desktop.

Select an option

Save muhannadadel/e7f7fb190f31e6cf71e9dc16efc82f2a to your computer and use it in GitHub Desktop.
//+------------------------------------------------------------------+
//| GoldProScalper.mq5 |
//+------------------------------------------------------------------+
#property copyright "Gold Pro Scalper"
#property version "3.0"
#property description "بوت محسّن للذهب مع تأمين متقدم و 10 صفقات"
#property strict
//+------------------------------------------------------------------+
//| إعدادات المدخلات |
//+------------------------------------------------------------------+
input double LotSize = 0.01; // حجم اللوت
input int MaxTrades = 10; // أقصى عدد صفقات (زاد لـ 10)
input int TradesPerHour = 15; // صفقات في الساعة
input int StopLoss = 100; // وقف الخسارة
input int TakeProfit = 50; // جني الأرباح
input double MinProfitRatio = 1.5; // نسبة الربح/الخسارة
input int TrailingStart = 20; // بدء التريلنغ عند هذه النقاط ربح
input int TrailingStep = 10; // خطوة التريلنغ
input bool UsePartialClose = true; // إغلاق جزئي عند الربح
input double PartialClosePct = 50.0; // نسبة الإغلاق الجزئي (%)
input int PartialProfit = 30; // نقاط الربح للإغلاق الجزئي
//+------------------------------------------------------------------+
//| متغيرات عامة |
//+------------------------------------------------------------------+
int MagicNumber = 202412;
int SuccessfulTrades = 0;
int FailedTrades = 0;
double TotalProfit = 0;
datetime LastTradeTime = 0;
int CurrentSignal = 0; // 0 = لا إشارة، 1 = شراء، -1 = بيع
//+------------------------------------------------------------------+
//| دالة التهيئة |
//+------------------------------------------------------------------+
int OnInit()
{
Print("=== GOLD PRO SCALPER STARTED ===");
Print("Advanced Features: Trailing Stop + Partial Close");
Print("Max trades: ", MaxTrades, " | Trades/Hour: ", TradesPerHour);
Print("StopLoss: ", StopLoss, " | TakeProfit: ", TakeProfit);
Print("Trailing: Start ", TrailingStart, " pts, Step ", TrailingStep);
Print("Partial Close: ", (UsePartialClose ? "Yes (" + string(PartialClosePct) + "%)" : "No"));
MagicNumber = (int)(ChartID() + TimeCurrent() % 10000);
// بدء التايمر كل 3 ثواني (أسرع للتأمين)
EventSetTimer(3);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| دالة التايمر الرئيسية |
//+------------------------------------------------------------------+
void OnTimer()
{
static int timerCount = 0;
timerCount++;
// تحديث الإحصائيات كل 30 ثانية
if(timerCount % 10 == 0) // كل 30 ثانية (10 × 3)
{
DisplayInfo();
}
// 1. تحديث التأمين للصفقات المفتوحة (الأولوية)
UpdateAdvancedProtection();
// 2. التحقق من عدد الصفقات المفتوحة
int openTrades = CountOpenTrades();
if(openTrades >= MaxTrades)
{
return;
}
// 3. التحقق من الوقت بين الصفقات
if(TimeCurrent() - LastTradeTime < CalculateDynamicDelay())
{
return;
}
// 4. الحصول على إشارة تداول محسنة
CurrentSignal = GetEnhancedSignal();
if(CurrentSignal != 0)
{
// 5. فتح صفقة ذكية
if(OpenEnhancedTrade(CurrentSignal))
{
LastTradeTime = TimeCurrent();
Print("Enhanced trade opened. Next trade in ", CalculateDynamicDelay(), " seconds...");
}
}
}
//+------------------------------------------------------------------+
//| حساب تأخير ديناميكي بين الصفقات |
//+------------------------------------------------------------------+
int CalculateDynamicDelay()
{
int baseDelay = 60; // 60 ثانية أساسية
int openTrades = CountOpenTrades();
// كلما زادت الصفقات المفتوحة، زاد التأخير
if(openTrades >= 8) return 180; // 3 دقائق
if(openTrades >= 5) return 120; // 2 دقائق
if(openTrades >= 3) return 90; // 1.5 دقيقة
return baseDelay; // 1 دقيقة
}
//+------------------------------------------------------------------+
//| الحصول على إشارة محسنة |
//+------------------------------------------------------------------+
int GetEnhancedSignal()
{
// استراتيجية متقدمة للذهب
double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// 1. تحليل متعدد الأطراف الزمنية
double h1High = iHigh(_Symbol, PERIOD_H1, 1);
double h1Low = iLow(_Symbol, PERIOD_H1, 1);
double h1Close = iClose(_Symbol, PERIOD_H1, 1);
double m15High = iHigh(_Symbol, PERIOD_M15, 1);
double m15Low = iLow(_Symbol, PERIOD_M15, 1);
double m15Close = iClose(_Symbol, PERIOD_M15, 1);
double m5High = iHigh(_Symbol, PERIOD_M5, 1);
double m5Low = iLow(_Symbol, PERIOD_M5, 1);
double m5Close = iClose(_Symbol, PERIOD_M5, 1);
// 2. حساب المتوسط المتحرك البسيط
double ma20 = CalculateSMA(20, PERIOD_M15);
double ma50 = CalculateSMA(50, PERIOD_H1);
// 3. تقييم الاتجاه
bool trendUp = (currentPrice > ma20) && (ma20 > ma50);
bool trendDown = (currentPrice < ma20) && (ma20 < ma50);
// 4. حساب مستويات الدعم والمقاومة
double range = h1High - h1Low;
double strongSupport = h1Low + (range * 0.25); // دعم قوي
double weakSupport = h1Low + (range * 0.4); // دعم ضعيف
double weakResistance = h1High - (range * 0.4); // مقاومة ضعيفة
double strongResistance = h1High - (range * 0.25); // مقاومة قوية
// 5. قواعد التداول المحسنة
// إشارة شراء قوية
if(currentPrice <= strongSupport && trendUp && m15Close > m15Open())
{
Print("STRONG BUY: At strong support + uptrend");
return 1;
}
// إشارة شراء متوسطة
if(currentPrice <= weakSupport && !trendDown && m5Close > m5Open())
{
Print("MEDIUM BUY: At weak support + not downtrend");
return 1;
}
// إشارة بيع قوية
if(currentPrice >= strongResistance && trendDown && m15Close < m15Open())
{
Print("STRONG SELL: At strong resistance + downtrend");
return -1;
}
// إشارة بيع متوسطة
if(currentPrice >= weakResistance && !trendUp && m5Close < m5Open())
{
Print("MEDIUM SELL: At weak resistance + not uptrend");
return -1;
}
// 6. استراتيجية اختراق (Breakout)
if(currentPrice > h1High && volumeIncreasing())
{
Print("BREAKOUT BUY: Above previous H1 high");
return 1;
}
if(currentPrice < h1Low && volumeIncreasing())
{
Print("BREAKOUT SELL: Below previous H1 low");
return -1;
}
return 0; // لا توجد إشارة
}
//+------------------------------------------------------------------+
//| حساب المتوسط المتحرك البسيط |
//+------------------------------------------------------------------+
double CalculateSMA(int period, ENUM_TIMEFRAMES tf)
{
double sum = 0;
for(int i = 1; i <= period; i++)
{
sum += iClose(_Symbol, tf, i);
}
return sum / period;
}
//+------------------------------------------------------------------+
//| الحصول على سعر افتتاح شمعة M15 |
//+------------------------------------------------------------------+
double m15Open()
{
return iOpen(_Symbol, PERIOD_M15, 1);
}
//+------------------------------------------------------------------+
//| الحصول على سعر افتتاح شمعة M5 |
//+------------------------------------------------------------------+
double m5Open()
{
return iOpen(_Symbol, PERIOD_M5, 1);
}
//+------------------------------------------------------------------+
//| التحقق من زيادة الحجم |
//+------------------------------------------------------------------+
bool volumeIncreasing()
{
long vol1 = iVolume(_Symbol, PERIOD_M5, 1);
long vol2 = iVolume(_Symbol, PERIOD_M5, 2);
long vol3 = iVolume(_Symbol, PERIOD_M5, 3);
return (vol1 > vol2) && (vol2 > vol3);
}
//+------------------------------------------------------------------+
//| فتح صفقة محسنة |
//+------------------------------------------------------------------+
bool OpenEnhancedTrade(int signal)
{
ENUM_ORDER_TYPE type = (signal == 1) ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
double price = (type == ORDER_TYPE_BUY) ?
SymbolInfoDouble(_Symbol, SYMBOL_ASK) :
SymbolInfoDouble(_Symbol, SYMBOL_BID);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
// حساب وقف الخسارة وجني الأرباح ديناميكياً
double atr = CalculateATR();
double dynamicSL = MathMax(StopLoss * point, atr * 0.6);
double dynamicTP = MathMax(TakeProfit * point, atr * 0.4);
// التأكد من نسبة الربح/الخسارة
if((dynamicTP / dynamicSL) < MinProfitRatio)
{
dynamicTP = dynamicSL * MinProfitRatio;
}
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = LotSize;
request.type = type;
request.price = price;
request.deviation = 5;
request.magic = MagicNumber;
request.comment = (signal == 1) ? "Gold Pro Buy" : "Gold Pro Sell";
request.type_filling = ORDER_FILLING_IOC;
if(type == ORDER_TYPE_BUY)
{
request.sl = NormalizeDouble(price - dynamicSL, (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS));
request.tp = NormalizeDouble(price + dynamicTP, (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS));
}
else
{
request.sl = NormalizeDouble(price + dynamicSL, (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS));
request.tp = NormalizeDouble(price - dynamicTP, (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS));
}
// إرسال الأمر
bool sent = OrderSend(request, result);
if(sent && result.retcode == TRADE_RETCODE_DONE)
{
SuccessfulTrades++;
Print("✓ Pro ", (type == ORDER_TYPE_BUY ? "BUY" : "SELL"),
" opened #", result.order,
" at ", price,
" SL: ", request.sl,
" TP: ", request.tp,
" Points: ", NormalizeDouble((request.tp - price)/point, 0), "/",
NormalizeDouble((price - request.sl)/point, 0));
return true;
}
else
{
FailedTrades++;
Print("✗ Trade failed: Error ", GetLastError());
return false;
}
}
//+------------------------------------------------------------------+
//| تحديث التأمين المتقدم |
//+------------------------------------------------------------------+
void UpdateAdvancedProtection()
{
for(int i = PositionsTotal()-1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(PositionSelectByTicket(ticket))
{
if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double currentSL = PositionGetDouble(POSITION_SL);
double currentTP = PositionGetDouble(POSITION_TP);
double currentPrice = PositionGetDouble(POSITION_PRICE_CURRENT);
double profit = PositionGetDouble(POSITION_PROFIT);
double volume = PositionGetDouble(POSITION_VOLUME);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
// حساب الربح بالنقاط
double profitPoints = (type == POSITION_TYPE_BUY) ?
(currentPrice - openPrice) / point :
(openPrice - currentPrice) / point;
// 1. التريلنغ ستوب المحسّن
if(profitPoints > TrailingStart)
{
double newSL = currentSL;
if(type == POSITION_TYPE_BUY)
{
newSL = NormalizeDouble(currentPrice - (TrailingStep * point), (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS));
if(newSL > currentSL && newSL > openPrice)
{
ModifyStopLoss(ticket, newSL);
Print("Trailing updated: Ticket #", ticket, " New SL: ", newSL, " (Profit: ", profitPoints, " pts)");
}
}
else if(type == POSITION_TYPE_SELL)
{
newSL = NormalizeDouble(currentPrice + (TrailingStep * point), (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS));
if((currentSL == 0 || newSL < currentSL) && newSL < openPrice)
{
ModifyStopLoss(ticket, newSL);
Print("Trailing updated: Ticket #", ticket, " New SL: ", newSL, " (Profit: ", profitPoints, " pts)");
}
}
}
// 2. الإغلاق الجزئي عند تحقيق ربح معين
if(UsePartialClose && profitPoints > PartialProfit && volume > LotSize * 1.5)
{
double closeVolume = volume * (PartialClosePct / 100.0);
ClosePartialPosition(ticket, closeVolume);
Print("Partial close: Ticket #", ticket, " Closed ", closeVolume, " lots at ", profitPoints, " pts profit");
}
// 3. تحريك وقف الخسارة إلى نقطة التعادل بعد ربح جيد
if(profitPoints > (TakeProfit * 0.7)) // 70% من TP
{
double breakevenSL = openPrice;
if(type == POSITION_TYPE_BUY)
{
if(breakevenSL > currentSL)
{
ModifyStopLoss(ticket, breakevenSL);
Print("Breakeven SL: Ticket #", ticket, " SL moved to entry");
}
}
else if(type == POSITION_TYPE_SELL)
{
if(breakevenSL < currentSL || currentSL == 0)
{
ModifyStopLoss(ticket, breakevenSL);
Print("Breakeven SL: Ticket #", ticket, " SL moved to entry");
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| حساب ATR محسّن |
//+------------------------------------------------------------------+
double CalculateATR()
{
double atrValue = 0;
// حساب ATR من 3 أطراف زمنية
for(int i = 1; i <= 7; i++) // 7 فترات
{
double h1High = iHigh(_Symbol, PERIOD_H1, i);
double h1Low = iLow(_Symbol, PERIOD_H1, i);
double h1PrevClose = iClose(_Symbol, PERIOD_H1, i + 1);
double m15High = iHigh(_Symbol, PERIOD_M15, i);
double m15Low = iLow(_Symbol, PERIOD_M15, i);
double m15PrevClose = iClose(_Symbol, PERIOD_M15, i + 1);
double tr1 = MathMax(h1High - h1Low, MathMax(MathAbs(h1High - h1PrevClose), MathAbs(h1Low - h1PrevClose)));
double tr2 = MathMax(m15High - m15Low, MathMax(MathAbs(m15High - m15PrevClose), MathAbs(m15Low - m15PrevClose)));
atrValue += (tr1 + tr2) / 2;
}
return atrValue / 7;
}
//+------------------------------------------------------------------+
//| إغلاق جزئي للمركز |
//+------------------------------------------------------------------+
bool ClosePartialPosition(ulong ticket, double closeVolume)
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
if(PositionSelectByTicket(ticket))
{
ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
request.action = TRADE_ACTION_DEAL;
request.position = ticket;
request.symbol = _Symbol;
request.volume = NormalizeDouble(closeVolume, 2);
request.deviation = 5;
request.magic = MagicNumber;
request.type_filling = ORDER_FILLING_IOC;
if(type == POSITION_TYPE_BUY)
{
request.type = ORDER_TYPE_SELL;
request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
else
{
request.type = ORDER_TYPE_BUY;
request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
}
bool success = OrderSend(request, result);
if(success && result.retcode == TRADE_RETCODE_DONE)
{
return true;
}
}
return false;
}
//+------------------------------------------------------------------+
//| تعديل وقف الخسارة |
//+------------------------------------------------------------------+
bool ModifyStopLoss(ulong ticket, double newSL)
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.action = TRADE_ACTION_SLTP;
request.position = ticket;
request.symbol = _Symbol;
request.sl = newSL;
request.magic = MagicNumber;
return OrderSend(request, result);
}
//+------------------------------------------------------------------+
//| عد الصفقات المفتوحة |
//+------------------------------------------------------------------+
int CountOpenTrades()
{
int count = 0;
for(int i = 0; i < PositionsTotal(); i++)
{
ulong ticket = PositionGetTicket(i);
if(PositionSelectByTicket(ticket))
{
if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
count++;
}
}
}
return count;
}
//+------------------------------------------------------------------+
//| تحديث الإحصائيات |
//+------------------------------------------------------------------+
void UpdateStatistics()
{
double currentProfit = 0;
for(int i = 0; i < PositionsTotal(); i++)
{
ulong ticket = PositionGetTicket(i);
if(PositionSelectByTicket(ticket))
{
if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
currentProfit += PositionGetDouble(POSITION_PROFIT);
}
}
}
HistorySelect(0, TimeCurrent());
int totalDeals = HistoryDealsTotal();
double closedProfit = 0;
for(int i = 0; i < totalDeals; i++)
{
ulong ticket = HistoryDealGetTicket(i);
if(HistoryDealGetString(ticket, DEAL_SYMBOL) == _Symbol &&
HistoryDealGetInteger(ticket, DEAL_MAGIC) == MagicNumber)
{
closedProfit += HistoryDealGetDouble(ticket, DEAL_PROFIT);
}
}
TotalProfit = currentProfit + closedProfit;
}
//+------------------------------------------------------------------+
//| عرض معلومات التداول |
//+------------------------------------------------------------------+
void DisplayInfo()
{
UpdateStatistics();
int openTrades = CountOpenTrades();
Print("=== GOLD PRO SCALPER INFO ===");
Print("Time: ", TimeToString(TimeCurrent(), TIME_SECONDS));
Print("Open trades: ", openTrades, "/", MaxTrades);
Print("Signal: ", (CurrentSignal == 1 ? "BUY" : (CurrentSignal == -1 ? "SELL" : "NONE")));
Print("Successful/Failed: ", SuccessfulTrades, "/", FailedTrades);
Print("Success rate: ", (SuccessfulTrades + FailedTrades > 0 ?
NormalizeDouble((SuccessfulTrades * 100.0 / (SuccessfulTrades + FailedTrades)), 1) : 0), "%");
Print("Total P/L: $", NormalizeDouble(TotalProfit, 2));
Print("Gold price: ", SymbolInfoDouble(_Symbol, SYMBOL_BID));
Print("Active protection: Trailing + Partial Close");
Print("===========================================");
}
//+------------------------------------------------------------------+
//| دالة الإنهاء |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
EventKillTimer();
UpdateStatistics();
Print("=== GOLD PRO SCALPER FINAL RESULTS ===");
Print("Total trades: ", SuccessfulTrades + FailedTrades);
Print("Successful: ", SuccessfulTrades);
Print("Failed: ", FailedTrades);
Print("Success rate: ", (SuccessfulTrades + FailedTrades > 0 ?
NormalizeDouble((SuccessfulTrades * 100.0 / (SuccessfulTrades + FailedTrades)), 1) : 0), "%");
Print("Total profit/loss: $", NormalizeDouble(TotalProfit, 2));
Print("Max trades achieved: ", CountOpenTrades(), "/", MaxTrades);
Print("======================================");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment