Value Enjoyer

CryptoCurrentlyYT · study · 163 行 · 点赞 80 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CryptoCurrentlyYT
 
//@version=6
indicator("Value Enjoyer", shorttitle = "Value Enjoyer", overlay = true, behind_chart = true, max_boxes_count = 20, max_lines_count = 20, max_labels_count = 30)
 
// ─── Inputs ──────────────────────────────────────────────────────────────────
const string G_MA = "Moving average"
maLen  = input.int(200, "Length", minval = 1, group = G_MA, tooltip = "Uses the chart's timeframe: 200 on a weekly chart is the 200W MA, 200 on a daily chart is the 200D MA.")
 
const string G_LVL = "Zone boundaries (% from MA)"
lv0 = input.float(-25.0, "Fire Sale → Very Cheap", minval = -99, group = G_LVL, tooltip = "Distance from the MA where the zone above starts. Negative = below the MA, 0 = on the MA. Keep them in increasing order.")
lv1 = input.float(0.0, "Very Cheap → Cheap", minval = -99, group = G_LVL)
lv2 = input.float(25.0, "Cheap → Fair Value", minval = -99, group = G_LVL)
lv3 = input.float(50.0, "Fair Value → Expensive", minval = -99, group = G_LVL)
lv4 = input.float(75.0, "Expensive → Very Expensive", minval = -99, group = G_LVL)
 
const string G_ZONE = "Zone colors"
cF = input.color(#3B1F6680, "Fire Sale", group = G_ZONE)
c0 = input.color(#1A2F8A80, "Very Cheap", group = G_ZONE)
c1 = input.color(#2E6B3380, "Cheap", group = G_ZONE)
c2 = input.color(#8A841F80, "Fair Value", group = G_ZONE)
c3 = input.color(#8A4D0080, "Expensive", group = G_ZONE)
c4 = input.color(#8A1F3080, "Very Expensive", group = G_ZONE)
 
const string G_DISP = "Display"
bandMode   = input.string("Current", "View", options = ["Current", "Historical"], group = G_DISP, tooltip = "Current: today's zones drawn across the whole chart.\nHistorical: zones move with the MA through time, showing which zone price was in at any point in the past.")
cMA        = input.color(#9598A1, "MA color", group = G_DISP)
showLbl    = input.bool(true, "Zone labels", group = G_DISP)
showPctLbl = input.bool(true, "Boundary % labels", group = G_DISP)
lblOffset  = input.int(30, "Label position (bars after last candle)", minval = 0, maxval = 400, group = G_DISP, tooltip = "Raise it if the longest label overlaps the candles; add right margin in Chart settings > Canvas if labels run into the price scale.")
scaleMode  = input.string("Linear", "Chart price scale", options = ["Linear", "Log"], group = G_DISP, tooltip = "Match your chart's price scale so labels sit in the middle of each zone.")
 
// ─── MA and levels ───────────────────────────────────────────────────────────
float ma = ta.sma(close, maLen)
 
float y0 = ma * (1 + lv0 / 100)
float y1 = ma * (1 + lv1 / 100)
float y2 = ma * (1 + lv2 / 100)
float y3 = ma * (1 + lv3 / 100)
float y4 = ma * (1 + lv4 / 100)
float extNow = (close / ma - 1) * 100
 
// Outer zones are capped at the height of their neighboring zone, so autoscale stays tight
float bot = scaleMode == "Log" ? y0 * y0 / y1 : math.max(0, y0 - (y1 - y0))
float top = scaleMode == "Log" ? y4 * y4 / y3 : y4 + (y4 - y3)
 
// Zone index: 0 Fire Sale, 1 Very Cheap, 2 Cheap, 3 Fair Value, 4 Expensive, 5 Very Expensive
int zone = na(ma) ? na : close < y0 ? 0 : close < y1 ? 1 : close < y2 ? 2 : close < y3 ? 3 : close < y4 ? 4 : 5
 
// Chart timeframe as a short suffix: W, D, M, or minutes/hours for intraday
int tfSec = timeframe.in_seconds()
string tfTxt = timeframe.isweekly and timeframe.multiplier == 1 ? "W" : timeframe.isdaily and timeframe.multiplier == 1 ? "D" : timeframe.ismonthly and timeframe.multiplier == 1 ? "M" : tfSec >= 3600 and tfSec < 86400 ? str.tostring(tfSec / 3600) + "H" : timeframe.period
string maName = str.tostring(maLen) + tfTxt + " MA"
 
f_zoneName(int z) =>
    switch z
        0 => "Fire Sale"
        1 => "Very Cheap"
        2 => "Cheap"
        3 => "Fair Value"
        4 => "Expensive"
        => "Very Expensive"
 
f_zoneColor(int z) =>
    switch z
        0 => cF
        1 => c0
        2 => c1
        3 => c2
        4 => c3
        => c4
 
// Bright, opaque version of a zone color, used for its label and its bottom boundary line
f_bright(color c) =>
    float r = color.r(c)
    float g = color.g(c)
    float b = color.b(c)
    float k = 255 / math.max(1, math.max(r, math.max(g, b)))
    color.rgb(r * k + (255 - r * k) * 0.2, g * k + (255 - g * k) * 0.2, b * k + (255 - b * k) * 0.2)
 
f_mid(float lo, float hi) =>
    scaleMode == "Log" ? math.sqrt(lo * hi) : (lo + hi) / 2
 
f_pct(float v) =>
    (v > 0 ? "+" : "") + str.tostring(v, "#.#") + "%"
 
color clear = color.new(color.black, 100)
 
plot(ma, "MA", cMA, 2)
 
// ─── Historical view ─────────────────────────────────────────────────────────
bool hist = bandMode == "Historical"
// In Historical view, hovering any candle shows each boundary's price at that date in the status line and Data Window
hDisp = hist ? display.all : display.none
pBot = plot(hist ? bot : na, "Bottom edge", clear, editable = false, display = display.none)
pY0  = plot(hist ? y0 : na, "Very Cheap start", f_bright(c0), 1, editable = false, display = hDisp)
pY1  = plot(hist ? y1 : na, "Cheap start", f_bright(c1), 1, editable = false, display = hDisp)
pY2  = plot(hist ? y2 : na, "Fair Value start", f_bright(c2), 1, editable = false, display = hDisp)
pY3  = plot(hist ? y3 : na, "Expensive start", f_bright(c3), 1, editable = false, display = hDisp)
pY4  = plot(hist ? y4 : na, "Very Expensive start", f_bright(c4), 1, editable = false, display = hDisp)
pTop = plot(hist ? top : na, "Top edge", clear, editable = false, display = display.none)
fill(pBot, pY0, cF)
fill(pY0, pY1, c0)
fill(pY1, pY2, c1)
fill(pY2, pY3, c2)
fill(pY3, pY4, c3)
fill(pY4, pTop, c4)
 
// ─── Current view zones and labels ───────────────────────────────────────────
// Labels on the right always show today's levels, in both views
var array<box>   boxes  = array.new<box>()
var array<line>  lines  = array.new<line>()
var array<label> labels = array.new<label>()
 
if barstate.islast
    for b in boxes
        b.delete()
    for l in lines
        l.delete()
    for lb in labels
        lb.delete()
    boxes.clear()
    lines.clear()
    labels.clear()
 
    if not na(ma)
        array<float> edges = array.from(bot, y0, y1, y2, y3, y4, top)
        array<float> pcts  = array.from(lv0, lv1, lv2, lv3, lv4)
        int x = bar_index + lblOffset
 
        for i = 0 to 5
            float lo = edges.get(i)
            float hi = edges.get(i + 1)
            color zc = f_zoneColor(i)
            color bc = f_bright(zc)
            if not hist
                boxes.push(box.new(bar_index - 1, hi, bar_index, lo, border_color = clear, border_width = 0, bgcolor = zc, extend = extend.both))
                // Boundary line at the bottom of each zone (skip the bottom edge of Fire Sale)
                if i > 0
                    lines.push(line.new(bar_index - 1, lo, bar_index, lo, color = bc, width = 1, extend = extend.both))
            if showLbl
                // Current zone: filled badge with the % from the MA. Other zones: bold colored text.
                bool cur = zone == i
                string txt = cur ? "● " + f_zoneName(i) + "  " + f_pct(extNow) : f_zoneName(i)
                labels.push(label.new(x, f_mid(lo, hi), txt, style = label.style_label_center, color = cur ? bc : clear, textcolor = cur ? #131722 : bc, size = size.large, text_formatting = text.format_bold))
            if showPctLbl and i > 0
                labels.push(label.new(x, lo, f_pct(pcts.get(i - 1)), style = label.style_label_center, color = #131722, textcolor = bc, size = size.normal, text_formatting = text.format_bold))
 
// ─── Alerts ──────────────────────────────────────────────────────────────────
// Zone is based on the live price, so alerts fire as soon as price crosses a boundary (once per candle), not at the candle close
bool changed = not na(zone) and not na(zone[1]) and zone != zone[1]
 
alertcondition(changed, "Any zone change", "{{ticker}} moved into a new MA valuation zone")
alertcondition(changed and zone == 0, "Entered Fire Sale", "{{ticker}} entered Fire Sale")
alertcondition(changed and zone == 1, "Entered Very Cheap", "{{ticker}} entered Very Cheap")
alertcondition(changed and zone == 2, "Entered Cheap", "{{ticker}} entered Cheap")
alertcondition(changed and zone == 3, "Entered Fair Value", "{{ticker}} entered Fair Value")
alertcondition(changed and zone == 4, "Entered Expensive", "{{ticker}} entered Expensive")
alertcondition(changed and zone == 5, "Entered Very Expensive", "{{ticker}} entered Very Expensive")
 
if changed
    alert(syminfo.ticker + " entered " + f_zoneName(zone) + " (" + f_pct(extNow) + " vs " + maName + ")", alert.freq_once_per_bar)

← 返回列表