多因子组合自适应移动平均策略


创建日期: 2023-12-15 11:30:09 最后修改: 2023-12-15 11:30:09
复制: 0 点击次数: 621
avatar of ChaoZhang ChaoZhang
1
关注
1621
关注者

多因子组合自适应移动平均策略

一、策略概述

多因子组合自适应移动平均策略是一种同时结合使用日内线、移动平均线、聚合交叉以及 HA 均线的复合策略。该策略旨在发掘更多的交易机会,在牛市中获得更高的累计收益。

二、策略原理

该策略的核心逻辑在于,结合使用多种技术指标对买入卖出信号进行评分,根据不同因子匹配的结果,给出不同强度的交易信号。

具体来说,策略所使用的四个主要技术指标包括:

  1. 日内线。策略利用日内线的颜色判断价格趋势,连续两根绿色实体 HA 均线为买入信号,连续两根红色空心 HA 均线为空头信号。

  2. 移动平均线。策略同时使用快、慢、过滤三条不同参数设置的移动平均线。当快速线上穿慢速线,慢速线上穿过滤线时,为买入信号;反之,则为卖出信号。移动平均线能有效判断中长期趋势。

  3. Stochastic 指标。该指标判断多空交叉时机。当 %K 线从下方向上突破 %D 线时,为买入信号;从上方向下突破时,为卖出信号。

  4. 匹配评分机制。根据上述多个因子的匹配情况,策略采用评分机制。匹配因子越多,对应信号的强度也就越大。

通过多因子综合判断,策略可以在中短期内捕捉到更多细微的交易机会,从而在牛市中获得超额收益。

三、策略优势

多因子组合自适应移动平均策略最大的优势在于增强了信号的可靠性。单一技术指标容易出现错误信号,而本策略结合使用了多种指标进行配对,可以有效减少假信号的干扰。

另外,相比于仅仅跟随单一指标,多因子组合更能提高交易胜率。在牛市中,策略可以获得更高的累计收益。

四、策略风险

该策略的主要风险在于,多因子组合本身会增加策略的复杂度。需要同时兼顾多个指标的参数设置、频繁的调整等。

另外,在熊市中,策略持仓时间可能会过长。即使设置止损,也难以避免较大的损失。

此外,Stochastic 指标和 HA 均线等技术指标易受突发事件影响,容易产生错误信号,导致不必要的亏损。

五、策略优化方向

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

  1. 优化每个指标参数的设置,找到最优参数组合。

  2. 增加模型训练和参数自适应模块,实时优化参数。

  3. 增加止损策略,降低策略最大回撤。

  4. 增加仓位控制模块,根据市场情况动态调整仓位。

  5. 结合机器学习算法,建立多因子评分的神经网络模型。

六、总结

多因子组合自适应移动平均策略综合运用了多种技术指标的优势。该策略可以有效增加信号质量,在牛市中获得超额收益。但同时也增加了策略复杂度,需要进一步测试和优化。

策略源码
/*backtest
start: 2022-12-08 00:00:00
end: 2023-12-14 00:00:00
period: 1d
basePeriod: 1h
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/
// © cyrule
//@version=4
strategy("2nd Grade Strategy", overlay=true, shorttitle="2GTS", max_lines_count = 500, max_labels_count = 500, calc_on_every_tick = true, calc_on_order_fills = true, pyramiding = 1, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)

source = input(close, title = "Source")

// **********************
// * Heikin-Ahshi       *
// * kudos to garethyeo *
// **********************
showHA   = input(true, title = "Show Heikin Ashi?", group = "Heikin Ashi")
ha_open  = security(heikinashi(syminfo.tickerid), timeframe.period, open)
ha_high  = security(heikinashi(syminfo.tickerid), timeframe.period, high)
ha_low   = security(heikinashi(syminfo.tickerid), timeframe.period, low)
ha_close = security(heikinashi(syminfo.tickerid), timeframe.period, close)

bgcolor(iff(showHA and ha_open < ha_close , color.new(#53b987, transp = 92.5), na), title = 'Green HA')
bgcolor(iff(showHA and ha_open >= ha_close, color.new(#eb4d5c, transp = 92.5), na), title = 'Red HA'  )


// ******************
// * Moving Average *
// ******************
// MA Settings
showMA         = input(true, title = "Show Moving Averages?", group = "Moving Averages")
fastMALength   = input(title = "Fast MA Length", minval = 1, step = 1, defval = 20, group = "Moving Averages")
slowMALength   = input(title = "Slow MA Length", minval = 1, step = 1, defval = 50, group = "Moving Averages")
maType         = input(title = "Moving Average Type", defval = "SMA", options = ["SMA", "EMA", "RMA", "WMA", "VWMA"], group = "Moving Averages")
filterMALength = input(title = "Filter MA Length", minval = 1, step = 1, defval = 200, group = "Moving Averages")
filterMAType   = input(title = "Filter MA Type", defval = "EMA", options = ["SMA", "EMA", "RMA", "WMA", "VWMA"], group = "Moving Averages")

// Calculate MA
var float maFast   = na
var float maSlow   = na
var float maFilter = na

if (maType   == "SMA")
    maFast   := sma(source, fastMALength)
    maSlow   := sma(source, slowMALength)
if (maType   == "EMA")
    maFast   := ema(source, fastMALength)
    maSlow   := ema(source, slowMALength)
if (maType   == "RMA")
    maFast   := rma(source, fastMALength)
    maSlow   := rma(source, slowMALength)
    maFilter := rma(source, filterMALength)
if (maType   == "WMA")
    maFast   := wma(source, fastMALength)
    maSlow   := wma(source, slowMALength)
if (maType   == "VWMA")
    maFast   := vwma(source, fastMALength)
    maSlow   := vwma(source, slowMALength)

if (filterMAType == "SMA")
    maFilter     := sma(source, filterMALength)
if (filterMAType == "EMA")
    maFilter     := ema(source, filterMALength)
if (filterMAType == "RMA")
    maFilter     := rma(source, filterMALength)
if (filterMAType == "WMA")
    maFilter     := wma(source, filterMALength)
if (filterMAType == "VWMA")
    maFilter     := vwma(source, filterMALength)

BiruAtasMerah = (maFast >= maSlow) and (maSlow >= maFilter)
MerahAtasBiru = (maFast <= maSlow) and (maSlow <= maFilter)

// Lukis MA
plot(series = showMA ? maFast   : na, color = color.blue, title = "MA Fast")
plot(series = showMA ? maSlow   : na, color = color.red,  title = "MA Slow")
plot(series = showMA ? maFilter : na, color = #FFCC00,    title = "MA Filter")


// **************
// * Stochastic *
// **************
// Stochastic Settings
showSSC = input(true, title = "Show Stochastic Crossovers?", group = "Stochastic")
Length = input (10, minval = 1, title = "%K Length", group = "Stochastic")
SmoothK = input (3, minval = 1, title = "%K Smoothing", group = "Stochastic")
SmoothD = input (3, minval = 1, title = "%D Smoothing", group = "Stochastic")

// Calculate Stochastic
var float K = na
var float D = na

if (maType ==  "SMA")
	K      := sma(stoch(source, high, low, Length), SmoothK)
	D      := sma(K, SmoothD)
if (maType ==  "EMA")
	K      := ema(stoch(source, high, low, Length), SmoothK)
	D      := ema(K, SmoothD)
if (maType ==  "RMA")
	K      := rma(stoch(source, high, low, Length), SmoothK)
	D      := rma(K, SmoothD)
if (maType ==  "WMA")
	K      := wma(stoch(source, high, low, Length), SmoothK)
	D      := wma(K, SmoothD)
if (maType ==  "VWMA")
	K      := vwma(stoch(source, high, low, Length), SmoothK)
	D      := vwma(K, SmoothD)

StochasticCrossOver  = crossover(K, D)
StochasticCrossUnder = crossunder(K, D)

// Lukis SS
plotshape(showSSC and StochasticCrossOver  and K <=  20            ? K : na, text = "Golden\nCrossover",  color = color.new(color.green, transp = 25), location = location.belowbar, size = size.tiny, title = "Golden Crossover" )
plotshape(showSSC and StochasticCrossUnder and K >=  80            ? D : na, text = "Deadly\nCrossover",  color = color.new(color.red, transp = 25),   location = location.belowbar, size = size.tiny, title = "Deadly Crossover" )
plotshape(showSSC and StochasticCrossOver  and K <=  80 and K > 20 ? K : na, text = "Bullish\nCrossover", color = color.new(color.green, transp = 50), location = location.belowbar, size = size.tiny, title = "Bullish Crossover")
plotshape(showSSC and StochasticCrossUnder and K >=  20 and K < 80 ? D : na, text = "Bearish\nCrossover", color = color.new(color.red, transp = 50),   location = location.belowbar, size = size.tiny, title = "Bearish Crossover")

showBull = input(true, title = "Show Bullish Signal?", group = "Signal")
showBear = input(false, title = "Show Bearish Signal?", group = "Signal")

bullishCriteria = 0
if (ha_open < ha_close) and (ha_open[1] < ha_close[1]) and (ha_open[2] >= ha_close[2])
    bullishCriteria := bullishCriteria + 1
if (maFast > maSlow) and (maSlow > maFilter)
    bullishCriteria := bullishCriteria + 1
if (K > D) and (K > K[1]) and (D > D[1])
    bullishCriteria := bullishCriteria + 1

bearishCriteria = 0
if (ha_open >= ha_close) and (ha_open[1] >= ha_close[1]) and (ha_open[2] < ha_close[2])
    bearishCriteria := bearishCriteria + 1
if (maFast < maSlow) and (maSlow < maFilter)
    bearishCriteria := bearishCriteria + 1
if (K < D) and (K < K[1]) and (D < D[1])
    bearishCriteria := bearishCriteria + 1

signal = color.new(color.white, transp = 0)
if bearishCriteria == 2
    signal := color.new(color.orange, transp = 50)
if bearishCriteria == 3
    signal := color.new(color.red, transp = 50)
if bullishCriteria == 2
    signal := color.new(color.aqua, transp = 50)
if bullishCriteria == 3
    signal := color.new(color.green, transp = 50)

bullishCriteria := showBull ? bullishCriteria : 0
bearishCriteria := showBear ? bearishCriteria : 0

bgcolor(iff(bullishCriteria > 1, signal, na), title = 'Bullish Signal')
bgcolor(iff(bearishCriteria > 1, signal, na), title = 'Bearish Signal')

longTPPerc  = input(title = "Take Profit Threshold (%)"            , minval = 0.0, step = 0.5, defval = 2.5, group = "Trading") / 100
profitRatio = input(title = "Profit-to-Loss ratio (risk tolerance)", minval = 1.0, step = 0.1, defval = 1.4, group = "Trading")
longSLPerc  = longTPPerc / profitRatio
takeProfit  = strategy.position_avg_price * (1 + longTPPerc)
stopLoss    = strategy.position_avg_price * (1 - longSLPerc)
strategy.initial_capital = 50000
strategy.entry("Long" , strategy.long , floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = bullishCriteria > 1)
strategy.entry("Short", strategy.short, floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = bearishCriteria > 1)
strategy.close("Long" , when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss))
strategy.close("Short", when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss))

plotshape(bullishCriteria, location = location.belowbar, color = color.new(color.black, transp = 100))
plotshape(bearishCriteria, location = location.belowbar, color = color.new(color.black, transp = 100))