This document outlines the design of
a Mechanical Trading System II, an
automated trading strategy developed
for MetaTrader 5 using Python. The
system uses a multi-timeframe approach,
determining market bias on the 4-hour
chart through price structure and
executing trades on the 15-minute
timeframe.
ALGORITHMIC
PRICE ACTION
TRADING SYSTEM
(APATS)
Full Technical Specification for Python
MT5 Expert Advisor
1. Introduction
This document provides a complete, step-by-step technical specification for building an
automated trading system using Python integrated with MetaTrader 5 (MT5). The system is
designed to operate purely on mechanical logic, meaning every trading decision must be
based on clearly defined, rule-based conditions that a computer can execute without
interpretation.
The purpose of this document is to guide a software developer with little or no trading
knowledge through the exact logic, structure, and behavior expected from the system. All
trading concepts used in this strategy are explained in simple terms and translated into
programmable conditions.
2. Core Concept of the System
The system is based on price action and market structure, which means it analyzes how
price moves over time rather than relying on complex indicators.
The system operates using three main trading models:
1. Trend Continuation (trading in the direction of the market)
2. Reversal (trading when the market changes direction)
3. Breakout (trading when price exits a consolidation zone)
The system follows a multi-timeframe approach, meaning:
• One timeframe is used to understand the overall direction
• Another timeframe is used for precise trade entries
3. Timeframe Configuration
The system uses two timeframes:
3.1 Higher Timeframe (H4 – 4 Hour Chart)
Purpose:
• Determine overall market direction (trend)
3.2 Lower Timeframe (M15 – 15 Minute Chart)
Purpose:
• Identify precise trade entries and confirmations
4. Market Structure (How Direction is Determined)
Market structure defines whether the market is going up, down, or sideways.
4.1 Bullish Market (Uptrend)
Definition:
• Price forms Higher Highs (HH) and Higher Lows (HL)
Simplified:
• Each peak is higher than the previous peak
• Each dip is higher than the previous dip
4.2 Bearish Market (Downtrend)
Definition:
• Price forms Lower Lows (LL) and Lower Highs (LH)
Simplified:
• Each drop is lower than the previous drop
• Each bounce is lower than the previous bounce
4.3 Ranging Market
Definition:
• Price moves sideways without forming new highs or lows
4.4 Developer Implementation
The system must:
1. Identify swing highs and swing lows
2. Compare recent swings
3. Classify market as:
o BULLISH
o BEARISH
o RANGING
5. Key Concepts Explained for the Developer
5.1 Swing High
A point where price temporarily stops rising and starts falling.
5.2 Swing Low
A point where price temporarily stops falling and starts rising.
5.3 Liquidity Sweep
Definition:
• Price moves beyond a previous high or low and then quickly reverses.
Purpose:
• Indicates potential trap or exhaustion in the market.
5.4 Engulfing Candle
Definition:
• A candle that fully covers the body of the previous candle.
Types:
• Bullish engulfing (price likely to go up)
• Bearish engulfing (price likely to go down)
6. Trend Continuation Strategy
This strategy trades in the direction of the main trend.
6.1 Conditions for Trade
Step 1: Confirm Trend (H4)
• Only BUY if market is bullish
• Only SELL if market is bearish
Step 2: Wait for Pullback (M15)
• Price must perform a liquidity sweep
o Break previous swing
o Return back inside
Step 3: Confirmation
• Wait for an engulfing candle in direction of trend
6.2 Trade Execution
BUY Trade:
• Market = Bullish
• Liquidity sweep below swing low
• Bullish engulfing appears
→ Execute BUY
SELL Trade:
• Market = Bearish
• Liquidity sweep above swing high
• Bearish engulfing appears
→ Execute SELL
6.3 Stop Loss
• Place at last swing point:
o BUY → last swing low
o SELL → last swing high
6.4 Take Profit Logic
Step 1:
• At Risk:Reward 1:2 → close 50%
Step 2:
• Move stop loss to entry (Break Even)
Step 3:
• Remaining trade runs to next structure level
7. Reversal Strategy
This strategy trades when the market changes direction.
7.1 Reversal Signals
The system must detect ANY of the following:
• Liquidity sweep
• Break of Structure (price breaks previous trend pattern)
• Change of Character (trend shifts direction)
• Double top or double bottom pattern
7.2 Entry Logic
After reversal signal:
1. Wait for engulfing candle
2. Enter in new direction
7.3 Example
• Market was bullish
• Price sweeps high and drops
• Structure breaks downward
• Bearish engulfing appears
→ Execute SELL
8. Breakout Strategy
This strategy trades when price exits a consolidation zone.
8.1 Range Definition
A range exists when:
• Price forms swing highs and lows
• No new higher highs or lower lows are created
Simplified:
• Price moves sideways
8.2 Breakout Detection
• Price must close outside the range
8.3 Entry Method (Retest)
Steps:
1. Wait for breakout
2. Wait for price to return (retest the level)
3. Confirm with engulfing candle
4. Enter trade
9. Risk Management System
Risk management ensures capital protection.
9.1 Risk Per Trade
• Configurable (e.g. 1%)
9.2 Position Handling
1. Open full position
2. At 1:2 RR:
o Close 50%
3. Move SL to entry
4. Let remaining run
9.3 Trade Limits
• Max trades at a time (configurable)
10. Trade Execution Logic (MT5)
10.1 Order Type
• Market execution only
10.2 Filling Modes
The system must attempt:
• IOC
• FOK
• RETURN
Until order succeeds.
10.3 Error Handling
• Retry failed trades
• Log errors
10.4 Magic Number
• Assign unique identifier to EA trades
11. System Inputs (User Settings)
The EA must allow:
• Risk percentage
• Lot size
• Stop loss rules
• Take profit rules
• Enable/disable:
o Trend trades
o Reversal trades
o Breakout trades
12. Logging System
The system must log:
• Trade entries
• Trade exits
• Errors
• Daily performance
13. Final Notes for Developer
• All logic must be strictly rule-based
• No subjective interpretation allowed
• Every condition must be coded explicitly
• System must be modular and easy to modify
14. Conclusion
This system is designed to transform discretionary trading concepts into a fully automated,
rule-based algorithm. By combining market structure, liquidity behavior, and confirmation
signals, the EA aims to execute trades with consistency and precision. Proper
implementation of this document will result in a robust trading system capable of operating
independently under clearly defined conditions.