
本策略名为加权量化移动平均线交叉策略(Weighted Quantitative Moving Average Crossover Strategy),其基本思路是结合价格、交易量等多种指标,设计快速线和慢速线,并在它们发生金叉和死叉时发出买入和卖出信号。
本策略的核心指标是量化移动平均线(Quantitative Moving Average,QMA)。QMA通过计算一段时间内的加权平均价格来测量趋势方向,它与普通移动平均线的不同之处在于其中价格的权重(权重=价格*交易量)会随着时间的推移而衰减。这样,最近期价格的权重更大,可以更快速地响应市场变化。
具体来说,本策略构建了快速QMA线和慢速QMA线。快速线参数设置为25天,慢速线参数设置为29天。当快速线从下方上穿慢速线时产生买入信号;当快速线从上方下穿慢速线时产生卖出信号。
相比普通移动平均线,本策略具有以下优势:
本策略也存在一些风险:
可以通过适当调整参数频率,严格的walk forward analysis,以及结合其他指标来缓解上述风险。
本策略仍有进一步优化的空间:
本策略总体来说是一个稳定性较好的短期交易策略。相比单一价格平均,其指标更能反映市场供需关系。通过参数调整和风控手段的引入,本策略可以长期稳定运作,获得良好收益。
/*backtest
start: 2022-11-29 00:00:00
end: 2023-12-05 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("Brad VWMACD Strategy 2233", overlay=false, max_bars_back=500,default_qty_type=strategy.percent_of_equity,commission_type=strategy.commission.percent, commission_value=0.18, default_qty_value=100)
// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2018, title = "From Year", minval = 2017)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)
// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
// === INPUT SMA ===
//fastMA = input(defval = 16, type = integer, title = "FastMA", minval = 1 )
//slowMA = input(defval = 23, type = integer, title = "SlowMA", minval = 1)
fastMA = input(defval = 25, title = "FastMA", minval = 1 )
slowMA = input(defval = 29, title = "SlowMA", minval = 1)
Long_period = slowMA
Short_period = fastMA
Smoothing_period = input(9, minval=1)
xLongMAVolPrice = ema(volume * close, Long_period)
xLongMAVol = ema(volume, Long_period)
xResLong = (xLongMAVolPrice * Long_period) / (xLongMAVol * Long_period)
xShortMAVolPrice = ema(volume * close, Short_period)
xShortMAVol = ema(volume, Short_period)
xResShort = (xShortMAVolPrice * Short_period) / (xShortMAVol * Short_period)
xVMACD = xResShort - xResLong
xVMACDSignal = ema(xVMACD, Smoothing_period)
nRes = xVMACD - xVMACDSignal
//plot(nRes*20+slowMA, color=blue, style = line )
//plot(3000, color=red, style = line )
// === SERIES SETUP ===
buy = crossover( xVMACD,xVMACDSignal) // buy when fastMA crosses over slowMA
sell = crossunder( xVMACD,xVMACDSignal) // sell when fastMA crosses under slowMA
// === SERIES SETUP ===
//buy = crossover(vwma(close, fastMA),7+vwma(close, slowMA)) // buy when fastMA crosses over slowMA
//sell = crossunder(vwma(close, fastMA),vwma(close, slowMA)-7) // sell when fastMA crosses under slowMA
// === EXECUTION ===
strategy.entry("L", strategy.long, when = window() and buy) // buy long when "within window of time" AND crossover
strategy.close("L", when = window() and sell) // sell long when "within window of time" AND crossunder
// === EXECUTION ===
strategy.entry("S", strategy.short, when = window() and sell) // buy long when "within window of time" AND crossover
strategy.close("S", when = window() and buy) // sell long when "within window of time" AND crossunder
plotshape(window() and buy, style=shape.triangleup, color=green, text="up")
plotshape(window() and sell, style=shape.triangledown, color=red, text="down")
plot(xVMACD*100, title = 'FastMA', color = orange, linewidth = 2, style = line) // plot FastMA
plot(xVMACDSignal*100, title = 'SlowMA', color = aqua, linewidth = 2, style = line) // plot SlowMA