Spécifications
//+------------------------------------------------------------------+
//| Grid_Martingale_XAUUSD_M1.mq5 |
//| Copyright 2026, AI Assistant |
//| https://mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, AI Assistant"
#property link "https://mql5.com"
#property version "1.03"
#property description "Grid Martingale Optimized for XAUUSD Gold - Ready to Use"
//--- Include Trade Library
#include <Trade\Trade.mqh>
CTrade trade;
//--- Input Parameters (ปรับจูนสำหรับทองคำเซฟตี้แล้ว)
input group "--- Risk & Lot Settings ---"
input double InpStartLot = 0.01; // Initial Lot Size (ขนาดล็อตเริ่มต้น)
input double InpMultiplier = 1.5; // Lot Multiplier (ลดเหลือ 1.5 เพื่อไม่ให้ล็อตโตไวเกินไป)
input double InpTargetProfitUSD = 5.0; // Basket Target Profit (USD) (เป้าหมายกำไรปิดรวบ)
input double InpMaxLossUSD = 300.0; // Basket Stop Loss (USD) (จุดคัทลอสรวมป้องกันพอร์ตแตก)
input group "--- Grid Settings (Optimized for Gold) ---"
input int InpGridSteps = 500; // Distance between levels (500 Points = 5 USD สำหรับทอง)
input int InpMaxLevels = 6; // Maximum levels per side (จำกัดสูงสุดแค่ 6 ไม้พอกันลาก)
input ulong InpMagicNumber = 998877; // EA Magic Number
//--- Global Variables
bool gridPlaced = false;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
trade.SetExpertMagicNumber(InpMagicNumber);
gridPlaced = false;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// แกะคอมเมนต์ออกหากต้องการลบออเดอร์ทั้งหมดตอนปิดบอท
// CloseAll();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// 1. ตรวจสอบกำไรรวมของ EA ตัวนี้
double totalProfit = GetTotalProfit();
// เช็กระยะปิดทำกำไร
if(totalProfit >= InpTargetProfitUSD)
{
Print("Target Profit Reached: $", totalProfit, ". Closing all.");
CloseAll();
gridPlaced = false;
return;
}
// เช็กคัทลอสรวมป้องกันพอร์ตแตก
if(totalProfit <= -InpMaxLossUSD)
{
Print("Max Allowed Loss Reached: $", totalProfit, ". Emergency Closing all.");
CloseAll();
gridPlaced = false;
return;
}
int activePositions = CountPositions();
int pendingOrders = CountPendingOrders();
// 2. ตรวจสอบและวางกริดออเดอร์ใหม่
if(activePositions == 0 && !gridPlaced)
{
DeletePendingOrders();
double currentAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double buyLot = InpStartLot;
double sellLot = InpStartLot;
// --- วางออเดอร์ฝั่ง BUY STOP ---
for(int i = 1; i <= InpMaxLevels; i++)
{
double targetBuyPrice = currentAsk + (i * InpGridSteps * _Point);
targetBuyPrice = NormalizeDouble(targetBuyPrice, _Digits);
trade.BuyStop(buyLot, targetBuyPrice, _Symbol, 0, 0, ORDER_TIME_GTC);
buyLot = NormalizeDouble(buyLot * InpMultiplier, 2);
}
// --- วางออเดอร์ฝั่ง SELL STOP ---
for(int i = 1; i <= InpMaxLevels; i++)
{
double targetSellPrice = currentBid - (i * InpGridSteps * _Point);
targetSellPrice = NormalizeDouble(targetSellPrice, _Digits);
trade.SellStop(sellLot, targetSellPrice, _Symbol, 0, 0, ORDER_TIME_GTC);
sellLot = NormalizeDouble(sellLot * InpMultiplier, 2);
}
gridPlaced = true;
Print("Gold Grid Orders successfully placed.");
}
// ถ้าระบบเคลียร์หมดแล้ว ให้รีเซ็ตเพื่อเริ่มรอบใหม่
if(activePositions == 0 && pendingOrders == 0)
{
gridPlaced = false;
}
}
//+------------------------------------------------------------------+
//| คำนวณกำไรรวมเฉพาะโพซิชันของ EA ตัวนี้ |
//+------------------------------------------------------------------+
double GetTotalProfit()
{
double profit = 0.0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket > 0)
{
if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
{
profit += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP);
}
}
}
return profit;
}
//+------------------------------------------------------------------+
//| ปิดโพซิชันทั้งหมดและลบออเดอร์ที่ค้างอยู่ |
//+------------------------------------------------------------------+
void CloseAll()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket > 0)
{
if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
{
trade.PositionClose(ticket);
}
}
}
DeletePendingOrders();
}
//+------------------------------------------------------------------+
//| ลบเฉพาะออเดอร์ Pending Orders ที่ค้างอยู่ |
//+------------------------------------------------------------------+
void DeletePendingOrders()
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
ulong ticket = OrderGetTicket(i);
if(ticket > 0)
{
if(OrderGetString(ORDER_SYMBOL) == _Symbol && OrderGetInteger(ORDER_MAGIC) == InpMagicNumber)
{
trade.OrderDelete(ticket);
}
}
}
}
//+------------------------------------------------------------------+
//| นับจำนวนโพซิชันที่เปิดทำงานอยู่จริงของบอทตัวนี้ |
//+------------------------------------------------------------------+
int CountPositions()
{
int count = 0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket > 0 && PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
{
count++;
}
}
return count;
}
//+------------------------------------------------------------------+
//| นับจำนวนออเดอร์ Pending ที่ค้างอยู่จริงของบอทตัวนี้ |
//+------------------------------------------------------------------+
int CountPendingOrders()
{
int count = 0;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
ulong ticket = OrderGetTicket(i);
if(ticket > 0 && OrderGetString(ORDER_SYMBOL) == _Symbol && OrderGetInteger(ORDER_MAGIC) == InpMagicNumber)
{
count++;
}
}
return count;
}
Répondu
1
Évaluation
Projets
32
34%
Arbitrage
0
En retard
2
6%
Gratuit
2
Évaluation
Projets
829
62%
Arbitrage
33
27%
/
45%
En retard
23
3%
Gratuit
Publié : 1 code
3
Évaluation
Projets
64
20%
Arbitrage
11
27%
/
55%
En retard
5
8%
Gratuit
4
Évaluation
Projets
1
0%
Arbitrage
0
En retard
0
Gratuit
5
Évaluation
Projets
0
0%
Arbitrage
0
En retard
0
Gratuit
6
Évaluation
Projets
16
25%
Arbitrage
1
0%
/
0%
En retard
4
25%
Travail
7
Évaluation
Projets
333
35%
Arbitrage
66
12%
/
58%
En retard
87
26%
Gratuit
8
Évaluation
Projets
233
56%
Arbitrage
13
15%
/
54%
En retard
43
18%
Gratuit
Informations sur le projet
Budget
100+ USD
Délais
de 1 à 200 jour(s)
Client
Commandes passées1
Nombre d'arbitrages0