0bps

Strategy vault / Mean reversion

Buying Dips MA200 Optimized Strategy

This strategy combines a contrarian approach (buying dips) with trend-following logic (only when the price is above the MA200). The strategy aims to find the best timing to buy dips that is most likely to be…

Author
ChaoZhang
Complexity
Intermediate · 49 lines
Parameters
9
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy combines a contrarian approach (buying dips) with trend-following logic (only when the price is above the MA200). The strategy aims to find the best timing to buy dips that is most likely to be profitable. The price above the long-term moving average indicates momentum that increases the possibility of profiting from buying assets during short-term weakness.

Strategy Principle

This strategy calculates the overall percentage change of the price over the lookback period to determine if the price is at a relative dip. When the overall change percentage is below -3%, the price is considered at the dip. In addition, the strategy also sets the 200-day simple moving average as an indicator to judge the trend. Buy signals are only triggered when the price is above the 200-day moving average. By utilizing both the mean reversion principle and long-short pairing, the strategy buys the dip during an upward trend to make a profit.

Advantage Analysis

This strategy combines the advantages of both trend trading and contrarian trading. On one hand, using the long-term moving average to determine the trend avoids blindly buying during a downward trend. On the other hand, buying dips provides better entry opportunities during short-term corrections. The combination ensures both trading security and higher profit probability. Moreover, the strategy has large optimization space for parameters that can be adjusted to fit different markets, giving it strong adaptability.

Risk Analysis

The biggest risk is that the price may continue to decline after the buy signal triggers, leading to enlarged losses. In addition, if the market remains range-bound for a long time and the price fails to break through the moving average, the strategy would also fail. To mitigate such risks, the moving average period could be shortened accordingly and buy criteria can be optimized to ensure sufficient margin of safety.

Optimization Directions

The strategy can be optimized in several aspects: 1) optimize the moving average period to adapt to different markets; 2) optimize buy criteria to ensure sufficient margin; 3) add stop loss to control losses; 4) combine other indicators to judge trend and dips to improve accuracy.

Summary

In general, this is a typical strategy that combines trend following and contrarian trading ideas. It ensures both trading security and higher winning probability, with strong practical value. Further enhancements on stability and real trading effects can be achieved through parameter optimization and stop loss optimization.

Parameters

NameDefaultWhat it controls
v_input_1100MA
v_input_2trueLookback Period
v_input_3trueFrom Month
v_input_4trueFrom Day
v_input_52020From Year
v_input_6trueThru Month
v_input_7trueThru Day
v_input_82112Thru Year
v_input_9trueShow Date Range

Source Pine Script

/*backtest
start: 2023-12-08 00:00:00
end: 2024-01-07 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Buy The Dips - MA200 Optimised", overlay=false)

//Moving average
MAinp = input(defval = 100, title = "MA", type = input.integer, minval = 1, step = 1)
MA=sma(close, MAinp)

//Percent change
inp_lkb = input(1, title='Lookback Period')
 
perc_change(lkb) =>
    overall_change = ((close[0] - close[lkb]) / close[lkb]) * 100

// Call the function    
overall = perc_change(inp_lkb)

// === INPUT BACKTEST RANGE ===
fromMonth = input(defval = 1,    title = "From Month",      type = input.integer, minval = 1, maxval = 12)
fromDay   = input(defval = 1,    title = "From Day",        type = input.integer, minval = 1, maxval = 31)
fromYear  = input(defval = 2020, title = "From Year",       type = input.integer, minval = 1970)
thruMonth = input(defval = 1,    title = "Thru Month",      type = input.integer, minval = 1, maxval = 12)
thruDay   = input(defval = 1,    title = "Thru Day",        type = input.integer, minval = 1, maxval = 31)
thruYear  = input(defval = 2112, title = "Thru Year",       type = input.integer, minval = 1970)

showDate  = input(defval = true, title = "Show Date Range", type = input.bool)

start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish window
window()  => true       // create function "within window of time"

//Entry/Exit
strategy.entry(id="long", long = true, when = window() and overall<-3 and close > MA) 
strategy.close(id="long", when = window() and overall>1)


bgcolor(color = showDate and window() ? color.gray : na, transp = 90) 
plot(overall, color=color.black, title='Overall Percentage Change', linewidth=3)
band1 = hline(1, "Upper Band", color=#C0C0C0)
band0 = hline(-2, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")
hline(0, title='Center Line', color=color.orange, linestyle=hline.style_solid, linewidth=2)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-08. 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/Buying-Dips-MA200-Optimized-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