Trading Code Library
119 free MQL4, MQL5 and Pine Script snippets grouped by topic. Each page shows the full source and how to use it.
Bollinger Bands
- MQL4 BB Breakout EA — Bollinger Band breakout strategy trading closes outside the bands.
- MQL4 Bollinger %B — Bollinger %B showing price position relative to bands.
- MQL5 BB Mean Reversion — Mean reversion using Bollinger Bands for ranging markets.
- Pine Script BB Squeeze — BB squeeze detector identifying low volatility before big moves.
- Pine Script Double BB — Double Bollinger Bands with 1 and 2 SD for trend zones.
Breakout
- MQL4 Channel Breakout EA — Donchian Channel breakout strategy.
- MQL5 London Breakout — London breakout capturing Asian range.
- Pine Script Range Breakout — Opening range breakout from first hour high/low.
- Pine Script Volatility Breakout — ATR-based volatility breakout from daily open.
Candlestick Patterns
- MQL4 Morning Star — Detect morning star three-candle reversal pattern.
- MQL5 Doji Detector — Detect Doji patterns including dragonfly and gravestone.
- MQL5 Pin Bar Detector — Detect bullish and bearish pin bars.
- Pine Script 3 White Soldiers — Detect Three White Soldiers and Three Black Crows.
- Pine Script Engulfing Pattern — Detect bullish and bearish engulfing patterns.
- Pine Script Hammer/Shooting Star — Detect hammer and shooting star reversal patterns.
Entry Signals
- Bollinger Band Breakout — Highlights Bollinger Band breakout signals.
- Candlestick Patterns — Detect engulfing and doji patterns.
- Moving Average Cross EA — MQL5 EA that trades on EMA crossovers.
- RSI Crossover Strategy — Pine Script strategy with RSI crossover buy/sell signals.
Fibonacci
- MQL4 Fibonacci Extensions — Calculate Fibonacci extension TP targets.
- MQL5 Auto Fibonacci — Auto Fibonacci retracement from recent swing high/low.
- Pine Script Auto Fibonacci — Auto Fibonacci levels from recent swing.
- Pine Script Fib Time Zones — Fibonacci time zones for potential trend change dates.
Grid Trading
- MQL4 Anti-Grid Recovery System — Anti-grid recovery system increasing position size at each level.
- MQL4 Grid Trading EA — Simple grid trading EA placing buy and sell orders at fixed intervals.
- MQL5 Dynamic Grid Spacing — Grid trading with ATR-based dynamic spacing adapting to volatility.
- MQL5 Hedging Grid Strategy — Hedging grid strategy using pending orders at calculated levels.
- Pine Script Grid Level Visualizer — Visualize grid trading levels on TradingView charts.
- Pine Script Grid Trading Backtest — Backtest a grid trading strategy on TradingView.
Ichimoku
- MQL4 Ichimoku Filter — Ichimoku cloud as trade direction filter.
- MQL5 Ichimoku EA — Ichimoku EA trading TK cross with cloud confirmation.
- Pine Script Ichimoku Cloud — Complete Ichimoku Cloud with all 5 lines and cloud fill.
- Pine Script Ichimoku Scanner — Multi-TF Ichimoku scanner in a dashboard table.
Indicators
- EMA Ribbon — Color EMA ribbon for trend visualization.
- MACD Histogram — Custom MACD histogram indicator for MQL5.
- MTF RSI Confluence — Multi-timeframe RSI confluence checker.
- Pivot Points — Daily pivot point calculator.
- Stochastic RSI — Stochastic RSI oscillator.
- Supertrend Indicator — Supertrend with buy/sell signal arrows.
- Volume Bars — Relative volume colored bars.
MACD
- MQL4 MACD Histogram EA — Trade MACD histogram reversals for momentum entries.
- MQL4 MACD Multi-TF — Multi-timeframe MACD confluence across M15, H1, H4.
- MQL5 MACD Divergence — Scan for MACD divergences.
- Pine Script MACD Crossover — MACD crossover strategy with zero-line filter.
- Pine Script MACD+EMA Filter — MACD filtered by 200 EMA trend direction.
Martingale
- MQL4 Anti-Martingale Strategy — Anti-martingale: increases lot after wins, resets after losses.
- MQL4 Safe Martingale — Martingale with max drawdown limit and level cap for safety.
- MQL5 Fibonacci Martingale — Fibonacci sequence for lot sizing instead of doubling. Slower progression.
- MQL5 Martingale EA — Classic martingale EA doubling lot size after each loss with safety limits.
- Pine Script Martingale Backtest — Backtest martingale position sizing on TradingView.
Moving Average
- MQL4 EMA Crossover EA — Classic EMA crossover EA trading golden cross and death cross.
- MQL4 Hull Moving Average — Hull Moving Average (HMA) with reduced lag.
- MQL4 MA Slope Filter — Filter trades by MA slope angle threshold.
- MQL5 Adaptive Moving Average — Kaufman Adaptive Moving Average (KAMA) for MQL5.
- MQL5 DEMA — Double Exponential Moving Average with reduced lag.
- Pine Script MA Ribbon — Moving average ribbon with 8 EMAs for trend strength visualization.
- Pine Script Triple MA — Triple MA system with fast, medium, and slow for trend confirmation.
- Pine Script VWMA Strategy — Volume Weighted Moving Average strategy.
Order Management
- MQL4 Close All Orders — Close all open orders and delete pending orders.
- MQL4 Partial Close — Close 50% at TP1, trail the rest.
- MQL4 Pending Order Manager — Place and manage pending orders with expiration.
- MQL4 Trade Copier — Local trade copier using global variables.
- MQL5 Close All Positions — Close all positions with optional filtering.
- MQL5 OCO Orders — One-Cancels-Other: when one fills, cancel the other.
- MQL5 Order Modify Helper — Helper functions for modifying SL and TP on positions.
Risk Management
- ATR Stop Loss — Dynamic stop loss using ATR in MQL5.
- Break Even Stop Loss — Move SL to break even once in profit.
- Lot Size Calculator — Calculate lot size based on risk percentage and stop loss.
- MQL4 Kelly Criterion Sizing — Kelly Criterion lot sizing based on win rate and R:R ratio.
- MQL4 Risk Percent Sizing — Position sizing based on fixed risk percentage.
- MQL5 Correlation Filter — Prevent overexposure by checking position correlation.
- MQL5 Drawdown Monitor — Pause trading when drawdown exceeds threshold.
- MQL5 Lot Size Calculator — Calculate lot size based on risk percentage and stop loss distance.
- MQL5 Risk-Reward Validator — Validate minimum risk-reward ratio before entry.
- Pine Script Equity Curve — Track equity curve with drawdown visualization.
- Pine Script Position Calculator — On-chart position size calculator.
- Simple Trailing Stop — A basic trailing stop implementation for MQL4 Expert Advisors.
RSI Strategies
- MQL4 RSI Pullback Entry — Trade RSI pullbacks to 50 level during a trend.
- MQL4 RSI with MA Filter — RSI trading filtered by moving average trend direction.
- MQL5 RSI Overbought/Oversold EA — MQL5 EA trading RSI overbought and oversold levels.
- MQL5 RSI Range Trading — RSI range-bound trading for sideways markets.
- Pine Script Multi-TF RSI — Multi-timeframe RSI from multiple timeframes for confluence.
- Pine Script RSI Divergence — Detect bullish and bearish RSI divergences on TradingView.
- Pine Script Stochastic RSI — Stochastic RSI for precise entry signals on TradingView.
Session/Time
- MQL4 News Avoidance — Pause EA during high-impact news events.
- MQL4 Session Filter — Filter trades by trading session for optimal liquidity.
- MQL5 Time-Based EA — EA opening trades at specific times.
- Pine Script Session Highlighter — Highlight Asian, London, and NY sessions.
Strategies
- Grid Trading — Simple grid trading framework.
Supertrend
- MQL4 Supertrend EA — Supertrend-based EA for MQL4 MetaTrader 4.
- MQL5 Supertrend — Supertrend indicator for MQL5 MetaTrader 5.
- Pine Script MTF Supertrend — Multi-timeframe Supertrend showing trend from multiple TFs.
- Pine Script Supertrend — Classic Supertrend with buy/sell signals.
- Pine Script Supertrend+RSI — Supertrend with RSI for high-probability entries.
Support/Resistance
- MQL4 Round Number Levels — Plot psychological round number price levels.
- MQL5 Supply Demand Zones — Identify supply and demand zones from strong candles.
- MQL5 Support Resistance Zones — Auto-detect S/R zones from pivot highs and lows.
- Pine Script Dynamic S/R — Auto-detect dynamic S/R from fractal pivots.
- Pine Script Pivot Points — Auto pivot point calculator with S1-S3 and R1-R3.
Trade Management
- News Time Filter — Pause trading during high-impact news.
- Order Manager Class — OOP order management with retry logic.
- Session Time Filter — Trade only during specific market sessions.
- Spread Monitor — Filter trades when spread is too high.
Trailing Stop
- MQL4 Breakeven Stop — Move stop loss to breakeven after specified pips profit. Essential risk management.
- MQL4 Step Trailing Stop — Step-based trailing stop moving SL in fixed increments for volatile markets.
- MQL5 ATR Trailing Stop — ATR-based dynamic trailing stop for MQL5. Adjusts trailing distance based on market volatility.
- MQL5 Multi-Level Trailing Stop — Multi-level trailing stop that tightens as profit increases.
- MQL5 Parabolic SAR Trailing Stop — Use Parabolic SAR as a dynamic trailing stop in MQL5.
- Pine Script Chandelier Exit — Chandelier Exit indicator for TradingView using ATR to set trailing stops.
- Pine Script Percentage Trailing Stop — Percentage-based trailing stop strategy for TradingView backtesting.
Volume Analysis
- MQL5 Volume Indicator — Detect volume spikes, climax bars, and dry-up periods.
- Pine Script OBV Strategy — On Balance Volume strategy with MA confirmation.
- Pine Script Volume Momentum — Volume-weighted momentum oscillator.
- Pine Script Volume Profile — Volume distribution by price level.
VWAP
- Pine Script Anchored VWAP — Anchored VWAP from a significant date.
- Pine Script Multi-Day VWAP — Daily and weekly VWAP for confluence levels.
- Pine Script VWAP Bands — VWAP with standard deviation bands for intraday levels.
- Pine Script VWAP Crossover — Intraday VWAP crossover strategy with volume confirmation.
- Pine Script VWAP Mean Reversion — Fade price extending beyond 2 SD from VWAP.