OPEN-SOURCE SCRIPT

A股趋势跟踪买卖点策略

56
//version=6
indicator("A股趋势跟踪买卖点策略", overlay=true)

// ===== 参数设置 =====
// 趋势参数
trendLength = input.int(50, title="趋势周期", minval=20, maxval=100)
atrLength = input.int(14, title="ATR周期", minval=5, maxval=30)
trendFilter = input.float(1.5, title="趋势过滤倍数", minval=1.0, maxval=3.0, step=0.1)

// 相对强度参数
rsPeriod = input.int(30, title="相对强度周期", minval=10, maxval=100)
rsThreshold = input.float(70, title="相对强度阈值", minval=50, maxval=90)

// 成交量参数
volumeMultiplier = input.float(1.5, title="成交量放大阈值", minval=1.0, maxval=3.0, step=0.1)

// 风险管理参数
stopLossMultiplier = input.float(1.0, title="止损ATR倍数", minval=0.5, maxval=2.0, step=0.1)
profitTargetPct = input.float(20.0, title="止盈目标百分比", minval=10.0, maxval=50.0, step=0.5) / 100

// ===== 核心逻辑 =====
// 1. 趋势判断
atr = ta.atr(atrLength)
trendUp = close > ta.sma(close, trendLength) + atr * trendFilter
trendDown = close < ta.sma(close, trendLength) - atr * trendFilter

// 2. 相对强度(RS)计算
marketIndex = request.security("SH000001", timeframe.period, close) // 上证指数
stockReturn = (close - close[rsPeriod]) / close[rsPeriod]
marketReturn = (marketIndex - marketIndex[rsPeriod]) / marketIndex[rsPeriod]
relativeStrength = (stockReturn - marketReturn) * 100
rsValid = relativeStrength >= rsThreshold

// 3. 成交量放大条件
volumeAvg = ta.sma(volume, 50)
volumeSpike = volume > volumeAvg * volumeMultiplier

// 4. 买入条件:趋势向上 + 相对强度强 + 成交量放大
buyCondition = trendUp and rsValid and volumeSpike

// 5. 卖出条件:趋势向下或触发止盈止损
var float entryPrice = na
var bool positionActive = false
if buyCondition and not positionActive
entryPrice := close
positionActive := true

takeProfitLevel = entryPrice * (1 + profitTargetPct)
stopLossLevel = entryPrice * (1 - stopLossMultiplier * atr / entryPrice)
sellCondition = positionActive and (trendDown or close >= takeProfitLevel or close <= stopLossLevel)

// ===== 可视化 =====
// 绘制趋势线
plot(ta.sma(close, trendLength), color=color.blue, title="趋势线")

// 绘制相对强度
hline(rsThreshold, "RS阈值", color=color.orange)
plot(relativeStrength, color=color.purple, title="相对强度")

// 绘制买卖信号
plotshape(series=buyCondition, title="买入信号", location=location.belowbar,
color=color.green, style=shape.labelup, text="买入",
textcolor=color.white, size=size.normal)
plotshape(series=sellCondition, title="卖出信号", location=location.abovebar,
color=color.red, style=shape.labeldown, text="卖出",
textcolor=color.white, size=size.normal)

// ===== 警报 =====
alertcondition(buyCondition, title="买入信号",
message="趋势向上 + 相对强度强 + 成交量放大")
alertcondition(sellCondition, title="卖出信号",
message="趋势向下或触发止盈止损")

Clause de non-responsabilité

Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.