การทดสอบย้อนหลัง (Backtesting) และ Strategy Tester ของ MT5Backtesting and the MT5 Strategy Tester
วิธีทดสอบกฎการเทรดกับข้อมูลในอดีตให้ได้ผลที่เชื่อถือได้ ทั้งแบบมือและด้วย Strategy Tester ของ MT5 พร้อมกับดักที่ทำให้ผลสวยเกินจริงHow to test trading rules on past data in a way you can trust, by hand and with the MT5 Strategy Tester, plus the traps that make results look better than they are.

ในบทความนี้ · 7
In this article · 7
การทดสอบย้อนหลัง (Backtesting) คือการเอากฎการเทรดที่เขียนไว้ชัดเจน ไปลองใช้กับกราฟในอดีต แล้วจดว่าถ้าทำตามกฎจริง ๆ จะได้ผลอย่างไร มันไม่ได้บอกว่าอนาคตจะเป็นอย่างไร แต่ช่วยตัดไอเดียที่ไม่เคยได้ผลแม้แต่ในอดีตออกไปก่อนที่จะเสียเงินจริง และช่วยให้เรารู้ว่าระบบนี้ "หน้าตาปกติ" เป็นอย่างไร เช่น แพ้ติดกันได้กี่ไม้ หรือขาดทุนสะสมลึกแค่ไหน
Backtest คืออะไร และทำอะไรได้บ้าง
แก่นของการ backtest คือคำถามเดียว: ถ้าฉันทำตามกฎนี้แบบไม่มีข้อยกเว้นในช่วงเวลาที่ผ่านมา ผลจะออกมาอย่างไร คำตอบที่ได้มีประโยชน์สามอย่าง
- คัดกรองไอเดีย ถ้ากฎขาดทุนในข้อมูลหลายปี ก็ไม่มีเหตุผลที่จะเอาไปเสี่ยงต่อ
- รู้จักนิสัยของระบบ จำนวนไม้ต่อเดือน อัตราชนะ (Win rate) ระยะขาดทุนสะสมสูงสุด (Max drawdown) ช่วงแพ้ติดกันยาวที่สุด ตัวเลขเหล่านี้ทำให้เราไม่ตกใจเวลาเจอของจริง
- ฝึกตา การไล่ดูกราฟทีละแท่งเป็นร้อยไม้ทำให้เห็นรูปแบบราคาได้เร็วกว่าการนั่งรอในตลาดจริง
สิ่งที่ backtest ทำไม่ได้คือรับประกันอนาคต ตลาดเปลี่ยนนิสัยได้ และข้อมูลในอดีตไม่มีความรู้สึกกลัวหรือโลภของเราอยู่ในนั้น
ก่อนเริ่ม: กฎต้องชัดพอให้คนอื่นทำตามได้
ถ้ากฎยังมีคำว่า "ดูทรงแล้วสวย" หรือ "แล้วแต่สถานการณ์" ผล backtest จะไม่มีความหมาย เพราะเราจะเลือกเข้าเฉพาะไม้ที่รู้แล้วว่าชนะโดยไม่ตั้งใจ ให้เขียนให้ครบทุกข้อ
- ตลาดและกรอบเวลา (Timeframe) เช่น XAUUSD กราฟ H1
- เงื่อนไขเข้าไม้แบบตรวจได้ เช่น "แท่งปิดเหนือเส้นค่าเฉลี่ย 50 แท่ง และ RSI ต่ำกว่า 70"
- จุดตัดขาดทุน (Stop loss) และจุดทำกำไร (Take profit) เป็นตัวเลขหรือสูตร
- ขนาดไม้ (Position size) เช่น เสี่ยง 1% ของพอร์ตต่อไม้
- ช่วงเวลาที่ห้ามเทรด เช่น ก่อนข่าวแรง
- ต้นทุน: สเปรด (Spread) ค่าคอมมิชชัน และค่าสว็อป (Swap)
Backtest ด้วยมือ
เหมาะกับระบบที่ใช้ดุลพินิจบางส่วน หรือคนที่ยังเขียนโค้ดไม่เป็น
- เปิดกราฟย้อนไปอย่างน้อยหลายเดือน (ยิ่งหลายปียิ่งดี) แล้วเลื่อนทีละแท่งไปข้างหน้า อย่ามองแท่งทางขวาก่อน
- ทุกครั้งที่เงื่อนไขครบ จดลงตาราง: วันที่ ทิศทาง ราคาเข้า SL TP ผลลัพธ์เป็น R (หน่วยความเสี่ยง)
- ใส่ต้นทุนสเปรดทุกไม้ แม้จะดูเล็กน้อย
- เก็บให้ได้อย่างน้อย 100 ไม้ก่อนสรุปอะไร จำนวนไม้น้อยเกินไปทำให้ผลขึ้นกับโชคมาก
ข้อดีคือเห็นบริบทจริง ข้อเสียคือช้าและเอนเอียงง่าย เพราะสมองเรา "เห็น" สัญญาณชัดขึ้นเมื่อรู้ว่าราคาไปทางไหนต่อ
Strategy Tester ของ MT5 แบบพื้นฐาน
ถ้ากฎเขียนเป็นโปรแกรม (Expert Advisor หรือ EA) ได้ MT5 มีเครื่องมือ Strategy Tester ในตัว (เปิดจากเมนู View หรือกด Ctrl+R) ค่าที่ต้องเข้าใจมีดังนี้
| ค่าที่ตั้ง | ความหมาย | ข้อแนะนำ |
|---|---|---|
| Modelling | วิธีจำลองราคา | ใช้ Every tick based on real ticks ถ้าโบรกมีข้อมูล ส่วน Open prices only เร็วแต่หยาบ |
| Spread | สเปรดที่ใช้ทดสอบ | อย่าใช้ค่าต่ำกว่าที่เจอจริงในช่วงเวลาที่จะเทรด |
| Delays | จำลองความหน่วงของคำสั่ง | ลองใส่ดีเลย์ดูว่าผลเปลี่ยนมากไหม |
| Deposit / Leverage | ทุนเริ่มต้นและเลเวอเรจ | ตั้งให้ใกล้บัญชีจริงที่จะใช้ |
| Forward | กันข้อมูลช่วงท้ายไว้ทดสอบแยก | ใช้คู่กับการปรับค่า (Optimization) เสมอ |
หลังทดสอบเสร็จ แท็บ Backtest จะสรุปตัวเลขสำคัญ เช่น Profit Factor, Expected Payoff, Maximum drawdown และกราฟเส้นทุน (Equity curve) ส่วนโหมด Visualization ช่วยให้ดูว่า EA เข้าออกตรงกับกฎที่ตั้งใจไว้จริงหรือไม่ ซึ่งควรเช็คทุกครั้งก่อนเชื่อตัวเลข
ถ้าเขียนโค้ดไม่เป็น สามารถให้ AI ช่วยร่าง EA จากกฎที่เขียนไว้ได้ ดูตัวอย่างคำสั่งได้ที่ หน้ารวม prompt แต่ต้องตรวจในโหมด Visualization ว่า EA ทำตามกฎจริงเสมอ
สมมติ backtest ทองคำ (XAUUSD) ขนาด 0.10 ล็อต ได้ 100 ไม้ ชนะ 40 ไม้ เฉลี่ยไม้ละ +$30 แพ้ 60 ไม้ เฉลี่ยไม้ละ −$15 กำไรรวม = 40 × 30 = $1,200 ขาดทุนรวม = 60 × 15 = $900 สุทธิ = +$300 แต่ทดสอบโดยตั้งสเปรดเป็นศูนย์ ถ้าสเปรดจริงประมาณ $0.30 ต่อออนซ์ ที่ 0.10 ล็อต (10 ออนซ์) ต้นทุนคือ 0.30 × 10 = $3 ต่อไม้ 100 ไม้ = $300 ผลสุทธิจริงจึงเหลือ $300 − $300 = $0 ระบบที่ดูมีกำไร กลายเป็นแค่เท่าทุน
ดูภาพด้านล่าง
จุดอ่อน / ข้อผิดพลาดที่พบบ่อย
- มองอนาคต (Look-ahead bias) ใช้ข้อมูลที่ยังไม่เกิดตอนเข้าไม้ เช่น ใช้ราคาปิดของแท่งที่ยังไม่ปิด หรือใช้อินดิเคเตอร์ที่วาดใหม่ย้อนหลัง (Repaint)
- ต้นทุนต่ำกว่าจริง สเปรดคงที่ต่ำ ๆ ไม่ใส่ค่าคอมมิชชัน ไม่คิดสว็อปของไม้ที่ถือข้ามคืน
- ข้อมูลไม่ดี ข้อมูลมีช่องว่าง หรือโหมดจำลองหยาบเกินไปสำหรับระบบที่ SL/TP แคบมาก
- จำนวนไม้น้อย 20–30 ไม้สรุปอะไรแทบไม่ได้
- ปรับค่าจนพอดีอดีต (Overfitting) ลองค่าเป็นพัน ๆ ชุดแล้วเลือกชุดที่สวยที่สุด ดูรายละเอียดที่ บทความ Overfitting
- ทดสอบช่วงตลาดแบบเดียว เช่น ทดสอบแต่ช่วงขาขึ้นยาว ระบบตามเทรนด์จะดูเก่งเกินจริง
- เชื่อตัวเลขก่อนดูไม้จริง ไม่เปิดดูว่าเข้าออกตรงกฎหรือเปล่า
ใช้บนมือถือ
Strategy Tester ไม่มีในแอป MT4/MT5 บนมือถือ แต่ backtest ด้วยมือบนมือถือทำได้: เลื่อนกราฟย้อนไป ซูมให้เห็นทีละแท่ง แล้วจดผลลงสเปรดชีตในโทรศัพท์ จอเล็กทำให้พลาดง่าย จึงควรจำกัดกฎให้เรียบง่ายและใช้เครื่องคอมพิวเตอร์เมื่อจะทดสอบจริงจัง
สรุป
- Backtest คือการทำตามกฎแบบเคร่งครัดกับข้อมูลอดีต เพื่อคัดกรองไอเดียและรู้จักนิสัยของระบบ
- กฎต้องชัดพอที่คนอื่นทำตามแล้วได้ผลเหมือนกัน
- ใส่ต้นทุนจริงเสมอ ต้นทุนเล็ก ๆ คูณจำนวนไม้อาจกินกำไรหมด
- เก็บตัวอย่างอย่างน้อยประมาณ 100 ไม้ และทดสอบหลายสภาพตลาด
- ผลดีในอดีตเป็นแค่ด่านแรก ต้องตามด้วย Forward test เสมอ
Backtesting means taking a clearly written set of trading rules, applying them to past charts, and recording what would have happened had you followed them exactly. It cannot tell you the future. What it can do is throw out ideas that never worked even in hindsight, before they cost real money, and show you what "normal" looks like for a system: how many losses in a row it takes, and how deep its drawdowns go.
What a backtest is and what it is for
Every backtest answers one question: if I had followed these rules with no exceptions over this period, what would the result be? The answer is useful in three ways.
- Filtering ideas. If the rules lose money over several years of data, there is no reason to risk capital on them.
- Knowing the system's personality. Trades per month, win rate, maximum drawdown, longest losing streak. Knowing these numbers keeps you calm when the same things happen live.
- Training your eye. Stepping through hundreds of setups bar by bar teaches pattern recognition faster than waiting for them in real time.
What a backtest cannot do is guarantee anything. Markets change character, and historical data contains none of your fear or greed.
Before you start: rules someone else could follow
If your rules still say "looks good" or "depends on the situation", the backtest is meaningless, because you will unconsciously pick the trades you already know worked. Write down every part:
- Market and timeframe, e.g. XAUUSD on H1.
- Entry conditions you can check, e.g. "candle closes above the 50-period moving average and RSI is below 70".
- Stop loss and take profit as numbers or formulas.
- Position size, e.g. 1% of the account at risk per trade.
- When not to trade, e.g. just before high-impact news.
- Costs: spread, commission and swap.
Backtesting by hand
This suits discretionary elements or traders who do not code.
- Scroll back at least several months (years is better) and step forward one bar at a time. Do not peek at the bars to the right.
- Each time the conditions are met, log it: date, direction, entry, SL, TP, result in R (units of risk).
- Deduct spread on every trade, even if it looks tiny.
- Collect at least 100 trades before concluding anything. Small samples are mostly luck.
The upside is that you see real context. The downside is that it is slow and easy to bias, because signals look much clearer when you already know where price went.
MT5 Strategy Tester basics
If the rules can be coded as an Expert Advisor (EA), MT5 has a built-in Strategy Tester (View menu, or Ctrl+R). The settings that matter most:
| Setting | Meaning | Advice |
|---|---|---|
| Modelling | How prices are simulated | Use Every tick based on real ticks when your broker provides it; Open prices only is fast but crude |
| Spread | Spread used in the test | Never lower than what you see in the hours you will trade |
| Delays | Simulated execution delay | Add delay and see whether results change a lot |
| Deposit / Leverage | Starting capital and leverage | Match the account you will actually use |
| Forward | Holds back the last part of data | Always use it together with optimization |
When the run finishes, the Backtest tab summarises Profit Factor, Expected Payoff, Maximum drawdown and the equity curve. Visualization mode lets you watch the EA trade, so you can confirm it enters and exits the way you intended. Check this every time before you trust the numbers.
If you do not code, an AI assistant can draft an EA from your written rules; see the prompt library for examples. Always verify in Visualization mode that the EA really follows your rules.
Suppose a gold (XAUUSD) backtest at 0.10 lots produces 100 trades: 40 winners averaging +$30 and 60 losers averaging −$15. Gross profit = 40 × 30 = $1,200. Gross loss = 60 × 15 = $900. Net = +$300. But the test used zero spread. If the real spread is about $0.30 per ounce, at 0.10 lots (10 oz) the cost is 0.30 × 10 = $3 per trade. Over 100 trades that is $300, so the real net result is $300 − $300 = $0. The "profitable" system only breaks even.
See the charts below.
Weaknesses / common mistakes
- Look-ahead bias. Using information that did not exist at entry time, such as the close of a bar that has not closed yet, or indicators that repaint.
- Costs set too low. A fixed, optimistic spread, no commission, no swap on overnight trades.
- Bad data. Gaps in history, or a modelling mode too coarse for a system with very tight stops and targets.
- Too few trades. 20–30 trades tell you almost nothing.
- Overfitting. Trying thousands of parameter sets and keeping the prettiest one. See Overfitting.
- One market regime only. Test only a long uptrend and any trend-following system looks brilliant.
- Trusting numbers without looking at trades. Not checking that entries and exits match the rules.
On a phone
The mobile MT4/MT5 apps have no Strategy Tester, but you can still backtest by hand: scroll back, zoom in to see bars one at a time, and log results in a spreadsheet app. The small screen makes mistakes easy, so keep the rules simple and move to a computer for serious testing.
Summary
- A backtest applies rules strictly to past data to filter ideas and learn how a system behaves.
- Rules must be clear enough that someone else would get the same result.
- Always include real costs; small costs multiplied by many trades can erase the edge.
- Aim for roughly 100 trades or more, across different market conditions.
- A good backtest is only the first gate. Follow it with a forward test.
