本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("OF Agent Score - 15min", shorttitle = "OF Score 15", overlay = true, max_labels_count = 500, max_lines_count = 500)
// Order Flow Agent scores, 15-MIN model (frozen weights, learned on Jun - Oct 2026 candles; only ~5 months of footprint, treat as a first look).
// Bull score = chance next candle makes a higher high. Bear score = chance next candle makes a lower low.
// UP = bull >= 60 and bear < 50 | DOWN = bear >= 60 and bull < 50 | BIG = both >= 60 | QUIET = both < 40 | MIXED = rest
// v2 (8 Nov 2026): old labels kept on every candle; ADDED tested close-location read + stop line (lines marked ADDED).
// v3 (9 Nov 2026): live candle gets its own label (before, the live score overwrote the previous candle's label).
// Use on the 15-minute chart of NIFTY1!. The live candle's score changes until it closes.
// --- Inputs ---
rowPts = input.float(3.0, "Row size in points (15-min model used 3)", minval = 1.0)
imbPct = input.float(300.0, "Diagonal imbalance %", minval = 100.0, step = 50.0)
showLbl = input.bool(true, "Show scores on candles")
colorBars = input.bool(true, "Colour candles by reading")
showRead = input.bool(true, "Add tested close-location read to labels")
showLine = input.bool(true, "Draw protected-side (stop) line")
// --- Footprint ---
int ticks = math.max(1, math.round(rowPts / syminfo.mintick))
footprint fp = request.footprint(ticks, 70, imbPct)
float rng = high - low
float botLine = low + rng / 3.0
float topLine = high - rng / 3.0
float buyAll = 0.0
float sellAll = 0.0
float pocP = na
float bBuy = 0.0, float bSell = 0.0
float tBuy = 0.0, float tSell = 0.0
float[] mids = array.new_float()
bool[] bImb = array.new_bool()
bool[] sImb = array.new_bool()
if not na(fp)
volume_row poc = fp.poc()
pocP := na(poc) ? na : (poc.up_price() + poc.down_price()) / 2.0
for r in fp.rows()
float mid = (r.up_price() + r.down_price()) / 2.0
float bv = r.buy_volume()
float sv = r.sell_volume()
int prt = mid <= botLine ? 1 : mid >= topLine ? 3 : 2
buyAll += bv
sellAll += sv
bBuy += prt == 1 ? bv : 0.0
bSell += prt == 1 ? sv : 0.0
tBuy += prt == 3 ? bv : 0.0
tSell += prt == 3 ? sv : 0.0
mids.push(mid)
bImb.push(r.has_buy_imbalance())
sImb.push(r.has_sell_imbalance())
// --- Stacked imbalance: longest run and where it sits (1 bottom, 2 middle, 3 top) ---
int bestB = 0, float bestBMid = na, int runB = 0, float runBStart = na
int bestS = 0, float bestSMid = na, int runS = 0, float runSStart = na
int n = mids.size()
if n > 0
int[] idx = mids.sort_indices(order.ascending)
for k = 0 to n - 1
int i = idx.get(k)
float m = mids.get(i)
bool hb = bImb.get(i)
bool hs = sImb.get(i)
runB := hb ? runB + 1 : 0
runBStart := hb and runB == 1 ? m : runBStart
bool newB = hb and runB > bestB
bestBMid := newB ? (runBStart + m) / 2.0 : bestBMid
bestB := newB ? runB : bestB
runS := hs ? runS + 1 : 0
runSStart := hs and runS == 1 ? m : runSStart
bool newS = hs and runS > bestS
bestSMid := newS ? (runSStart + m) / 2.0 : bestSMid
bestS := newS ? runS : bestS
int stackBPart = na(bestBMid) ? 0 : bestBMid <= botLine ? 1 : bestBMid >= topLine ? 3 : 2
int stackSPart = na(bestSMid) ? 0 : bestSMid <= botLine ? 1 : bestSMid >= topLine ? 3 : 2
// --- Variables (same as the Python research) ---
bool ok = not na(fp) and buyAll + sellAll > 0 and rng > 0
float vol = ok ? buyAll + sellAll : na
float delta = ok ? buyAll - sellAll : na
float atr = ta.sma(ta.tr(true), 14)
float deltaPct = delta / vol * 100.0
float closePos = rng > 0 ? (close - low) / rng * 100.0 : na
float upWick = rng > 0 ? (high - math.max(open, close)) / rng * 100.0 : na
float loWick = rng > 0 ? (math.min(open, close) - low) / rng * 100.0 : na
float pocPos = ok and not na(pocP) ? (pocP - low) / rng * 100.0 : na
float pocQ = math.ceil(math.max(0.01, math.min(100.0, pocPos)) / 25.0)
float pocPrice = low + rng * pocPos / 100.0
float botDltPct = (bBuy - bSell) / vol * 100.0
float topDltPct = (tBuy - tSell) / vol * 100.0
bool green = close > open
float pocMove1 = (pocPrice - pocPrice[1]) / atr
float pocMove3 = (pocPrice - pocPrice[3]) / atr
float cd3 = math.sum(delta, 3) / math.sum(vol, 3) * 100.0
float cd5 = math.sum(delta, 5) / math.sum(vol, 5) * 100.0
bool insideC = high <= high[1] and low >= low[1]
bool outsideC = high > high[1] and low < low[1]
f(bool b) => b ? 1.0 : 0.0
// --- Bull clues ---
float cPocTop = f(pocQ == 4)
float cCloseTop25 = f(closePos >= 75)
float cStrongBuy = f(deltaPct > 15)
float cTrapSellers = f(botDltPct < -5 and closePos > 60)
float cStackBuyBot = f(bestB >= 3 and stackBPart == 1)
float cRedBuyers = f(not green and delta > 0)
float cLongLoWick = f(loWick >= 40)
float cPocUp2 = f(pocPrice > pocPrice[1] and pocPrice[1] > pocPrice[2])
float cPocJumpUp = f(pocMove1 > 0.3)
float cCd3Buy = f(cd3 > 10)
float cCd5Buy = f(cd5 > 10)
float cDivBull = f(low < low[1] and deltaPct > deltaPct[1])
float cCloseAbvHi = f(close > high[1])
float cOutsideTop = f(outsideC and closePos >= 60)
float cInsideAftUp = f(insideC and pocMove3 > 0)
// --- Bear clues ---
float cPocBot = f(pocQ == 1)
float cCloseBot25 = f(closePos <= 25)
float cStrongSell = f(deltaPct < -15)
float cTrapBuyers = f(topDltPct > 5 and closePos < 40)
float cStackSellTop = f(bestS >= 3 and stackSPart == 3)
float cGreenSellers = f(green and delta < 0)
float cLongUpWick = f(upWick >= 40)
float cPocDown2 = f(pocPrice < pocPrice[1] and pocPrice[1] < pocPrice[2])
float cPocJumpDn = f(pocMove1 < -0.3)
float cCd3Sell = f(cd3 < -10)
float cCd5Sell = f(cd5 < -10)
float cDivBear = f(high > high[1] and deltaPct < deltaPct[1])
float cCloseBlwLo = f(close < low[1])
float cOutsideBot = f(outsideC and closePos <= 40)
float cInsideAftDn = f(insideC and pocMove3 < 0)
// --- HH judge (bull score), frozen 15-min weights ---
float hhPush = 2.2075 * cCloseTop25 + 2.6345 * cTrapSellers + 2.4676 * cLongLoWick + 1.1283 * cDivBull
float hhPull = 1.6944 * cPocBot + 8.2534 * cStrongSell + 1.3490 * cStackSellTop + 4.9160 * cGreenSellers + 4.9780 * cPocDown2 + 4.4359 * cPocJumpDn + 10.5354 * cCd3Sell + 10.7539 * cCd5Sell + 1.7205 * cCloseBlwLo + 2.1492 * cInsideAftDn
float hhZ = 6.0141 + hhPush - hhPull
// --- LL judge (bear score), frozen 15-min weights ---
float llPush = 1.7079 * cCloseBot25 + 0.0476 * cStrongSell + 2.2818 * cTrapBuyers + 0.8139 * cLongUpWick + 0.6090 * cPocDown2 + 0.6631 * cPocJumpDn + 0.5050 * cCd5Sell + 0.4797 * cCloseBlwLo + 0.2801 * cOutsideBot + 0.4893 * cInsideAftDn
float llPull = 0.6627 * cTrapSellers + 7.5442 * cRedBuyers + 9.7223 * cLongLoWick + 3.5438 * cDivBull + 1.4771 * cInsideAftUp
float llZ = 1.0905 + llPush - llPull
bool valid = ok and not na(atr)
float bull = valid ? math.round(100.0 / (1.0 + math.exp(-hhZ))) : na
float bear = valid ? math.round(100.0 / (1.0 + math.exp(-llZ))) : na
// --- Reading ---
bool isUp = bull >= 60 and bear < 50
bool isDown = bear >= 60 and bull < 50
bool isBig = bull >= 60 and bear >= 60
bool isQuiet = bull < 40 and bear < 40
string reading = not valid ? "" : isUp ? "UP" : isDown ? "DOWN" : isBig ? "BIG" : isQuiet ? "QUIET" : "MIXED"
color rdCol = isUp ? color.new(color.green, 0) : isDown ? color.new(color.red, 0) : isBig ? color.new(color.orange, 0) : isQuiet ? color.new(color.gray, 0) : color.new(color.blue, 0)
// --- Draw ---
barcolor(colorBars and valid ? rdCol : na)
// --- ADDED: tested close-location read (of15_states_FROZEN.txt; outlier = THIS candle only, checked in of_outlier_days_check.txt) ---
float closePs = rng > 0 ? (close - low) / rng * 100.0 : 50.0
float sizeAtr = rng / atr
bool isOpen = hour == 9 and minute == 15
bool bigNow = not na(atr) and sizeAtr > 2.5
bool isTop = closePs >= 75
bool isBottom = closePs <= 25
bool bigC = sizeAtr > 1.5 * 0.85
bool smallC = sizeAtr < 0.75 * 0.85
string grp = bigNow ? "OUTLIER - no call" : isTop ? "TOP" : isBottom ? "BOTTOM" : "MIDDLE - no call"
string odds = bigNow ? "" : isTop ? "\nhigh +0.25ATR ~42% | low -0.25ATR ~9%" : isBottom ? "\nlow -0.25ATR ~45% | high +0.25ATR ~7%" : ""
string tags = (not bigNow and (isTop or isBottom) and bigC ? "\nbig: stop held 94-97%" : "") + (not bigNow and isTop and smallC ? "\nsmall: weak, stop held 52-63%" : "") + (isOpen ? "\n09:15 open (not tested)" : "")
string extra = showRead ? "\n--\n" + grp + odds + tags : ""
bool drawLine = barstate.isconfirmed and showLine and not bigNow and not na(atr) and (isTop or isBottom)
float stopLvl = isTop ? low : high
string stopTxt = isTop ? "stop: low not touched 79-82% next candle" : "stop: high not touched 81-85% next candle"
string txt = reading + "\nB " + str.tostring(bull) + " | S " + str.tostring(bear) + extra
var label cur = na
var int curBar = -1
if showLbl and valid and curBar != bar_index
cur := label.new(bar_index, high, txt, style = label.style_label_down, color = color.new(rdCol, 20), textcolor = color.white, size = size.small)
curBar := bar_index
if showLbl and valid and not na(cur)
label.set_text(cur, txt)
label.set_y(cur, high)
label.set_color(cur, color.new(rdCol, 20))
// --- ADDED: protected-side (stop) line for the next candle ---
if drawLine
line.new(bar_index, stopLvl, bar_index + 1, stopLvl, color = isTop ? color.green : color.red, width = 2)
if drawLine
label.new(bar_index + 1, stopLvl, stopTxt, style = label.style_none, textcolor = isTop ? color.green : color.red, size = size.tiny)
plot(bull, "Bull score", display = display.data_window)
plot(bear, "Bear score", display = display.data_window)
var table tb = table.new(position.top_right, 1, 2, bgcolor = color.new(color.black, 20))
if barstate.islast
table.cell(tb, 0, 0, timeframe.multiplier == 15 and timeframe.isminutes ? "OF Score 15: " + reading + " B " + str.tostring(bull) + " | S " + str.tostring(bear) : "15-min model - put on the 15-minute chart", text_color = color.white, text_size = size.small)
table.cell(tb, 0, 1, "row " + str.tostring(rowPts) + " pts", text_color = color.gray, text_size = size.tiny)