本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script(R) code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) Antonio Jones (AJ)
//
// Base Hits 2.0
// Open source. Free to use, study and improve.
// Indicator only: it paints signals and fires alerts. It places no orders.
// EMA 8/14/50/200 + TDI (RSI 21) + order flow estimate + multi-timeframe EMA table.
//@version=6
indicator("Base Hits 2.0", shorttitle="Base Hits 2.0", overlay=false, max_lines_count=100, max_labels_count=500)
// ---------- TDI ----------
length_rsi = input.int(21, "RSI Length", group="TDI")
length_band = input.int(34, "Band Length", group="TDI")
length_fast = input.int(2, "Fast (RSI)", group="TDI")
length_sl = input.int(7, "Trend Line", group="TDI")
// ---------- Appearance ----------
gBuy = input.color(#00FF00, "Buy color", group="Appearance")
gSell = input.color(#FF0000, "Sell color", group="Appearance")
gChop = input.color(#FF69B4, "Chop (pink) color", group="Appearance")
bgTr = input.int(88, "Background shade (chart + TDI match)", minval=60, maxval=99, group="Appearance")
tPos = input.string("top_right", "Table position", options=["top_left","top_right","bottom_left","bottom_right","middle_left","middle_right"], group="Appearance")
tSize = input.string("small", "Table size", options=["tiny","small","normal","large","huge"], group="Appearance")
// ---------- Candles (independent - only colors the candles) ----------
candleUp = input.color(#00C853, "Candle up (buyers)", group="Candles")
candleDown = input.color(#D50000, "Candle down (sellers)", group="Candles")
// ---------- Order Flow ----------
showDelta = input.bool(true, "Color candles by delta (up=buyers, down=sellers)", group="Order Flow")
showBg = input.bool(true, "Chart background (50 vs 200 EMA)", group="Order Flow")
lookback = input.int(300, "Volume profile lookback", minval=50, group="Order Flow")
bins = input.int(24, "Profile resolution", minval=6, maxval=50, group="Order Flow")
volMult = input.float(2.0, "Absorption wall size (higher=fewer)", step=0.5, group="Order Flow")
wickPct = input.float(0.5, "Wall rejection wick %", step=0.1, group="Order Flow")
cYellow = #FFD600
cWhite = #FFFFFF
cBlack = color.black
cHdr = #232f42
cDiv = #3d4a5c
tblSize = tSize=="tiny" ? size.tiny : tSize=="small" ? size.small : tSize=="large" ? size.large : tSize=="huge" ? size.huge : size.normal
// ---------- EMAs ----------
ema8 = ta.ema(close, 8)
ema14 = ta.ema(close, 14)
ema50 = ta.ema(close, 50)
ema200 = ta.ema(close, 200)
// ---------- TDI ----------
r = ta.rsi(close, length_rsi)
ma = ta.sma(r, length_band)
offs = 1.6185 * ta.stdev(r, length_band)
up = ma + offs
dn = ma - offs
l50 = ma
rsi_pl = ta.sma(r, length_fast)
tl = ta.sma(r, length_sl)
buyZone = l50 > 50
sellZone = l50 < 50
// ---------- EMA stack / regime / direction gate ----------
emaUp3 = ema8 > ema14 and ema14 > ema50
emaDn3 = ema8 < ema14 and ema14 < ema50
bull200 = ema8 > ema200 and ema14 > ema200 and ema50 > ema200
bear200 = ema8 < ema200 and ema14 < ema200 and ema50 < ema200
buyAllowed = bull200 and buyZone
sellAllowed = bear200 and sellZone
inBand = close <= math.max(ema50, ema200) and close >= math.min(ema50, ema200)
emaCol = inBand ? gChop : emaUp3 ? gBuy : emaDn3 ? gSell : color.gray
// ---------- 8/14 EMAs turn green / red (fast cross) ----------
ema814Green = ta.crossover(ema8, ema14)
ema814Red = ta.crossunder(ema8, ema14)
// ---------- Chart + TDI color-match (shading alerts) ----------
chartGreen = ema50 > ema200
chartRed = ema50 < ema200
tdiGreen = l50 > 50
tdiRed = l50 < 50
bothGreen = chartGreen and tdiGreen
bothRed = chartRed and tdiRed
bothGreenNow = bothGreen and not bothGreen[1]
bothRedNow = bothRed and not bothRed[1]
// ---------- A+ logic (gated by direction) ----------
tdiUp = rsi_pl > tl and tl > l50 and l50 > 50
tdiDn = rsi_pl < tl and tl < l50 and l50 < 50
isValidBuy = emaUp3 and tdiUp and buyAllowed
isValidSell = emaDn3 and tdiDn and sellAllowed
aplusBuy = isValidBuy and not isValidBuy[1]
aplusSell = isValidSell and not isValidSell[1]
tdiBuy = ((rsi_pl > l50 and tl > l50) and not (rsi_pl[1] > l50[1] and tl[1] > l50[1])) and buyAllowed
tdiSell = ((rsi_pl < l50 and tl < l50) and not (rsi_pl[1] < l50[1] and tl[1] < l50[1])) and sellAllowed
statusTxt = inBand ? "NO-GO" : buyAllowed ? "BUYS ONLY" : sellAllowed ? "SELLS ONLY" : "WAIT"
statusCol = inBand ? gChop : buyAllowed ? gBuy : sellAllowed ? gSell : color.gray
aplusNow = isValidBuy ? "BUY" : isValidSell ? "SELL" : "-"
// ---------- Console (Pine Logs) ----------
if aplusBuy
log.info("A+ BUY fired @ {0}", close)
if aplusSell
log.info("A+ SELL fired @ {0}", close)
if barstate.islast
log.info("LIVE -> A+ {0} | buyAllowed={1} sellAllowed={2} | bull200={3} bear200={4} | L50={5}", aplusNow, buyAllowed, sellAllowed, bull200, bear200, l50)
// ---------- Order flow delta ----------
rng = high - low
buyVol = rng > 0 ? volume * (close - low) / rng : volume * 0.5
sellVol = volume - buyVol
delta = buyVol - sellVol
buyPct = volume > 0 ? buyVol / volume * 100 : 0.0
sellPct = 100 - buyPct
// ============================================================
// PRICE LAYER
// ============================================================
plot(ema8, "8 EMA", color=emaCol, linewidth=2, force_overlay=true)
plot(ema14, "14 EMA", color=emaCol, linewidth=2, force_overlay=true)
plot(ema50, "50 EMA", color=cYellow, linewidth=2, force_overlay=true)
plot(ema200, "200 EMA", color=cWhite, linewidth=2, force_overlay=true)
priceBg = showBg ? (ema50 > ema200 ? color.new(gBuy, bgTr) : ema50 < ema200 ? color.new(gSell, bgTr) : na) : na
bgcolor(priceBg, title="Chart background", force_overlay=true)
deltaCol = delta >= 0 ? candleUp : candleDown
plotcandle(showDelta?open:na, showDelta?high:na, showDelta?low:na, showDelta?close:na, "Delta candles", deltaCol, deltaCol, bordercolor=deltaCol, force_overlay=true)
// Chart flags (gated): BUY on BOTTOM, SELL on TOP
if tdiBuy
label.new(bar_index, na, "BUY", yloc=yloc.belowbar, style=label.style_label_up, color=cBlack, textcolor=#00FF00, size=size.normal, force_overlay=true)
if tdiSell
label.new(bar_index, na, "SELL", yloc=yloc.abovebar, style=label.style_label_down, color=cBlack, textcolor=#FF0000, size=size.normal, force_overlay=true)
// A+ SETUP - ONLY on the live/most-recent candle
var label aLive = na
if barstate.islast
label.delete(aLive)
if isValidBuy
aLive := label.new(bar_index, na, "A+ SETUP BUY", yloc=yloc.abovebar, style=label.style_label_down, color=cBlack, textcolor=gBuy, size=size.large, force_overlay=true)
else if isValidSell
aLive := label.new(bar_index, na, "A+ SETUP SELL", yloc=yloc.belowbar, style=label.style_label_up, color=cBlack, textcolor=gSell, size=size.large, force_overlay=true)
// ---------- Point of Control + absorption walls ----------
var line pocLine = na
var label pocLbl = na
var float[] vBin = array.new_float(bins, 0.0)
var float[] bBin = array.new_float(bins, 0.0)
if barstate.islast
lb = math.min(lookback, bar_index)
hh = ta.highest(high, lb)
ll = ta.lowest(low, lb)
binSize = (hh - ll) / bins
if binSize > 0
for k = 0 to bins - 1
array.set(vBin, k, 0.0)
array.set(bBin, k, 0.0)
for i = 0 to lb - 1
p = (high[i] + low[i]) / 2
idx = math.max(0, math.min(bins - 1, int((p - ll) / binSize)))
r2 = high[i] - low[i]
bv = r2 > 0 ? volume[i] * (close[i] - low[i]) / r2 : volume[i] * 0.5
array.set(vBin, idx, array.get(vBin, idx) + volume[i])
array.set(bBin, idx, array.get(bBin, idx) + bv)
pocIdx = 0
mx = 0.0
for j = 0 to bins - 1
v = array.get(vBin, j)
if v > mx
mx := v
pocIdx := j
pocPrice = ll + (pocIdx + 0.5) * binSize
pocBuy = array.get(bBin, pocIdx)
pocSell = array.get(vBin, pocIdx) - pocBuy
pocCol = pocBuy >= pocSell ? gBuy : gSell
line.delete(pocLine)
label.delete(pocLbl)
pocLine := line.new(bar_index - lb, pocPrice, bar_index, pocPrice, color=pocCol, width=4, extend=extend.right, force_overlay=true)
pocLbl := label.new(bar_index + 30, pocPrice, "POINT OF CONTROL", color=cBlack, textcolor=pocCol, style=label.style_label_left, size=size.large, force_overlay=true)
avgV = ta.sma(volume, 10)
massive = volume > avgV * volMult
bodyAbs = math.abs(close - open)
smallBdy = rng > 0 and bodyAbs < rng * 0.3
rejDn = rng > 0 and (math.min(open, close) - low) >= rng * wickPct
rejUp = rng > 0 and (high - math.max(open, close)) >= rng * wickPct
isDemand = massive and smallBdy and rejDn
isSupply = massive and smallBdy and rejUp
var line dWall = na
var label dLbl = na
if isDemand
line.delete(dWall)
label.delete(dLbl)
dWall := line.new(bar_index, low, bar_index + 1, low, color=gBuy, width=4, extend=extend.right, force_overlay=true)
dLbl := label.new(bar_index, na, "BUY WALL", yloc=yloc.belowbar, color=cBlack, textcolor=gBuy, style=label.style_label_up, size=size.large, force_overlay=true)
var line sWall = na
var label sLbl = na
if isSupply
line.delete(sWall)
label.delete(sLbl)
sWall := line.new(bar_index, high, bar_index + 1, high, color=gSell, width=4, extend=extend.right, force_overlay=true)
sLbl := label.new(bar_index, na, "SELL WALL", yloc=yloc.abovebar, color=cBlack, textcolor=gSell, style=label.style_label_down, size=size.large, force_overlay=true)
// ============================================================
// TDI PANE
// ============================================================
hline(70, "Upper", color=color.new(color.gray,40), linestyle=hline.style_dashed)
hline(50, "Static 50", color=color.white, linewidth=2)
hline(30, "Lower", color=color.new(color.gray,40), linestyle=hline.style_dashed)
pUp = plot(up, "Up band", color=color.new(color.gray,80))
pDn = plot(dn, "Dn band", color=color.new(color.gray,80))
fill(pUp, pDn, color=color.new(color.gray,95))
pL = plot(l50, "L50", color=cYellow, linewidth=3)
pG = plot(rsi_pl, "RSI", color=#00FF00, linewidth=2)
pR = plot(tl, "trend", color=#FF0000, linewidth=2)
fill(pG, pR, color=rsi_pl > tl ? color.new(#00FF00,80) : color.new(#FF0000,80))
paneBg = buyZone ? color.new(gBuy, bgTr) : sellZone ? color.new(gSell, bgTr) : na
bgcolor(paneBg, title="TDI zone wash")
if tdiBuy
label.new(bar_index, 10, "BUY", style=label.style_label_up, color=cBlack, textcolor=#00FF00, size=size.small)
if tdiSell
label.new(bar_index, 80, "SELL", style=label.style_label_down, color=cBlack, textcolor=#FF0000, size=size.small)
// ============================================================
// ONE COMBINED TABLE - sectioned + framed
// ============================================================
f_emaDir(tf) =>
[e8, e14, e50, e200] = request.security(syminfo.tickerid, tf, [ta.ema(close,8), ta.ema(close,14), ta.ema(close,50), ta.ema(close,200)], barmerge.gaps_off, barmerge.lookahead_off)
e8 > e14 and e14 > e50 and e50 > e200 ? 1 : e8 < e14 and e14 < e50 and e50 < e200 ? -1 : 0
dir30 = f_emaDir("30S")
dir15 = f_emaDir("15")
dir60 = f_emaDir("60")
dir240 = f_emaDir("240")
tpos = tPos=="top_left"?position.top_left : tPos=="bottom_left"?position.bottom_left : tPos=="bottom_right"?position.bottom_right : tPos=="middle_left"?position.middle_left : tPos=="middle_right"?position.middle_right : position.top_right
var table t = table.new(tpos, 2, 12, bgcolor=cBlack, frame_width=2, frame_color=cDiv, border_width=0)
f_div(row) =>
table.cell(t, 0, row, "", bgcolor=cDiv, text_size=size.tiny)
table.cell(t, 1, row, "", bgcolor=cDiv, text_size=size.tiny)
f_emaRow(nm, d, row) =>
table.cell(t, 0, row, nm, bgcolor=cBlack, text_color=cWhite, text_size=tblSize)
table.cell(t, 1, row, d==1?"BUY":d==-1?"SELL":"NOT ALIGNED", bgcolor=cBlack, text_color=d==1?gBuy:d==-1?gSell:color.gray, text_size=tblSize)
if barstate.islast
// --- A+ NOW ---
table.cell(t, 0, 0, "A+ NOW", bgcolor=cHdr, text_color=cWhite, text_size=tblSize)
table.cell(t, 1, 0, aplusNow, bgcolor=cHdr, text_color=isValidBuy?gBuy:isValidSell?gSell:color.gray, text_size=tblSize)
f_div(1)
// --- Order flow ---
table.cell(t, 0, 2, "BUYERS", bgcolor=cBlack, text_color=gBuy, text_size=tblSize)
table.cell(t, 1, 2, str.tostring(buyPct, "#") + "%", bgcolor=cBlack, text_color=gBuy, text_size=tblSize)
table.cell(t, 0, 3, "SELLERS", bgcolor=cBlack, text_color=gSell, text_size=tblSize)
table.cell(t, 1, 3, str.tostring(sellPct, "#") + "%", bgcolor=cBlack, text_color=gSell, text_size=tblSize)
f_div(4)
// --- Status ---
table.cell(t, 0, 5, "STATUS", bgcolor=cBlack, text_color=cWhite, text_size=tblSize)
table.cell(t, 1, 5, statusTxt, bgcolor=cBlack, text_color=statusCol, text_size=tblSize)
f_div(6)
// --- EMA trend ---
table.cell(t, 0, 7, "TIME", bgcolor=cHdr, text_color=cWhite, text_size=tblSize)
table.cell(t, 1, 7, "EMA TREND", bgcolor=cHdr, text_color=cWhite, text_size=tblSize)
f_emaRow("30S", dir30, 8)
f_emaRow("15M", dir15, 9)
f_emaRow("1H", dir60, 10)
f_emaRow("4H", dir240, 11)
// ============================================================
// ALERTS
// ============================================================
// 1) A+ setup (full alignment)
if aplusBuy
alert("A+ SETUP BUY @ " + str.tostring(close), alert.freq_once_per_bar_close)
if aplusSell
alert("A+ SETUP SELL @ " + str.tostring(close), alert.freq_once_per_bar_close)
alertcondition(aplusBuy, title="A+ SETUP BUY", message="A+ SETUP BUY - full alignment")
alertcondition(aplusSell, title="A+ SETUP SELL", message="A+ SETUP SELL - full alignment")
// 2) SHADING: chart + TDI both same color at the same time
if bothGreenNow
alert("CHART + TDI BOTH GREEN - BUY @ " + str.tostring(close), alert.freq_once_per_bar_close)
if bothRedNow
alert("CHART + TDI BOTH RED - SELL @ " + str.tostring(close), alert.freq_once_per_bar_close)
alertcondition(bothGreenNow, title="CHART + TDI BOTH GREEN (BUY)", message="Chart green AND TDI green at the same time - BUY")
alertcondition(bothRedNow, title="CHART + TDI BOTH RED (SELL)", message="Chart red AND TDI red at the same time - SELL")
// 3) SIGNAL: buy/sell signal firing on chart + TDI at the same time
if tdiBuy
alert("CHART + TDI BUY SIGNAL @ " + str.tostring(close), alert.freq_once_per_bar_close)
if tdiSell
alert("CHART + TDI SELL SIGNAL @ " + str.tostring(close), alert.freq_once_per_bar_close)
alertcondition(tdiBuy, title="CHART + TDI BUY SIGNAL", message="BUY signal on chart AND TDI at the same time")
alertcondition(tdiSell, title="CHART + TDI SELL SIGNAL", message="SELL signal on chart AND TDI at the same time")
// 4) 8/14 EMAs turn GREEN or RED (fast EMA cross)
if ema814Green
alert("8/14 EMAs turned GREEN @ " + str.tostring(close), alert.freq_once_per_bar_close)
if ema814Red
alert("8/14 EMAs turned RED @ " + str.tostring(close), alert.freq_once_per_bar_close)
alertcondition(ema814Green, title="8/14 EMAs TURNED GREEN", message="The 8 and 14 EMAs turned green - 8 crossed above 14")
alertcondition(ema814Red, title="8/14 EMAs TURNED RED", message="The 8 and 14 EMAs turned red - 8 crossed below 14")