Fat01

MQL5 その他

指定

//+------------------------------------------------------------------+
//|                                   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
開発者 1
評価
(26)
プロジェクト
32
34%
仲裁
0
期限切れ
2
6%
2
開発者 2
評価
(545)
プロジェクト
829
62%
仲裁
33
27% / 45%
期限切れ
23
3%
パブリッシュした人: 1 code
3
開発者 3
評価
(50)
プロジェクト
64
20%
仲裁
11
27% / 55%
期限切れ
5
8%
4
開発者 4
評価
(1)
プロジェクト
1
0%
仲裁
0
期限切れ
0
5
開発者 5
評価
プロジェクト
0
0%
仲裁
0
期限切れ
0
6
開発者 6
評価
(12)
プロジェクト
16
25%
仲裁
1
0% / 0%
期限切れ
4
25%
仕事中
7
開発者 7
評価
(206)
プロジェクト
333
35%
仲裁
66
12% / 58%
期限切れ
87
26%
8
開発者 8
評価
(190)
プロジェクト
233
56%
仲裁
13
15% / 54%
期限切れ
43
18%
9
開発者 9
評価
(8)
プロジェクト
8
0%
仲裁
2
50% / 0%
期限切れ
1
13%
仕事中
10
開発者 10
評価
プロジェクト
0
0%
仲裁
0
期限切れ
0
仕事中

プロジェクト情報

予算
100+ USD
締め切り
最低 1 最高 200 日

依頼者

出された注文1
裁定取引数0