本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ============================================================================
// HTF CANDLES 1 · 2 · 3 + SMC Bias & Candle-Based Inversions
// ============================================================================
indicator("HTF Candles 1·2·3 + Bias & Inversions", shorttitle="CRYPTOMASH_HTF Bias+Inversions", overlay=true,
max_boxes_count=500, max_lines_count=500, max_labels_count=100)
// ─────────────────────────────── Layout ────────────────────────────────────
var string GL = "Layout"
numC = input.int(6, "Candles per timeframe", minval=1, maxval=30, group=GL)
cWidth = input.int(3, "Candle body width (bars)", minval=1, maxval=20, group=GL)
cGap = input.int(1, "Gap between candles", minval=0, maxval=10, group=GL)
grpGap = input.int(6, "Gap between timeframe groups", minval=0, maxval=40, group=GL)
offset = input.int(5, "Offset from right edge (bars)", minval=1, maxval=100, group=GL)
pinView = input.bool(true, "Follow the visible chart (right edge of screen)", group=GL)
showLive = input.bool(true, "Show the forming (unclosed) candle", group=GL)
showLabel = input.bool(true, "Show timeframe label", group=GL)
bodyTr = input.int(15, "Body transparency", minval=0, maxval=100, group=GL)
liveTr = input.int(60, "Forming-candle transparency", minval=0, maxval=100, group=GL)
upCol = input.color(color.new(#26a69a, 0), "Up candle", inline="c", group=GL)
dnCol = input.color(color.new(#ef5350, 0), "Down candle", inline="c", group=GL)
// ─────────────────────────── High / Low lines ──────────────────────────────
var string GH = "High / Low lines"
showHL = input.bool(true, "Show high & low lines", group=GH)
hlMode = input.string("Previous candle", "Draw lines on", options=["Previous candle","Every candle"], group=GH)
hlLen = input.int(0, "Line length in bars (0 = from where that HTF candle started)", minval=0, maxval=300, group=GH)
hlWidth = input.int(1, "Line width", minval=1, maxval=4, group=GH)
hlStyle = input.string("Dashed", "Line style", options=["Solid","Dashed","Dotted"], group=GH)
// ───────────────────────────── Countdown ───────────────────────────────────
var string GT = "Countdown timer"
showTimer = input.bool(true, "Show countdown per HTF candle", group=GT)
timerSize = input.string("Small", "Text size", options=["Tiny","Small","Normal","Large"], group=GT)
// ───────────────────────────── SMC Bias Panel ──────────────────────────────
var string GB = "SMC Bias & Inversions"
showBias = input.bool(true, "Show Bias Table", group=GB)
showInvL = input.bool(true, "Show Inversion Zones from Candle", group=GB)
tablePos = input.string("Top Right", "Table Position", options=["Top Right","Bottom Right","Top Left","Bottom Left"], group=GB)
// ──────────────────────────── Timeframes ───────────────────────────────────
var string G1 = "HTF 1"
use1 = input.bool(true, "Enable", inline="t1", group=G1)
tf1 = input.timeframe("D", "", inline="t1", group=G1)
hl1 = input.color(color.new(#42a5f5, 0), "H/L line & timer color", group=G1)
var string G2 = "HTF 2"
use2 = input.bool(true, "Enable", inline="t2", group=G2)
tf2 = input.timeframe("240", "", inline="t2", group=G2)
hl2 = input.color(color.new(#ab47bc, 0), "H/L line & timer color", group=G2)
var string G3 = "HTF 3"
use3 = input.bool(true, "Enable", inline="t3", group=G3)
tf3 = input.timeframe("60", "", inline="t3", group=G3)
hl3 = input.color(color.new(#ffa726, 0), "H/L line & timer color", group=G3)
// ───────────────────── Build HTF candles from chart bars ───────────────────
f_collect(simple string tf, int maxN) =>
var array<float> ao = array.new<float>()
var array<float> ah = array.new<float>()
var array<float> al = array.new<float>()
var array<float> ac = array.new<float>()
var array<int> ast = array.new<int>()
var float wo = na
var float wh = na
var float wl = na
var int ws = na
tNow = time(tf)
newBar = na(tNow[1]) or tNow != tNow[1]
if newBar
if not na(wo)
array.push(ao, wo)
array.push(ah, wh)
array.push(al, wl)
array.push(ac, close[1])
array.push(ast, ws)
while array.size(ao) > maxN
array.shift(ao)
array.shift(ah)
array.shift(al)
array.shift(ac)
array.shift(ast)
wo := open
wh := high
wl := low
ws := bar_index
else
wh := math.max(nz(wh, high), high)
wl := math.min(nz(wl, low), low)
[ao, ah, al, ac, ast, wo, wh, wl, close, ws]
[o1, h1, l1, c1, s1, w1o, w1h, w1l, w1c, w1s] = f_collect(tf1, numC)
[o2, h2, l2, c2, s2, w2o, w2h, w2l, w2c, w2s] = f_collect(tf2, numC)
[o3, h3, l3, c3, s3, w3o, w3h, w3l, w3c, w3s] = f_collect(tf3, numC)
// ─────────────── Sweep & Inversion Detection Logic ────────────────────────
f_analyze_bias(array<float> ao, array<float> ah, array<float> al, array<float> ac, array<int> ast, float curH, float curL, float curC) =>
int n = array.size(ao)
string bias = "Neutral"
bool sweptHigh = false
bool sweptLow = false
bool inverted = false
float invLevel = na
int invBar = na
int invCandleIdx = -1
if n >= 2
float prevH = array.get(ah, n - 1)
float prevL = array.get(al, n - 1)
float prevC = array.get(ac, n - 1)
int prevS = array.get(ast, n - 1)
sweptHigh := (curH > prevH and curC < prevH) or (prevH > array.get(ah, n - 2) and prevC < prevH)
sweptLow := (curL < prevL and curC > prevL) or (prevL < array.get(al, n - 2) and prevC > prevL)
if curC > prevH
inverted := true
bias := "Bullish"
invLevel := prevH
invBar := prevS
invCandleIdx := n - 1
else if curC < prevL
inverted := true
bias := "Bearish"
invLevel := prevL
invBar := prevS
invCandleIdx := n - 1
else if sweptHigh
bias := "Bearish (Sweep)"
else if sweptLow
bias := "Bullish (Sweep)"
[bias, sweptHigh, sweptLow, inverted, invLevel, invBar, invCandleIdx]
[b1, swH1, swL1, inv1, invLvl1, invB1, invIdx1] = f_analyze_bias(o1, h1, l1, c1, s1, w1h, w1l, w1c)
[b2, swH2, swL2, inv2, invLvl2, invB2, invIdx2] = f_analyze_bias(o2, h2, l2, c2, s2, w2h, w2l, w2c)
[b3, swH3, swL3, inv3, invLvl3, invB3, invIdx3] = f_analyze_bias(o3, h3, l3, c3, s3, w3h, w3l, w3c)
// ─────────────── Time left until each HTF candle closes (ms) ───────────────
cd1 = time_close(tf1) - timenow
cd2 = time_close(tf2) - timenow
cd3 = time_close(tf3) - timenow
// ───────────────── Bar index at the right edge of the screen ───────────────
var int rightIdx = 0
if time <= chart.right_visible_bar_time
rightIdx := bar_index
// ───────────────────────────── Drawing store ───────────────────────────────
var array<box> gBox = array.new<box>()
var array<line> gLin = array.new<line>()
var array<label> gLab = array.new<label>()
f_clear() =>
if array.size(gBox) > 0
for i = 0 to array.size(gBox) - 1
box.delete(array.get(gBox, i))
array.clear(gBox)
if array.size(gLin) > 0
for i = 0 to array.size(gLin) - 1
line.delete(array.get(gLin, i))
array.clear(gLin)
if array.size(gLab) > 0
for i = 0 to array.size(gLab) - 1
label.delete(array.get(gLab, i))
array.clear(gLab)
f_lineStyle() =>
hlStyle == "Dashed" ? line.style_dashed : hlStyle == "Dotted" ? line.style_dotted : line.style_solid
f_tSize() =>
timerSize == "Tiny" ? size.tiny : timerSize == "Normal" ? size.normal : timerSize == "Large" ? size.large : size.small
f_pad(int v) =>
v < 10 ? "0" + str.tostring(v) : str.tostring(v)
f_fmt(int ms) =>
int tot = math.floor(math.max(0, ms) / 1000.0)
int d = math.floor(tot / 86400.0)
int h = math.floor((tot % 86400) / 3600.0)
int m = math.floor((tot % 3600) / 60.0)
int s = tot % 60
d > 0 ? str.tostring(d) + "d " + f_pad(h) + ":" + f_pad(m) : f_pad(h) + ":" + f_pad(m) + ":" + f_pad(s)
// One candle: wick + body, with optional inversion highlight border
f_candle(int xl, int xr, float o, float h, float l, float c, int tr, color hlCol, bool drawHL, int startX, bool isInvertedCandle) =>
color col = c >= o ? upCol : dnCol
float bTop = math.max(o, c)
float bBot = math.min(o, c)
int xc = math.round((xl + xr) / 2)
// Highlight inversion candle with a distinct gold/bright border
color borderColor = isInvertedCandle ? color.yellow : color.new(col, tr)
int borderWidth = isInvertedCandle ? 2 : 1
array.push(gLin, line.new(xc, h, xc, l, xloc.bar_index, extend.none, color.new(col, tr), line.style_solid, 1))
array.push(gBox, box.new(xl, bTop, xr, bBot, border_color=borderColor, border_width=borderWidth, bgcolor=color.new(col, math.min(99, tr + bodyTr))))
if showHL and drawHL
color lc = color.new(hlCol, tr)
int x1 = hlLen > 0 ? xl - hlLen : (na(startX) ? xl : startX)
array.push(gLin, line.new(x1, h, xr, h, xloc.bar_index, extend.none, lc, f_lineStyle(), hlWidth))
array.push(gLin, line.new(x1, l, xr, l, xloc.bar_index, extend.none, lc, f_lineStyle(), hlWidth))
f_group(array<float> ao, array<float> ah, array<float> al, array<float> ac, array<int> ast,
float wo, float wh, float wl, float wc, int ws, int cdMs, int xStart, string tfTxt, color hlCol, int invIdx) =>
int step = cWidth + cGap
int x = xStart
float hiY = na
float lastLo = na
int lastXc = na
int n = array.size(ao)
bool liveOn = showLive and not na(wo)
if n > 0
for i = 0 to n - 1
float o = array.get(ao, i)
float h = array.get(ah, i)
float l = array.get(al, i)
float c = array.get(ac, i)
int s = array.get(ast, i)
bool isPrev = i == n - 1
bool drawHL = hlMode == "Every candle" or isPrev
bool isInvCandle = (i == invIdx)
f_candle(x, x + cWidth - 1, o, h, l, c, 0, hlCol, drawHL, s, isInvCandle)
hiY := na(hiY) ? h : math.max(hiY, h)
lastLo := l
lastXc := math.round(x + (cWidth - 1) / 2.0)
x += step
if liveOn
f_candle(x, x + cWidth - 1, wo, wh, wl, wc, liveTr, hlCol, false, ws, false)
hiY := na(hiY) ? wh : math.max(hiY, wh)
lastLo := wl
lastXc := math.round(x + (cWidth - 1) / 2.0)
x += step
if showLabel and not na(hiY) and x > xStart
int xMid = math.round((xStart + x - cGap - 1) / 2)
array.push(gLab, label.new(xMid, hiY, tfTxt, xloc.bar_index, yloc.price,
color.new(color.gray, 100), label.style_label_down, color.gray, size.small))
if showTimer and not na(cdMs) and not na(lastLo) and not na(lastXc)
array.push(gLab, label.new(lastXc, lastLo, f_fmt(cdMs), xloc.bar_index, yloc.price,
color.new(color.gray, 100), label.style_label_up, hlCol, f_tSize()))
x
// ────────────────────────── Bias Dashboard Table ───────────────────────────
var table biasTable = table.new(
tablePos == "Top Right" ? position.top_right : tablePos == "Bottom Right" ? position.bottom_right : tablePos == "Top Left" ? position.top_left : position.bottom_left,
4, 4, bgcolor=color.new(color.black, 20), border_color=color.gray, border_width=1)
if barstate.islast and showBias
table.cell(biasTable, 0, 0, "TF", text_color=color.white, bgcolor=color.gray, text_size=size.small)
table.cell(biasTable, 1, 0, "Sweep", text_color=color.white, bgcolor=color.gray, text_size=size.small)
table.cell(biasTable, 2, 0, "Inversion", text_color=color.white, bgcolor=color.gray, text_size=size.small)
table.cell(biasTable, 3, 0, "Bias", text_color=color.white, bgcolor=color.gray, text_size=size.small)
table.cell(biasTable, 0, 1, tf1, text_color=color.white, text_size=size.small)
table.cell(biasTable, 1, 1, swH1 ? "High Sw" : swL1 ? "Low Sw" : "None", text_color=color.yellow, text_size=size.small)
table.cell(biasTable, 2, 1, inv1 ? "Yes" : "No", text_color=inv1 ? color.green : color.gray, text_size=size.small)
table.cell(biasTable, 3, 1, b1, text_color=str.contains(b1, "Bullish") ? color.green : str.contains(b1, "Bearish") ? color.red : color.gray, text_size=size.small)
table.cell(biasTable, 0, 2, tf2, text_color=color.white, text_size=size.small)
table.cell(biasTable, 1, 2, swH2 ? "High Sw" : swL2 ? "Low Sw" : "None", text_color=color.yellow, text_size=size.small)
table.cell(biasTable, 2, 2, inv2 ? "Yes" : "No", text_color=inv2 ? color.green : color.gray, text_size=size.small)
table.cell(biasTable, 3, 2, b2, text_color=str.contains(b2, "Bullish") ? color.green : str.contains(b2, "Bearish") ? color.red : color.gray, text_size=size.small)
table.cell(biasTable, 0, 3, tf3, text_color=color.white, text_size=size.small)
table.cell(biasTable, 1, 3, swH3 ? "High Sw" : swL3 ? "Low Sw" : "None", text_color=color.yellow, text_size=size.small)
table.cell(biasTable, 2, 3, inv3 ? "Yes" : "No", text_color=inv3 ? color.green : color.gray, text_size=size.small)
table.cell(biasTable, 3, 3, b3, text_color=str.contains(b3, "Bullish") ? color.green : str.contains(b3, "Bearish") ? color.red : color.gray, text_size=size.small)
// ────────────────────────────── Render ─────────────────────────────────────
if barstate.islast
f_clear()
// Draw Inversion Zone lines starting directly from the candle where the inversion occurred
if showInvL
if inv1 and not na(invLvl1) and not na(invB1)
array.push(gLin, line.new(invB1, invLvl1, bar_index + offset, invLvl1, xloc.bar_index, extend.none, color.new(hl1, 20), line.style_solid, 2))
if inv2 and not na(invLvl2) and not na(invB2)
array.push(gLin, line.new(invB2, invLvl2, bar_index + offset, invLvl2, xloc.bar_index, extend.none, color.new(hl2, 20), line.style_solid, 2))
if inv3 and not na(invLvl3) and not na(invB3)
array.push(gLin, line.new(invB3, invLvl3, bar_index + offset, invLvl3, xloc.bar_index, extend.none, color.new(hl3, 20), line.style_solid, 2))
int baseIdx = pinView ? rightIdx : bar_index
int x = baseIdx + offset
if use1
x := f_group(o1, h1, l1, c1, s1, w1o, w1h, w1l, w1c, w1s, cd1, x, tf1, hl1, invIdx1) + grpGap
if use2
x := f_group(o2, h2, l2, c2, s2, w2o, w2h, w2l, w2c, w2s, cd2, x, tf2, hl2, invIdx2) + grpGap
if use3
x := f_group(o3, h3, l3, c3, s3, w3o, w3h, w3l, w3c, w3s, cd3, x, tf3, hl3, invIdx3) + grpGap