
多时间周期标准偏差K线交叉策略是一种典型的趋势跟踪策略。该策略通过计算不同时间周期(如日线、周线、月线等)的标准偏差值,构建多组K线和D线,然后取这些线的平均值构建均线,当快线上穿慢线的时候做多,下穿的时候做空。该策略充分利用了不同周期标准偏差的预测能力,通过组合多个周期的标准偏差均线,可以有效filtrate市场 noise,锁定市场主要趋势。
该策略的核心逻辑是计算多时间周期的标准偏差,然后取平均构建交易信号。
首先,策略通过stoch()函数计算不同参数下的标准偏差K值,这里一共计算了5组K值,对应时间周期是日线、周线、月线级别。
smoothK = input(55)
SMAsmoothK = input(13)
k = sma(stoch(price, high, low, smoothK), SMAsmoothK)
smoothK1 = input(89)
SMAsmoothK1 = input(8)
k1 = sma(stoch(price, high, low, smoothK1), SMAsmoothK1)
...
smoothK4 = input(377)
SMAsmoothK4 = input(2)
k4 = sma(stoch(price, high, low, smoothK4), SMAsmoothK4)
然后分别用不同的参数计算D线:
smoothD = input(34)
d = sma(k, smoothD)
...
smoothD4 = input(233)
d4 = sma(k4, smoothD4)
随后,计算各组K线和D线的平均值,构建快线Kavg和慢线Davg:
Kavg = avg(k,k1,k2,k3,k4)
Davg = avg(d,d1,d2,d3,d4)
最后,当快线上穿慢线时做多,下穿时做空:
long = crossover(Kavg, Davg)
short = crossunder(Kavg, Davg)
通过组合多个时间周期的标准偏差均线,可以滤去较大时间周期下的市场noise,锁定主要趋势方向。
解决方案:
增加过滤条件,避免被短期假突破误导
使用自适应周期设置,根据市场波动程度调整周期参数
设置移动止损来及时止损,避免追高杀低
优化均线周期参数,找到最佳平衡点
组合更多指标信号,提高策略稳定性
该策略可以从以下几个方面进行进一步优化:
引入其它指标信号进行组合,如引入MACD、Bollinger Bands等,可以提高信号质量
添加趋势过滤,如引入SMA均线方向、ADX等指标判断趋势,避免逆势交易
使用自适应周期设置,根据市场波动程度动态调整周期参数
增加移动止损策略,根据策略参数设置止损点,及时止损
优化快线和慢线的均线周期参数,找到最佳参数组合
添加开仓过滤条件,避免被短期噪声误导信号
尝试Breakout入场策略,在突破均线后开仓
测试不同的退出策略,如Chandelier Exit,优化止盈止损
多时间周期标准偏差K线交叉策略整合了标准偏差指标的趋势跟踪能力和均线策略的稳定性。通过计算多周期标准偏差的K线和D线均值,构建交易信号,可以有效利用不同时间尺度下标准偏差指标的预测力,过滤市场噪音,捕捉主要趋势方向。该策略具有 parameter tuning 的空间,可以通过调整周期参数以及进一步引入过滤条件、止损策略等进行优化,以获得更好的策略效果。总体来说,该策略融合了多种技术分析工具的优势,是一个值得探索和优化的高效趋势跟踪策略。
/*backtest
start: 2023-09-23 00:00:00
end: 2023-10-23 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=2
strategy(title="Slow Stochastic Multi K&D Average Crossover Strategy", overlay=false, pyramiding=0, calc_on_order_fills=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, currency="USD", default_qty_value=100)
price = input(close)
///////////////////////////////
smoothK = input(55)
SMAsmoothK = input(13)
k = sma(stoch(price, high, low, smoothK), SMAsmoothK)
smoothD = input(34)
d = sma(k, smoothD)
///////////////////////////
smoothK1 = input(89)
SMAsmoothK1 = input(8)
k1 = sma(stoch(price, high, low, smoothK1), SMAsmoothK1)
smoothD1 = input(55)
d1 = sma(k1, smoothD1)
//////////////////////////////////////
smoothK2 = input(144)
SMAsmoothK2 = input(5)
k2 = sma(stoch(price, high, low, smoothK2), SMAsmoothK2)
smoothD2 = input(89)
d2 = sma(k2, smoothD2)
/////////////////////////////////////
smoothK3 = input(233)
SMAsmoothK3 = input(3)
k3 = sma(stoch(price, high, low, smoothK3), SMAsmoothK3)
smoothD3 = input(144)
d3 = sma(k3, smoothD3)
////////////////////////////////////////////////
smoothK4 = input(377)
SMAsmoothK4 = input(2)
k4 = sma(stoch(price, high, low, smoothK4), SMAsmoothK4)
smoothD4 = input(233)
d4 = sma(k4, smoothD4)
/////////////////////////////////////////////////
Kavg = avg(k,k1,k2,k3,k4, k4)
plot(Kavg, color=green)
Davg = avg(d,d1,d2,d3,d4, d4)
plot(Davg, color=red)
///////////////////////////////////////
hline(50, color=gray)
long = crossover(Kavg, Davg)// and d < 50
short = crossunder(Kavg, Davg)// and d > 50
last_long = long ? time : nz(last_long[1])
last_short = short ? time : nz(last_short[1])
long_signal = crossover(last_long, last_short)
short_signal = crossover(last_short, last_long)
strategy.entry("Long", strategy.long, when=long_signal)
strategy.entry("Short", strategy.short, when=short_signal)
//len1 = input(3)
//closelong = d[1] < k[len1]
//closeshort = d[1] > k[len1]
//strategy.close("Long", when=closelong)
//strategy.close("Short", when=closeshort)