Tried & True Composite Strategy (Filtered Zero Cross)

brad4315 · strategy · 174 行 · 点赞 56 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
strategy("Tried & True Composite Strategy (Filtered Zero Cross)", shorttitle="TT Composite (Filtered)", overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
 
// ═══════════════════════════════════════════════════════════════
// INPUTS
// ═══════════════════════════════════════════════════════════════
grp_strat = "Strategy Settings"
useAtr       = input.bool(true, "Enable ATR Stop/Profit?", group=grp_strat)
atrLength    = input.int(14, "ATR Length", minval=1, group=grp_strat)
atrSlMult    = input.float(1.5, "ATR Stop Loss Multiplier", step=0.1, group=grp_strat)
atrTpMult    = input.float(3.0, "ATR Take Profit Multiplier", step=0.1, group=grp_strat)
 
grp_ma   = "Moving Averages (EMA)"
emaFast  = input.int(50,  "Fast EMA", minval=1, group=grp_ma)
emaSlow  = input.int(200, "Slow EMA", minval=1, group=grp_ma)
 
grp_rsi  = "RSI"
rsiLen   = input.int(14, "RSI Length", minval=1, group=grp_rsi)
 
grp_macd = "MACD"
macdFast = input.int(12, "MACD Fast", minval=1, group=grp_macd)
macdSlow = input.int(26, "MACD Slow", minval=1, group=grp_macd)
macdSig  = input.int(9,  "MACD Signal", minval=1, group=grp_macd)
 
grp_stoch = "Stochastic"
stochLen  = input.int(14, "Stoch Length", minval=1, group=grp_stoch)
stochSmoothK = input.int(3, "Smooth K", minval=1, group=grp_stoch)
stochSmoothD = input.int(3, "Smooth D", minval=1, group=grp_stoch)
 
grp_bb   = "Bollinger Bands"
bbLen    = input.int(20, "BB Length", minval=1, group=grp_bb)
bbMult   = input.float(2.0, "BB Multiplier", step=0.1, group=grp_bb)
 
grp_obv  = "OBV"
obvMaLen = input.int(20, "OBV EMA Length", minval=1, group=grp_obv)
 
// ═══════════════════════════════════════════════════════════════
// CALCULATIONS
// ═══════════════════════════════════════════════════════════════
 
// ATR Calculation
atrValue = ta.atr(atrLength)
 
// 1. Moving Averages (EMA Trend Filter)
ema50  = ta.ema(close, emaFast)
ema200 = ta.ema(close, emaSlow)
 
score_ma = 0.0
if close > ema50
    score_ma += 1
else if close < ema50
    score_ma -= 1
 
if close > ema200
    score_ma += 1
else if close < ema200
    score_ma -= 1
 
// 2. RSI
rsi = ta.rsi(close, rsiLen)
score_rsi = rsi > 55 ? 1.0 : rsi < 45 ? -1.0 : 0.0
 
// 3. MACD
[macdLine, signalLine, hist] = ta.macd(close, macdFast, macdSlow, macdSig)
score_macd = 0.0
if macdLine > signalLine
    score_macd += 1
else
    score_macd -= 1
if hist > 0
    score_macd += 0.5
else
    score_macd -= 0.5
 
// 4. Stochastic
k = ta.stoch(close, high, low, stochLen)
d = ta.sma(k, stochSmoothD)
score_stoch = 0.0
if k > d and k > 50
    score_stoch += 1
else if k < d and k < 50
    score_stoch -= 1
 
// 5. Bollinger Bands
[bbMid, bbUpper, bbLower] = ta.bb(close, bbLen, bbMult)
score_bb = 0.0
if close > bbMid
    score_bb += 1
else
    score_bb -= 1
if close > bbUpper
    score_bb += 0.5
else if close < bbLower
    score_bb -= 0.5
 
// 6. OBV (Volume - using EMA)
obv = ta.obv
obvEma = ta.ema(obv, obvMaLen)
score_obv = obv > obvEma ? 1.0 : obv < obvEma ? -1.0 : 0.0
 
// ═══════════════════════════════════════════════════════════════
// COMPOSITE SCORE
// ═══════════════════════════════════════════════════════════════
rawScore = score_ma + score_rsi + score_macd + score_stoch + score_bb + score_obv
maxPossible = 7.0
composite = (rawScore / maxPossible) * 100
 
// ═══════════════════════════════════════════════════════════════
// THRESHOLD TRACKING LOGIC
// ═══════════════════════════════════════════════════════════════
var bool visitedPlus50  = false
var bool visitedMinus50 = false
 
// Track if score reached threshold
if composite >= 50
    visitedPlus50 := true
    visitedMinus50 := false
 
if composite <= -50
    visitedMinus50 := true
    visitedPlus50 := false
 
// Entry Conditions
longCondition  = visitedPlus50 and ta.crossunder(composite, 0)
shortCondition = visitedMinus50 and ta.crossover(composite, 0)
 
// Reset state when trade condition fires
if longCondition
    visitedPlus50 := false
 
if shortCondition
    visitedMinus50 := false
 
// ═══════════════════════════════════════════════════════════════
// STRATEGY EXECUTION
// ═══════════════════════════════════════════════════════════════
if (longCondition)
    strategy.entry("Sell", strategy.long)
    if useAtr
        longSL = close - (atrValue * atrSlMult)
        longTP = close + (atrValue * atrTpMult)
        strategy.exit("Buy Exit", "Buy", stop=longSL, limit=longTP)
 
if (shortCondition)
    strategy.entry("Buy", strategy.short)
    if useAtr
        shortSL = close + (atrValue * atrSlMult)
        shortTP = close - (atrValue * atrTpMult)
        strategy.exit("Sell Exit", "Sell", stop=shortSL, limit=shortTP)
 
// ═══════════════════════════════════════════════════════════════
// PLOTTING (With Gray Neutral Zone)
// ═══════════════════════════════════════════════════════════════
col = composite > 20 ? color.new(color.green, 0) :
      composite < -20 ? color.new(color.red, 0) :
      color.new(color.gray, 30)
 
plot(composite, title="TT Composite Score", color=col, linewidth=2)
 
// Horizontal Reference Lines
hline(0, "Zero Line", color=color.gray, linestyle=hline.style_dashed, linewidth=1)
hline(50,  "+50 Level", color=color.new(color.green, 70), linestyle=hline.style_dotted)
hline(-50, "-50 Level", color=color.new(color.red, 70), linestyle=hline.style_dotted)
hline(80,  "+50 Level", color=color.new(color.green, 70), linestyle=hline.style_dotted)
hline(-80, "-50 Level", color=color.new(color.red, 70), linestyle=hline.style_dotted)
 
 
// Plot Crossover Triangles
plotshape(longCondition, title="Buy Signal", style=shape.triangleup, location=location.bottom, color=color.rgb(230, 42, 9), size=size.small)
plotshape(shortCondition, title="Sell Signal", style=shape.triangledown, location=location.top, color=color.rgb(27, 168, 50), size=size.small)
 
// Background Tinting
bgcolor(composite > 50 ? color.new(color.green, 92) :
        composite < -50 ? color.new(color.red, 92) : na)

← 返回列表