Strategy vault / Trend following
Multi Timeframe Trend Following Strategy with Dynamic Position Sizing and ATR Based Take Profit Stop Loss System
This strategy is a comprehensive trading system that combines multi-timeframe analysis, trend following, and dynamic position sizing. It uses EMA as the primary trend indicator, MACD as a secondary confirmation…

Overview
This strategy is a comprehensive trading system that combines multi-timeframe analysis, trend following, and dynamic position sizing. It uses EMA as the primary trend indicator, MACD as a secondary confirmation indicator, and incorporates ATR for risk control and take profit/stop loss settings. The strategy's uniqueness lies in its use of 8-hour timeframe volume-price analysis for signal filtering and dynamic position sizing based on trend strength.
Strategy Principles
The strategy employs a layered design approach with the following core components:
- Trend Identification System: Uses 7-period and 90-period EMA crossovers and relative positions to determine trend direction
- Signal Confirmation System: Uses MACD indicator's golden and death crosses as entry signal confirmation
- Multi-timeframe Validation: Ensures larger timeframe support through 8-hour period EMA and volume analysis
- Dynamic Position Management: Adjusts position size based on trend strength (calculated through EMA difference to ATR ratio)
- Risk Control System: Sets stop loss at 1.5x ATR and take profit at 3x ATR
Strategy Advantages
- Multi-level Signal Filtering: Significantly improves signal quality through multi-timeframe analysis and multiple indicator confirmation
- Intelligent Position Management: Automatically adjusts position size based on trend strength, increasing profit potential in strong trends while controlling risk in weak trends
- Comprehensive Risk Control: Uses ATR to dynamically adjust stop loss and take profit levels, adapting to market volatility changes
- Systematic Design: Strong logical correlation between strategy components, forming a complete trading system
Strategy Risks
- Trend Reversal Risk: Multiple stop losses may occur at trend turning points
- Slippage Risk: Actual stop loss prices may deviate from expectations during highly volatile periods
- Parameter Sensitivity: Strategy involves multiple timeframe parameters, excessive optimization may lead to overfitting
- Market Environment Dependency: May generate frequent false signals in ranging markets
Strategy Optimization Directions
- Enhanced Signal Filtering: Add trend strength filter to trade only when trend strength exceeds specific thresholds
- Dynamic Stop Loss Optimization: Adjust stop loss multiplier based on market volatility and holding time
- Position Management Improvement: Incorporate more market state indicators to optimize position calculation logic
- Market Environment Recognition: Add market type identification to use different parameter combinations in different market environments
Summary
The strategy builds a complete trend following trading system through multi-timeframe analysis and dynamic position management. Its strengths lie in its systematic design approach and comprehensive risk control mechanisms, while attention needs to be paid to market environment adaptability and parameter optimization issues. Through the suggested optimization directions, the strategy can further enhance its stability and profit potential.
Source Pine Script
/*backtest
start: 2024-02-22 00:00:00
end: 2025-02-19 08:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
//@version=6
strategy('Optimized Trend Strategy', overlay = true, initial_capital = 10000, default_qty_type = strategy.cash, default_qty_value = 50, commission_value = 0.1)
// ? Core Indicators
ema7 = ta.ema(close, 7)
ema90 = ta.ema(close, 90)
atr = ta.atr(14)
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
// ? 8 Hours multi-time frame confirmation
h8Close = request.security(syminfo.tickerid, '480', close)
h8Volume = request.security(syminfo.tickerid, '480', volume)
h8Ema7 = ta.ema(h8Close, 7)
h8Signal = h8Close > h8Ema7 and h8Volume > ta.sma(h8Volume, 50)
// ? Dynamic Risk Control
stopLoss = close - 1.5 * atr
takeProfit = close + 3 * atr
// ? Trading Signals
longCondition = close > ema7 and ema7 > ema90 and ta.crossover(macdLine, signalLine) and h8Signal
shortCondition = close < ema7 and ema7 < ema90 and ta.crossunder(macdLine, signalLine) and h8Signal
// ? Position management (according to trend strength))
trendStrength = (ema7 - ema90) / (atr / close)
var float positionSize = na
if trendStrength > 2
positionSize := strategy.equity * 0.7 / close
positionSize
else if trendStrength < 0.5
positionSize := strategy.equity * 0.3 / close
positionSize
else
positionSize := strategy.equity * 0.5 / close
positionSize
// ? Order Execution
if longCondition
strategy.entry('Long', strategy.long, qty = positionSize)
strategy.exit('Long Exit', from_entry = 'Long', stop = stopLoss, limit = takeProfit)
if shortCondition
strategy.entry('Short', strategy.short, qty = positionSize)
strategy.exit('Short Exit', from_entry = 'Short', stop = stopLoss, limit = takeProfit)
Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-27. 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/Multi-Timeframe-Trend-Following-Strategy-with-Dynamic-Position-Sizing-and-ATR-Based-Take-Profit-Stop-Loss-System.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
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dual MA Crossover Trend Tracking StrategyPine Script · Trend following · 1h
- Dynamic Dual EMA Crossover Strategy with Adaptive Profit Loss ControlPine Script · Trend following · 1d
- EMA and RSI Trend Trading StrategyPine Script · Trend following · 1d
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h