Multi-Timeframe Moving Average Crossover Optimierungsstrategie

Schriftsteller:ChaoZhang, Datum: 2024-01-05 12:05:42
Tags:

img

Übersicht

Diese Strategie basiert auf dem berühmten CM_Ultimate_MA_MTF-Indikator und wird in eine Handelsstrategie umgeschrieben. Sie kann gleitende Durchschnitte über mehrere Zeitrahmen hinweg zeichnen und Crossover-Signale zwischen MAs unterschiedlicher Perioden generieren. Die Strategie beinhaltet auch einen Trailing-Stop-Loss-Mechanismus.

Strategie Logik

  1. Auf der Grundlage der Benutzerkonfiguration werden MA-Linien verschiedener Typen auf dem Hauptdiagramm-Zeitrahmen und höheren Zeitrahmen gezeichnet.
  2. Bei einer schnelleren MA überschreitet eine langere MA; bei einer schnelleren MA überschreitet eine langsamere MA wird eine kurze MA ausgeführt.
  3. Hinzufügen von Trailing Stop Loss zu weiteren Kontrollrisiken.

Analyse der Vorteile

  1. Durch die Überschneidung von MA über Zeitrahmen hinweg kann die Signalqualität verbessert und Falschsignale reduziert werden.
  2. Die Kombination verschiedener MA-Typen nutzt die Stärken einzelner Indikatoren für eine bessere Stabilität.
  3. Ein Stop Loss hilft, Verluste rechtzeitig zu begrenzen.

Risikoanalyse

  1. Die Verzögerung des MA kann kurzfristige Chancen verpassen.
  2. Eine schlechte Optimierung der MA-Perioden kann zu übermäßigen falschen Signalen führen.
  3. Eine falsche Stop-Loss-Platzierung kann zu einem unnötigen Exit führen.

Optimierungsrichtlinien

  1. Versuche Kombinationen von MA-Parametern, um eine optimale Einstellung zu finden.
  2. Hinzufügen anderer Indikatoren zur Signalfiltration und Qualitätsverbesserung.
  3. Optimierung der Stop-Loss-Strategie, um dem Marktprofil anzupassen.

Schlussfolgerung

Die Strategie integriert Multi-Timeframe-Analysen und Trailing-Stop-Ansätze von gleitenden Durchschnitten, um die Signalqualität und die Risikokontrolle zu verbessern.


/*backtest
start: 2022-12-29 00:00:00
end: 2024-01-04 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2

strategy(title = "Ultimate Moving Average Strategy", shorttitle = "UMA Strategy", overlay = true)

//Created by user ChrisMoody 4-24-2014
//Converted to strategy by Virtual_Machinist 7-11-2018
//Plots The Majority of Moving Averages
//Defaults to Current Chart Time Frame --- But Can Be Changed to Higher Or Lower Time Frames
//2nd MA Capability with Show Crosses Feature

//inputs
src = close
useCurrentRes = input(true, title="Use Current Chart Resolution?")
resCustom = input(title="Use Different Timeframe? Uncheck Box Above",  defval="D")
len = input(20, title="Moving Average Length - LookBack Period")
atype = input(1,minval=1,maxval=7,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA")
cc = input(true,title="Change Color Based On Direction?")
smoothe = input(2, minval=1, maxval=10, title="Color Smoothing - 1 = No Smoothing")
doma2 = input(false, title="Optional 2nd Moving Average")
len2 = input(50, title="Moving Average Length - Optional 2nd MA")
atype2 = input(1,minval=1,maxval=7,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA")
cc2 = input(true,title="Change Color Based On Direction 2nd MA?")
warn = input(false, title="***You Can Turn On The Show Dots Parameter Below Without Plotting 2nd MA to See Crosses***")
warn2 = input(false, title="***If Using Cross Feature W/O Plotting 2ndMA - Make Sure 2ndMA Parameters are Set Correctly***")
sd = input(false, title="Show Dots on Cross of Both MA's")

useStop     = input(defval = true, title = "Use Trailing Stop?")
slPoints    = input(defval = 200, title = "Stop Loss Trail Points", minval = 1)
slOffset    = input(defval = 400, title = "Stop Loss Trail Offset", minval = 1)

res = useCurrentRes ? timeframe.period : resCustom
//hull ma definition
hullma = wma(2*wma(src, len/2)-wma(src, len), round(sqrt(len)))
//TEMA definition
ema1 = ema(src, len)
ema2 = ema(ema1, len)
ema3 = ema(ema2, len)
tema = 3 * (ema1 - ema2) + ema3

avg = atype == 1 ? sma(src,len) : atype == 2 ? ema(src,len) : atype == 3 ? wma(src,len) : atype == 4 ? hullma : atype == 5 ? vwma(src, len) : atype == 6 ? rma(src,len) : tema
//2nd Ma - hull ma definition
hullma2 = wma(2*wma(src, len2/2)-wma(src, len2), round(sqrt(len2)))
//2nd MA TEMA definition
sema1 = ema(src, len2)
sema2 = ema(sema1, len2)
sema3 = ema(sema2, len2)
stema = 3 * (sema1 - sema2) + sema3

avg2 = atype2 == 1 ? sma(src,len2) : atype2 == 2 ? ema(src,len2) : atype2 == 3 ? wma(src,len2) : atype2 == 4 ? hullma2 : atype2 == 5 ? vwma(src, len2) : atype2 == 6 ? rma(src,len2) : tema

out = avg 
out_two = avg2

out1 = request.security(syminfo.tickerid, res, out)
out2 = request.security(syminfo.tickerid, res, out_two)

ma_up = out1 >= out1[smoothe]
ma_down = out1 < out1[smoothe]

col = cc ? ma_up ? lime : ma_down ? red : aqua : aqua
col2 = cc2 ? ma_up ? lime : ma_down ? red : aqua : aqua

circleYPosition = out2

plot(out1, title="Multi-Timeframe Moving Avg", style=line, linewidth=4, color = col)
plot(doma2 and out2 ? out2 : na, title="2nd Multi-TimeFrame Moving Average", style=circles, linewidth=4, color=col2)
plot(sd and cross(out1, out2) ? circleYPosition : na,style=cross, linewidth=5, color=yellow)

// Strategy conditions

longCond    = ma_up
shortCond   = ma_down
// entries and base exit
strategy.entry("long", strategy.long, when = longCond)
strategy.entry("short", strategy.short, when = shortCond)

if (useStop)
    strategy.exit("XL", from_entry = "long", trail_points = slPoints, trail_offset = slOffset)
    strategy.exit("XS", from_entry = "short", trail_points = slPoints, trail_offset = slOffset)
// not sure needed, but just incase..
strategy.exit("XL", from_entry = "long", when = shortCond)
strategy.exit("XS", from_entry = "short", when = longCond)

Mehr