This strategy utilizes the deviation between short-term high-low and short-term & long-term average cost to determine the trend. It aims to increase short-term sensitivity and reduce the cost of consolidation by enlarging the previous and subsequent smoothing average functions, so as to decrease small losses during consolidation while maintaining significant profits when trends emerge.
Calculate short-term cost: Use ta.highest and ta.lowest functions to calculate the highest and lowest prices of the recent shortTerm candles, and take the average as the short-term cost
Calculate long-term cost: Use ta.sma function to calculate the simple moving average of closing prices of recent longTerm candles as the long-term cost
Calculate deviation: Subtract long-term cost from short-term cost
Smooth deviation: Smooth the deviation to reduce misjudgements using ta.sma for simple moving average
Determine trend: If the smoothed deviation is greater than the threshold, judge it as an upward trend. If less than the negative threshold, judge it as a downward trend.
Entry and exit: Go long when tracing upward trend and go short when tracing downward trend.
Risk Resolution:
Overall this is a very simple and direct trend following strategy. Compared to common indicators like moving averages, by calculating the deviation between short and long term costs, it can judge trend changes faster. Meanwhile, the smoothing processing also provides greater flexibility in parameter optimization, allowing sensitivity and misjudgement rates to be balanced by adjusting the smoothing parameters. In summary, this strategy has characteristics like agility, directness and high customizability. It is a promising strategy worth deeper exploration. By continuing to optimize parameters and adding auxiliary judgement conditions, there is potential to further enhance the strategy’s performance.
/*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)