Pullback Trading Strategy Based on Dynamic Moving Average
This strategy employs a dual moving average system to identify potential breakout opportunities in selected stocks or cryptocurrencies. The core principle is to buy when the shorter-term moving average bounces back…

Overview
This strategy employs a dual moving average system to identify potential breakout opportunities in selected stocks or cryptocurrencies. The core principle is to buy when the shorter-term moving average bounces back from below the longer-term moving average and sell when prices retest the longer-term moving average.
Strategy Logic
The strategy utilizes two simple moving averages (SMA) with different periods as trading signals. The first SMA has a longer period to represent the overall trend direction. The second SMA has a shorter period to capture short-term price fluctuations.
When the shorter-term SMA crosses above the longer-term SMA from below, it signals an uptrend in prices overall hence the strategy opens a long position. When prices pull back down to retest the longer-term SMA, it indicates the short-term pullback has ended and the strategy considers stopping out or taking profit on the position.
In addition, the strategy has "oversold" and "overbought" conditions to avoid trading in extreme situations. It only opens positions when both SMA crossover and reasonable valuation criteria are met.
Advantages
- Dual moving average system effectively identifies medium-term trends
- Combines the merits of trend following and pullback trading
- Embedded oversold and overbought conditions reduce unnecessary trades
Risk Analysis
- Difficult to determine precise pullback end timing, may stop out prematurely
- Unable to quickly cut losses when trend changes, could suffer large drawdowns
- Poor parameter tuning may result in over-trading or conservative trading
Optimization Directions
There is further room to optimize this strategy:
- Utilize more advanced technical indicators like Bollinger Bands and KD to gauge price waves and trends
- Incorporate more factors like volume change, volatility to determine pullback completion
- Dynamically size positions to maximize profit potential
- Optimize stop loss logic with KAMA, Ichimoku clouds and lower timeframe signals
Conclusion
This strategy combines the strengths of trend following and pullback trading using a dual moving average system to detect opportunities. At the same time, embedded overbought/oversold conditions avoid unnecessary position opening. It is a very practical quant trading strategy worth deeper research and optimization.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_int_1 | 280 | (?Moving Avg. Parameters)MA length 1 |
| v_input_int_2 | 13 | MA length 2 |
| v_input_float_1 | 0.07 | Stop Loss (%) |
| v_input_float_2 | 0.27 | (?Too Deep and Thin conditions)Too Deep (%) |
| v_input_float_3 | 0.03 | Too Thin (%) |
Source Pine Script
/*backtest
start: 2023-02-20 00:00:00
end: 2024-02-26 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// @version=5
strategy("Profitable Pullback Trading Strategy", overlay=true,initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Inputs
ma_length1 = input.int(280,'MA length 1', step = 10,group = 'Moving Avg. Parameters', inline = 'MA')
ma_length2 = input.int(13,'MA length 2', step = 1,group = 'Moving Avg. Parameters', inline = 'MA')
sl = input.float(title="Stop Loss (%)", defval=0.07, step=0.1, group="Moving Avg. Parameters")
too_deep = input.float(title="Too Deep (%)", defval=0.27, step=0.01, group="Too Deep and Thin conditions", inline = 'Too')
too_thin = input.float(title="Too Thin (%)", defval=0.03, step=0.01, group="Too Deep and Thin conditions", inline = 'Too')
// Calculations
ma1 = ta.sma(close,ma_length1)
ma2 = ta.sma(close,ma_length2)
too_deep2 = (ma2/ma1-1) < too_deep
too_thin2 = (ma2/ma1-1) > too_thin
// Entry and close condtions
var float buy_price = 0
buy_condition = (close > ma1) and (close < ma2) and strategy.position_size == 0 and too_deep2 and too_thin2
close_condition1 = (close > ma2) and strategy.position_size > 0 and (close < low[1])
stop_distance = strategy.position_size > 0 ? ((buy_price - close) / close) : na
close_condition2 = strategy.position_size > 0 and stop_distance > sl
stop_price = strategy.position_size > 0 ? buy_price - (buy_price * sl) : na
// Entry and close orders
if buy_condition
strategy.entry('Long',strategy.long)
if buy_condition[1]
buy_price := open
if close_condition1 or close_condition2
strategy.close('Long',comment="Exit" + (close_condition2 ? "SL=true" : ""))
buy_price := na
plot(ma1,color = color.blue)
plot(ma2,color = color.orange)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-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/Pullback-Trading-Strategy-Based-on-Dynamic-Moving-Average.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
- Ichimoku Entries StrategyPine Script · Breakout · 1d
- MACD Bollinger Turtle Trading StrategyPine Script · Breakout · 1d
- Ichimoku Cloud and Bollinger Bands Combination Trading StrategyPine Script · Breakout · 10m
- Adaptive Bollinger Breakout with Moving Average Quantitative Strategy SystemPine Script · Breakout · 1h
- BB Breakout StrategyPine Script · Breakout · 1d
- Bollinger Band Breakout Trend Following StrategyPine Script · Breakout · 1d