资源加载中... loading...

Buying Dips - MA200 Optimized Strategy

Author: ChaoZhang, Date: 2024-01-08 16:54:21
Tags:

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.


/*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)

More