Strategy vault / Trend following
EMA and Candlestick Based Dynamic Price Trend Following Strategy
This strategy is a dynamic trend following system that combines Exponential Moving Averages (EMA) with candlestick patterns. It identifies specific candlestick patterns (Pin Bars and Engulfing Patterns), uses fast…

Overview
This strategy is a dynamic trend following system that combines Exponential Moving Averages (EMA) with candlestick patterns. It identifies specific candlestick patterns (Pin Bars and Engulfing Patterns), uses fast and slow EMAs to determine market trends, and employs the ATR indicator to measure market volatility. The core concept is to identify precise entry points through candlestick patterns when the market trend is confirmed.
Strategy Principles
The strategy consists of three core components:
- Candlestick Pattern Recognition System: Detects Pin Bars and Engulfing Patterns. Pin Bars require shadow length to be at least twice the body length, while Engulfing Patterns require the current candle to completely encompass the previous candle's body.
- Dynamic Trend System: Uses 8-period and 21-period EMAs to determine market trends. An uptrend is confirmed when the fast EMA is above the slow EMA; conversely for downtrends.
- Volatility Monitoring: Uses 14-period ATR to measure market volatility and provide reference for potential stop-loss settings.
Entry conditions strictly require both trend and pattern confirmation: long entries need bullish candlestick patterns during uptrends, while short entries need bearish patterns during downtrends.
Strategy Advantages
- Multiple Confirmation Mechanism: Combines trend and pattern indicators to reduce false signals.
- Dynamic Adaptability: Uses dynamic indicators like EMA and ATR to adapt to different market conditions.
- Clear Visual Feedback: Strategy marks entry signals and trend lines on the chart for intuitive market understanding.
- Structured Code Design: Strategy code is well-organized for easy maintenance and optimization.
Strategy Risks
- Lack of Stop-Loss Mechanism: Current version lacks automatic stop-loss functionality, requiring manual risk management.
- Trend Dependency: May generate frequent false signals in ranging markets.
- Lag Risk: EMAs as lagging indicators may cause slightly delayed entries.
- Over-sensitivity: Pattern recognition may be too frequent under certain market conditions.
Optimization Directions
- Implement Stop-Loss Mechanism: Suggest designing a dynamic stop-loss system based on ATR to protect profits.
- Add Filters: Can add volume confirmation or other technical indicators to reduce false signals.
- Optimize Parameters: EMA and ATR periods can be optimized for different trading instruments and timeframes.
- Add Position Management: Implement a dynamic position sizing system based on volatility.
Summary
This is a well-structured trend following strategy that provides a relatively reliable trading system by combining multiple technical analysis tools. While the current version has some areas for improvement, its core logic is sound. Through implementing the suggested optimizations, this strategy has the potential to become a more comprehensive trading system. It may perform particularly well in trending markets.
Source Pine Script
/*backtest
start: 2024-02-21 00:00:00
end: 2025-02-19 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
//@version=6
strategy("Candlestick Bible: Dynamic Price Follower (Corrected)", overlay=true, pyramiding=0, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
//=======================
// 1. PATTERN DETECTION
//=======================
// Pin Bar Detection
bodySize = math.abs(close - open)
upperShadow = high - math.max(close, open)
lowerShadow = math.min(close, open) - low
isBullishPin = (lowerShadow >= 2 * bodySize) and (upperShadow <= bodySize / 2)
isBearishPin = (upperShadow >= 2 * bodySize) and (lowerShadow <= bodySize / 2)
// Engulfing Pattern
isBullishEngulf = (close[1] < open[1]) and (close > open) and (close > open[1]) and (open < close[1])
isBearishEngulf = (close[1] > open[1]) and (close < open) and (close < open[1]) and (open > close[1])
//=======================
// 2. DYNAMIC TREND SYSTEM
//=======================
emaFast = ta.ema(close, 8)
emaSlow = ta.ema(close, 21)
marketTrend = emaFast > emaSlow ? "bullish" : "bearish"
//=======================
// 3. PRICE MOVEMENT SYSTEM
//=======================
atr = ta.atr(14)
//=======================
// 4. STRATEGY RULES
//=======================
longCondition = (isBullishPin or isBullishEngulf) and marketTrend == "bullish" and close > emaSlow
shortCondition = (isBearishPin or isBearishEngulf) and marketTrend == "bearish" and close < emaSlow
//=======================
// 5. STRATEGY ENTRIES
//=======================
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
//=======================
// 6. VISUAL FEEDBACK
//=======================
plot(emaFast, "Fast EMA", color=color.blue)
plot(emaSlow, "Slow EMA", color=color.red)
plotshape(longCondition, "Long Signal", shape.triangleup, location.belowbar, color=color.green, size=size.small)
plotshape(shortCondition, "Short Signal", shape.triangledown, location.abovebar, color=color.red, size=size.small)
Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-20. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/EMA-and-Candlestick-Based-Dynamic-Price-Trend-Following-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Dynamic Dual EMA Trend Following StrategyPine Script · Trend following · 1d
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h
- Multi Exponential Moving Average Crossover Strategy with Volume Based ATR Dynamic Stop Loss OptimizationPine Script · Trend following · 2d
- Multi Factor Trend Price Action Strategy with Dynamic Risk Management SystemPine Script · Trend following · 2h
- Multi Period Moving Average Ribbon and MACD Crossover Strategy SystemPine Script · Trend following · 1h
- Multi Period Trend Following with Volume Confirmation StrategyPine Script · Trend following · 5d