ApexWave Live Sentiment

Michael_Fx_Trader · study · 330 行 · 点赞 69 · TradingView 原页

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

Pine Script

//@version=6
// ═══════════════════════════════════════════════════════════════════════════════════════
//  ApexWave Live Sentiment
//  TradingView Indicator - Pine Script Version 6 - Release 1.2
// ═══════════════════════════════════════════════════════════════════════════════════════
//
//  AUTHOR & PUBLISHER
//  ------------------
//  Author     : Michael_Fx_Trader
//  Publisher  : Michael_Fx_Trader
//  Rights     : All rights reserved by Michael_Fx_Trader.
//
//  ORIGINALITY DECLARATION
//  -----------------------
//  I, Michael_Fx_Trader, declare that:
//   1. "ApexWave Live Sentiment" is an ORIGINAL work, designed and implemented specifically
//      from the author's own written specification and reference screenshot.
//   2. The code is NOT copied, cloned, decompiled or derived from any third-party script,
//      open-source or closed-source, published or private.
//   3. The oscillator formula (ATR-normalised momentum -> tanh bounding -> sine transform
//      -> EMA smoothing), the sentiment formula (four-factor weighted score -> tanh ->
//      EMA smoothing -> clamp) and the 3D histogram renderer (layered box construction)
//      are original compositions of standard, publicly known mathematical building blocks
//      (EMA, SMA, ATR, tanh, sine) and standard Pine Script drawing primitives. These
//      generic tools are not owned by anyone; the specific combination, structure and
//      presentation are the author's own work.
//   4. Copyright (c) 2026 Michael_Fx_Trader. Redistribution, resale, modification or
//      re-publication without the written permission of the author is prohibited.
//
//  VERIFICATION NOTE
//  -----------------
//  Original script ........ YES
//  Author ................. Michael_Fx_Trader
//  Publisher .............. Michael_Fx_Trader
//  Rights holder .......... Michael_Fx_Trader
//  Version ................ 1.2 (Pine Script v6)
//  Created ................ 2026-10-31
//
//  DISCLAIMER
//  ----------
//  This indicator is an analytical tool for educational purposes. It is not financial
//  advice and does not guarantee profits. Trading involves substantial risk of loss.
//
// ───────────────────────────────────────────────────────────────────────────────────────
//  ARCHITECTURE NOTE
//  -----------------
//  The script runs in its own lower pane (overlay = false) and shows the oscillator there:
//  the smooth wave plus a 3D histogram. The sentiment meter (table) uses
//  `force_overlay = true`, which makes TradingView draw it on the MAIN PRICE CHART, like
//  the reference screenshot.
//  Release 1.2: the volume bars on the price chart were removed.
// ───────────────────────────────────────────────────────────────────────────────────────
 
indicator(title = "ApexWave Live Sentiment", shorttitle = "ApexWave", overlay = false,
     precision = 2, max_boxes_count = 500)
 
// ═══════════════════════════════════════════════════════════════════════════════════════
//  1. INPUTS
// ═══════════════════════════════════════════════════════════════════════════════════════
 
// ---- Oscillator settings ----
string GRP_OSC = "Oscillator Settings"
int   momLen     = input.int(10,    "Momentum Calculation Length",   minval = 1,   maxval = 200,  group = GRP_OSC)
int   fastLen    = input.int(8,     "Fast Smoothing Length",         minval = 1,   maxval = 200,  group = GRP_OSC)
int   slowLen    = input.int(21,    "Slow Smoothing Length",         minval = 2,   maxval = 500,  group = GRP_OSC)
int   oscSmooth  = input.int(5,     "Oscillator Smoothing Length",   minval = 1,   maxval = 100,  group = GRP_OSC)
float waveAmp    = input.float(100.0, "Wave Amplitude",              minval = 1.0, maxval = 1000.0, step = 5.0, group = GRP_OSC)
int   volLen     = input.int(14,    "Volatility Normalization Length (ATR)", minval = 1, maxval = 200, group = GRP_OSC)
bool  showHist   = input.bool(true, "Show Histogram",                group = GRP_OSC)
bool  showWave   = input.bool(true, "Show Wave",                     group = GRP_OSC)
bool  showZero   = input.bool(true, "Show Zero Line",                group = GRP_OSC)
 
// ---- 3D histogram settings ----
string GRP_3D = "3D Histogram Settings"
bool  hist3D       = input.bool(true, "Enable 3D Histogram Bars",    group = GRP_3D)
int   hist3DBars   = input.int(150,   "3D Bars Drawn (max 160)",     minval = 10, maxval = 160, group = GRP_3D,
     tooltip = "Newest candles are drawn as 3D pillars. Older candles use clean flat columns.")
float barWidthPct  = input.float(80.0, "Bar Width (% of candle)",    minval = 30.0, maxval = 98.0, step = 1.0, group = GRP_3D)
float depthPct     = input.float(10.0, "3D Depth (% of amplitude)",  minval = 2.0,  maxval = 30.0, step = 1.0, group = GRP_3D)
bool  barEdge      = input.bool(true, "Bright Edge on Front Face",   group = GRP_3D)
 
// ---- Sentiment settings ----
string GRP_SENT = "Sentiment Settings"
float sentSens   = input.float(1.0, "Bullish % Calculation Sensitivity", minval = 0.1, maxval = 5.0, step = 0.1, group = GRP_SENT)
int   sentSmooth = input.int(3,     "Sentiment Smoothing",           minval = 1, maxval = 50,    group = GRP_SENT)
float minPct     = input.float(10.0, "Minimum Displayed Percentage", minval = 1.0,  maxval = 49.0, step = 1.0, group = GRP_SENT)
float maxPct     = input.float(90.0, "Maximum Displayed Percentage", minval = 51.0, maxval = 99.0, step = 1.0, group = GRP_SENT)
int   consLen    = input.int(10,    "Momentum Consistency Lookback", minval = 2, maxval = 100,   group = GRP_SENT)
int   accelLen   = input.int(3,     "Acceleration Lookback",         minval = 1, maxval = 50,    group = GRP_SENT)
color bullCol    = input.color(#4CAF50, "Bullish Color",             group = GRP_SENT)
color bearCol    = input.color(#F23645, "Bearish Color",             group = GRP_SENT)
bool  showMeter  = input.bool(true, "Show Sentiment Meter",          group = GRP_SENT)
bool  showLabels = input.bool(true, "Show Percentage Labels",        group = GRP_SENT)
string meterPosIn = input.string("Middle Right", "Meter Position on Price Chart",
     options = ["Top Right", "Middle Right", "Bottom Right"], group = GRP_SENT)
float meterH     = input.float(45.0, "Meter Total Height (% of chart)", minval = 15.0, maxval = 90.0, step = 5.0, group = GRP_SENT)
float meterW     = input.float(4.0,  "Meter Bar Width (% of chart)",  minval = 1.0,  maxval = 20.0, step = 0.5, group = GRP_SENT)
float labelW     = input.float(12.0, "Label Box Width (% of chart)",  minval = 5.0,  maxval = 30.0, step = 0.5, group = GRP_SENT)
float lblRowH    = input.float(5.0,  "Label Box Height (% of chart)", minval = 2.0,  maxval = 12.0, step = 0.5, group = GRP_SENT)
 
// ---- Appearance settings ----
string GRP_APP = "Appearance Settings"
color bgCol       = input.color(#000000, "Oscillator Background Color", group = GRP_APP)
color waveBullCol = input.color(#1ABC9C, "Wave Color (Bullish - Turquoise)", group = GRP_APP)
color waveBearCol = input.color(#E91E8C, "Wave Color (Bearish - Magenta)",   group = GRP_APP)
color histBullCol = input.color(#4CAF50, "Histogram Bullish Color",  group = GRP_APP)
color histBearCol = input.color(#F23645, "Histogram Bearish Color",  group = GRP_APP)
color zeroCol     = input.color(#F23645, "Zero Line Color",          group = GRP_APP)
int   histTransp  = input.int(0,  "Histogram Transparency",          minval = 0, maxval = 100, group = GRP_APP)
int   waveWidth   = input.int(2,  "Wave Thickness",                  minval = 1, maxval = 6,   group = GRP_APP)
 
// ═══════════════════════════════════════════════════════════════════════════════════════
//  2. HELPER FUNCTIONS
// ═══════════════════════════════════════════════════════════════════════════════════════
 
// Clamp a value between lo and hi.
f_clamp(float x, float lo, float hi) =>
    math.max(lo, math.min(hi, x))
 
// Hyperbolic tangent (bounded output between -1 and +1). Input is clamped to avoid overflow.
f_tanh(float x) =>
    float xc = f_clamp(x, -20.0, 20.0)
    float e  = math.exp(2.0 * xc)
    (e - 1.0) / (e + 1.0)
 
// Darker shade of a color (factor < 1) - used for the shadowed faces of the 3D bars.
f_shade(color c, float f, int t) =>
    color.rgb(color.r(c) * f, color.g(c) * f, color.b(c) * f, t)
 
// Lighter tint of a color (amt 0..1 mixes toward white) - used for the lit top face.
f_tint(color c, float amt, int t) =>
    color.rgb(color.r(c) + (255.0 - color.r(c)) * amt,
              color.g(c) + (255.0 - color.g(c)) * amt,
              color.b(c) + (255.0 - color.b(c)) * amt, t)
 
// ═══════════════════════════════════════════════════════════════════════════════════════
//  3. OSCILLATOR CALCULATION
//     Step 1: ATR-based volatility (scale-free, works on any symbol and timeframe).
//     Step 2: Trend component = (fast EMA - slow EMA) / ATR.
//     Step 3: Momentum component = EMA(price change over momLen) / (ATR * sqrt(momLen)).
//     Step 4: Blend both components (average) -> one normalised momentum value.
//     Step 5: Bound it with tanh to range [-1, +1].
//     Step 6: Cyclical transformation: sin(bounded * pi/2) -> smooth wave in [-1, +1].
//     Step 7: Multiply by amplitude and smooth with EMA -> final oscillator.
//     Uses only current and past data: no lookahead, no future bars, no security() calls.
// ═══════════════════════════════════════════════════════════════════════════════════════
 
float src      = close                               // live price (updates on every tick)
float atrRaw   = ta.atr(volLen)
float atrSafe  = math.max(nz(atrRaw), syminfo.mintick)   // never zero, adapts to price scale
 
float fastMa   = ta.ema(src, fastLen)
float slowMa   = ta.ema(src, slowLen)
float trendN   = (fastMa - slowMa) / atrSafe
 
float momDelta = src - nz(src[momLen], src)
float momSm    = ta.ema(momDelta, fastLen)
float momN     = momSm / (atrSafe * math.sqrt(momLen))
 
float blended  = (trendN + momN) * 0.5
float bounded  = f_tanh(blended * 1.5)
float cyclic   = math.sin(bounded * math.pi / 2.0)
float osc      = ta.ema(waveAmp * cyclic, oscSmooth)
 
// ═══════════════════════════════════════════════════════════════════════════════════════
//  4. SENTIMENT CALCULATION (Bullish % + Bearish % = 100%)
//     Factor 1 (40%): Oscillator direction & strength   = osc / amplitude
//     Factor 2 (20%): Momentum acceleration             = tanh(change of osc over accelLen)
//     Factor 3 (25%): Price trend direction             = tanh((price - slow EMA) / ATR)
//     Factor 4 (15%): Momentum consistency              = SMA of candle direction (+1 / -1)
//     score in [-1, +1] -> raw bullish % = 50 + 50 * tanh(score * sensitivity * 1.5)
//     Then: EMA smoothing (anti-flicker) -> clamp to min/max -> round -> bear = 100 - bull.
// ═══════════════════════════════════════════════════════════════════════════════════════
 
float s1 = osc / waveAmp
float s2 = f_tanh(((osc - nz(osc[accelLen], osc)) / waveAmp) * 4.0)
float s3 = f_tanh((src - slowMa) / atrSafe)
float candleDir = close > open ? 1.0 : close < open ? -1.0 : 0.0
float s4 = ta.sma(candleDir, consLen)
 
float score    = 0.40 * s1 + 0.20 * s2 + 0.25 * s3 + 0.15 * s4
float rawBull  = 50.0 + 50.0 * f_tanh(score * sentSens * 1.5)
float smBull   = ta.ema(rawBull, sentSmooth)
float pctLo    = math.min(minPct, maxPct)
float pctHi    = math.max(minPct, maxPct)
float bullPct  = math.round(f_clamp(nz(smBull, 50.0), pctLo, pctHi))
float bearPct  = 100.0 - bullPct
 
// ═══════════════════════════════════════════════════════════════════════════════════════
//  5. PLOTTING - OSCILLATOR PANE
// ═══════════════════════════════════════════════════════════════════════════════════════
 
bgcolor(bgCol)
 
// Flat histogram columns (full candle width = much clearer than thin lines).
// With 3D enabled they are used only for OLD candles; the newest `hist3DBars` candles
// are drawn as 3D pillars (section 6) from the very same oscillator value.
bool in3DZone = hist3D and bar_index > last_bar_index - hist3DBars
plot(showHist and not in3DZone ? osc : na, title = "ApexWave Histogram", style = plot.style_columns,
     color = osc >= 0 ? color.new(histBullCol, histTransp) : color.new(histBearCol, histTransp))
 
// Invisible baseline used only for the soft fill under the wave.
zeroBase = plot(showWave ? 0.0 : na, title = "Fill Baseline", color = color.new(color.white, 100), editable = false)
 
// Wave.
wavePlot = plot(showWave ? osc : na, title = "ApexWave", linewidth = waveWidth,
     color = osc >= 0 ? waveBullCol : waveBearCol)
 
fill(wavePlot, zeroBase, color = osc >= 0 ? color.new(waveBullCol, 88) : color.new(waveBearCol, 88), title = "Wave Fill")
 
// Zero line.
plot(showZero ? 0.0 : na, title = "Zero Line", color = zeroCol, linewidth = 1)
 
// Values in the Data Window only (no clutter on the chart).
plot(bullPct, title = "Bullish %", display = display.data_window)
plot(bearPct, title = "Bearish %", display = display.data_window)
 
// ═══════════════════════════════════════════════════════════════════════════════════════
//  6. 3D HISTOGRAM (newest candles, drawn inside the oscillator pane with boxes)
//     Every bar is a small 3D pillar growing from the zero line (up if positive, down if
//     negative) and made of 3 boxes:
//       - FRONT face : lit colour with bright edge
//       - SIDE face  : darker shade (shadow)
//       - TOP cap    : lightest tint, glossy highlight at the outer end of the bar
//     The pillar never grows past the oscillator value, so it always matches the wave.
//     Boxes use time coordinates so each face can be a fraction of a candle wide.
// ═══════════════════════════════════════════════════════════════════════════════════════
 
var array<box> histBoxes = array.new<box>()
 
if barstate.islast
    // Clear previous drawing so objects never pile up on live ticks.
    while histBoxes.size() > 0
        box.delete(histBoxes.pop())
 
    if showHist and hist3D
        int   dt     = timeframe.in_seconds() * 1000
        int   nBars  = math.min(hist3DBars, bar_index + 1)
        float wFrac  = barWidthPct / 100.0
        float maxD   = waveAmp * depthPct / 100.0
 
        for i = 0 to nBars - 1
            float v = nz(osc[i])
            float a = math.abs(v)
            if a > waveAmp * 0.004
                float sgn   = v >= 0 ? 1.0 : -1.0
                float d     = math.min(a * 0.35, maxD)       // cap height (shrinks on tiny bars)
                float yEnd  = v - sgn * d                    // where the front/side faces stop
                int   t     = time[i]
                int   xL    = t - math.round(dt * wFrac * 0.5)
                int   xR    = t + math.round(dt * wFrac * 0.5)
                int   xM    = xL + math.round(dt * wFrac * 0.62)   // front / side boundary
                int   xT    = xL + math.round(dt * wFrac * 0.14)   // cap starts a little right (depth)
                color faceC = v >= 0 ? histBullCol : histBearCol
 
                // SIDE face (shadowed).
                box sideBx = box.new(left = xM, top = math.max(0.0, yEnd), right = xR, bottom = math.min(0.0, yEnd),
                     xloc = xloc.bar_time, border_color = na,
                     bgcolor = f_shade(faceC, 0.45, histTransp))
                // FRONT face (lit) with optional bright edge.
                box frontBx = box.new(left = xL, top = math.max(0.0, yEnd), right = xM, bottom = math.min(0.0, yEnd),
                     xloc = xloc.bar_time,
                     border_color = barEdge ? f_tint(faceC, 0.50, 0) : na, border_width = 1,
                     bgcolor = f_shade(faceC, 0.90, histTransp))
                // TOP cap (glossy highlight).
                box capBx = box.new(left = xT, top = math.max(yEnd, v), right = xR, bottom = math.min(yEnd, v),
                     xloc = xloc.bar_time, border_color = na,
                     bgcolor = f_tint(faceC, 0.60, histTransp))
 
                histBoxes.push(sideBx)
                histBoxes.push(frontBx)
                histBoxes.push(capBx)
 
// ═══════════════════════════════════════════════════════════════════════════════════════
//  7. SENTIMENT METER (table drawn on the PRICE CHART via force_overlay)
//     Columns: [0] label box | [1] small gap | [2] vertical bar | [3] right margin
//     Rows   : [0] bullish section | [1] bullish label row | [2] bearish section | [3] bearish label row
//     Row heights come from the live percentages, so the bar always fills 100% of the
//     meter and resizes automatically (e.g. 30/70 -> 30% green, 70% red).
//     Labels have a white box with coloured bold text, like the reference screenshot.
// ═══════════════════════════════════════════════════════════════════════════════════════
 
string meterPos = meterPosIn == "Top Right" ? position.top_right :
     meterPosIn == "Bottom Right" ? position.bottom_right : position.middle_right
 
var table meter = table.new(meterPos, 4, 4, bgcolor = na, border_width = 0, force_overlay = true)
 
if barstate.islast
    float bullH = meterH * bullPct / 100.0
    float bearH = meterH * bearPct / 100.0
    float lbB   = math.min(lblRowH, bullH * 0.85)      // label row inside the green section
    float lbR   = math.min(lblRowH, bearH * 0.85)      // label row inside the red section
    float h0    = math.max(bullH - lbB, 0.05)
    float h1    = math.max(lbB, 0.05)
    float h2    = math.max(bearH - lbR, 0.05)
    float h3    = math.max(lbR, 0.05)
 
    string bullTxt = showMeter and showLabels ? "BULLISH " + str.tostring(int(bullPct)) + "%" : ""
    string bearTxt = showMeter and showLabels ? "BEARISH " + str.tostring(int(bearPct)) + "%" : ""
    color  lblBg   = showMeter and showLabels ? color.white : na
    color  bullBg  = showMeter ? bullCol : na
    color  bearBg  = showMeter ? bearCol : na
 
    // Column 0 - label boxes (white box, coloured bold text)
    table.cell(meter, 0, 0, "",      width = labelW, height = h0, bgcolor = na)
    table.cell(meter, 0, 1, bullTxt, width = labelW, height = h1, text_color = bullCol,
         text_halign = text.align_center, text_valign = text.align_center,
         text_size = size.normal, text_formatting = text.format_bold, bgcolor = lblBg)
    table.cell(meter, 0, 2, "",      width = labelW, height = h2, bgcolor = na)
    table.cell(meter, 0, 3, bearTxt, width = labelW, height = h3, text_color = bearCol,
         text_halign = text.align_center, text_valign = text.align_center,
         text_size = size.normal, text_formatting = text.format_bold, bgcolor = lblBg)
 
    // Column 1 - small transparent gap
    table.cell(meter, 1, 0, "", width = 0.6, height = h0, bgcolor = na)
    table.cell(meter, 1, 1, "", width = 0.6, height = h1, bgcolor = na)
    table.cell(meter, 1, 2, "", width = 0.6, height = h2, bgcolor = na)
    table.cell(meter, 1, 3, "", width = 0.6, height = h3, bgcolor = na)
 
    // Column 2 - the vertical bar (green on top, red below)
    table.cell(meter, 2, 0, "", width = meterW, height = h0, bgcolor = bullBg)
    table.cell(meter, 2, 1, "", width = meterW, height = h1, bgcolor = bullBg)
    table.cell(meter, 2, 2, "", width = meterW, height = h2, bgcolor = bearBg)
    table.cell(meter, 2, 3, "", width = meterW, height = h3, bgcolor = bearBg)
 
    // Column 3 - right margin
    table.cell(meter, 3, 0, "", width = 1.5, height = h0, bgcolor = na)
    table.cell(meter, 3, 1, "", width = 1.5, height = h1, bgcolor = na)
    table.cell(meter, 3, 2, "", width = 1.5, height = h2, bgcolor = na)
    table.cell(meter, 3, 3, "", width = 1.5, height = h3, bgcolor = na)

← 返回列表