trade
Tradebuy MQL5 — คู่มือเทรด Forex ฉบับสมบูรณ์ 2026
MQL5 Trading

MQL5 MetaTrader 5 Expert Advisor Indicator Signal Provider Forex Automated Trading Backtest Strategy Tester
เนื้อหาเกี่ยวข้อง — อ่านต่อ: Bus RAM คืออะไร — ข้อมูลครบถ้วน 2026
| ประเภท | ชื่อ | หน้าที่ | ตัวอย่าง |
|---|---|---|---|
| Expert Advisor | EA | เทรดอัตโนมัติ เปิด-ปิด Order | MA Crossover EA, RSI EA |
| Indicator | Custom Indicator | แสดงข้อมูลบนกราฟ | Custom MA, Volume Profile |
| Script | Script | ทำงานครั้งเดียว | Close All Orders, Draw Lines |
| Signal | Signal Provider | Copy Trade อัตโนมัติ | MQL5.com Signal Service |
| Library | Library | Function ใช้ซ้ำ | Risk Calculator, Trade Manager |
เขียน Expert Advisor
// === Simple MA Crossover EA ===
// MQL5 Expert Advisor for MetaTrader 5
// #property copyright "SiamCafe"
// #property version "1.00"
//
// #include <Trade\Trade.mqh>
//
// input int FastMA_Period = 10;
// input int SlowMA_Period = 50;
// input double LotSize = 0.01;
// input int StopLoss = 100; // points
// input int TakeProfit = 200; // points
// input ENUM_MA_METHOD MA_Method = MODE_SMA;
//
// CTrade trade;
// int fastMA_handle, slowMA_handle;
//
// int OnInit() {
// fastMA_handle = iMA(_Symbol, PERIOD_H1, FastMA_Period, 0, MA_Method, PRICE_CLOSE);
// slowMA_handle = iMA(_Symbol, PERIOD_H1, SlowMA_Period, 0, MA_Method, PRICE_CLOSE);
// if(fastMA_handle == INVALID_HANDLE || slowMA_handle == INVALID_HANDLE) {
// Print("Error creating MA handles");
// return(INIT_FAILED);
// }
// return(INIT_SUCCEEDED);
// }
//
// void OnTick() {
// double fastMA[], slowMA[];
// ArraySetAsSeries(fastMA, true);
// ArraySetAsSeries(slowMA, true);
// CopyBuffer(fastMA_handle, 0, 0, 3, fastMA);
// CopyBuffer(slowMA_handle, 0, 0, 3, slowMA);
//
// // Check for crossover
// bool buySignal = fastMA[1] > slowMA[1] && fastMA[2] <= slowMA[2];
// bool sellSignal = fastMA[1] < slowMA[1] && fastMA[2] >= slowMA[2];
//
// // Check existing positions
// if(PositionSelect(_Symbol)) {
// long posType = PositionGetInteger(POSITION_TYPE);
// if(posType == POSITION_TYPE_BUY && sellSignal)
// trade.PositionClose(_Symbol);
// if(posType == POSITION_TYPE_SELL && buySignal)
// trade.PositionClose(_Symbol);
// }
//
// // Open new position
// if(!PositionSelect(_Symbol)) {
// double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
//
// if(buySignal) {
// double sl = ask - StopLoss * point;
// double tp = ask + TakeProfit * point;
// trade.Buy(LotSize, _Symbol, ask, sl, tp, "MA Cross Buy");
// }
// if(sellSignal) {
// double sl = bid + StopLoss * point;
// double tp = bid - TakeProfit * point;
// trade.Sell(LotSize, _Symbol, bid, sl, tp, "MA Cross Sell");
// }
// }
// }
//
// void OnDeinit(const int reason) {
// IndicatorRelease(fastMA_handle);
// IndicatorRelease(slowMA_handle);
// }
from dataclasses import dataclass
@dataclass
class EAComponent:
component: str
function: str
description: str
example: str
components = [
EAComponent("OnInit()",
"เริ่มต้น EA สร้าง Indicator Handle ตั้งค่าเริ่มต้น",
"เรียกครั้งเดียวเมื่อ EA เริ่มทำงาน",
"สร้าง MA handle, ตั้ง Trade object"),
EAComponent("OnTick()",
"ทำงานทุก Tick ตรวจสอบ Signal เปิด-ปิด Order",
"เรียกทุกครั้งที่ราคาเปลี่ยน",
"ตรวจ MA Cross, เปิด Buy/Sell, จัดการ Position"),
EAComponent("OnDeinit()",
"ปิด EA คืน Resource ปล่อย Indicator Handle",
"เรียกเมื่อ EA ถูกปิดหรือ Chart ปิด",
"IndicatorRelease(), Print summary"),
EAComponent("CTrade",
"Class สำหรับเปิด-ปิด Order ตั้ง SL TP",
"Include <Trade\\Trade.mqh>",
"trade.Buy(), trade.Sell(), trade.PositionClose()"),
EAComponent("CopyBuffer()",
"ดึงค่า Indicator จาก Handle",
"ดึง MA RSI MACD ค่าย้อนหลัง",
"CopyBuffer(ma_handle, 0, 0, 3, maBuffer)"),
]
print("=== EA Components ===")
for c in components:
print(f" [{c.component}] {c.function}")
print(f" {c.description}")
print(f" Example: {c.example}")
Risk Management

// === Risk Management ===
// double CalculateLotSize(double riskPercent, int slPoints) {
// double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
// double riskAmount = accountBalance * riskPercent / 100.0;
// double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
// double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
// double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
// double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
// double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
// double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
//
// double slValue = slPoints * point;
// double lots = riskAmount / (slValue / tickSize * tickValue);
// lots = MathFloor(lots / lotStep) * lotStep;
// lots = MathMax(minLot, MathMin(maxLot, lots));
// return lots;
// }
@dataclass
class RiskRule:
rule: str
setting: str
why: str
implementation: str
rules = [
RiskRule("Risk per Trade",
"1-2% ของ Account Balance",
"ขาดทุน 10 ครั้งติดยังเหลือ 80%+ ของทุน",
"CalculateLotSize(riskPercent, slPoints)"),
RiskRule("Stop Loss",
"ทุก Trade ต้องมี SL เสมอ",
"จำกัด Loss ต่อ Trade ไม่ให้เกิน Risk ที่ตั้ง",
"trade.Buy(lots, sym, ask, sl, tp)"),
RiskRule("Max Drawdown",
"ปิด EA เมื่อ Drawdown > 20%",
"ป้องกัน Account ล่มเมื่อ Strategy ไม่ทำงาน",
"ตรวจ AccountInfoDouble(ACCOUNT_EQUITY) ทุก Tick"),
RiskRule("Max Open Positions",
"จำกัดจำนวน Position เปิดพร้อมกัน 3-5",
"ลด Risk จากการเปิดมากเกินไป",
"PositionsTotal() < MaxPositions ก่อนเปิดใหม่"),
RiskRule("Trading Hours",
"เทรดเฉพาะช่วง London + New York Session",
"หลีกเลี่ยง Low Liquidity ช่วง Asian Session",
"ตรวจ TimeCurrent() ก่อน Trade"),
]
print("=== Risk Management ===")
for r in rules:
print(f" [{r.rule}] Setting: {r.setting}")
print(f" Why: {r.why}")
print(f" Code: {r.implementation}")
Backtesting
# === Strategy Tester Guide ===
@dataclass
class BacktestStep:
step: int
action: str
setting: str
what_to_check: str
steps = [
BacktestStep(1, "เลือก EA และ Symbol",
"เลือก EA ที่เขียน เลือกคู่เงิน เช่น EURUSD",
"EA Compile สำเร็จ Symbol มีข้อมูลย้อนหลัง"),
BacktestStep(2, "ตั้งค่า Period",
"ทดสอบ 2-5 ปีย้อนหลัง ใช้ Every tick based on real ticks",
"ข้อมูลครอบคลุม ทุกสภาวะตลาด Trend Sideways"),
BacktestStep(3, "รัน Backtest",
"กด Start รอผลลัพธ์ ดู Results Graph",
"Profit Factor > 1.5, Max Drawdown < 20%, Win Rate"),
BacktestStep(4, "Optimization",
"ใช้ Genetic Algorithm หา Parameter ที่ดีที่สุด",
"ไม่ Over-optimize ดู Out-of-sample test"),
BacktestStep(5, "Forward Test",
"ใช้ 70% data Backtest 30% Forward Test",
"Forward Test ผลลัพธ์ใกล้เคียง Backtest"),
BacktestStep(6, "Demo Account",
"รัน EA บน Demo Account 1-3 เดือน",
"ผลจริงใกล้เคียง Backtest Slippage Spread จริง"),
BacktestStep(7, "Live Trading",
"เริ่ม Live ด้วย Lot เล็กที่สุด ใช้ VPS",
"Monitor ทุกวัน ปิดถ้า Drawdown เกินที่ตั้ง"),
]
print("=== Backtest Steps ===")
for s in steps:
print(f" Step {s.step}: {s.action}")
print(f" Setting: {s.setting}")
print(f" Check: {s.what_to_check}")
เคล็ดลับ
- Risk: Risk ต่อ Trade ไม่เกิน 2% ของทุน ตั้ง SL ทุก Trade เสมอ
- Backtest: ทดสอบ 2-5 ปีก่อนใช้จริง อย่า Over-optimize
- VPS: ใช้ VPS รัน EA 24 ชั่วโมง เลือก VPS ใกล้ Broker Server
- Demo: รัน Demo 1-3 เดือนก่อน Live เสมอ
- Monitor: ตรวจสอบ EA ทุกวัน ปิดถ้า Drawdown เกินที่ตั้งไว้
MQL5 คืออะไร
ภาษาโปรแกรม MetaTrader 5 Expert Advisor Indicator Script C++ Market Data MetaEditor Strategy Tester MQL5 Market Signal Copy Trade
แนะนำเพิ่มเติม — สัญญาณเทรดรายวัน XM Signal
เนื้อหาเกี่ยวข้อง — ทำความเข้าใจ Passkeys WebAuthn Real-time Processing
เนื้อหาเกี่ยวข้อง — Uptime Kuma Monitoring Feature Flag Management
ทดลองเทรดฟรี XM — โบรกที่ อ.บอม ใช้เทรดจริง (พาร์ทเนอร์ XM)





