Trend Trader

Retired_Kopite · study · 92 行 · 点赞 20 · TradingView 原页

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

Pine Script

//@version=6
indicator("Trend Trader", shorttitle = "TT", overlay = true)
 
// ─── Inputs ───────────────────────────────────────────────
colorBars         = input.bool(true,  "Color Bars")
showBuySell       = input.bool(true,  "Show Buy/Sell Labels")
src               = input.source(close, "Source")
movAvgType        = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "LSMA"])
movAvgShortPeriod = input.int(50,  "Short MA Period", minval = 1)
movAvgLongPeriod  = input.int(100, "Long MA Period",  minval = 1)
macdFastLength    = input.int(12, "MACD Fast Length",   minval = 1)
macdSlowLength    = input.int(26, "MACD Slow Length",   minval = 1)
macdSignalLength  = input.int(9,  "MACD Signal Length", minval = 1)
adxLength         = input.int(14, "ADX Length", minval = 1)
adxThreshold      = input.float(25, "ADX Threshold")
 
// ─── ADX (close-to-close version, matching the original script) ───
float up   = close - close[1]
float down = -(close - close[1])
float plusDM  = na(up)   ? 0.0 : (up > down and up > 0 ? up : 0.0)
float minusDM = na(down) ? 0.0 : (down > up and down > 0 ? down : 0.0)
float truerange = ta.atr(adxLength)
 
float plus1  = 100 * ta.rma(plusDM,  adxLength) / truerange
float minus1 = 100 * ta.rma(minusDM, adxLength) / truerange
 
var float plus  = na
var float minus = na
plus  := plus1  == 0 ? plus  : plus1
minus := minus1 == 0 ? minus : minus1
 
float sum = plus + minus
float adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxLength)
float sig = adx
 
// ─── MACD ─────────────────────────────────────────────────
float fast_ma = ta.ema(src, macdFastLength)
float slow_ma = ta.ema(src, macdSlowLength)
float macd    = fast_ma - slow_ma
float signal  = ta.ema(macd, macdSignalLength)
 
// Crosses calculated on every bar (required for v6 lazy evaluation)
bool crossUp   = ta.crossover(macd, signal)
bool crossDown = ta.crossover(signal, macd)
 
bool macd_crossup   = macd < 0 and crossUp
bool macd_crossdown = macd > 0 and crossDown
bool bullish = sig > adxThreshold and macd_crossup
bool bearish = sig > adxThreshold and macd_crossdown
 
plotshape(showBuySell and bullish, title = "Buy",  text = "Buy",  style = shape.labelup,
     location = location.belowbar, color = color.green, textcolor = color.white, size = size.small)
plotshape(showBuySell and bearish, title = "Sell", text = "Sell", style = shape.labeldown,
     location = location.abovebar, color = color.red,   textcolor = color.white, size = size.small)
 
// ─── Moving averages (all calculated every bar) ──────────
float shortEMA  = ta.ema(src, movAvgShortPeriod)
float shortSMA  = ta.sma(src, movAvgShortPeriod)
float shortWMA  = ta.wma(src, movAvgShortPeriod)
float shortLSMA = ta.linreg(src, movAvgShortPeriod, 0)
 
float longEMA  = ta.ema(src, movAvgLongPeriod)
float longSMA  = ta.sma(src, movAvgLongPeriod)
float longWMA  = ta.wma(src, movAvgLongPeriod)
float longLSMA = ta.linreg(src, movAvgLongPeriod, 0)
 
float short = switch movAvgType
    "SMA"  => shortSMA
    "WMA"  => shortWMA
    "LSMA" => shortLSMA
    => shortEMA
 
float long = switch movAvgType
    "SMA"  => longSMA
    "WMA"  => longWMA
    "LSMA" => longLSMA
    => longEMA
 
color shortcol = short > short[1] ? color.green : short < short[1] ? color.red : color.gray
color longcol  = long  > long[1]  ? color.green : long  < long[1]  ? color.red : color.gray
 
p1 = plot(short, "Short Period", color = shortcol, linewidth = 1, style = plot.style_linebr)
p2 = plot(long,  "Long Period",  color = longcol,  linewidth = 2, style = plot.style_linebr)
fill(p1, p2, color = color.new(color.gray, 80), title = "MA Cloud")
 
// ─── Bar color ────────────────────────────────────────────
float usedEma = short
barcolor(colorBars ? (hlc3 > usedEma ? color.green : hlc3 < usedEma ? color.red : color.gray) : na)
 
// ─── Alerts ───────────────────────────────────────────────
alertcondition(bullish, "TT Buy",  "Trend Trader: Buy signal")
alertcondition(bearish, "TT Sell", "Trend Trader: Sell signal")

← 返回列表