การเทรดด้วยระบบอัตโนมัติ (Algorithmic / EA Trading)Algorithmic and EA Trading
ระบบเทรดอัตโนมัติ (EA) ทำอะไรได้และไม่ได้ ขั้นตอนจากไอเดียถึงบัญชีจริง ทำไมผลทดสอบย้อนหลังมักดีกว่าความจริง และวิธีดูแลระบบที่รันอยู่What an automated trading system (EA) can and cannot do, the path from idea to live account, why backtests usually beat reality, and how to supervise a running system.

ในบทความนี้ · 7
In this article · 7
การเทรดด้วยระบบอัตโนมัติ (Algorithmic Trading) คือการเขียนกติกาการเทรดเป็นโปรแกรม แล้วให้คอมพิวเตอร์เปิดและปิดไม้ตามกติกานั้น บน MT4/MT5 โปรแกรมแบบนี้เรียกว่า Expert Advisor หรือ EA ข้อดีชัดเจนคือระบบไม่เหนื่อย ไม่กลัว ไม่โลภ และทำตามกติกาเดิมทุกครั้ง แต่ EA ไม่ได้สร้างข้อได้เปรียบขึ้นมาเอง มันแค่ทำตามกติกาที่ใส่ไว้ ถ้ากติกาไม่มีข้อได้เปรียบ ระบบอัตโนมัติก็แค่ขาดทุนอย่างมีวินัยและรวดเร็วขึ้น
ระบบอัตโนมัติทำอะไรได้และไม่ได้
ทำได้ดี
- ทำตามกติกาที่ชัดเจนได้ทุกครั้ง ไม่ข้ามสัญญาณเพราะอารมณ์
- เฝ้าตลาดได้ตลอด 24 ชั่วโมง และหลายตลาดพร้อมกัน
- คำนวณขนาดไม้ SL และเป้าได้แม่นยำทุกไม้
- ทดสอบกับข้อมูลย้อนหลังได้ก่อนใช้เงินจริง
ทำไม่ได้
- ทำให้กติกาที่ไม่มีข้อได้เปรียบกลายเป็นกำไร
- ปรับตัวเองเมื่อสภาพตลาดเปลี่ยน ถ้าไม่ได้ถูกเขียนให้ทำ
- เข้าใจบริบทที่ไม่ได้อยู่ในข้อมูล เช่น เหตุการณ์ไม่คาดฝัน
- ป้องกันความผิดพลาดของโค้ด เช่น เปิดไม้ซ้ำ หรือไม่ใส่ SL
จากไอเดียถึงบัญชีจริง
- เขียนกติกาเป็นภาษาคน: จุดเข้า จุดออก SL ขนาดไม้ และเงื่อนไขที่ไม่เทรด ต้องชัดจนไม่ต้องตีความ ถ้าอธิบายเป็นข้อ ๆ ไม่ได้ ก็เขียนเป็นโค้ดไม่ได้ (สร้างระบบเทรด)
- แปลงเป็นโค้ด: ใช้ภาษา MQL4/MQL5 เขียนเอง จ้างคนเขียน หรือให้เครื่องมือ AI ช่วยร่าง แล้วตรวจโค้ดให้ละเอียด (พื้นฐาน EA)
- ทดสอบย้อนหลัง: ใช้ข้อมูลที่มีคุณภาพ ใส่สเปรด ค่าคอมมิชชัน และสวอปให้ใกล้ความจริง (การทดสอบย้อนหลัง)
- ตรวจการปรับจนเกินพอดี: แบ่งข้อมูลเป็นช่วงที่ใช้ปรับค่า กับช่วงที่ไม่เคยเห็น ถ้าผลในช่วงที่ไม่เคยเห็นแย่กว่ามาก แปลว่าระบบจำอดีตมากกว่าหาข้อได้เปรียบ (Overfitting)
- ทดสอบไปข้างหน้า: รันบนบัญชีเดโมหรือบัญชีจริงขนาดเล็กหลายสัปดาห์ถึงหลายเดือน เทียบผลกับการทดสอบย้อนหลัง (การทดสอบไปข้างหน้า)
- ใช้งานจริงแบบค่อยเป็นค่อยไป: เริ่มด้วยความเสี่ยงต่ำ แล้วเพิ่มเมื่อผลจริงสอดคล้องกับที่คาด
ทำไมผลทดสอบย้อนหลังมักดีกว่าความจริง
- ต้นทุนที่ใส่ต่ำเกินไป: สเปรดคงที่ในการทดสอบมักแคบกว่าสเปรดจริงที่กว้างขึ้นในบางช่วง
- ไม่มีการลื่นไถลหรือความล่าช้า: การทดสอบมักได้ราคาที่ต้องการทุกครั้ง
- ปรับค่าจนเข้ากับอดีต: ยิ่งมีตัวแปรให้ปรับมาก ยิ่งหาค่าที่ "สวย" ในอดีตได้ง่าย
- ช่วงข้อมูลสั้นหรือเลือกเฉพาะช่วงดี: ไม่ผ่านสภาพตลาดที่ระบบจะพัง
สมมติ EA เทรด EURUSD 400 ไม้ต่อปี ที่ 0.10 ล็อต (1 pip = $1) ผลทดสอบได้เฉลี่ย 3 pip ต่อไม้ก่อนหักต้นทุนเพิ่ม ผลทดสอบ 400 × 3 × $1 = $1,200 ถ้าการทดสอบใช้สเปรด 0.5 pip แต่สเปรดเฉลี่ยจริงคือ 1.2 pip ส่วนต่าง 0.7 pip ต้นทุนเพิ่ม 400 × 0.7 × $1 = $280 สมมติการลื่นไถลเฉลี่ยอีก 0.3 pip ต่อไม้ 400 × 0.3 × $1 = $120 ผลที่ใกล้ความจริงขึ้น $1,200 − $280 − $120 = $800 หายไปหนึ่งในสามจากต้นทุนที่ดูเล็กน้อย ถ้าระบบได้เฉลี่ยแค่ 1 pip ต่อไม้ในการทดสอบ ต้นทุนเพิ่มรวม 1.0 pip จะทำให้ผลเหลือศูนย์
ดูภาพด้านล่าง
ดูแลระบบที่รันอยู่
- โครงสร้างพื้นฐาน: EA ทำงานเฉพาะเมื่อแพลตฟอร์มเปิดอยู่และเชื่อมต่อได้ หลายคนจึงใช้เซิร์ฟเวอร์เสมือน (VPS) (VPS และการส่งคำสั่ง)
- ตรวจบันทึก (Log) ประจำ: ดูข้อความผิดพลาด คำสั่งที่ถูกปฏิเสธ หรือพฤติกรรมแปลก ๆ
- เทียบกับผลทดสอบ: ถ้าการติดลบสูงสุดเกินกว่าที่เคยเห็นในการทดสอบมาก ให้หยุดและตรวจสอบ
- ขีดจำกัดของบัญชี: ตั้งจุดหยุดระบบเมื่อขาดทุนรายวันหรือรวมถึงระดับที่กำหนด
- ระวังการเปลี่ยนแปลงภายนอก: เวลาเซิร์ฟเวอร์ สเปรด ขนาดสัญญา หรือเงื่อนไขของบัญชีอาจเปลี่ยน และทำให้ระบบทำงานต่างจากเดิม
เหมาะกับใคร
- เวลา: ใช้เวลามากตอนสร้างและทดสอบ แต่น้อยลงตอนรัน (ไม่ใช่ศูนย์)
- นิสัย: ชอบเป็นระบบ ทนกับการทดสอบซ้ำ ๆ และไม่เข้าไปแทรกแซงระบบตามอารมณ์
- ทักษะ: ไม่จำเป็นต้องเป็นโปรแกรมเมอร์ แต่ต้องอ่านผลทดสอบเป็น (ตัวชี้วัดผลงาน) และตรวจได้ว่าโค้ดทำตามกติกาจริง
- ขนาดบัญชี: ทุกขนาด แต่ระบบที่เปิดหลายไม้พร้อมกันต้องการทุนมากกว่า
จุดอ่อน / ข้อผิดพลาดที่พบบ่อย
- เชื่อกราฟทดสอบที่สวยเกินจริง: โดยเฉพาะกราฟที่เรียบมาก มักมาจากการปรับเกินพอดี หรือกริด/มาร์ติงเกลที่ซ่อนอยู่ (กริด, มาร์ติงเกล)
- ข้ามการทดสอบไปข้างหน้า: ใช้เงินจริงทันทีหลังทดสอบย้อนหลัง
- ปรับค่าทุกครั้งที่ขาดทุน: เป็นการปรับเกินพอดีแบบสด ๆ
- ไม่มี SL ในโค้ด: ถ้าการเชื่อมต่อหลุด ไม้ที่ไม่มี SL ที่ฝั่งเซิร์ฟเวอร์จะไม่มีอะไรป้องกัน
- แทรกแซงด้วยมือ: ปิดไม้หรือเลื่อน SL เอง ทำให้ผลจริงไม่ตรงกับระบบที่ทดสอบ
- ซื้อระบบจากคำโฆษณา: ผลที่ไม่สามารถตรวจสอบได้ไม่ใช่หลักฐาน (หลีกเลี่ยงการหลอกลวง)
ถ้าจะใช้ AI ช่วยร่างโค้ด EA ให้เริ่มจากคำอธิบายกติกาที่ครบ ทั้งจุดเข้า SL ขนาดไม้ และเงื่อนไขห้ามเทรด แล้วทดสอบโค้ดที่ได้เสมอ AI เขียนโค้ดผิดได้เหมือนคน
สรุป
- EA ทำตามกติกาได้สม่ำเสมอ แต่ไม่ได้สร้างข้อได้เปรียบเอง
- เส้นทางคือ กติกา → โค้ด → ทดสอบย้อนหลัง → ตรวจ Overfitting → ทดสอบไปข้างหน้า → ใช้จริงทีละน้อย
- ผลทดสอบมักดีกว่าความจริง เพราะต้นทุน การลื่นไถล และการปรับค่า
- ระบบที่รันอยู่ต้องมีคนดูแล ตรวจ Log และมีจุดหยุด
- ใส่ SL ในทุกคำสั่ง และอย่าแทรกแซงตามอารมณ์
Algorithmic trading means writing your trading rules as a program and letting a computer open and close trades by those rules. On MT4/MT5 such a program is called an Expert Advisor, or EA. The obvious benefits: it never tires, never fears, never gets greedy, and follows the same rules every time. But an EA does not create an edge on its own — it only executes the rules it was given. If the rules have no edge, automation just loses money with discipline, and faster.
What automation can and cannot do
Does well
- Follows clear rules every time, without skipping signals for emotional reasons.
- Watches the market 24 hours a day, and many markets at once.
- Calculates lot size, stop and target precisely on every trade.
- Can be tested on historical data before real money is used.
Cannot do
- Turn rules without an edge into profit.
- Adapt when market conditions change, unless it was written to.
- Understand context that is not in its data, such as unexpected events.
- Protect against its own code bugs, such as duplicate orders or missing stops.
From idea to live account
- Write the rules in plain language: entry, exit, stop, size, and when not to trade — clear enough to need no interpretation. If you cannot list it point by point, you cannot code it (building a system).
- Turn it into code: write MQL4/MQL5 yourself, hire someone, or let an AI tool draft it — then review the code carefully (EA automation basics).
- Backtest: use good data and set spread, commission and swap close to reality (backtesting).
- Check for overfitting: split data into a tuning period and an unseen period. If results on unseen data are much worse, the system memorised the past rather than finding an edge (overfitting).
- Forward-test: run it on demo or a small live account for weeks to months and compare with the backtest (forward testing).
- Go live gradually: start with low risk and increase only when live results match expectations.
Why backtests usually beat reality
- Costs set too low: a fixed spread in testing is often tighter than the real spread, which widens at times.
- No slippage or delay: tests usually get the desired price every time.
- Parameters tuned to the past: the more settings there are to tune, the easier it is to find values that look "beautiful" historically.
- Short or cherry-picked data: it never meets the market conditions that would break it.
Suppose an EA trades EURUSD 400 times a year at 0.10 lot (1 pip = $1) and the backtest averages 3 pips per trade before extra costs. Backtest result 400 × 3 × $1 = $1,200. If the test used a 0.5-pip spread but the real average spread is 1.2 pips, the difference is 0.7 pip. Extra cost 400 × 0.7 × $1 = $280. Suppose average slippage adds another 0.3 pip per trade: 400 × 0.3 × $1 = $120. A more realistic result: $1,200 − $280 − $120 = $800 — a third gone to costs that looked tiny. If the system averaged only 1 pip per trade in testing, the extra 1.0 pip of total cost would bring it to zero.
See the charts below.
Supervising a running system
- Infrastructure: an EA only works while the platform is running and connected, which is why many people use a virtual private server (VPS) (VPS and execution).
- Read the logs regularly: look for errors, rejected orders, or odd behaviour.
- Compare with the backtest: if drawdown goes well beyond anything seen in testing, stop and investigate.
- Account limits: set a point where the system stops when daily or total loss reaches a set level.
- Watch for outside changes: server time, spreads, contract sizes or account terms can change and make the system behave differently.
Who it suits
- Time: a lot of time to build and test; less to run (never zero).
- Temperament: systematic, patient with repeated testing, and able to keep emotional hands off the system.
- Skills: you need not be a programmer, but you must be able to read test results (performance metrics) and check that the code really follows the rules.
- Account size: any, though systems that hold several positions at once need more capital.
Weaknesses and common mistakes
- Trusting a test curve that is too good: very smooth curves often come from overfitting or a hidden grid or martingale (grid trading, martingale risks).
- Skipping forward testing: going live with real money straight after a backtest.
- Re-tuning after every loss: that is overfitting in real time.
- No stop loss in the code: if the connection drops, a trade without a server-side stop has no protection.
- Manual interference: closing trades or moving stops by hand means live results no longer match the tested system.
- Buying systems from advertising: results you cannot verify are not evidence (avoiding scams).
If you use AI to help draft an EA, start from a complete description of the rules — entry, stop, lot size and no-trade conditions — and always test the code you get. AI can write buggy code just as people can.
Summary
- An EA follows rules consistently but does not create an edge by itself.
- The path: rules → code → backtest → overfitting check → forward test → gradual live use.
- Backtests usually look better than reality because of costs, slippage and tuning.
- A running system needs supervision, log checks and a stop point.
- Put a stop on every order and do not interfere on emotion.
