动量均线盘整策略

Author: ChaoZhang, Date: 2024-01-25 11:39:00
Tags:

动量均线盘整策略

概述

该策略主要利用移动平均线HMA和EMA形成的均线盘整来判断买入时机。当HMA上穿EMA时视为盘整结束,形成新的上涨趋势,因此在HMA上穿EMA的同时买入。

该策略同时结合RSI指标来检测超买超卖情况。当RSI低于70时允许买入,RSI高于80时考虑部分止盈。

策略原理

该策略使用200周期的EMA和HMA构建均线系统。其中,HMA指标是根据EMA改进设计的更加灵敏的移动平均线指标。当HMA上穿EMA时,表示盘整阶段结束,股价开始上涨。此时若RSI指标显示没有超买,则产生买入信号。

在已建仓的情况下,若股价回落,HMA再度下穿EMA,表明新的盘整开始,则全部平仓。同时,如果RSI上穿80时,会部分止盈20%,防止亏损。

该策略的成交逻辑比较简单,主要就是HMA和EMA的多空交叉,结合RSI的高低位判断,形成较为稳健的交易策略。

优势分析

该策略最大的优势在于,利用EMA和HMA的盘整交易形态,可以过滤掉大部分False Break,从而提高获利率。同时,RSI指标的辅助也可以有效控制风险,这两者的结合使得该策略非常适合盘整震荡市。

另外,该策略仅仅使用3个指标且逻辑简单,这使得其参数优化和回测较为方便,有利于策略的验证和改进。

风险分析

尽管该策略有一定的优点,但仍存在一些风险需要注意。例如,持仓时间可能会比较长,需要有足够的资金支持。如果遇到横盘整理的时段,无法很快止损退出,容易出现亏损扩大的情况。

另外,该策略主要依赖均线指标,如果价格出现异常突破,止损措施可能来不及起作用,会带来更大的风险。此外,参数设置也会影响策略表现,需要进行大量测试去找到最优参数。

优化方向

考虑到上述风险,该策略可以从以下几个方面进行优化:

  1. 结合波动率指标,根据市场波动情况动态调整仓位。

  2. 增加趋势指标判断,避免不必要的反转交易。

  3. 优化移动平均线参数,使之更贴近当前市场特征。

  4. 采用时间止损,最大程度避免单笔损失过大的问题。

总结

本策略总体来说是一个比较经典简单的盘整震荡策略。它主要应用于股指和热门个股的短线和中期交易,可以获取比较稳定的 Alpha 值。随着参数的优化和风控措施的加强,该策略的表现还具有很大的提升空间。


/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
period: 1h
basePeriod: 15m
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/
// © mohanee

//@version=4
strategy(title="EMA_HMA_RSI_Strategy", overlay=true, pyramiding=2,     default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000, currency=currency.USD)  //default_qty_value=10, default_qty_type=strategy.fixed, 

//longCondition = crossover(sma(close, 14), sma(close, 28))
//if (longCondition)
    //strategy.entry("My Long Entry Id", strategy.long)

//shortCondition = crossunder(sma(close, 14), sma(close, 28))
//if (shortCondition)
  //  strategy.entry("My Short Entry Id", strategy.short)

//HMA
HMA(src1, length1) =>  wma(2 * wma(src1, length1/2) - wma(src1, length1), round(sqrt(length1)))

var stopLossVal=0.00

//variables BEGIN
length=input(200,title="EMA and HMA Length")   //square root of 13

rsiLength=input(13, title="RSI Length")

takePartialProfits = input(true, title="Take Partial Profits (if this selected, RSI 13 higher reading over 80 is considered for partial closing ) ")

stopLoss = input(title="Stop Loss%", defval=8, minval=1)
//variables  END

//RSI 
rsi13=rsi(close,rsiLength)


ema200=ema(close,length)
hma200=HMA(close,length)

//exitOnAroonOscCrossingDown = input(true, title="Exit when Aroon Osc cross down zero ")


// Drawings

//Aroon oscillator

arronUpper = 100 * (highestbars(high, length+1) + length)/length
aroonLower = 100 * (lowestbars(low, length+1) + length)/length

aroonOsc  = arronUpper - aroonLower

aroonMidpoint = 0
//oscPlot = plot(aroonOsc, color=color.green)
//midLine= plot(aroonMidpoint, color=color.green)
//topLine = plot(90,style=plot.style_circles, color=color.green)
//bottomLine = plot(-90,style=plot.style_circles, color=color.red)

//fill(oscPlot, midLine, color=aroonOsc>0?color.green:color.red, transp=50)
//fill(topLine,bottomLine, color=color.blue)
//fill(topLine,oscPlot, color=aroonOsc>90?color.purple:na, transp=25)


// RSI 
//plot(rsi13, title="RSI", linewidth=2, color=color.purple)
//hline(50, title="Middle Line", linestyle=hline.style_dotted)
//obLevel = hline(80, title="Overbought", linestyle=hline.style_dotted)
//osLevel = hline(30, title="Oversold", linestyle=hline.style_dotted)
//fill(obLevel, osLevel, title="Background", color=rsi13 >=30 ? color.green:color.purple, transp=65)  // longTermRSI >=50

hullColor = hma200 > hma200[2] ? #00ff00 : #ff0000

plot(hma200, title="HULL 200", color=hullColor,  transp=25)
plot(ema200, title="EMA 200", color=color.orange)

//Entry--
strategy.initial_capital = 50000
strategy.entry(id="Long Entry", comment="LE", qty=(strategy.initial_capital * 0.10)/close, long=true,  when=strategy.position_size<1 and hma200 < ema200   and  hma200 > hma200[2]  and rsi13<70 )     //  // aroonOsc<0

//Add
if(strategy.position_size>=1 and  close < strategy.position_avg_price and  ( crossover(rsi13,30) or  crossover(rsi13,40) ) )   // hma200 < ema200   and  hma200 > hma200[2]   and hma200[2] < hma200[3] )  //and crossover(rsi13,30)  aroonOsc<0    //and hma200 > hma200[2]  and hma200[2] <= hma200[3]  //crossover(rsi13,30)
    qty1=(strategy.initial_capital * 0.10)/close
    //stopLossVal:= abs(strategy.position_size)>1 ?  ( (close > close[1] and close > open and close>strategy.position_avg_price) ? close[1]*(1-stopLoss*0.01) : stopLossVal ) : 0.00 
    strategy.entry(id="Long Entry", comment="Add", qty=qty1, long=true )     //crossover(close,ema34)  //and close>ema34  //crossover(rsi5Val,rsiBuyLine)  --   SL="+tostring(stopLossVal, "####.##")


//stopLossVal:= abs(strategy.position_size)>1 ? strategy.position_avg_price*(1-0.5) : 0.00 

stopLossVal:= abs(strategy.position_size)>1 ?  ( (close > close[1] and close > open and close>strategy.position_avg_price) ? close[1]*(1-stopLoss*0.01) : stopLossVal ) : 0.00 
//stopLossVal:= abs(strategy.position_size)>1 ?  strategy.position_avg_price*(1-stopLoss*0.01) :  0.00

barcolor(color=strategy.position_size>=1? rsi13>80 ? color.purple: color.blue:na)

//close partial
if(takePartialProfits==true)
    strategy.close(id="Long Entry", comment="Partial X  points="+tostring(close - strategy.position_avg_price, "####.##"),  qty_percent=20 , when=abs(strategy.position_size)>=1 and crossunder(rsi13, 80)  and close > strategy.position_avg_price  )   //close<ema55 and rsi5Val<20 //ema34<ema55



//close All
//if(exitOnAroonOscCrossingDown)
  //  strategy.close(id="Long Entry", comment="Exit All points="+tostring(close - strategy.position_avg_price, "####.##"),  when=abs(strategy.position_size)>=1 and crossunder(aroonOsc, 0) )   //close<ema55 and rsi5Val<20 //ema34<ema55  //close<ema89

//close All on stop loss
strategy.close(id="Long Entry", comment="Stoploss X points="+tostring(close - strategy.position_avg_price, "####.##"),  when=abs(strategy.position_size)>=1 and close < stopLossVal  )  //close<ema55 and rsi5Val<20 //ema34<ema55  //close<ema89

strategy.close(id="Long Entry", comment="hmaXema X points="+tostring(close - strategy.position_avg_price, "####.##"),  when=abs(strategy.position_size)>=1 and close > strategy.position_avg_price  and  crossunder(hma200,ema200)  )  //close<ema55 and rsi5Val<20 //ema34<ema55  //close<ema89
    

更多内容