You check the weather (big timeframe) to see if it's a good day. Then you watch the waves (middle timeframe) to see where the fish are pulling back. Then you cast your rod (small timeframe) at the perfect moment. If the fish pulls the wrong way, you flip your rod to the other side instead of cutting the line.
This is a fully automated MT4 trading bot (Expert Advisor). You turn it on and it trades by itself. Here's what it does in plain English:
TV 1 (Helicopter view): Shows you which direction traffic is flowing on the highway.
TV 2 (Street camera): Shows you when a car briefly reverses into a parking spot.
TV 3 (Dashcam): Shows you the exact moment to hit the gas pedal.
THE BIG PICTURE
THE PULLBACK CHECK
THE TRIGGER
The bot looks at 3 different "zoom levels" of the same chart. The big zoom (H1) tells it the direction. The medium zoom (M5) tells it when the price pulls back. The small zoom (M1) tells it the exact second to open a trade.
Imagine dragging your finger across a foggy window. The trail you leave behind is like an EMA -- it shows the average direction price has been moving. A fast EMA (13) reacts quickly like a sports car. A slow EMA (62) reacts slowly like a truck.
▬ Fast EMA (13) crosses ABOVE
- - Slow EMA (62)
= Price is going UP = Look for BUY
▬ Fast EMA (13) crosses BELOW
- - Slow EMA (62)
= Price is going DOWN = Look for SELL
It simply says GREEN (Buy) or RED (Sell). When it switches from green to red, that's a sell signal. When it switches from red to green, that's a buy signal. The bot doesn't care about the Supertrend line itself -- just when the light changes color.
| Timeframe | Indicator | What We Read From It |
|---|---|---|
| Higher TF (H1) | EMA 13 + EMA 62 | Crossover direction + price touching EMA 13 |
| Pullback TF (M5) | Supertrend | Counter-signal (sell in buy setup, buy in sell setup) |
| Entry TF (M1) | Supertrend + EMA 13 | Entry signal + flip monitoring at pivot line |
The bot needs to unlock 4 locks in order before it opens a trade. If any lock doesn't unlock, or if lock 1 re-locks before all 4 are done, the whole thing resets. ALL 4 must be unlocked in sequence.
Each step acts as a filter. Step 1 says "the trend is up." Step 2 says "price confirmed the trend." Step 3 says "price pulled back." Step 4 says "the pullback is over, it's going back up." By the time all 4 are confirmed, you have a very high-quality entry point.
Let's walk through a complete BUY trade from start to finish. Imagine it's Monday morning, EURUSD is trending up, and your bot is watching...
A sell setup is the exact opposite of a buy. Everything is reversed.
You're waiting (Steps 1-3 done, waiting for Step 4). But then the ride breaks down (the EMAs cross back). The line gets cancelled and you have to go find a new ride (wait for a new crossover).
If the bot is waiting between Steps 1-3 (crossover happened, but the M1 entry signal hasn't fired yet), and the H1 EMAs cross back in the opposite direction, the ENTIRE setup is cancelled. The bot throws everything away and starts looking for a new crossover.
The bot keeps waiting for the touch. It won't move to Step 3 without it.
Once a trade IS opened (all 4 steps complete), future H1 crossovers do NOT affect that trade. The trade lives on its own with its pivot line and TP. New crossovers just create new independent sequences.
When the trade opens, the bot builds a permanent fence at the lowest point (for buys) or highest point (for sells) that price reached during the pullback. This fence NEVER moves. It's the line where the bot decides "if things go bad, I'll flip my trade direction."
Normal trading: You walk in, the door hits you, you fall down (stop loss).
YOUR strategy: You walk in, the door pushes you, you spin around and walk out the other side (flip). If the door pushes you again, you spin and walk back in. You keep spinning until you end up exactly where you want to be (TP hit).
This bot does NOT use a normal stop loss that just closes the trade at a loss. Instead, when EMA 13 on the entry timeframe crosses the pivot line, the bot closes the current trade AND immediately opens the opposite trade. This is called a "flip."
The EMA 13 might dip below the pivot line temporarily during a candle. That does NOT trigger a flip. The bot waits for the M1 candle to fully close with EMA 13 on the other side of the pivot. This prevents false signals from price spikes (wicks).
Result: +50 pips profit on 0.01 lots. Sequence DONE!
Trade 1: lost ~8 pips. Trade 2: lost ~4 pips. Trade 3: won 50 pips. Net profit!
By the time TP is hit, the lot size is bigger, so the win can cover the smaller earlier losses.
Every single trade (including flip trades) gets its own TP calculated from its own entry price.
| Trade | Direction | Entry Price | TP Price | Status |
|---|---|---|---|---|
| Trade 1 | BUY | 1.10500 | 1.11000 | Flipped (closed) |
| Trade 2 | SELL | 1.10420 | 1.09920 | Flipped (closed) |
| Trade 3 | BUY | 1.10380 | 1.10880 | ✅ TP HIT! |
The entire sequence ends. The flip logic stops. The pivot line gets cleaned up. That sequence is marked as completed. Any other sequences running in parallel are NOT affected.
You start betting 1 penny (0.01 lots). After 3 losses (flips), you add another penny. So now you bet 2 pennies. Then 3, then 4. You're NOT doubling (that's martingale/gambling). You're just adding one more penny each time. When you finally win, the slightly bigger bet helps cover the small earlier losses.
Martingale = 0.01, 0.02, 0.04, 0.08, 0.16... (doubles each time -- DANGEROUS)
Your Strategy = 0.01, 0.01, 0.01, 0.02, 0.03, 0.04... (adds a small fixed amount -- MUCH safer)
If FlipCount < StartAddingAfterFlips: Lot = BaseLotSize
If FlipCount >= StartAddingAfterFlips: Lot = BaseLotSize + ((FlipCount - StartAddingAfterFlips) + 1) × LotIncrement
| Trade # | Flip Count | Past Threshold? | Lot Size | Calculation |
|---|---|---|---|---|
| 1 (initial) | 0 | ❌ No (0 < 3) | 0.01 | Base lot |
| 2 (1st flip) | 1 | ❌ No (1 < 3) | 0.01 | Base lot |
| 3 (2nd flip) | 2 | ❌ No (2 < 3) | 0.01 | Base lot |
| 4 (3rd flip) | 3 | ✅ Yes (3 ≥ 3) | 0.02 | 0.01 + ((3-3)+1) × 0.01 |
| 5 (4th flip) | 4 | ✅ Yes | 0.03 | 0.01 + ((4-3)+1) × 0.01 |
| 6 (5th flip) | 5 | ✅ Yes | 0.04 | 0.01 + ((5-3)+1) × 0.01 |
| 7 (6th flip) | 6 | ✅ Yes | 0.05 | 0.01 + ((6-3)+1) × 0.01 |
| Trade # | Flip Count | Lot Size |
|---|---|---|
| 1 | 0 | 0.05 |
| 2 | 1 | 0.05 |
| 3 | 2 | 0.07 |
| 4 | 3 | 0.09 |
| 5 | 4 | 0.11 |
Each time a new H1 crossover happens, the bot starts a new board game. Each game has its own board (pivot line), its own dice (lot size), its own score (flip count), and its own winning condition (TP). Games run at the same time and NEVER interfere with each other.
Each sequence is a completely independent universe. Sequence A flipping from buy to sell does NOTHING to Sequence B. They each have their own pivot line, their own flip counter, their own lot size, and their own TP.
Every guest (trade) at the party wears a name tag that says which family (sequence) they belong to. The bot can look at any trade and immediately know "this buy trade belongs to Sequence 1" or "this sell trade belongs to Sequence 3."
MagicNumber = BaseMagicNumber + SequenceID
| Sequence | Base | + | ID | = | Magic Number | Used By |
|---|---|---|---|---|---|---|
| Sequence 1 | 50000 | + | 1 | = | 50001 | All trades in Seq 1 (buy, sell flips, everything) |
| Sequence 2 | 50000 | + | 2 | = | 50002 | All trades in Seq 2 |
| Sequence 3 | 50000 | + | 3 | = | 50003 | All trades in Seq 3 |
Each H1 crossover event can only create ONE sequence. Once the first valid entry is taken from that crossover, that crossover is "consumed." You can't get two sequences from the same crossover.
However, once a trade IS open, the flips within that sequence don't count as "new" sequences. And future crossovers create future independent sequences.
Imagine you're playing a game and the power goes out. When the power comes back, you don't want to start from scratch -- you want to continue from where you were. This bot does the same thing. It constantly saves its "game state" so if MT4 crashes, restarts, or your computer reboots, it picks up right where it left off.
Every time something changes, the bot saves to MT4 Global Variables or a file:
MT4 closes unexpectedly, computer restarts, internet drops, etc.
Bot stops running
When MT4 starts again, the bot:
| Data | Why It's Saved | What Happens Without It |
|---|---|---|
| Pivot Line Price | The pivot is the core of the flip logic. Without it, the bot doesn't know where to flip. | Bot loses its "fence" -- trades would go unmanaged |
| Flip Count | Determines the lot size for the next trade | Bot resets lot size to base -- incorrect lot progression |
| Sequence ID | Links trades to their sequence for independent management | Bot can't tell which trade belongs to which sequence |
| Direction | Knows if the current sequence is in buy or sell mode | Bot might flip the wrong way |
| Current Ticket | The MT4 order ticket of the currently open trade | Bot needs to search for it (can recover from magic number) |
MT4 has built-in "Global Variables" -- tiny pieces of data that survive restarts.
Pros: Fast, built-in, no file I/O
Cons: Limited to number values, can be lost if terminal data is cleared
Example variable name:SEQ_50001_PIVOT = 1.10350SEQ_50001_FLIPS = 4
The bot writes data to a CSV/text file on disk.
Pros: Survives even terminal data clearing, can store complex data
Cons: Slightly slower, requires file access
Example file content:50001,BUY,1.10350,4,0.02,1234567
The bot draws things on your chart so you can visually verify what it's doing. This is especially useful during backtesting.
| Object | What It Looks Like | Purpose |
|---|---|---|
| Pivot Line | Horizontal line, color-coded per sequence | Shows the exact price where flips happen |
| Sequence Label | Text label near the pivot line | Shows: Seq ID, direction, flip count, lot size, pivot price |
Each sequence gets its own color. Example: Seq 1 = Green, Seq 2 = Orange, Seq 3 = Purple
Below is every single setting in the bot with its default value and a plain-English explanation.
| Setting Name | Default | What It Does |
|---|---|---|
| INDICATOR SETTINGS | ||
| FastEMAPeriod | 13 | The "fast" moving average period on the higher timeframe. Lower number = reacts faster to price changes. This is the line that crosses the slow EMA to signal trend direction. |
| SlowEMAPeriod | 62 | The "slow" moving average period on the higher timeframe. Higher number = smoother, slower reaction. When fast crosses above slow = bullish. When fast crosses below slow = bearish. |
| SupertrendATRPeriod | 10 | How many candles the Supertrend indicator uses to calculate its Average True Range. Affects how sensitive the Supertrend signals are. |
| SupertrendMultiplier | 3.0 | A multiplier for the Supertrend bands. Higher = less sensitive (fewer signals). Lower = more sensitive (more signals, but more noise). |
| TIMEFRAME SETTINGS | ||
| HigherTF | PERIOD_H1 | The "big picture" timeframe used for EMA crossover and trend direction. You could change this to H4, D1, etc. |
| PullbackTF | PERIOD_M5 | The "pullback" timeframe used for Supertrend counter-signal. Must be smaller than HigherTF. |
| EntryTF | PERIOD_M1 | The "trigger" timeframe used for Supertrend entry signal and flip monitoring. Must be smallest of the three. |
| TRADE SETTINGS | ||
| BaseLotSize | 0.01 | Your starting trade size. This is the lot size used for the initial trade and all trades before the lot progression kicks in. 0.01 = micro lot. |
| TakeProfitPips | 50 | How many pips of profit each trade aims for. Every trade (including flip trades) uses this value from its own entry price. 50 pips = 500 points on 5-digit brokers. |
| Slippage | 3 | Maximum allowed price slippage when opening/closing trades. In points. If the price moves more than this during execution, the order might be rejected. |
| MaxSpreadFilter | 0 (disabled) | Optional. If set above 0, the bot won't open trades when the spread is wider than this value. Protects against trading during high-spread periods (news, low liquidity). |
| AllowBuy | true | Set to false to prevent the bot from opening any BUY sequences. Useful if you only want to trade sell setups. |
| AllowSell | true | Set to false to prevent the bot from opening any SELL sequences. Useful if you only want to trade buy setups. |
| LOT PROGRESSION SETTINGS | ||
| LotIncrement | 0.01 | How much lot size to ADD on each trade after the threshold is reached. This is pure addition, not multiplication. Example: if 0.01, lots go 0.01, 0.02, 0.03... |
| StartAddingAfterFlips | 3 | How many flips must happen before lot size starts increasing. If set to 3, the first 3 flips all use BaseLotSize. Starting from the 4th flip, lot increases begin. |
| SAFETY SETTINGS (all optional, disabled by default) | ||
| UseMaxFlips | false | Enable to set a maximum number of flips per sequence. If reached, the sequence closes and stops. |
| MaxFlips | 0 | Maximum number of flips allowed if UseMaxFlips is true. Example: 10 means the bot stops flipping after 10 flips and closes the trade. |
| UseMaxLotCap | false | Enable to set a ceiling on lot size. Even if the formula says the lot should be higher, it won't go above MaxLotCap. |
| MaxLotCap | 0.00 | Maximum lot size allowed per trade if UseMaxLotCap is true. Example: 0.50 means even after many flips, the lot won't go above 0.50. |
| UseMaxDrawdownPerSequence | false | Enable to set a max dollar loss per sequence. If total losses in one sequence exceed this amount, the sequence closes. |
| MaxDrawdownCurrency | 0.00 | Maximum money (in account currency, e.g. USD) that can be lost in one sequence before it's forcibly closed. |
| MAGIC NUMBER SETTINGS | ||
| BaseMagicNumber | 50000 | Starting number for magic number assignment. Sequence 1 = 50001, Sequence 2 = 50002, etc. Change this if running multiple instances of the bot on different charts. |
Every time price moves (every "tick"), the bot runs through this checklist:
| Feature | What It Prevents | Default |
|---|---|---|
| Max Flips Limit | Prevents infinite flipping in a choppy market | OFF (unlimited) |
| Max Lot Cap | Prevents lot size from growing too large | OFF (unlimited) |
| Max Drawdown/Sequence | Prevents one sequence from losing too much money | OFF (unlimited) |
| Max Spread Filter | Prevents trading during high-spread conditions | OFF |
| AllowBuy / AllowSell | Limits trading to one direction only | Both ON |
| Crash Recovery | Prevents losing track of trades after a restart | ALWAYS ON |
| Element | BUY Setup | SELL Setup |
|---|---|---|
| H1 Crossover | EMA 13 crosses ABOVE EMA 62 | EMA 13 crosses BELOW EMA 62 |
| H1 EMA Touch | Price touches EMA 13 from above | Price touches EMA 13 from below |
| M5 Pullback | Supertrend SELL signal | Supertrend BUY signal |
| M1 Entry | Supertrend BUY signal | Supertrend SELL signal |
| Pivot Line | LOWEST low between M5 sell & M1 buy | HIGHEST high between M5 buy & M1 sell |
| Flip Trigger | M1 EMA 13 crosses BELOW pivot | M1 EMA 13 crosses ABOVE pivot |
| Take Profit | Entry + 50 pips | Entry - 50 pips |
| Flip Direction | Flip to SELL | Flip to BUY |
| Invalidation | H1 EMA crosses back below | H1 EMA crosses back above |
| Field | Type | Purpose |
|---|---|---|
SequenceID | int | Unique identifier for this sequence |
MagicNumber | int | BaseMagicNumber + SequenceID |
Direction | int | Current trade side (BUY/SELL) |
IsActive | bool | Whether this sequence has an open trade |
IsCompleted | bool | Whether TP was hit (sequence finished) |
HigherTFCrossoverTime | datetime | When the H1 crossover occurred |
EMATouchConfirmed | bool | Whether Step 2 (touch) happened |
PullbackSignalTime | datetime | When M5 counter-signal appeared |
EntrySignalTime | datetime | When M1 entry signal appeared |
PivotPrice | double | The anchor price for the pivot line |
FlipCount | int | How many times this sequence has flipped |
CurrentLot | double | Current lot size based on progression |
CurrentTicket | int | MT4 order ticket of the open trade |
TakeProfitPrice | double | TP price for the current open trade |
Strategy Document: Multi-Timeframe Trend-Follower with Pivot-Flip Recovery
Platform: MetaTrader 4 | Language: MQL4 | Type: Fully Automated Expert Advisor
Generated from EMA Strategy.pdf -- Full Understanding Review