MQL5 Review — คู่มือเทรด Forex ฉบับสมบูรณ์ 2026
MQL5 Trading Platform
MQL5 MetaTrader 5 Expert Advisor Indicator Backtesting Algorithmic Trading Forex CFD Risk Management Strategy Tester Optimization
| Feature | MQL5 (MT5) | MQL4 (MT4) | Pine Script (TV) |
|---|---|---|---|
| Language | C++ like OOP | C like Procedural | Custom Scripting |
| Markets | Forex CFD Futures Stocks Crypto | Forex CFD | All (View only) |
| Backtesting | Multi-currency Real Ticks | Single Currency | Basic |
| Optimization | Genetic + Cloud Network | Basic | ไม่มี |
| Order Types | Market Limit Stop Stop-Limit | Market Limit Stop | Market Limit |
| Community | MQL5.com Market Signals | MQL5.com | TradingView Community |
Expert Advisor Development
# === MQL5 Expert Advisor Structure ===
# //+------------------------------------------------------------------+
# //| Expert Advisor - Moving Average Crossover |
# //+------------------------------------------------------------------+
# input int FastMA_Period = 20;
# input int SlowMA_Period = 50;
# input double LotSize = 0.1;
# input int StopLoss_Pips = 50;
# input int TakeProfit_Pips = 100;
# input int MagicNumber = 12345;
#
# #include
# CTrade trade;
#
# int fastMA_Handle, slowMA_Handle;
# double fastMA[], slowMA[];
#
# int OnInit() {
# trade.SetExpertMagicNumber(MagicNumber);
# fastMA_Handle = iMA(_Symbol, PERIOD_CURRENT, FastMA_Period, 0, MODE_EMA, PRICE_CLOSE);
# slowMA_Handle = iMA(_Symbol, PERIOD_CURRENT, SlowMA_Period, 0, MODE_EMA, PRICE_CLOSE);
# if(fastMA_Handle == INVALID_HANDLE || slowMA_Handle == INVALID_HANDLE) return(INIT_FAILED);
# ArraySetAsSeries(fastMA, true);
# ArraySetAsSeries(slowMA, true);
# return(INIT_SUCCEEDED);
# }
#
# void OnTick() {
# CopyBuffer(fastMA_Handle, 0, 0, 3, fastMA);
# CopyBuffer(slowMA_Handle, 0, 0, 3, slowMA);
#
# // Golden Cross: Fast MA crosses above Slow MA
# if(fastMA[1] > slowMA[1] && fastMA[2] <= slowMA[2]) {
# if(PositionsTotal() == 0) {
# double sl = SymbolInfoDouble(_Symbol, SYMBOL_BID) - StopLoss_Pips * _Point * 10;
# double tp = SymbolInfoDouble(_Symbol, SYMBOL_BID) + TakeProfit_Pips * _Point * 10;
# trade.Buy(LotSize, _Symbol, 0, sl, tp, "MA Cross Buy");
# }
# }
# // Death Cross: Fast MA crosses below Slow MA
# if(fastMA[1] < slowMA[1] && fastMA[2] >= slowMA[2]) {
# if(PositionsTotal() == 0) {
# double sl = SymbolInfoDouble(_Symbol, SYMBOL_ASK) + StopLoss_Pips * _Point * 10;
# double tp = SymbolInfoDouble(_Symbol, SYMBOL_ASK) - TakeProfit_Pips * _Point * 10;
# trade.Sell(LotSize, _Symbol, 0, sl, tp, "MA Cross Sell");
# }
# }
# }
from dataclasses import dataclass
@dataclass
class EAComponent:
component: str
function: str
purpose: str
example: str
components = [
EAComponent("OnInit()",
"Initialization",
"ตั้งค่า Indicator Handle Parameters",
"iMA() iRSI() ArraySetAsSeries()"),
EAComponent("OnTick()",
"Every Price Tick",
"ตรวจ Entry/Exit Signal Open/Close Order",
"CopyBuffer() trade.Buy() trade.Sell()"),
EAComponent("OnDeinit()",
"Cleanup",
"ปิด Handle ทำความสะอาด",
"IndicatorRelease() Comment('')"),
EAComponent("CTrade",
"Order Management",
"Open Close Modify Position",
"trade.Buy() trade.Sell() trade.PositionClose()"),
EAComponent("Risk Management",
"Position Sizing",
"คำนวณ Lot จาก Risk %",
"AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent"),
]
print("=== EA Components ===")
for c in components:
print(f" [{c.component}] {c.function}")
print(f" Purpose: {c.purpose}")
print(f" Example: {c.example}")
Backtesting & Optimization
# === Backtesting Best Practices ===
@dataclass
class BacktestSetting:
setting: str
recommended: str
reason: str
pitfall: str
settings = [
BacktestSetting("Tick Mode",
"Every Tick Based on Real Ticks",
"แม่นยำที่สุด ใช้ Tick Data จริงจาก Broker",
"Open Prices Only เร็วแต่ไม่แม่นสำหรับ Scalping"),
BacktestSetting("Date Range",
"อย่างน้อย 3-5 ปี ครอบคลุม Market Conditions",
"ต้องเจอ Trending Ranging Volatile Calm",
"ทดสอบแค่ 1 ปี อาจ Curve Fit กับ Market Phase"),
BacktestSetting("Spread",
"ใช้ Current Spread หรือ Custom (เพิ่ม 20-50%)",
"Spread จริงผันผวน ไม่คงที่",
"Fixed Spread ต่ำเกินจริง ผลลัพธ์ดีเกิน"),
BacktestSetting("Commission",
"ตั้งตาม Broker จริง (เช่น $7/lot round trip)",
"Commission กิน Profit มากสำหรับ Scalping",
"ไม่ตั้ง Commission ทำให้ผลดีเกินจริง"),
BacktestSetting("Initial Deposit",
"เท่ากับที่จะเทรดจริง",
"Lot Size Margin ต้องสมจริง",
"Deposit สูงเกิน ทำให้ไม่เจอ Margin Call"),
BacktestSetting("Optimization",
"Genetic Algorithm + Walk-forward",
"หา Parameter ดี + ทดสอบ Out-of-sample",
"Full Optimization = Over-fitting สูง"),
]
print("=== Backtest Settings ===")
for s in settings:
print(f" [{s.setting}] → {s.recommended}")
print(f" Reason: {s.reason}")
print(f" Pitfall: {s.pitfall}")
Risk Management
# === Risk Management Rules ===
@dataclass
class RiskRule:
rule: str
parameter: str
implementation: str
mql5_code: str
rules = [
RiskRule("Risk per Trade",
"1-2% of Balance",
"คำนวณ Lot Size จาก SL Distance + Risk %",
"LotSize = (Balance * 0.01) / (SL_Pips * PipValue)"),
RiskRule("Max Daily Loss",
"5% of Balance",
"หยุดเทรดเมื่อ Daily Loss > 5%",
"if(DailyLoss > Balance*0.05) StopTrading=true"),
RiskRule("Max Drawdown",
"20% of Peak Equity",
"หยุด EA เมื่อ Drawdown > 20%",
"if(Drawdown > 0.20) ExpertRemove()"),
RiskRule("Max Open Positions",
"3-5 positions",
"จำกัดจำนวน Position พร้อมกัน",
"if(PositionsTotal() >= MaxPositions) return"),
RiskRule("Trailing Stop",
"ย้าย SL ตามราคาเมื่อกำไร",
"เมื่อกำไร > X pips ย้าย SL ตาม",
"trade.PositionModify(ticket, newSL, tp)"),
RiskRule("News Filter",
"หยุดเทรดช่วง High Impact News",
"ตรวจ Economic Calendar ก่อนเทรด",
"MqlCalendarEvent ตรวจ News 30 นาทีก่อน-หลัง"),
]
print("=== Risk Rules ===")
for r in rules:
print(f" [{r.rule}] {r.parameter}")
print(f" Impl: {r.implementation}")
print(f" Code: {r.mql5_code}")
เคล็ดลับ
- Backtest: ทดสอบ 5 ปี+ ด้วย Real Ticks ก่อน Live
- Risk: ไม่เกิน 1-2% ต่อ Trade Max Drawdown 20%
- Walk-forward: ใช้ Walk-forward ป้องกัน Over-fitting
- Demo: ทดสอบ Demo 3 เดือนก่อน Live
- VPS: ใช้ VPS สำหรับ EA ป้องกัน Internet ขาด
MQL5 คืออะไร
MetaQuotes Language 5 Expert Advisor Indicator MetaTrader 5 C++ Syntax OOP Forex CFD Futures Stocks MQL5 Market Community MetaEditor