The Assay [TheSimplifier7]

TheSimplifier7 · study · 324 行 · 点赞 550 · TradingView 原页

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

Pine Script

// © TheSimplifier7
//@version=6
indicator("The Assay [TheSimplifier7]", shorttitle = "The Assay", overlay = false, max_lines_count = 500, max_labels_count = 300)
// THE ASSAY 0.1, draft 3, 11 November 2026. NOT COMPILED: only his chart compiles it. Draft 2 compiled on his chart
// (01:09, 11 Nov). Draft 3, his word on it: the threshold is drawn with lines and no longer with plots, so it can never
// stretch the price scale and squash the candles; a struck mark is a red cross drawn as a shape, the size of the gold
// coin (draft 2's grey coin read as a grey ball). The lab's own: the chart dot's hover no longer says the turn was
// known at the wick's bar (it was known at the close the pane marks); the gold line keeps its last 120 levels.
// Draft 2's notes follow.
// THE ASSAY 0.1, draft 2, 11 November 2026 (compiled). Draft 1 compiled at the first
// paste (his gold charts of 00:45 to 00:57, 11 Nov). Draft 2, four faults those charts showed: a struck mark is a coin
// the size of a made one, grey with a cross, so a miss reads as loudly as a hit; the live mark's two prices sit inside
// the chart on their own lines, clear of the price scale; one dot a wick on the chart, the newest mark's; the turn's
// word is kept for the latest turn only, the seam at every turn. Draft 1's notes follow.
// THE ASSAY 0.1, draft 1, 11 November 2026 (compiled). Working name; the public name is
// his. His word: "something like a market cipher b but with our own unique twist on it ... be ambitious and daring".
// A pane of waves, as that family of oscillators has, and three things none of the pages read on 10 Nov says it does:
//  THE WAVES CARRY THE STRUCTURE. They are lit blue while the structure is up and red while it is down. The structure is
//  The Threshold's: on one clock, a swing is a clock bar higher (lower) than 3 clock bars each side; a clock bar closing
//  past the last unbroken swing against the structure turns it. The threshold itself is drawn on the chart in gold.
//  EVERY MARK NAMES TWO PRICES. A mark is the wave crossing its signal on a close: up from under zero, down from over
//  zero. A turn up names the high it must close over to be made (the highest high since the turn up before it) and the low
//  it must not close under (the lowest low since the turn down before it). The mirror for a turn down. While a mark is
//  live its two prices are drawn on the chart.
//  EVERY MARK IS ASSAYED, IN THE OPEN. The first close past either price grades it: gold when a close made it, a cross when
//  a close struck it. Nothing is deleted. On his broker's gold, 3 minute chart, the last 258 gold days, at these settings:
//  6,598 marks, 3,153 made and 3,444 struck. The grade is a record of what price did, not a claim about what it will do.
//  THE WAVES: the unit is the mean bar move (an average of 120 bar-to-bar moves, read from the bar before). The wave is
//  the 12 close average less the 26, in that unit; the signal is its 7 bar average; the tide is the 26 less the 60.
//  Everything is read at a bar's close. It calls nothing ahead.
 
// ----------------------------------------------- settings
string g_w = "THE WAVES"
int    fN    = input.int(12, "The wave: its fast average, closes", minval = 2, maxval = 100, group = g_w)
int    mN    = input.int(26, "The wave: its slow average, closes", minval = 3, maxval = 300, group = g_w, tooltip = "The wave is the fast average of the close less the slow one, in mean bar moves. 12 and 26.")
int    sN    = input.int(60, "The tide: its slower average, closes", minval = 5, maxval = 600, group = g_w, tooltip = "The tide is the wave's slow average less this slower one, in mean bar moves. 60.")
int    gN    = input.int(7, "The signal: bars", minval = 2, maxval = 50, group = g_w, tooltip = "The wave's own average. A mark is the wave crossing it on a close. 7.")
int    mem   = input.int(120, "The unit: memory, bars", minval = 20, maxval = 1000, group = g_w, tooltip = "The mean bar move: an average of this many bar-to-bar moves, read from the bar before. The waves start two memories in.")
string g_s = "THE STRUCTURE"
string ck_in = input.string("Auto", "The structure's clock", options = ["Auto", "5", "15", "30", "60", "120", "240", "D"], group = g_s, tooltip = "The clock the swings are read on, from its closed bars. Auto: 5 minutes on charts up to 1 minute, 15 up to 3 minutes, 30 up to 5, 2 hours up to 15, 4 hours up to 1 hour, the day above. A clock smaller than the chart is not read.")
bool   th_in = input.bool(true, "The threshold on the chart", group = g_s, tooltip = "The close that would turn the structure, in gold on the price chart: the last unbroken swing low while the structure is up, the last unbroken swing high while it is down.")
string g_m = "THE MARKS"
bool   mc_in = input.bool(true, "The marks on the chart, at the wick each turned from", group = g_m)
bool   lv_in = input.bool(true, "The live mark's two prices on the chart", group = g_m, tooltip = "The newest mark still live: the price a close must pass to make it, and the price a close must not pass.")
bool   wd_in = input.bool(true, "The word", group = g_m, tooltip = "RISING: the structure is up and the push is up. PULLING BACK: up, and the push is down. FALLING: down and down. BOUNCING: down, and the push is up. A description of the close, never a call.")
 
// ----------------------------------------------- the house palette
color C_GOLD = #F2C45A
color C_BLUE = #8DB0F5
color C_RED  = #E2574C
color C_REDT = #EE7B70
color C_INK  = #F3EDE2
color C_INK6 = #9AA4B5
color C_INK9 = #7D8CA3
color C_BROKE = #F0604A
color C_ICE  = #DCE6FA
color C_ROSE = #F6C9C4
 
// ----------------------------------------------- the waves
int   TFS  = timeframe.in_seconds()
float mbm  = ta.ema(math.abs(close - close[1]), mem)
float u    = bar_index >= 2 * mem ? mbm[1] : na
float eF   = ta.ema(close, fN)
float eM   = ta.ema(close, mN)
float eS   = ta.ema(close, sN)
float wave = na(u) or u <= 0 ? na : (eF - eM) / u
float tide = na(u) or u <= 0 ? na : (eM - eS) / u
float sig  = ta.ema(wave, gN)
 
// ----------------------------------------------- the structure, The Threshold's rule on one clock
// Read when a new block of the clock opens, from the block just closed, so nothing here moves inside a block.
string ckA  = TFS <= 60 ? "5" : TFS <= 180 ? "15" : TFS <= 300 ? "30" : TFS <= 900 ? "120" : TFS <= 3600 ? "240" : "D"
string ckT  = ck_in == "Auto" ? ckA : ck_in
int    CKS  = timeframe.in_seconds(ckT)
bool   ckOk = CKS >= TFS
string ckNm = CKS >= 86400 ? "day" : CKS % 3600 == 0 ? str.tostring(int(CKS / 3600)) + "h" : str.tostring(int(CKS / 60)) + "m"
bool   ckNw = timeframe.change(ckT)
int    CK_N = 3
var float bH = na
var float bL = na
var float bC = na
var array<float> AH = array.new<float>()
var array<float> AL = array.new<float>()
var float rdg = na
var float flr = na
var int   sTr = 0
bool tUp = false
bool tDn = false
if ckOk and ckNw and not na(bH)
    array.push(AH, bH)
    array.push(AL, bL)
    if array.size(AH) > 60
        array.shift(AH)
        array.shift(AL)
    int an_ = array.size(AH)
    if an_ >= 2 * CK_N + 1
        int   ci_ = an_ - 1 - CK_N
        float sh_ = array.get(AH, ci_)
        float sl_ = array.get(AL, ci_)
        bool  isH = true
        bool  isL = true
        for j_ = ci_ - CK_N to ci_ + CK_N
            if j_ != ci_
                if array.get(AH, j_) >= sh_
                    isH := false
                if array.get(AL, j_) <= sl_
                    isL := false
        if isH
            rdg := sh_
        if isL
            flr := sl_
    if not na(rdg) and bC > rdg
        tUp := sTr < 0
        sTr := 1
        rdg := na
    if not na(flr) and bC < flr
        tDn := sTr > 0
        sTr := -1
        flr := na
if ckNw
    bH := high
    bL := low
    bC := close
else
    bH := math.max(nz(bH, high), high)
    bL := math.min(nz(bL, low), low)
    bC := close
// the threshold: the close that would turn the structure
float thr = sTr > 0 ? flr : sTr < 0 ? rdg : na
 
// ----------------------------------------------- the marks, and their assay
// A mark's dot is pale while it is live, a gold coin once a close has made it, and a red cross once a close has struck it.
f_stamp(label lb_, bool mk_) =>
    if not na(lb_)
        // draft 3: a made mark is a gold coin; a struck mark is a red cross of the same size, drawn as a shape
        label.set_size(lb_, size.small)
        if mk_
            label.set_color(lb_, C_GOLD)
        else
            label.set_style(lb_, label.style_xcross)
            label.set_color(lb_, C_BROKE)
    mk_
int MK_MAX = 60
var float hhv = na
var int   hhT = na
var float llv = na
var int   llT = na
var array<int>   mK  = array.new<int>()
var array<float> mMd = array.new<float>()
var array<float> mFl = array.new<float>()
var array<int>   mSt = array.new<int>()
var array<int>   mRT = array.new<int>()
var array<label> mLp = array.new<label>()
var array<label> mLc = array.new<label>()
int   evMk = 0
int   evGr = 0
float evMd = na
float evFl = na
if barstate.isconfirmed
    if na(hhv) or high > hhv
        hhv := high
        hhT := time
    if na(llv) or low < llv
        llv := low
        llT := time
    // the assay: the first close past either price grades a live mark
    int nm_ = array.size(mK)
    if nm_ > 0
        for q_ = 0 to nm_ - 1
            if array.get(mSt, q_) == 0
                int   gk_ = array.get(mK, q_)
                float gm_ = array.get(mMd, q_)
                float gf_ = array.get(mFl, q_)
                bool  gMd = gk_ > 0 ? close > gm_ : close < gm_
                bool  gSt = gk_ > 0 ? close < gf_ : close > gf_
                if gMd or gSt
                    array.set(mSt, q_, gMd ? 1 : 2)
                    evGr := gMd ? 1 : 2
                    f_stamp(array.get(mLp, q_), gMd)
                    f_stamp(array.get(mLc, q_), gMd)
    // a new mark: the wave crossing its signal on this close, up from under zero or down from over zero
    bool xUp = not na(wave) and not na(sig[1]) and wave > sig and wave[1] <= sig[1] and wave < 0
    bool xDn = not na(wave) and not na(sig[1]) and wave < sig and wave[1] >= sig[1] and wave > 0
    if (xUp or xDn) and not na(hhv) and not na(llv)
        int    nk_ = xUp ? 1 : -1
        float  nM_ = xUp ? hhv : llv
        float  nF_ = xUp ? llv : hhv
        int    nT_ = xUp ? llT : hhT
        bool   nB_ = xUp ? close > nM_ : close < nM_
        string nt_ = "The push turned " + (xUp ? "up" : "down") + ", known at this bar's close. Made on a close " + (xUp ? "over " : "under ") + str.tostring(nM_, format.mintick) + "; struck on a close " + (xUp ? "under " : "over ") + str.tostring(nF_, format.mintick) + ". Gold: made. A red cross: struck. A record of what price did, not a call."
        label  np_ = label.new(bar_index, wave, "", style = label.style_circle, color = nB_ ? C_GOLD : C_INK, size = nB_ ? size.small : size.tiny, tooltip = nt_)
        label  nc_ = na
        string nw_ = "The wick the push turned " + (xUp ? "up" : "down") + " from. The turn was known at the close the pane marks, which can be bars after this wick. Made on a close " + (xUp ? "over " : "under ") + str.tostring(nM_, format.mintick) + "; struck on a close " + (xUp ? "under " : "over ") + str.tostring(nF_, format.mintick) + ". Gold: made. A red cross: struck. A record of what price did, not a call."
        if mc_in
            // draft 2: one dot a wick on the chart, the newest mark's; an older mark that named the same wick gives up its dot
            int pm_ = array.size(mRT)
            if pm_ > 0
                for w_ = 0 to pm_ - 1
                    if array.get(mRT, w_) == nT_ and not na(array.get(mLc, w_))
                        label.delete(array.get(mLc, w_))
                        array.set(mLc, w_, label(na))
            nc_ := label.new(nT_, nF_, "", xloc = xloc.bar_time, yloc = xUp ? yloc.belowbar : yloc.abovebar, style = label.style_circle, color = nB_ ? C_GOLD : C_INK, size = nB_ ? size.small : size.tiny, tooltip = nw_, force_overlay = true)
        array.push(mK, nk_)
        array.push(mMd, nM_)
        array.push(mFl, nF_)
        array.push(mSt, nB_ ? 1 : 0)
        array.push(mRT, nT_)
        array.push(mLp, np_)
        array.push(mLc, nc_)
        if array.size(mK) > MK_MAX
            array.shift(mK)
            array.shift(mMd)
            array.shift(mFl)
            array.shift(mSt)
            array.shift(mRT)
            label.delete(array.shift(mLp))
            label.delete(array.shift(mLc))
        evMk := nk_
        evMd := nM_
        evFl := nF_
        if xUp
            hhv := high
            hhT := time
        else
            llv := low
            llT := time
 
// ----------------------------------------------- the pane
// Each wave is lit at its own edge and fades to nothing at the zero line, in the structure's colour. The wave's edge is
// gold, the pane's one glow.
color cS = sTr > 0 ? C_BLUE : sTr < 0 ? C_RED : C_INK6
color cW = sTr > 0 ? C_ICE : sTr < 0 ? C_ROSE : C_INK
pZ = plot(0.0, "The zero line", color = color.new(C_INK, 82), editable = false)
pT = plot(tide, "The tide", color = color.new(cS, 30), linewidth = 1)
fill(pZ, pT, math.max(nz(tide), 0.0), math.min(nz(tide), 0.0), color.new(cS, tide > 0 ? 42 : 96), color.new(cS, tide > 0 ? 96 : 42), title = "The tide's light")
plot(wave, "The wave, glow", color = color.new(C_GOLD, 80), linewidth = 7)
pW = plot(wave, "The wave", color = C_GOLD, linewidth = 2)
fill(pZ, pW, math.max(nz(wave), 0.0), math.min(nz(wave), 0.0), color.new(cW, wave > 0 ? 58 : 98), color.new(cW, wave > 0 ? 98 : 58), title = "The wave's light")
plot(sig, "The signal", color = color.new(C_INK, 55), linewidth = 1)
// a turn of the structure: a seam down the pane, and the word on the side the waves have left
bgcolor(tUp or tDn ? color.new(C_INK, 86) : na, title = "A turn of the structure")
float wR = ta.highest(math.abs(nz(wave)), 100)
// draft 2: the word is kept for the latest turn only; the seam stays at every turn
var label tnL = na
if tUp or tDn
    label.delete(tnL)
    tnL := label.new(bar_index, tUp ? -0.35 * nz(wR, 1.0) : 0.35 * nz(wR, 1.0), tUp ? "TURNED UP" : "TURNED DOWN", style = label.style_none, textcolor = tUp ? C_BLUE : C_REDT, size = size.tiny, tooltip = "A " + ckNm + " block closed past the threshold, and the structure turned " + (tUp ? "up." : "down.") + " A fact of the close, not a call.")
// the word, top right of the pane
var table wT = table.new(position.top_right, 1, 2, bgcolor = color(na), border_width = 0)
if barstate.islast and wd_in and sTr != 0 and not na(wave)
    bool   pu_ = wave > sig
    string ww_ = sTr > 0 ? (pu_ ? "RISING" : "PULLING BACK") : (pu_ ? "BOUNCING" : "FALLING")
    table.cell(wT, 0, 0, ww_, text_color = sTr > 0 ? C_BLUE : C_REDT, text_size = size.large, text_halign = text.align_right, bgcolor = color(na))
    table.cell(wT, 0, 1, (pu_ ? "the push is up" : "the push is down") + ", the structure " + (sTr > 0 ? "up" : "down") + " (" + ckNm + ")", text_color = C_INK6, text_size = size.small, text_halign = text.align_right, bgcolor = color(na))
 
// ----------------------------------------------- the chart
// The threshold in gold. Draft 3: drawn with lines, not plots. A plot on the price chart joins its scale, so a threshold
// far from the candles on screen stretched the scale and squashed them; a line never moves the scale. Level by level, a
// riser where the level changes, the last 120 levels kept.
int TH_MAX = 480
var array<line> thLn = array.new<line>()
var line thH = na
var line thG = na
bool thCh = (na(thr) != na(thr[1])) or (not na(thr) and not na(thr[1]) and thr != thr[1])
if th_in
    if thCh
        if not na(thH)
            line.set_x2(thH, time)
            line.set_x2(thG, time)
        if not na(thr) and not na(thr[1])
            array.push(thLn, line.new(time, thr[1], time, thr, xloc = xloc.bar_time, color = color.new(C_GOLD, 78), width = 7, force_overlay = true))
            array.push(thLn, line.new(time, thr[1], time, thr, xloc = xloc.bar_time, color = C_GOLD, width = 2, force_overlay = true))
        if not na(thr)
            thG := line.new(time, thr, time, thr, xloc = xloc.bar_time, color = color.new(C_GOLD, 78), width = 7, force_overlay = true)
            thH := line.new(time, thr, time, thr, xloc = xloc.bar_time, color = C_GOLD, width = 2, force_overlay = true)
            array.push(thLn, thG)
            array.push(thLn, thH)
        else
            thG := na
            thH := na
        while array.size(thLn) > TH_MAX
            line.delete(array.shift(thLn))
    else if not na(thH)
        line.set_x2(thH, time)
        line.set_x2(thG, time)
plot(thr, "The threshold, as read", color = C_INK6, display = display.data_window, editable = false)
// the live mark's two prices. Draft 2: inside the chart, each on its own line and ending three bars past the last bar,
// the upper one over its line and the lower one under it, so neither runs under the price scale
var line  lvA  = na
var line  lvB  = na
var label lvLa = na
var label lvLb = na
if barstate.islast
    line.delete(lvA)
    line.delete(lvB)
    label.delete(lvLa)
    label.delete(lvLb)
    int lq_ = -1
    int ln_ = array.size(mK)
    if lv_in and ln_ > 0
        for z_ = ln_ - 1 to 0
            if lq_ < 0 and array.get(mSt, z_) == 0
                lq_ := z_
    if lq_ >= 0
        int   lk_ = array.get(mK, lq_)
        float lm_ = array.get(mMd, lq_)
        float lf_ = array.get(mFl, lq_)
        int   lt_ = array.get(mRT, lq_)
        int   t2_ = time + 3 * TFS * 1000
        lvA  := line.new(lt_, lm_, t2_, lm_, xloc = xloc.bar_time, color = color.new(C_GOLD, 15), style = line.style_dotted, width = 1, force_overlay = true)
        lvB  := line.new(lt_, lf_, t2_, lf_, xloc = xloc.bar_time, color = color.new(C_INK6, 20), style = line.style_dotted, width = 1, force_overlay = true)
        lvLa := label.new(t2_, lm_, (lk_ > 0 ? "made on a close over " : "made on a close under ") + str.tostring(lm_, format.mintick), xloc = xloc.bar_time, style = lk_ > 0 ? label.style_label_lower_right : label.style_label_upper_right, color = color(na), textcolor = C_INK, size = size.small, force_overlay = true)
        lvLb := label.new(t2_, lf_, (lk_ > 0 ? "struck on a close under " : "struck on a close over ") + str.tostring(lf_, format.mintick), xloc = xloc.bar_time, style = lk_ > 0 ? label.style_label_upper_right : label.style_label_lower_right, color = color(na), textcolor = C_INK6, size = size.small, force_overlay = true)
 
// ----------------------------------------------- alerts
alertcondition(evMk == 1, "The Assay: the push turns up", "The Assay: the wave crossed over its signal from under zero, a mark up")
alertcondition(evMk == -1, "The Assay: the push turns down", "The Assay: the wave crossed under its signal from over zero, a mark down")
alertcondition(evGr == 1, "The Assay: a mark made", "The Assay: a close passed the price a live mark named, the mark is made")
alertcondition(evGr == 2, "The Assay: a mark struck", "The Assay: a close passed the price a live mark must not pass, the mark is struck")
alertcondition(tUp, "The Assay: the structure turned up", "The Assay: a block of the structure's clock closed over the threshold, the structure turned up")
alertcondition(tDn, "The Assay: the structure turned down", "The Assay: a block of the structure's clock closed under the threshold, the structure turned down")
// one message at a bar's close carrying the mark's two prices, for an alert set to "Any alert() function call"
if barstate.isconfirmed and evMk != 0
    alert("The Assay, " + syminfo.ticker + ": the push turned " + (evMk > 0 ? "up" : "down") + ". Made on a close " + (evMk > 0 ? "over " : "under ") + str.tostring(evMd, format.mintick) + ", struck on a close " + (evMk > 0 ? "under " : "over ") + str.tostring(evFl, format.mintick) + ".", alert.freq_once_per_bar_close)

← 返回列表