指定
//+------------------------------------------------------------------+
//| 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;
}
反馈
1
等级
项目
32
34%
仲裁
0
逾期
2
6%
空闲
2
等级
项目
829
62%
仲裁
33
27%
/
45%
逾期
23
3%
空闲
发布者: 1 代码
3
等级
项目
64
20%
仲裁
11
27%
/
55%
逾期
5
8%
空闲
4
等级
项目
1
0%
仲裁
0
逾期
0
空闲
5
等级
项目
0
0%
仲裁
0
逾期
0
空闲
6
等级
项目
16
25%
仲裁
1
0%
/
0%
逾期
4
25%
工作中
7
等级
项目
333
35%
仲裁
66
12%
/
58%
逾期
87
26%
空闲
8
等级
项目
233
56%
仲裁
13
15%
/
54%
逾期
43
18%
空闲
9
等级
项目
8
0%
仲裁
2
50%
/
0%
逾期
1
13%
工作中
10
等级
项目
0
0%
仲裁
0
逾期
0
工作中
项目信息
预算
100+ USD
截止日期
从 1 到 200 天
客户
所下订单1
仲裁计数0