该策略通过构建Price Channel,计算价格偏离中心线的距离,再结合均线过滤信号,实现对趋势的识别和跟踪。当价格突破Channel时产生交易信号。该策略同时具有趋势跟踪和突破两个特点。
构建Price Channel
判断趋势方向
产生交易信号
该策略整体较为稳健,能有效跟踪中长线趋势,同时结合趋势突破产生交易信号。通过参数优化和信号过滤可进一步改进策略,使之能适应更多品种和市场环境。
/*backtest
start: 2023-01-30 00:00:00
end: 2024-02-05 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=2
strategy("Noro's Bands Strategy v1.1", shorttitle = "NoroBands str 1.1", overlay=true)
//Settings
needlong = input(true, defval = true, title = "Long")
needshort = input(true, defval = true, title = "Short")
len = input(20, defval = 20, minval = 2, maxval = 200, title = "Period")
color = input(true, "Color")
needbb = input(true, defval = false, title = "Show Bands")
needbg = input(true, defval = false, title = "Show Background")
src = close
//PriceChannel 1
lasthigh = highest(src, len)
lastlow = lowest(src, len)
center = (lasthigh + lastlow) / 2
//dist
dist = abs(src - center)
distsma = sma(dist, len)
hd = center + distsma
ld = center - distsma
//Trend
trend = close < ld and high < hd ? -1 : close > hd and low > ld ? 1 : trend[1]
//Lines
colo = needbb == false ? na : black
plot(hd, color = colo, linewidth = 1, transp = 0, title = "High band")
plot(center, color = colo, linewidth = 1, transp = 0, title = "center")
plot(ld, color = colo, linewidth = 1, transp = 0, title = "Low band")
//Background
col = needbg == false ? na : trend == 1 ? lime : red
bgcolor(col, transp = 90)
//Signals
up = trend == 1 and ((close < open or color == false) or close < hd) ? 1 : 0
dn = trend == -1 and ((close > open or color == false) or close > ld) ? 1 : 0
longCondition = up == 1
if (longCondition)
strategy.entry("Long", strategy.long, needlong == false ? 0 : na)
shortCondition = dn == 1
if (shortCondition)
strategy.entry("Short", strategy.short, needshort == false ? 0 : na)