0bps

Strategy vault / Mean reversion

Moving Average Pullback Trading Strategy

The moving average pullback trading strategy is a trend-following strategy. It utilizes the relationship between long-term and short-term moving averages to determine the overall trend direction and makes long…

Author
ChaoZhang
Complexity
Starter · 48 lines
Parameters
6
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

The moving average pullback trading strategy is a trend-following strategy. It utilizes the relationship between long-term and short-term moving averages to determine the overall trend direction and makes long entries during short-term pullbacks when prices are relatively low.

Strategy Logic

The key decision rules of this strategy are:

  1. When the closing price is above the long-term moving average, it confirms an upward trend that meets the opening position criteria
  2. When the closing price pulls back from above the short-term moving average to below the short-term moving average, there is a short-term pullback
  3. At this time, if the RSI indicator is less than 30, it is considered oversold and a buy signal is generated
  4. Establish a long position with the stop loss set below 5% of the entry price and take profit set above 10% of the entry price

With such combined criteria, we can establish positions during short-term pullbacks while the trend direction matches expectations.

Advantages of the Strategy

The biggest advantage of this strategy is that it only carries out long trades in an expected upward trend, which can effectively avoid the risk of a volatile market. At the same time, it chases the buy on the pullback of the short-term moving average, which allows entering the market at a relatively better price.

In addition, the strategy has set up mechanisms for stop loss and take profit. This allows us to control losses through stop loss even if the judgment is wrong and the market moves in the opposite direction; for profits, take profit allows locking in some gains.

Risks of the Strategy

Although this strategy considers the major trend judgment and sets up stop loss and take profit, there are still certain risks:

  1. Risk of incorrect judgment of the major trend. When judging that the market has entered a bull market after opening long positions, the actual market has turned from bullish to sideways or bearish, which will cause huge losses.

  2. The risk of stop loss being penetrated. Especially when major negative events occur, the market may gap down beyond the predetermined stop loss line, resulting in uncontrollable losses.

Accordingly, we can consider the following methods to mitigate risks:

  1. Make good analyses of the general market to avoid misjudging the trend in the shock zone. Or set longer cycle moving averages to confirm the major trend.

  2. Adopt conditional orders which get triggered on gap-down moves instead of simple stop loss orders. This can prevent stop loss orders from being penetrated to some extent.

Strategy Optimization

Considering the characteristics of this strategy with long-term judgment and short-term entry, we can further optimize it in the following aspects:

  1. Optimize the cycle parameters of moving averages to find the best parameter combination

  2. Increase other technical indicator filters. Such as adding volume analysis, or combining other overbought-oversold indicators on the basis of RSI

  3. Dynamically adjust the stop loss and take profit range. We can make adaptive adjustments based on market volatility, appropriately widening the stop loss range during high volatility periods

  4. Test adaptiveness across different products. This type of strategy may be more suitable for index products. Additional filters are needed when applied to individual stocks.

Conclusion

In general, the moving average pullback trading strategy is a relatively mature and steady strategy idea. It mainly considers the major trend and chances for short-term pullbacks, obtaining good entry opportunities without chasing new highs. At the same time, it locks in profits and controls risks through stop loss and take profit settings. This strategy is especially suitable for investors with strong comprehensive analytical capabilities and rich trading experience.

Parameters

NameDefaultWhat it controls
v_input_int_1200(?パラメータ)Long-term moving averageBASE200/period of long term sma
v_input_int_210Long-term moving averageBASE10/period of short term sma
v_input_int_35Stop loss ratio%/stoploss percentages
v_input_int_420Take profit ratio%/take profit percentages
v_input_1timestamp(01 Jan 2000 13:30 +0000)(?(Period) Start date for backtesting/start trade day
v_input_2timestamp(1 Jan 2099 19:30 +0000)バックテスをFinal day/finish date day

Source Pine Script

/*backtest
start: 2022-11-30 00:00:00
end: 2023-12-06 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tsujimoto0403

//@version=5
strategy("simple pull back", overlay=true,default_qty_type=strategy.percent_of_equity,
     default_qty_value=100)

//input value 
malongperiod=input.int(200,"Long-term moving averageBASE200/period of long term sma",group = "パラメータ")
mashortperiod=input.int(10,"Long-term moving averageBASE10/period of short term sma",group = "パラメータ")
stoprate=input.int(5,title = "Stop loss ratio%/stoploss percentages",group = "パラメータ")
profit=input.int(20,title = "Take profit ratio%/take profit percentages",group = "パラメータ")
startday=input(title="バックテストをThe day to open/start trade day", defval=timestamp("01 Jan 2000 13:30 +0000"), group="period")
endday=input(title="バックテスをFinish date day", defval=timestamp("1 Jan 2099 19:30 +0000"), group="Period")


//polt indicators that we use 
malong=ta.sma(close,malongperiod)
mashort=ta.sma(close,mashortperiod)

plot(malong,color=color.aqua,linewidth = 2)
plot(mashort,color=color.yellow,linewidth = 2)

//date range 
datefilter = true

//open conditions
if close>malong and close<mashort and strategy.position_size == 0 and datefilter and ta.rsi(close,3)<30 
    strategy.entry(id="long", direction=strategy.long)
    
//sell conditions 
strategy.exit(id="cut",from_entry="long",stop=(1-0.01*stoprate)*strategy.position_avg_price,limit=(1+0.01*profit)*strategy.position_avg_price)


if close>mashort and close<low[1] and strategy.position_size>0
    strategy.close(id ="long")
        


Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-07. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Moving-Average-Pullback-Trading-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open