Overview
This lecture covers the step-by-step process of coding a Forex scalping trading robot (Expert Advisor) in MetaTrader 5 using MQL5, including strategy logic, backtesting, and detailed variable/function explanations.
Strategy Performance & Testing
- EA (Expert Advisor) starts with $1,000 and can reach ~$41,800 over a year.
- Achieves 92% win rate on both long and short trades; 11-17% relative drawdown.
- No running drawdowns; best performance on USDJPY, GBPUSD, and EURUSD, 5-minute charts.
- Performs well in trending markets; may stagnate or lose in ranging/volatile periods.
- Requires tight spreads (<0.5 pips) and brokers that allow close trailing stops.
Strategy Logic
- EA bets that when price breaks the last high or low, it will continue 1.5 pips further.
- Entry orders placed at last high for buys, last low for sells, based on chosen criteria (e.g. highs/lows, moving average crossovers, RSI).
- Trailing stop loss activates after 1.5 pips in profit, trailing by 1 pip.
MetaTrader 5 and Coding Setup
- Download and install MetaTrader 5 for your OS from official site.
- Use Market Watch to select currency pairs; Navigator to log in to broker account.
- Strategy Tester is used for backtesting EAs.
- Code EAs in MetaEditor using MQL5 language.
Key Variables & Inputs
- User-defined inputs: risk percent per trade, chart timeframe, bars to analyze for highs/lows, TP/SL/trailing values, trading hours, magic number (EA identifier), order distances, expiry bars.
- Use 'input' keyword for user-defined variables; provide clear descriptions for each input.
Key Functions Implemented
- isNewBar: Checks if a new candle has opened to avoid repeating actions every tick.
- findHigh / findLow: Identifies local highs/lows using user-defined bar range.
- calculateLots: Calculates position size based on risk %, stop loss, account balance, and broker constraints.
- executeBuy / executeSell: Places buy/sell stop orders at defined entry points with proper SL, TP, lot size, expiry.
- closeAllOrders: Closes pending orders outside trading hours or after expiry.
- trailStop: Aggressively trails stop loss once trade moves into profit; works on every tick.
EA Logic Flow
- On EA initialization, sets magic number and loads user inputs.
- On each new bar, checks trading hours and for existing orders/positions.
- Finds new high/low and places stop orders if none exist.
- During each tick, trails stop losses if position moves in profit.
- Periodically closes pending orders if outside trading hours.
Testing & Final Advice
- Code is visually tested and debugged using breakpoints and MetaTrader's visual mode.
- EA is highly sensitive to market conditions and broker settings.
- Recommended to forward-test in demo account for several months before live deployment.
Key Terms & Definitions
- EA (Expert Advisor) — automated trading robot for MetaTrader.
- MQL5 — programming language for MetaTrader 5 EAs.
- Pip/Point — basic unit of price movement in Forex; 10 points = 1 pip.
- Trailing Stop Loss — dynamic stop order that follows price movement.
- Magic Number — unique identifier for EA’s trades.
- Backtesting — running EA on historical data to evaluate performance.
Action Items / Next Steps
- Download and install MetaTrader 5, set up broker account, and access Market Watch and Strategy Tester.
- Code the EA as taught, inputting required variables and functions step by step.
- Test the EA in a demo account for several months to observe performance.
- Watch next lecture for modifications to support Gold, Bitcoin, and US indices trading.