该策略是一种利用短期高低点和短期与长期平均成本之间均差判断趋势的指标策略。策略旨在增加短线敏感度,通过增大前后的均值平滑函数来降低盘整的损耗,以减少盘整中的小损失,同时在波段出现时保持大盈利。
计算短期成本:利用ta.highest和ta.lowest函数计算最近shortTerm根K线的最高价和最低价,然后求平均作为短期成本
计算长期成本:利用ta.sma函数计算最近longTerm根K线的收盘价的简单移动平均作为长期成本
计算均差:短期成本减去长期成本
平滑均差:对均差进行平滑处理,以减少误判,这里采用ta.sma进行简单移动平均
判断趋势:设定阈值threshold,当平滑均差大于threshold则判断为上涨趋势,当小于负的threshold则判断为下跌趋势
进出场:做多时跟踪上涨趋势,做空时跟踪下跌趋势
风险解决方法:
该策略整体是一个非常简单直接的趋势跟踪策略。相比常见的移动平均等指标,其通过计算短长期成本的均差,可以更快地判断趋势转折。同时平滑处理也使其参数优化空间较大,可以通过调整平滑参数来平衡敏感度与误判率。总的来说,该策略具有敏捷、直接、可定制性强等特点,是一种值得深度挖掘的有潜力的策略思路。通过继续优化参数以及加入辅助判断条件,有望进一步增强策略表现。
/*backtest
start: 2024-01-01 00:00:00
end: 2024-01-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dead0001ing1
//@version=5
strategy("Trend-Following Indicator", overlay=true)
// 設置參數
shortTerm = input(5, "Short Term")
longTerm = input(20, "Long Term")
smooth = input(5, "Smoothing")
threshold = input(0, "Threshold")
// 計算短期成本
shortH = ta.highest(high, shortTerm)
shortL = ta.lowest(low, shortTerm)
shortCost = (shortH + shortL) / 2
// 計算長期成本
longCost = ta.sma(close, longTerm)
// 計算均差
deviation = shortCost - longCost
// 平滑均差
smoothedDeviation = ta.sma(deviation, smooth)
// 判斷順勢
isTrendingUp = smoothedDeviation > threshold
isTrendingDown = smoothedDeviation < -threshold
// 顯示順勢信號
plotshape(isTrendingUp, title="Trending Up", location=location.belowbar, color=color.green, style=shape.labelup, text="Up", size=size.small)
plotshape(isTrendingDown, title="Trending Down", location=location.abovebar, color=color.red, style=shape.labeldown, text="Down", size=size.small)
// 定義進出場策略
if isTrendingUp
strategy.entry("Long", strategy.long)
strategy.close("Long", when=isTrendingDown)
if isTrendingDown
strategy.entry("Short", strategy.short)
strategy.close("Short", when=isTrendingUp)