双均线SuperTrend量化交易策略

Author: ChaoZhang, Date: 2024-02-05 12:05:10
Tags:

双均线SuperTrend量化交易策略

概述

本策略综合运用双均线和SuperTrend两个指标构建交易信号,同时结合不同周期判断趋势方向,实现高效盈利。

策略原理

本策略使用MACD和SuperTrend两个指标判断入市时机。其中,MACD双均线判断短期趋势方向,Supertrend判断中长期趋势方向。

当快线从下向上突破慢线时为买入信号,此时如果中长期Supertrend同为上升趋势,则产生最终买入信号,做多;反之,当快线从上向下突破慢线时为卖出信号,此时如果中长期Supertrend同为下跌趋势,则产生最终卖出信号,做空。

止损和止盈设置为固定值。

优势分析

本策略最大优势在于同时运用双均线和SuperTrend判断市场走向,中短期和中长期结合,大幅提高决策效率,避免出现假突破。此外,Supertrend可根据市场波动性调节参数,适应更广泛的市场环境。

风险分析

本策略主要风险在于固定止损止盈设置可能错失更大盈利空间。此外,如果中短期和中长期判断出现分歧,则策略无法正常工作。我们可以通过止损止盈浮动设置来减少此风险。

优化方向

本策略可以从以下几个方面进行优化:

  1. 增加止损止盈动态调整机制,根据市场波动性和趋势设置止损止盈。

  2. 优化MACD参数,找到更适合目标品种的均线参数。

  3. 优化Supertrend参数,调整其对市场的敏感度。

  4. 增加其它指标判断,提供更多维度的信号,提高策略效果。

总结

本策略成功结合了双均线和SuperTrend两个指标的优势,通过不同周期的组合判断,过滤掉错误信号,从而在趋势市中获得较好收益。我们可以通过参数优化和机制调整进一步增强该策略的稳定性和盈利能力。


/*backtest
start: 2024-01-28 00:00:00
end: 2024-02-04 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
//Supertrend Strategy by breizh29 using *rajandran.r* Supertrend Indicator

strategy("Super Trend 2 MACD", overlay=true)
// MACD input
source = input(close)
fastLength = input(12, minval=1, title="MACD fast moving average")
slowLength=input(26,minval=1, title="MACD slow moving average")
signalLength=input(9,minval=1, title="MACD signal line moving average")

// Calculation
fastMA = sma(source, fastLength)
slowMA = sma(source, slowLength)

Macd = fastMA - slowMA
Signal = sma(Macd, signalLength)


res = input(title="Main SuperTrend Time Frame",  defval="120")
Factor=input(1, minval=1,maxval = 100)
Pd=input(1, minval=1,maxval = 100)

tp = input(500,title="Take Profit")
sl = input(400,title="Stop Loss")


Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))
MUp=request.security(syminfo.tickerid,res,hl2-(Factor*atr(Pd)))
MDn=request.security(syminfo.tickerid,res,hl2+(Factor*atr(Pd)))

Mclose=request.security(syminfo.tickerid,res,close)

TrendUp=close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
TrendDown=close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn

MTrendUp=Mclose[1]>MTrendUp[1]? max(MUp,MTrendUp[1]) : MUp
MTrendDown=Mclose[1]<MTrendDown[1]? min(MDn,MTrendDown[1]) : MDn

Trend = close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
Tsl = Trend==1? TrendUp: TrendDown

MTrend = Mclose > MTrendDown[1] ? 1: Mclose< MTrendUp[1]? -1: nz(MTrend[1],1)
MTsl = MTrend==1? MTrendUp: MTrendDown

linecolor = Trend == 1 ? green : red
plot(Tsl, color = linecolor , style = line , linewidth = 2,title = "SuperTrend")

Mlinecolor = MTrend == 1 ? blue : orange
plot(MTsl, color = Mlinecolor , style = line , linewidth = 2,title = "Main SuperTrend")

plotshape(cross(close,Tsl) and close>Tsl , "Up Arrow", shape.triangleup,location.belowbar,green,0,0)
plotshape(cross(Tsl,close) and close<Tsl , "Down Arrow", shape.triangledown , location.abovebar, red,0,0)

up = Trend == 1 and Trend[1] == -1 and MTrend == 1 
down = Trend == -1 and Trend[1] == 1 and MTrend == -1 
plotarrow(up ? Trend : na, title="Up Entry Arrow", colorup=lime, maxheight=60, minheight=50, transp=0)
plotarrow(down ? Trend : na, title="Down Entry Arrow", colordown=red, maxheight=60, minheight=50, transp=0)


golong = Trend == 1 and Trend[1] == -1 and MTrend == 1 and Macd > Signal
goshort = Trend == -1 and Trend[1] == 1 and MTrend == -1 and Macd < Signal

strategy.entry("Buy", strategy.long,when=golong)
strategy.exit("Close Buy","Buy",profit=tp,loss=sl)
   
   
strategy.entry("Sell", strategy.short,when=goshort)
strategy.exit("Close Sell","Sell",profit=tp,loss=sl)


更多内容