Soul Investor | Trend Structure & YTD

fidtrading · study · 158 行 · 点赞 7 · TradingView 原页

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

Pine Script

//@version=6
indicator("Soul Investor | Trend Structure & YTD", overlay=true)
 
// =============================================
// SOUL INVESTOR | TREND STRUCTURE & YTD
// Optimized for 1Y View / Daily Candles
// =============================================
 
// 1. SETTINGS
emaLength = input.int(200, "EMA Period", minval=1, group="Trend Structure")
slopePeriod = input.int(10, "EMA Slope Lookback", minval=1, group="Trend Structure")
 
showSignals = input.bool(true, "Show Trend Transitions", group="Display")
showYTD = input.bool(true, "Show YTD Reference", group="Display")
showDashboard = input.bool(true, "Show Dashboard", group="Display")
 
// 2. LONG-TERM TREND
emaBase = ta.ema(close, emaLength)
 
emaRising = emaBase > emaBase[slopePeriod]
emaFalling = emaBase < emaBase[slopePeriod]
 
aboveEMA = close > emaBase
belowEMA = close < emaBase
 
bull = aboveEMA and emaRising
bear = belowEMA and emaFalling
 
// 3. TREND VISUALIZATION
flowColor = bull ? color.new(color.green, 85) :
     bear ? color.new(color.red, 85) :
     color.new(color.gray, 92)
 
pricePlot = plot(close, display=display.none)
 
emaPlot = plot(
     emaBase,
     title="Long-Term EMA",
     color=color.rgb(48, 51, 58),
     linewidth=2)
 
fill(
     pricePlot,
     emaPlot,
     color=flowColor,
     title="Trend Structure")
 
// 4. CONFIRMED TREND TRANSITIONS
bullSignal = bull and not bull[1] and barstate.isconfirmed
bearSignal = bear and not bear[1] and barstate.isconfirmed
 
plotshape(
     showSignals and bullSignal,
     title="Positive Trend Transition",
     style=shape.triangleup,
     location=location.belowbar,
     color=color.new(color.green, 25),
     size=size.tiny)
 
plotshape(
     showSignals and bearSignal,
     title="Negative Trend Transition",
     style=shape.triangledown,
     location=location.abovebar,
     color=color.new(color.red, 25),
     size=size.tiny)
 
// 5. YEAR-TO-DATE REFERENCE
// Previous year's final trading-day close.
// Designed for daily charts.
 
newYear = timeframe.change("12M")
 
var float previousYearClose = na
 
if newYear and not na(close[1])
    previousYearClose := close[1]
 
// YTD price performance
ytdReturn = not na(previousYearClose) and previousYearClose != 0 ?
     (close / previousYearClose - 1) * 100 : na
 
// Gold horizontal reference line
// plot.style_linebr avoids joining across na values.
plot(
     showYTD ? previousYearClose : na,
     title="Previous Year Close",
     color=color.rgb(190, 145, 45),
     linewidth=2,
     style=plot.style_linebr)
 
// 6. DISTANCE TO EMA
emaDistance = not na(emaBase) and emaBase != 0 ?
     (close / emaBase - 1) * 100 : na
 
// 7. DASHBOARD
var table dashboard = table.new(
     position.top_right,
     2, 5,
     bgcolor=color.rgb(58, 62, 73),
     border_width=0)
 
if barstate.islast
    if showDashboard
        trendText = bull ? "POSITIVE" :
             bear ? "NEGATIVE" : "TRANSITION"
 
        trendColor = bull ? color.lime :
             bear ? color.red : color.silver
 
        slopeText = emaRising ? "RISING" :
             emaFalling ? "FALLING" : "FLAT"
 
        distanceText = na(emaDistance) ? "N/A" :
             str.tostring(emaDistance, "#.##") + "%"
 
        ytdText = na(ytdReturn) ? "N/A" :
             str.tostring(ytdReturn, "#.##") + "%"
 
        table.cell(dashboard, 0, 0, "SOUL INVESTOR",
             text_color=color.white)
        table.cell(dashboard, 1, 0, "TREND & YTD",
             text_color=color.white)
 
        table.cell(dashboard, 0, 1, "TREND",
             text_color=color.white)
        table.cell(dashboard, 1, 1, trendText,
             text_color=trendColor)
 
        table.cell(dashboard, 0, 2, "EMA SLOPE",
             text_color=color.white)
        table.cell(dashboard, 1, 2, slopeText,
             text_color=color.white)
 
        table.cell(dashboard, 0, 3, "DISTANCE TO EMA",
             text_color=color.white)
        table.cell(dashboard, 1, 3, distanceText,
             text_color=na(emaDistance) ? color.silver :
                 emaDistance >= 0 ? color.lime : color.red)
 
        table.cell(dashboard, 0, 4, "YTD",
             text_color=color.white)
        table.cell(dashboard, 1, 4, ytdText,
             text_color=na(ytdReturn) ? color.silver :
                 ytdReturn >= 0 ? color.lime : color.red)
    else
        table.clear(dashboard, 0, 0, 1, 4)
 
// 8. ALERT CONDITIONS
alertcondition(
     bullSignal,
     title="Positive Trend Transition",
     message="Soul Investor: Positive trend transition on {{ticker}}")
 
alertcondition(
     bearSignal,
     title="Negative Trend Transition",
     message="Soul Investor: Negative trend transition on {{ticker}}")

← 返回列表