Breakout Trading EA

IUX Markets Bonus

ตัวอย่าง EA แบบ Breakout Trading

EA Breakout Trading
EA Breakout Trading

คำอธิบายองค์ประกอบหลักของ EA แบบ Breakout Trading:

  1. Input Parameters:
    • LookbackPeriod: จำนวนแท่งเทียนย้อนหลังที่ใช้ในการหาจุดสูงสุดและต่ำสุด
    • RiskPercent: เปอร์เซ็นต์ความเสี่ยงต่อการเทรดแต่ละครั้ง
    • StopLoss และ TakeProfit: ระยะ Stop Loss และ Take Profit ในหน่วย pips
  2. OnInit():
    • ตรวจสอบความถูกต้องของ input parameters
  3. OnTick():
    • ตรวจสอบว่าเป็นแท่งเทียนใหม่หรือไม่
    • คำนวณจุดสูงสุดและต่ำสุดใหม่
    • ตรวจสอบการเกิด breakout และเปิดออเดอร์
  4. CalculateHighLow():
    • คำนวณจุดสูงสุดและต่ำสุดในช่วง LookbackPeriod
  5. CheckBreakout():
    • ตรวจสอบว่าราคาปัจจุบันทะลุจุดสูงสุดหรือต่ำสุดหรือไม่
    • เปิดออเดอร์ Buy หรือ Sell ตามทิศทางของ breakout
  6. OpenBuyOrder() และ OpenSellOrder():
    • เปิดออเดอร์ Buy หรือ Sell พร้อมกำหนด Stop Loss และ Take Profit
  7. CalculateLotSize():
    • คำนวณขนาดล็อตตามเปอร์เซ็นต์ความเสี่ยงที่กำหนด
  8. IsOpenOrdersExist():
    • ตรวจสอบว่ามีออเดอร์ที่เปิดอยู่แล้วหรือไม่

EA นี้จะตรวจสอบการเกิด breakout โดยเปรียบเทียบราคาปัจจุบันกับจุดสูงสุดและต่ำสุดในช่วง LookbackPeriod ที่กำหนด เมื่อเกิด breakout จะเปิดออเดอร์ในทิศทางนั้นพร้อมกำหนด Stop Loss และ Take Profit

 

ตัวอย่าง Code

 



//+------------------------------------------------------------------+
//|                                        Breakout Trading EA.mq4   |
//|                        Copyright 2024, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

// Input parameters
input int    LookbackPeriod = 20;  // Number of bars to look back for high/low
input double RiskPercent    = 1.0; // Risk percent per trade
input int    StopLoss       = 50;  // Stop Loss in pips
input int    TakeProfit     = 100; // Take Profit in pips

// Global variables
double g_high, g_low;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Validate input parameters
   if(LookbackPeriod <= 0)
   {
      Print("Invalid LookbackPeriod. Must be greater than 0.");
      return INIT_PARAMETERS_INCORRECT;
   }
   
   if(RiskPercent <= 0 || RiskPercent > 100)
   {
      Print("Invalid RiskPercent. Must be between 0 and 100.");
      return INIT_PARAMETERS_INCORRECT;
   }
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // Wait for a new bar
   if(IsNewBar() == false) return;
   
   // Calculate new high and low
   CalculateHighLow();
   
   // Check for breakouts and place orders
   CheckBreakout();
}

//+------------------------------------------------------------------+
//| Check if it's a new bar                                          |
//+------------------------------------------------------------------+
bool IsNewBar()
{
   static datetime last_time = 0;
   datetime current_time = iTime(Symbol(), Period(), 0);
   
   if(current_time != last_time)
   {
      last_time = current_time;
      return true;
   }
   
   return false;
}

//+------------------------------------------------------------------+
//| Calculate high and low for the lookback period                   |
//+------------------------------------------------------------------+
void CalculateHighLow()
{
   g_high = High[iHighest(NULL, 0, MODE_HIGH, LookbackPeriod, 1)];
   g_low  = Low[iLowest(NULL, 0, MODE_LOW, LookbackPeriod, 1)];
}

//+------------------------------------------------------------------+
//| Check for breakouts and place orders                             |
//+------------------------------------------------------------------+
void CheckBreakout()
{
   double current_price = Close[0];
   
   // Check for bullish breakout
   if(current_price > g_high && !IsOpenOrdersExist())
   {
      OpenBuyOrder();
   }
   // Check for bearish breakout
   else if(current_price < g_low && !IsOpenOrdersExist())
   {
      OpenSellOrder();
   }
}

//+------------------------------------------------------------------+
//| Open a buy order                                                 |
//+------------------------------------------------------------------+
void OpenBuyOrder()
{
   double lotSize = CalculateLotSize(Ask, Ask - StopLoss * Point);
   int ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, Ask - StopLoss * Point, Ask + TakeProfit * Point, "Breakout Buy", 0, 0, Green);
   
   if(ticket < 0)
   {
      Print("OrderSend failed with error #", GetLastError());
   }
}

//+------------------------------------------------------------------+
//| Open a sell order                                                |
//+------------------------------------------------------------------+
void OpenSellOrder()
{
   double lotSize = CalculateLotSize(Bid, Bid + StopLoss * Point);
   int ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, Bid + StopLoss * Point, Bid - TakeProfit * Point, "Breakout Sell", 0, 0, Red);
   
   if(ticket < 0)
   {
      Print("OrderSend failed with error #", GetLastError());
   }
}

//+------------------------------------------------------------------+
//| Calculate lot size based on risk percentage                      |
//+------------------------------------------------------------------+
double CalculateLotSize(double entry, double stopLoss)
{
   double riskAmount = AccountBalance() * (RiskPercent / 100);
   double tickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);
   double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   
   double riskPips = MathAbs(entry - stopLoss) / Point;
   double lotSize = NormalizeDouble(riskAmount / (riskPips * tickValue / tickSize), 2);
   
   double minLot = MarketInfo(Symbol(), MODE_MINLOT);
   double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
   
   lotSize = MathMax(minLot, MathMin(maxLot, lotSize));
   return NormalizeDouble(lotSize / lotStep, 0) * lotStep;
}

//+------------------------------------------------------------------+
//| Check if there are open orders                                   |
//+------------------------------------------------------------------+
bool IsOpenOrdersExist()
{
   for(int i = 0; i < OrdersTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && (OrderType() == OP_BUY || OrderType() == OP_SELL))
         {
            return true;
         }
      }
   }
   return false;
}

 Exness Promotion
PNFPB Install PWA using share icon

For IOS and IPAD browsers, Install PWA using add to home screen in ios safari browser or add to dock option in macos safari browser