本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0.
// © PickMyTrade
// Original implementation, version 0.4.1. 9 October 2026. Publication title.
// Buy/sell volume is inferred from intrabar price movement, not measured bid/ask trades.
// Profile bins intrabar HLC3 or cached side-group weighted HLC3. No order-book claim.
// ML uses only already-resolved cases. Its vote is conditional on decisive outcomes.
indicator("Order Flow AI [Pickmytrade]", "Flow AI", overlay = true, behind_chart = false, max_boxes_count = 180, max_lines_count = 30, max_labels_count = 60, max_polylines_count = 30)
// Index-based drawings internally need time history. Reserve it explicitly for
// historical shelves/profile anchors, whose age is not inferable on early bars.
max_bars_back(time, 5000)
const string G_DATA = "01 · Intrabar data"
const string G_IMPACT = "02 · Effort and response"
const string G_VIEW = "03 · Chart presentation"
const string G_ML = "04 · Outcome learning (ML)"
const string G_PUB = "05 · Publication presentation"
bool autoTfInput = input.bool(true, "Automatic intrabar timeframe", group = G_DATA, tooltip = "1 minute on charts above 1m through 1h; 5m through 4h; 15m through 1D; 60m through 1W; 1D above 1W. On 1m or faster charts choose a smaller custom timeframe supported by your account.")
string customTfInput = input.timeframe("1", "Custom intrabar timeframe", group = G_DATA, tooltip = "Used when Automatic is off. Must be strictly smaller than the chart timeframe. Seconds data depends on plan and feed availability.")
int flowWindowInput = input.int(20, "Flow distribution window", minval = 5, maxval = 80, group = G_DATA, tooltip = "Rolling chart bars for the volume-weighted intrabar HLC3 mean and standard deviation. The ribbon requires complete valid data on at least 80% of these bars.")
int profileWindowInput = input.int(48, "Side profile window", minval = 12, maxval = 120, group = G_DATA, tooltip = "Exactly this many most recent closed chart bars, including missing-data bars. At most 120 consecutive intrabar groups per chart bar are cached; group side volumes use separate weighted prices.")
float dominanceInput = input.float(0.25, "Minimum directional volume share", minval = 0.05, maxval = 0.9, step = 0.05, group = G_IMPACT, tooltip = "Absolute estimated delta divided by buy + sell + neutral volume. 0.25 means a 25% net imbalance in classified volume, not a future probability.")
float rvolInput = input.float(1.2, "Minimum relative volume", minval = 0.5, maxval = 4, step = 0.1, group = G_IMPACT, tooltip = "Current classified volume divided by its prior 30-bar average. Missing or invalid source bars disable events until a full valid baseline is available.")
float driveInput = input.float(0.35, "Drive response (ATR)", minval = 0.15, maxval = 2, step = 0.05, group = G_IMPACT, tooltip = "A DRIVE needs the chart candle body to move with estimated delta by at least this multiple of the prior candle's ATR.")
float resistedInput = input.float(0.10, "Resisted response ceiling (ATR)", minval = 0, maxval = 0.3, step = 0.05, group = G_IMPACT, tooltip = "Heavy directional activity with a body response no greater than this amount in its direction. Includes movement AGAINST the pressure. This is inferred resistance, not proven absorption or a reversal signal.")
int spacingInput = input.int(3, "Minimum bars between event marks", minval = 1, maxval = 20, group = G_IMPACT, tooltip = "One shared spacing rule across all four event types. An event can fire again when at least this many bars have passed.")
bool ribbonInput = input.bool(true, "Flow distribution ribbon", group = G_VIEW, tooltip = "Neutral mean plus three translucent layers out to one volume-weighted standard deviation. These describe intrabar HLC3 distribution, not support/resistance or predicted bounds.")
bool profileInput = input.bool(true, "Buy / sell side profile", group = G_VIEW, tooltip = "Rolling classified intrabar HLC3 profile. Orange sell left; blue buy right; grey neutral appended to the right. Widths use one shared maximum. Positive widths have a one-bar minimum for visibility.")
string placementInput = input.string("Inside chart", "Profile placement", options = ["Inside chart", "Right of candles"], group = G_VIEW, tooltip = "Inside chart stays within the visible candle area and reduces width when zoomed in. Right of candles requires extra right margin. The profile follows the rightmost visible closed candle when you pan.")
int rowsInput = input.int(24, "Profile rows", minval = 12, maxval = 36, group = G_VIEW, tooltip = "Fixed equal-price bins across the cached intrabar sample range. Volume is allocated once per side group to its weighted price; this is an approximation of volume-at-price.")
int widthInput = input.int(18, "Profile half-width (bars)", minval = 8, maxval = 28, group = G_VIEW)
int offsetInput = input.int(6, "Space after candles (bars)", minval = 2, maxval = 20, group = G_VIEW)
bool depthInput = input.bool(true, "Profile depth shading", group = G_VIEW, tooltip = "Decorative top and side faces on peak-row segments only. Faces do not encode confidence or additional volume.")
bool zonesInput = input.bool(true, "Resisted-event highlights", group = G_VIEW, tooltip = "Show the latest eight event markers as short translucent shelves around their candle bodies. They are historical annotations, not tested future trading levels.")
bool dashboardInput = input.bool(true, "Dashboard and chart guide", group = G_VIEW, tooltip = "Shows pressure/response meanings and the latest marked event's frozen ML read and age. The ML percentage belongs to that historical event, not every later candle. Detailed samples, coverage and error scores are in diagnostics.")
bool researchInput = input.bool(false, "Show model and data diagnostics", group = G_VIEW, tooltip = "Adds library/base, decisive/discarded coverage, source availability, match distance and model-vs-base error. Use it when checking new symbols/timeframes; hide it for a concise chart guide.")
color buyInput = input.color(#246BFF, "Classified buying", group = G_VIEW)
color sellInput = input.color(#FF8A18, "Classified selling", group = G_VIEW)
bool mlInput = input.bool(true, "Learn pressure outcomes", group = G_ML, tooltip = "A distance-weighted k-nearest-neighbour classifier learns from the DRIVE / RESISTED events marked by this script. It compares prior resolved cases only. No trained external AI, native bid/ask feed or guaranteed predictive edge.")
int neighboursInput = input.int(9, "K neighbours", minval = 3, maxval = 21, group = G_ML, tooltip = "The exact K nearest feature vectors vote. Every selected neighbour must pass the distance ceiling. Equal-distance ties favour newer resolved cases.")
int libraryInput = input.int(240, "Resolved-case library", minval = 100, maxval = 400, group = G_ML, tooltip = "Keep this many most recently resolved cases. Ambiguous and timed-out cases never enter this library. A chart reload rebuilds it from available intrabar history.")
int warmupInput = input.int(40, "Minimum resolved cases", minval = 20, maxval = 100, group = G_ML, tooltip = "Below this count the engine reads LEARNING. Both outcome classes must also be present. Sample collection continues while the model abstains.")
float shrinkInput = input.float(4.0, "Base-rate shrinkage", minval = 0, maxval = 20, step = 0.5, group = G_ML, tooltip = "Pull the weighted vote toward the library's smoothed continuation base rate. Uses effective neighbour weight count to account for weight concentration. Neighbour cases can overlap; they are not independent observations.")
float distanceInput = input.float(0.16, "Maximum mean log-distance", minval = 0.01, maxval = 0.70, step = 0.01, group = G_ML, tooltip = "Average of log(1 + absolute feature difference) over seven features scaled to 0–1. If the Kth nearest case exceeds this ceiling, say NO CLOSE ANALOGUE. Smaller is stricter; this is not a learned or optimized distance threshold.")
float barrierInput = input.float(0.75, "Outcome distance (prior ATR)", minval = 0.25, maxval = 3, step = 0.05, group = G_ML, tooltip = "Freeze symmetric barriers this far from the event's CLOSE, using its prior-bar ATR. From the NEXT bar: pressure-side first = continuation, opposite-side first = opposition. Both touched on one candle = ambiguous; no assumed intrabar order. These are labels, not trade targets or stops.")
int horizonInput = input.int(12, "Outcome deadline (bars)", minval = 3, maxval = 50, group = G_ML, tooltip = "A case must touch one barrier within this many later chart bars. Touches on the final bar still count. Otherwise discard it as a timeout. Votes describe decisive cases, not all pressure events.")
float convictionInput = input.float(0.65, "Historical vote threshold", minval = 0.55, maxval = 0.90, step = 0.05, group = G_ML, tooltip = "Continuation vote at/above this threshold, or at/below 1 minus it, enables an informational ML alert on the event's confirmed close. The vote is not a calibrated probability or an entry instruction.")
bool mlBadgesInput = input.bool(false, "Historical ML badges", group = G_ML, tooltip = "Optional compact percentages beside scored event marks. At most 24 recent badges. Frozen when the event closes; never filled in later after warmup. Colour still denotes the original pressure side. Hidden by default to keep the cover readable.")
bool legendInput = input.bool(true, "Explain chart marks in dashboard", group = G_VIEW, tooltip = "Four colour/shape meanings plus explanations for short rectangles, the ribbon and profile bars. Blue/orange describes estimated pressure; it does not automatically mean enter a buy/sell order.")
bool publicationInput = input.bool(false, "Publication cover mode", group = G_PUB, tooltip = "A short, viewport-anchored heading, stronger ribbon contrast and a compact shape key replace the large research dashboard. Right-side profile placement gives the histogram its own space; leave enough chart margin. Changes presentation only: events, ML features, training and alerts are unchanged. Turn it off for the full chart guide.")
int coverTitleSizeInput = input.int(64, "Cover title size", minval = 32, maxval = 80, step = 4, group = G_PUB, tooltip = "Font size in pixels for the short ORDER FLOW AI heading. It is anchored to the chart viewport rather than a price/date. 64 is intended for a full-width publication chart; reduce it if using a small window.")
string profilePlacement = publicationInput ? "Right of candles" : placementInput
if barstate.isfirst and resistedInput >= driveInput
runtime.error("Resisted response ceiling must be smaller than Drive response.")
// Evaluated in the lower-timeframe context. Dojis inherit the last known direction
// only after open/close and previous-close comparisons also fail to resolve them.
f_direction() =>
var int direction = 0
if close > open
direction := 1
else if close < open
direction := -1
else if not na(close[1]) and close > close[1]
direction := 1
else if not na(close[1]) and close < close[1]
direction := -1
direction
float chartSeconds = timeframe.in_seconds()
string autoTf = chartSeconds <= 3600 ? "1" : chartSeconds <= 14400 ? "5" : chartSeconds <= 86400 ? "15" : chartSeconds <= 604800 ? "60" : "1D"
string lowerTf = autoTfInput ? autoTf : customTfInput
bool validTf = not na(chartSeconds) and timeframe.in_seconds(lowerTf) < chartSeconds
[intraPrice, intraVolume, intraDirection] = request.security_lower_tf(syminfo.tickerid, lowerTf, [hlc3, volume, f_direction()], ignore_invalid_timeframe = true, calc_bars_count = 100000)
int sourceCount = validTf and not na(intraPrice) and not na(intraVolume) and not na(intraDirection) ? intraPrice.size() : 0
bool manageable = sourceCount <= 3000
bool standard = chart.is_standard
float buyVolume = 0.0
float sellVolume = 0.0
float neutralVolume = 0.0
float priceMoment = 0.0
float squareMoment = 0.0
int validSources = 0
bool invalidSource = false
if validTf and standard and manageable and sourceCount > 0
for i = 0 to sourceCount - 1
float samplePrice = intraPrice.get(i)
float sampleVolume = intraVolume.get(i)
if na(samplePrice) or na(sampleVolume) or sampleVolume < 0
invalidSource := true
else
validSources += 1
int side = intraDirection.get(i)
buyVolume += side == 1 ? sampleVolume : 0.0
sellVolume += side == -1 ? sampleVolume : 0.0
neutralVolume += side == 0 ? sampleVolume : 0.0
priceMoment += samplePrice * sampleVolume
squareMoment += samplePrice * samplePrice * sampleVolume
float classifiedTotal = buyVolume + sellVolume + neutralVolume
bool usable = validTf and standard and manageable and sourceCount > 0 and not invalidSource and classifiedTotal > 0
float netShare = usable ? (buyVolume - sellVolume) / classifiedTotal : na
float observedVolume = usable ? classifiedTotal : na
float priorAverage = ta.sma(observedVolume, 30)[1]
float priorValidBars = math.sum(usable ? 1.0 : 0.0, 30)[1]
float priorAtr = ta.atr(14)[1]
float relativeVolume = usable and priorValidBars == 30 and priorAverage > 0 ? classifiedTotal / priorAverage : na
float bodyResponse = usable and priorAtr > 0 ? math.sign(netShare) * (close - open) / priorAtr : na
// Source statistics are computed every bar, but plotted values and event state commit
// on confirmed closes. Open bars carry the last closed reading, not a new signal.
float windowVolume = math.sum(usable ? classifiedTotal : 0.0, flowWindowInput)
float windowMoment = math.sum(usable ? priceMoment : 0.0, flowWindowInput)
float windowSquare = math.sum(usable ? squareMoment : 0.0, flowWindowInput)
float windowBuy = math.sum(usable ? buyVolume : 0.0, flowWindowInput)
float windowSell = math.sum(usable ? sellVolume : 0.0, flowWindowInput)
float windowValid = math.sum(usable ? 1.0 : 0.0, flowWindowInput)
bool windowReady = windowValid >= math.ceil(flowWindowInput * 0.8) and windowVolume > 0
float rawMean = windowReady ? windowMoment / windowVolume : na
float rawDeviation = windowReady ? math.sqrt(math.max(0, windowSquare / windowVolume - rawMean * rawMean)) : na
float windowShare = windowReady ? (windowBuy - windowSell) / windowVolume : na
float flowMean = barstate.isconfirmed ? rawMean : rawMean[1]
float flowDeviation = barstate.isconfirmed ? rawDeviation : rawDeviation[1]
float flowShare = barstate.isconfirmed ? windowShare : windowShare[1]
color flowInk = na(flowShare) or math.abs(flowShare) < 0.05 ? #8090A6 : flowShare > 0 ? buyInput : sellInput
float meanShown = ribbonInput ? flowMean : na
pMean = plot(meanShown, "Flow mean (intrabar HLC3)", #CFD8E6, 2, plot.style_linebr)
pUpper1 = plot(ribbonInput ? flowMean + flowDeviation : na, "Upper distribution", color.new(flowInk, publicationInput ? 45 : 80), 1, plot.style_linebr)
pLower1 = plot(ribbonInput ? flowMean - flowDeviation : na, "Lower distribution", color.new(flowInk, publicationInput ? 45 : 80), 1, plot.style_linebr)
pUpper2 = plot(ribbonInput ? flowMean + flowDeviation * 0.67 : na, "Upper inner", color.new(flowInk, 100), 1, plot.style_linebr, display = display.none)
pLower2 = plot(ribbonInput ? flowMean - flowDeviation * 0.67 : na, "Lower inner", color.new(flowInk, 100), 1, plot.style_linebr, display = display.none)
pUpper3 = plot(ribbonInput ? flowMean + flowDeviation * 0.33 : na, "Upper core", color.new(flowInk, 100), 1, plot.style_linebr, display = display.none)
pLower3 = plot(ribbonInput ? flowMean - flowDeviation * 0.33 : na, "Lower core", color.new(flowInk, 100), 1, plot.style_linebr, display = display.none)
fill(pUpper1, pLower1, color.new(flowInk, publicationInput ? 88 : 94), title = "Distribution outer", fillgaps = false)
fill(pUpper2, pLower2, color.new(flowInk, publicationInput ? 78 : 91), title = "Distribution middle", fillgaps = false)
fill(pUpper3, pLower3, color.new(flowInk, publicationInput ? 62 : 85), title = "Distribution core", fillgaps = false)
type FlowBar
int index
int stamp
int samples
bool available
array<float> pricesBuy
array<float> pricesSell
array<float> pricesNeutral
array<float> volumesBuy
array<float> volumesSell
array<float> volumesNeutral
type Highlight
int index
int side
float top
float bottom
type LearnedCase
array<float> features
int outcome
type PendingCase
int index
float origin
float atr
int side
array<float> features
float vote
float base
type ModelBadge
int index
float price
int side
float vote
float base
int librarySize
float distance
f_clip(float value, float lower, float upper) =>
math.max(lower, math.min(upper, value))
// All seven features are bounded on fixed scales, never fitted on future history.
// Buying and selling cases use the same pressure-relative coordinates.
f_features(int side, float share, float activity, float response, float mean, float atr, float rollingShare) =>
float span = high - low
float alignedWick = side == 1 ? (high - math.max(open, close)) / span : (math.min(open, close) - low) / span
float alignedClose = side == 1 ? (close - low) / span : (high - close) / span
array.from(math.abs(share), activity / (1.0 + activity), (f_clip(response, -2.0, 2.0) + 2.0) / 4.0, f_clip(alignedWick, 0.0, 1.0), f_clip(alignedClose, 0.0, 1.0), (f_clip(side * (close - mean) / atr, -3.0, 3.0) + 3.0) / 6.0, f_clip((side * rollingShare + 1.0) / 2.0, 0.0, 1.0))
// Mean logarithmic L1 distance. Often called Lorentzian distance in similarity
// literature; no spacetime geometry and no imported classifier/library code.
f_distance(array<float> current, array<float> past) =>
float distance = 0.0
for feature = 0 to 6
distance += math.log(1.0 + math.abs(current.get(feature) - past.get(feature)))
distance / 7.0
f_model(array<float> features, array<LearnedCase> library) =>
int count = library.size()
int wins = 0
if count > 0
for example in library
wins += example.outcome
float base = count > 0 ? (wins + 1.0) / (count + 2.0) : na
float vote = na
float farthest = na
float effective = na
string state = count < math.max(warmupInput, neighboursInput) ? "LEARNING" : wins == 0 or wins == count ? "NEED BOTH OUTCOMES" : "NO CLOSE ANALOGUE"
if count >= math.max(warmupInput, neighboursInput) and wins > 0 and wins < count
array<float> distances = array.new<float>()
array<int> outcomes = array.new<int>()
// Maintain sorted exact-nearest K. Newer cases win exact distance ties.
for example in library
float distance = f_distance(features, example.features)
int place = distances.size()
if distances.size() > 0
for j = 0 to distances.size() - 1
if distance <= distances.get(j)
place := j
break
if place < neighboursInput
distances.insert(place, distance)
outcomes.insert(place, example.outcome)
if distances.size() > neighboursInput
distances.pop()
outcomes.pop()
farthest := distances.last()
if farthest <= distanceInput
float weightSum = 0.0
float squareWeights = 0.0
float weightedOutcome = 0.0
for j = 0 to distances.size() - 1
float weight = 1.0 / math.max(0.01, distances.get(j))
weightSum += weight
squareWeights += weight * weight
weightedOutcome += weight * outcomes.get(j)
effective := weightSum * weightSum / squareWeights
float neighbourVote = weightedOutcome / weightSum
float blend = effective / (effective + shrinkInput)
vote := base + (neighbourVote - base) * blend
state := "HISTORICAL VOTE"
[vote, base, farthest, effective, state]
var array<LearnedCase> caseLibrary = array.new<LearnedCase>()
var array<PendingCase> pending = array.new<PendingCase>()
var array<ModelBadge> badges = array.new<ModelBadge>()
var int resolvedCount = 0
var int timeoutCount = 0
var int ambiguousCount = 0
var int invalidOutcomeCount = 0
var int scoredCount = 0
var float brierSum = 0.0
var float baseBrierSum = 0.0
var float modelVote = na
var float modelBase = na
var float modelDistance = na
var float modelEffective = na
var string modelState = "WAITING FOR EVENT"
var int modelIndex = na
var int modelSide = 0
var int modelLibrarySize = 0
// Freeze the dashboard at the visible endpoint too: scrolling never displays a
// library or validation score learned from bars to the right of that endpoint.
var float viewVote = na
var float viewBase = na
var float viewDistance = na
var float viewEffective = na
var string viewModelState = "WAITING FOR EVENT"
var int viewModelAge = na
var int viewModelSide = 0
var int viewQuerySize = 0
var int viewLibrary = 0
var int viewResolved = 0
var int viewTimeout = 0
var int viewAmbiguous = 0
var int viewInvalid = 0
var int viewPending = 0
var int viewScored = 0
var float viewBrier = na
var float viewBaseBrier = na
var array<FlowBar> history = array.new<FlowBar>()
var array<Highlight> highlights = array.new<Highlight>()
var int lastEvent = na
var int lastStamp = na
var string lastState = "WAITING FOR DATA"
var float lastShare = na
var float lastRvol = na
var float lastResponse = na
var int lastSamples = 0
var bool lastUsable = false
var bool lastPartial = false
var int visibleLeft = na
var int visibleRight = na
if time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
visibleLeft := na(visibleLeft) ? bar_index : visibleLeft
visibleRight := bar_index
bool buyDrive = false
bool sellDrive = false
bool buyResisted = false
bool sellResisted = false
bool mlContinuation = false
bool mlOpposition = false
float eventVote = na
float eventDistance = na
if barstate.isconfirmed
// Resolve before querying the current close. Only observations already known
// by this close can train it. A case cannot resolve on its own birth candle.
if mlInput
int cursor = 0
while cursor < pending.size()
PendingCase candidate = pending.get(cursor)
int age = bar_index - candidate.index
bool invalidPrice = na(high) or na(low) or high < low
bool withPressure = not invalidPrice and (candidate.side == 1 ? high >= candidate.origin + barrierInput * candidate.atr : low <= candidate.origin - barrierInput * candidate.atr)
bool againstPressure = not invalidPrice and (candidate.side == 1 ? low <= candidate.origin - barrierInput * candidate.atr : high >= candidate.origin + barrierInput * candidate.atr)
bool complete = age > 0 and (invalidPrice or withPressure or againstPressure or age >= horizonInput)
if complete
if invalidPrice
invalidOutcomeCount += 1
else if withPressure and againstPressure
ambiguousCount += 1
else if withPressure or againstPressure
int outcome = withPressure ? 1 : 0
caseLibrary.push(LearnedCase.new(candidate.features, outcome))
if caseLibrary.size() > libraryInput
caseLibrary.shift()
resolvedCount += 1
if not na(candidate.vote)
scoredCount += 1
brierSum += math.pow(candidate.vote - outcome, 2)
baseBrierSum += math.pow(candidate.base - outcome, 2)
else
timeoutCount += 1
pending.remove(cursor)
else
cursor += 1
// Preserve all source volume; consecutive aggregation affects profile detail only.
array<float> pb = array.new<float>()
array<float> ps = array.new<float>()
array<float> pn = array.new<float>()
array<float> vb = array.new<float>()
array<float> vs = array.new<float>()
array<float> vn = array.new<float>()
if usable and time <= chart.right_visible_bar_time
int stride = int(math.max(1, math.ceil(sourceCount / 120.0)))
for start = 0 to sourceCount - 1 by stride
int finish = math.min(sourceCount - 1, start + stride - 1)
float bv = 0.0
float sv = 0.0
float nv = 0.0
float bm = 0.0
float sm = 0.0
float nm = 0.0
for i = start to finish
float v = intraVolume.get(i)
float p = intraPrice.get(i)
int side = intraDirection.get(i)
if side == 1
bv += v
bm += v * p
else if side == -1
sv += v
sm += v * p
else
nv += v
nm += v * p
vb.push(bv)
vs.push(sv)
vn.push(nv)
pb.push(bv > 0 ? bm / bv : na)
ps.push(sv > 0 ? sm / sv : na)
pn.push(nv > 0 ? nm / nv : na)
if time <= chart.right_visible_bar_time
history.push(FlowBar.new(bar_index, time, sourceCount, usable, pb, ps, pn, vb, vs, vn))
if history.size() > profileWindowInput
history.shift()
lastStamp := time_close
lastShare := netShare
lastRvol := relativeVolume
lastResponse := bodyResponse
lastSamples := sourceCount
lastUsable := usable
lastPartial := invalidSource
bool pressure = usable and not na(relativeVolume) and relativeVolume >= rvolInput and math.abs(netShare) >= dominanceInput and priorAtr > 0
bool drive = pressure and bodyResponse >= driveInput
bool resisted = pressure and bodyResponse <= resistedInput
if time <= chart.right_visible_bar_time
lastState := not standard ? "STANDARD CANDLES REQUIRED" : not validTf ? "CHOOSE A SMALLER TIMEFRAME" : not manageable ? "COARSEN INTRABAR TIMEFRAME" : not usable ? (invalidSource ? "INVALID / PARTIAL SOURCE" : "NO INTRABAR VOLUME") : na(relativeVolume) ? "BUILDING VOLUME BASELINE" : drive ? (netShare > 0 ? "BUYING DRIVE" : "SELLING DRIVE") : resisted ? (netShare > 0 ? "BUYING RESISTED" : "SELLING RESISTED") : pressure ? "PRESSURE / MIXED RESPONSE" : "BALANCED / BELOW THRESHOLDS"
if (drive or resisted) and (na(lastEvent) or bar_index - lastEvent >= spacingInput)
buyDrive := drive and netShare > 0
sellDrive := drive and netShare < 0
buyResisted := resisted and netShare > 0
sellResisted := resisted and netShare < 0
lastEvent := bar_index
if mlInput
modelIndex := bar_index
modelSide := netShare > 0 ? 1 : -1
modelLibrarySize := caseLibrary.size()
modelVote := na
modelBase := na
modelDistance := na
modelEffective := na
modelState := "CONTEXT NOT READY"
if windowReady and not na(rawMean) and not na(windowShare) and high > low
array<float> features = f_features(modelSide, netShare, relativeVolume, bodyResponse, rawMean, priorAtr, windowShare)
[vote, base, distance, effective, state] = f_model(features, caseLibrary)
modelVote := vote
modelBase := base
modelDistance := distance
modelEffective := effective
modelState := state
eventVote := vote
eventDistance := distance
mlContinuation := not na(vote) and vote >= convictionInput
mlOpposition := not na(vote) and vote <= 1.0 - convictionInput
// Collection is unconditional on model readiness or its vote.
pending.push(PendingCase.new(bar_index, close, priorAtr, modelSide, features, vote, base))
if not na(vote) and time <= chart.right_visible_bar_time
float badgePrice = (sellDrive or buyResisted) ? high + priorAtr * 0.55 : low - priorAtr * 0.55
badges.push(ModelBadge.new(bar_index, badgePrice, modelSide, vote, base, caseLibrary.size(), distance))
if badges.size() > 24
badges.shift()
if resisted and time <= chart.right_visible_bar_time
highlights.push(Highlight.new(bar_index, netShare > 0 ? 1 : -1, math.max(open, close) + priorAtr * 0.12, math.min(open, close) - priorAtr * 0.12))
if highlights.size() > 8
highlights.shift()
if time <= chart.right_visible_bar_time
viewVote := modelVote
viewBase := modelBase
viewDistance := modelDistance
viewEffective := modelEffective
viewModelState := mlInput ? modelState : "ML OFF"
viewModelAge := na(modelIndex) ? na : bar_index - modelIndex
viewModelSide := modelSide
viewQuerySize := modelLibrarySize
viewLibrary := caseLibrary.size()
viewResolved := resolvedCount
viewTimeout := timeoutCount
viewAmbiguous := ambiguousCount
viewInvalid := invalidOutcomeCount
viewPending := pending.size()
viewScored := scoredCount
viewBrier := scoredCount > 0 ? brierSum / scoredCount : na
viewBaseBrier := scoredCount > 0 ? baseBrierSum / scoredCount : na
// Sparse marks; circle = aligned drive, diamond = resisted pressure. No arrows.
plotshape(buyDrive, "Buying drive mark", shape.circle, location.belowbar, buyInput, size = size.small)
plotshape(sellDrive, "Selling drive mark", shape.circle, location.abovebar, sellInput, size = size.small)
plotshape(buyResisted, "Buying resisted mark", shape.diamond, location.abovebar, buyInput, size = size.small)
plotshape(sellResisted, "Selling resisted mark", shape.diamond, location.belowbar, sellInput, size = size.small)
plot(eventVote * 100.0, "ML continuation vote (%) · event close", display = display.data_window)
plot(eventDistance, "ML Kth mean log-distance · event close", display = display.data_window)
var array<box> drawnBoxes = array.new<box>()
var array<polyline> drawnFaces = array.new<polyline>()
var array<label> drawnLabels = array.new<label>()
var array<line> drawnLines = array.new<line>()
f_face(int x1, float y1, int x2, float y2, int x3, float y3, int x4, float y4, color ink) =>
array<chart.point> points = array.from(chart.point.from_index(x1, y1), chart.point.from_index(x2, y2), chart.point.from_index(x3, y3), chart.point.from_index(x4, y4))
drawnFaces.push(polyline.new(points, closed = true, fill_color = ink, line_color = color.new(ink, 100)))
f_segment(int left, int right, float top, float bottom, color ink, float intensity, bool peak) =>
// The guide sits above candles. Keep an inside-chart profile much lighter so
// its rectangles still allow the candle bodies underneath to be read.
bool inside = profilePlacement == "Inside chart"
int transparency = inside ? int(90 - 18 * math.sqrt(intensity)) : int(math.max(18, 70 - 48 * math.sqrt(intensity)))
if depthInput and peak
float lift = (top - bottom) * 0.18
color light = color.rgb(int(math.min(255, color.r(ink) + 45)), int(math.min(255, color.g(ink) + 45)), int(math.min(255, color.b(ink) + 45)))
f_face(left, top, left + 1, top + lift, right + 1, top + lift, right, top, color.new(light, inside ? 85 : 20))
f_face(right, top, right + 1, top + lift, right + 1, bottom + lift, right, bottom, color.new(ink, inside ? 90 : 62))
drawnBoxes.push(box.new(left, top, right, bottom, bgcolor = color.new(ink, transparency), border_color = peak ? color.new(#D5DFEE, inside ? 45 : 0) : color.new(ink, inside ? 85 : 70), border_width = 1))
f_label(int x, float y, string caption, color ink) =>
drawnLabels.push(label.new(x, y, caption, style = label.style_none, textcolor = ink, size = size.small))
var table dash = table.new(position.top_right, 2, 23, bgcolor = #0C1424, frame_color = #2C3A50, frame_width = 1)
var table coverHeading = table.new(position.top_center, 1, 2)
var table coverKey = table.new(position.bottom_center, 2, 3, bgcolor = color.new(#0C1424, 12), frame_color = #23324A, frame_width = 1)
if barstate.islast
table.clear(coverHeading, 0, 0, 0, 1)
table.clear(coverKey, 0, 0, 1, 2)
if publicationInput
table.cell(coverHeading, 0, 0, "ORDER FLOW AI", text_color = #F2F5FA, text_size = coverTitleSizeInput, text_formatting = text.format_bold)
table.cell(coverHeading, 0, 1, mlInput ? "Estimated pressure · Price response · Historical ML" : "Estimated pressure · Price response", text_color = #A9B6CA, text_size = 18)
table.cell(coverKey, 0, 0, "BLUE · buying pressure", text_color = buyInput, text_size = 16, text_halign = text.align_left)
table.cell(coverKey, 1, 0, "ORANGE · selling pressure", text_color = sellInput, text_size = 16, text_halign = text.align_left)
table.cell(coverKey, 0, 1, "● Drive", text_color = #EAF0FA, text_size = 16, text_halign = text.align_left)
table.cell(coverKey, 1, 1, "◆ Resisted", text_color = #EAF0FA, text_size = 16, text_halign = text.align_left)
table.cell(coverKey, 0, 2, "Pressure moved price", text_color = #A9B6CA, text_size = 12, text_halign = text.align_left)
table.cell(coverKey, 1, 2, "Small / opposing response", text_color = #A9B6CA, text_size = 12, text_halign = text.align_left)
while drawnBoxes.size() > 0
box.delete(drawnBoxes.pop())
while drawnFaces.size() > 0
polyline.delete(drawnFaces.pop())
while drawnLabels.size() > 0
label.delete(drawnLabels.pop())
while drawnLines.size() > 0
line.delete(drawnLines.pop())
if mlBadgesInput and badges.size() > 0
for badge in badges
if badge.index >= bar_index - 4900 and (na(visibleRight) or badge.index <= visibleRight)
string tip = "Frozen historical continuation vote: " + str.tostring(badge.vote * 100, "#.#") + "%\nBase at birth: " + str.tostring(badge.base * 100, "#.#") + "% · library " + str.tostring(badge.librarySize) + "\nKth mean log-distance: " + str.tostring(badge.distance, "#.###") + "\nConditional on one barrier resolving within the deadline. Not a calibrated probability or trade win rate."
drawnLabels.push(label.new(badge.index, badge.price, "C " + str.tostring(badge.vote * 100, "#") + "%", style = label.style_none, textcolor = badge.side == 1 ? buyInput : sellInput, size = size.small, tooltip = tip))
if zonesInput and highlights.size() > 0
for event in highlights
if event.index >= bar_index - 4900
color base = event.side == 1 ? buyInput : sellInput
int right = math.min(event.index + 8, bar_index + 2)
drawnBoxes.push(box.new(event.index, event.top, right, event.bottom, bgcolor = color.new(base, 90), border_color = color.new(base, 65)))
float minimum = na
float maximum = na
int validBars = 0
float sumBuy = 0.0
float sumSell = 0.0
float sumNeutral = 0.0
if history.size() > 0
for candle in history
if candle.available
validBars += 1
sumBuy += candle.volumesBuy.sum()
sumSell += candle.volumesSell.sum()
sumNeutral += candle.volumesNeutral.sum()
for i = 0 to candle.volumesBuy.size() - 1
if candle.volumesBuy.get(i) > 0
minimum := na(minimum) ? candle.pricesBuy.get(i) : math.min(minimum, candle.pricesBuy.get(i))
maximum := na(maximum) ? candle.pricesBuy.get(i) : math.max(maximum, candle.pricesBuy.get(i))
if candle.volumesSell.get(i) > 0
minimum := na(minimum) ? candle.pricesSell.get(i) : math.min(minimum, candle.pricesSell.get(i))
maximum := na(maximum) ? candle.pricesSell.get(i) : math.max(maximum, candle.pricesSell.get(i))
if candle.volumesNeutral.get(i) > 0
minimum := na(minimum) ? candle.pricesNeutral.get(i) : math.min(minimum, candle.pricesNeutral.get(i))
maximum := na(maximum) ? candle.pricesNeutral.get(i) : math.max(maximum, candle.pricesNeutral.get(i))
float profileTotal = sumBuy + sumSell + sumNeutral
bool profileInRange = na(visibleRight) or visibleRight >= bar_index - 4900
if profileInput and profileTotal > 0 and not na(minimum) and profileInRange
float unit = math.max((maximum - minimum) / rowsInput, syminfo.mintick)
array<float> binsBuy = array.new<float>(rowsInput, 0.0)
array<float> binsSell = array.new<float>(rowsInput, 0.0)
array<float> binsNeutral = array.new<float>(rowsInput, 0.0)
for candle in history
if candle.available
for i = 0 to candle.volumesBuy.size() - 1
if candle.volumesBuy.get(i) > 0
int bin = int(math.max(0, math.min(rowsInput - 1, math.floor((candle.pricesBuy.get(i) - minimum) / unit))))
binsBuy.set(bin, binsBuy.get(bin) + candle.volumesBuy.get(i))
if candle.volumesSell.get(i) > 0
int bin = int(math.max(0, math.min(rowsInput - 1, math.floor((candle.pricesSell.get(i) - minimum) / unit))))
binsSell.set(bin, binsSell.get(bin) + candle.volumesSell.get(i))
if candle.volumesNeutral.get(i) > 0
int bin = int(math.max(0, math.min(rowsInput - 1, math.floor((candle.pricesNeutral.get(i) - minimum) / unit))))
binsNeutral.set(bin, binsNeutral.get(bin) + candle.volumesNeutral.get(i))
float peakSide = 0.0
float peakTotal = 0.0
int peakRow = 0
for i = 0 to rowsInput - 1
peakSide := math.max(peakSide, math.max(binsSell.get(i), binsBuy.get(i) + binsNeutral.get(i)))
float rowTotal = binsBuy.get(i) + binsSell.get(i) + binsNeutral.get(i)
if rowTotal > peakTotal
peakTotal := rowTotal
peakRow := i
int anchor = na(visibleRight) ? bar_index : visibleRight
int visibleBars = not na(visibleLeft) ? anchor - visibleLeft + 1 : profileWindowInput
int profileWidth = profilePlacement == "Inside chart" ? int(math.max(2, math.min(widthInput, math.floor((visibleBars - 3) / 2.0)))) : widthInput
int axis = profilePlacement == "Inside chart" ? anchor - profileWidth : anchor + offsetInput + profileWidth
for i = 0 to rowsInput - 1
float bottom = minimum + i * unit + unit * 0.07
float top = minimum + (i + 1) * unit - unit * 0.07
float buy = binsBuy.get(i)
float sell = binsSell.get(i)
float neutral = binsNeutral.get(i)
int buyWidth = buy > 0 ? int(math.max(1, math.round(profileWidth * buy / peakSide))) : 0
int sellWidth = sell > 0 ? int(math.max(1, math.round(profileWidth * sell / peakSide))) : 0
int neutralWidth = neutral > 0 ? int(math.max(1, math.round(profileWidth * neutral / peakSide))) : 0
if sellWidth > 0
f_segment(axis - sellWidth, axis, top, bottom, sellInput, sell / peakSide, i == peakRow)
if buyWidth > 0
f_segment(axis, axis + buyWidth, top, bottom, buyInput, buy / peakSide, i == peakRow)
if neutralWidth > 0
f_segment(axis + buyWidth, axis + buyWidth + neutralWidth, top, bottom, #76849A, neutral / peakSide, i == peakRow)
drawnLines.push(line.new(axis, minimum, axis, minimum + rowsInput * unit, color = #506079))
f_label(axis - int(profileWidth * 0.5), minimum + (rowsInput + 1.1) * unit, "SELL VOL", sellInput)
f_label(axis + int(profileWidth * 0.5), minimum + (rowsInput + 1.1) * unit, "BUY VOL", buyInput)
string title = "VOLUME PROFILE · " + str.tostring(validBars) + "/" + str.tostring(history.size()) + " bars"
f_label(axis, minimum - unit * 0.9, title, #A9B6CA)
f_label(axis, minimum - unit * 1.8, "Intrabar estimate · grey = neutral", #78879E)
table.clear(dash, 0, 0, 1, 22)
if dashboardInput and not publicationInput
bool directionalState = lastState == "BUYING DRIVE" or lastState == "SELLING DRIVE" or lastState == "BUYING RESISTED" or lastState == "SELLING RESISTED" or lastState == "PRESSURE / MIXED RESPONSE"
color stateInk = lastUsable and directionalState ? (lastShare >= 0 ? buyInput : sellInput) : #A9B6CA
string stateText = lastState == "BALANCED / BELOW THRESHOLDS" ? "BELOW EVENT THRESHOLDS" : lastState
table.cell(dash, 0, 0, "ORDER FLOW AI", text_color = #EAF0FA, text_size = size.small)
table.cell(dash, 1, 0, mlInput ? "ML · 0.4.1" : "0.4.1 / PMT", text_color = #A9B6CA, text_size = size.small)
table.cell(dash, 0, 1, "Last closed pressure", text_color = #91A0B7, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 1, not profileInRange ? "PROFILE VIEW TOO OLD" : stateText, text_color = stateInk, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 0, 2, "Net pressure / activity", text_color = #91A0B7, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 2, (na(lastShare) ? "—" : str.tostring(lastShare * 100, "+#.#;-#.#") + "% Δ") + " · " + (na(lastRvol) ? "warming up" : str.tostring(lastRvol, "#.##") + "x vol"), text_color = #CED8E8, text_size = size.small, text_halign = text.align_left)
string ageText = na(viewModelAge) ? "" : " · " + str.tostring(viewModelAge) + " bars ago"
string caseText = not mlInput ? "ML OFF" : na(viewModelAge) ? "WAITING FOR EVENT" : (viewModelSide == 1 ? "Buying pressure" : "Selling pressure") + ageText
string leanText = not na(viewVote) ? (viewVote >= convictionInput ? "continuation threshold met" : viewVote <= 1.0 - convictionInput ? "opposition threshold met" : "no threshold met") : ""
string voteText = viewModelState == "LEARNING" ? "LEARNING " + str.tostring(viewQuerySize) + "/" + str.tostring(math.max(warmupInput, neighboursInput)) : na(viewVote) ? viewModelState : str.tostring(viewVote * 100, "#.#") + "% · " + leanText
table.cell(dash, 0, 3, "Historical ML case", text_color = #91A0B7, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 3, caseText, text_color = #CED8E8, text_size = size.small, text_halign = text.align_left, tooltip = "This is the latest marked case at the visible endpoint. Its original pressure side and age identify the event. A past case is not a fresh entry signal.")
table.cell(dash, 0, 4, "Continuation vote", text_color = #91A0B7, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 4, voteText, text_color = #EAF0FA, text_size = size.small, text_halign = text.align_left, tooltip = "Historical vote for continuation in THAT case's original pressure direction. Frozen at its close, conditional on a decisive outcome before the deadline. Continuation = same pressure direction; opposition = against it. Not a calibrated probability or trade win rate.")
if legendInput
table.cell(dash, 0, 5, "CHART GUIDE", text_color = #EAF0FA, text_size = size.small, text_halign = text.align_left, bgcolor = #152238)
table.cell(dash, 1, 5, "Colour = estimated pressure", text_color = #A9B6CA, text_size = size.small, text_halign = text.align_left, bgcolor = #152238)
table.cell(dash, 0, 6, "● Buying drive", text_color = buyInput, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 6, "Pressure moved price up", text_color = #CED8E8, text_size = size.small, text_halign = text.align_left, tooltip = "Heavy estimated buying plus an upward candle body meeting the Drive ATR threshold. Describes observed response; it does not place a buy order.")
table.cell(dash, 0, 7, "● Selling drive", text_color = sellInput, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 7, "Pressure moved price down", text_color = #CED8E8, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 0, 8, "◆ Buying resisted", text_color = buyInput, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 8, "Small / opposing response", text_color = #CED8E8, text_size = size.small, text_halign = text.align_left, tooltip = "Estimated buying was strong but price moved only a little with it, or moved against it. A blue diamond can accompany a falling candle. It does not mean buy.")
table.cell(dash, 0, 9, "◆ Selling resisted", text_color = sellInput, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 9, "Small / opposing response", text_color = #CED8E8, text_size = size.small, text_halign = text.align_left, tooltip = "Estimated selling was strong but price moved only a little with it, or moved against it. An orange diamond can accompany a rising candle. It does not mean sell.")
table.cell(dash, 0, 10, "Short shaded rectangles", text_color = #91A0B7, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 10, "Past resisted candles", text_color = #CED8E8, text_size = size.small, text_halign = text.align_left, tooltip = "Short shelves highlight candle bodies associated with resisted events. They are historical annotations, not validated support/resistance or entry zones.")
table.cell(dash, 0, 11, "White line / ribbon", text_color = #91A0B7, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 11, "Flow mean / distribution", text_color = #CED8E8, text_size = size.small, text_halign = text.align_left, tooltip = "White = rolling volume-weighted intrabar HLC3 mean. Band = weighted distribution; its colour follows net estimated pressure in the window. Context, not an entry trigger or session VWAP.")
table.cell(dash, 0, 12, "Profile rectangles", text_color = #91A0B7, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 12, "Width = estimated volume", text_color = #CED8E8, text_size = size.small, text_halign = text.align_left, tooltip = "Orange sell volume left, blue buy volume right, grey neutral. These are approximate volume-at-price bins. Width has rounding and a one-bar minimum. Profile rectangles are not buy/sell signals.")
table.cell(dash, 0, 13, "Reading the chart", text_color = #91A0B7, text_size = size.small, text_halign = text.align_left)
table.cell(dash, 1, 13, "Pressure observations · not orders", text_color = #CED8E8, text_size = size.small, text_halign = text.align_left)
if researchInput
table.cell(dash, 0, 14, "Library / query n", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 14, str.tostring(viewLibrary) + " / " + str.tostring(viewQuerySize), text_color = #CED8E8, text_size = size.small)
table.cell(dash, 0, 15, "Base at event", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 15, na(viewBase) ? "—" : str.tostring(viewBase * 100, "#.#") + "%", text_color = #CED8E8, text_size = size.small, tooltip = "Smoothed continuation base = (continuations + 1) / (resolved library size + 2). Uses the library known at that event close.")
table.cell(dash, 0, 16, "Decisive / discarded", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 16, str.tostring(viewResolved) + " / " + str.tostring(viewTimeout + viewAmbiguous + viewInvalid) + " · " + str.tostring(viewPending) + " pending", text_color = #CED8E8, text_size = size.small, tooltip = "All eligible marked cases in this run. Discarded cases do not vote or enter Brier scores. Votes are conditional on decisive cases.")
table.cell(dash, 0, 17, "Price response", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 17, na(lastResponse) ? "—" : str.tostring(lastResponse, "+#.##;-#.##") + " ATR with pressure", text_color = #CED8E8, text_size = size.small)
table.cell(dash, 0, 18, "Source · estimate", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 18, lowerTf + " · " + str.tostring(lastSamples) + " intrabars", text_color = #CED8E8, text_size = size.small)
table.cell(dash, 0, 19, "Profile data", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 19, str.tostring(validBars) + " / " + str.tostring(history.size()) + " closed bars", text_color = #CED8E8, text_size = size.small)
table.cell(dash, 0, 20, "Kth distance / weight ESS", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 20, (na(viewDistance) ? "—" : str.tostring(viewDistance, "#.###") + " / " + str.tostring(distanceInput, "#.##")) + " · " + (na(viewEffective) ? "—" : str.tostring(viewEffective, "#.#")), text_color = #CED8E8, text_size = size.small)
table.cell(dash, 0, 21, "Brier model / base", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 21, (na(viewBrier) ? "—" : str.tostring(viewBrier, "#.###") + " / " + str.tostring(viewBaseBrier, "#.###")) + " · n=" + str.tostring(viewScored), text_color = #CED8E8, text_size = size.small, tooltip = "Sequential mean squared error of votes frozen before decisive outcomes. Lower is better. Overlapping cases are dependent. Not trading profit or independent test-set performance.")
table.cell(dash, 0, 22, "Timeout / both / invalid", text_color = #91A0B7, text_size = size.small)
table.cell(dash, 1, 22, str.tostring(viewTimeout) + " / " + str.tostring(viewAmbiguous) + " / " + str.tostring(viewInvalid), text_color = #CED8E8, text_size = size.small)
if not validTf or not standard or not lastUsable
string reason = not standard ? "Use standard candles" : not validTf ? "Use chart > 1m, or choose a smaller intrabar timeframe" : not manageable ? "Over 3,000 intrabars: choose a coarser intrabar timeframe" : lastPartial ? "Intrabar volume incomplete / invalid" : "No usable intrabar volume. Check symbol, timeframe and data access."
int warningIndex = na(visibleRight) ? bar_index : visibleRight
if warningIndex >= bar_index - 4900
drawnLabels.push(label.new(warningIndex, nz(flowMean, close), reason, style = label.style_label_down, color = #15223A, textcolor = #E7EDF7, size = size.normal))
// Informational confirmed-close conditions. No orders or execution payload.
alertcondition(buyDrive, "Buying pressure drives price", "Estimated buying pressure moved price on {{ticker}} {{interval}}. Confirmed close; informational.")
alertcondition(sellDrive, "Selling pressure drives price", "Estimated selling pressure moved price on {{ticker}} {{interval}}. Confirmed close; informational.")
alertcondition(buyResisted, "Buying pressure resisted", "Heavy estimated buying met limited/opposing price response on {{ticker}} {{interval}}. Confirmed close; informational.")
alertcondition(sellResisted, "Selling pressure resisted", "Heavy estimated selling met limited/opposing price response on {{ticker}} {{interval}}. Confirmed close; informational.")
alertcondition(mlContinuation, "ML continuation vote above threshold", "Past close-analogue decisive cases favour continuation of the marked pressure on {{ticker}} {{interval}}. Confirmed close; historical ML vote, not a trade instruction.")
alertcondition(mlOpposition, "ML opposition vote above threshold", "Past close-analogue decisive cases favour a move against the marked pressure on {{ticker}} {{interval}}. Confirmed close; historical ML vote, not a trade instruction.")
// PUBLICATION DESCRIPTION — reference copy; paste the clean .txt in the form.
// The Publish script dialog has its own Description field; Pine comments do not fill it.
// Use Order_Flow_Publication_Description.txt to copy text without comment prefixes.
// Prepare the chart before opening the dialog. Floating editor mode preserves chart width.
// Cover mode is display-only. Check the actual preview before the final Publish action.
//
// Order Flow AI [Pickmytrade]
//
// Does strong estimated buying or selling move price, or meet limited response? Order Flow AI compares directional activity with candle movement, maps its price distribution, and learns what followed similar marked events.
//
// ――――――――――――――――――――
//
// 🔷 WHAT THE CHART SHOWS
// 🔸 Blue represents estimated buying, orange selling, and grey unresolved neutral volume. Lower-timeframe up/down candles classify volume as buying/selling. Dojis compare with the previous intrabar close, then inherit the last resolved direction; initially unresolved observations remain neutral.
// 🔸 Circles mark DRIVE: strong estimated pressure with aligned candle-body movement. Diamonds mark RESISTED: pressure with limited or opposing movement. Buying resisted can accompany falling price; selling resisted can accompany rising price. Neither confirms absorption or instructs an entry.
// 🔸 Default marks require ≥25% absolute estimated imbalance and ≥1.2× relative volume. DRIVE requires ≥0.35 prior ATR aligned response; RESISTED requires ≤0.10 ATR. Intermediate responses receive no mark. Shared spacing is three bars.
// 🔸 The response measures close minus open, normalized by the previous bar’s 14-period ATR. Opening gaps are excluded. Relative volume compares with the 30 immediately preceding chart bars, all requiring usable source data.
//
// ――――――――――――――――――――
//
// 🔷 PROFILE AND DISTRIBUTION RIBBON
// 🔸 The profile places selling left, buying right, and neutral volume in grey. A pale outline marks the highest-volume approximate bin. Peak-row depth faces are decorative, not confidence readings.
// 🔸 Contributions are allocated once to intrabar HLC3, or cached side-group weighted HLC3. This approximates volume at price. Both sides share a width scale; rounding and minimum one-bar widths enlarge tiny segments.
// 🔸 The ribbon shows a rolling volume-weighted HLC3 mean and three translucent layers extending to one weighted standard deviation. At least 80% of its window needs usable source data. Colour follows net classified volume, grey near balance. Its bounds are descriptive, not defended levels or projected targets; its mean is not session-anchored VWAP.
// 🔸 The default profile samples 48 chart bars ending at the rightmost visible closed candle. Panning rebuilds this sample and selects historical dashboard readings; it does not profile every visible candle.
//
// ――――――――――――――――――――
//
// 🔷 REAL OUTCOME LEARNING
// 🔸 AI here means an optional, distance-weighted k-nearest-neighbour ML classifier that learns from resolved DRIVE/RESISTED cases, collecting eligible examples during warm-up and abstention. No external classifier or pretrained AI model is imported. Its historical similarity vote is not a guarantee of future movement.
// 🔸 Seven features, normalized to 0–1: absolute imbalance, relative activity, aligned body response, pressure-side wick, aligned close location, signed distance from flow mean, and aligned rolling volume share. Buying/selling use comparable pressure-relative coordinates.
// 🔸 Features, origin close and prior ATR freeze at the confirmed event close. From the NEXT bar, ±0.75 frozen ATR barriers define continuation if the pressure-side barrier is first, opposition if the other is first. The twelfth later bar counts. Same-candle double touches, timeouts and invalid outcome prices are discarded.
// 🔸 Only already-resolved cases train the classifier. Exact K nearest cases use mean log(1 + absolute feature difference) across seven features. This Lorentzian-style similarity does not model spacetime or detect news.
// 🔸 Inverse-distance weights favour nearer cases. Effective neighbour weight count controls shrinkage toward base rate (continuations + 1) / (library size + 2). The library retains the latest 240 resolved cases.
// 🔸 Warm-up requires 40 resolved cases and both outcomes. All selected neighbours must pass the 0.16 mean log-distance ceiling; otherwise the engine reports LEARNING, NEED BOTH OUTCOMES or NO CLOSE ANALOGUE.
//
// ――――――――――――――――――――
//
// 🔷 READING THE ML VALUE
// 🔸 C means historical continuation vote in the original pressure direction; lower values favour opposition. Discarded cases do not vote: the reading is CONDITIONAL on a decisive outcome within the deadline, not a calibrated probability for every event or a trade win rate.
// 🔸 Vote and baseline freeze at the event close and are never backfilled. The dashboard identifies that event’s age and spells out its continuation vote. Between the two configured thresholds it says “no threshold met”. Optional diagnostics show library, base and decisive/discarded counts. C badges preserve pressure colour; circles/diamonds still describe observed response.
// 🔸 The chart guide explains all four markers. Blue circle: buying pressure moved price up. Orange circle: selling pressure moved price down. Blue diamond: buying met small or opposing response. Orange diamond: selling met small or opposing response. Colours describe estimated pressure, not automatic entry orders.
// 🔸 Short shaded rectangles highlight past resisted candles. Profile rectangles represent estimated volume at price. “SELL VOL” / “BUY VOL” identify the volume sides; neither the shelves nor profile bars are entry signals. The guide can be hidden independently of the dashboard.
// 🔸 Research diagnostics compare saved votes with later outcomes using Brier error and the frozen base. Overlapping cases are dependent. A fixed 30-day BTCUSDT/ETHUSDT 15m reconstruction did not outperform the baseline on either symbol; no trading edge is established.
//
// ――――――――――――――――――――
//
// 🔷 ALERTS AND AUTOMATION
// 🔸 Six informational conditions: Buying pressure drives price; Selling pressure drives price; Buying pressure resisted; Selling pressure resisted; ML continuation vote above threshold; ML opposition vote above threshold.
// 🔸 Create the required TradingView alert using Once Per Bar Close. Default ML thresholds are C ≥65% for continuation and C ≤35% for opposition. Messages identify the ticker and interval.
// 🔸 External automation requires compatible messages, execution rules and receiver testing. There are no orders, stops, targets or built-in execution payloads; no execution integration was tested. Outcome barriers are learning labels, not trade exits.
//
// ――――――――――――――――――――
//
// 🔷 SETTINGS AND PRACTICAL USE
// 🔸 Defaults: Automatic source on / Custom 1m; ribbon window 20; profile 48 bars / 24 rows; Inside chart placement / half-width 18 / offset 6.
// 🔸 Pressure defaults: imbalance 0.25; relative volume 1.2; drive response 0.35; resisted ceiling 0.10; spacing 3 bars.
// 🔸 ML defaults: learning on; K 9; library 240; warm-up 40; shrinkage 4; distance ceiling 0.16; barrier 0.75 prior ATR; deadline 12 bars; vote threshold 0.65; historical badges off.
// 🔸 Ribbon, profile, depth, shelves, dashboard and chart guide default on; diagnostics default off. Blue/orange colours are configurable. Shelves annotate eight recent resisted events, extending up to eight bars; they are not validated levels. The guide draws above candles; inside-chart profile shading is kept lighter to preserve candle visibility.
// 🔸 Publication cover mode defaults off. It replaces the large dashboard with a short viewport-anchored title and a compact colour/shape key, strengthens ribbon contrast and places the profile to the right of candles. Leave sufficient right margin. It does not alter pressure events, ML features, outcomes, training or alerts. Switch it off to restore the full chart guide.
// 🔸 Use standard candles and inspect diagnostics first. Inside chart keeps the profile visible; Right of candles needs right margin. Shelves/dashboard can be hidden.
// 🔸 Cover: BTCUSDT 15m / 1m source, profile 72 bars / 24 rows, width 22 / offset 3, cover title 64 pixels and enough right margin for the profile; shelves hidden. The heading and compact key are generated by Pine. These display settings illustrate the tool, not optimized trading parameters.
//
// ――――――――――――――――――――
//
// 🔷 REQUIREMENTS AND LIMITATIONS
// 🔸 Volume direction is inferred, not measured bid/ask, native footprint, resting liquidity or identified institutions. Some feeds supply tick volume.
// 🔸 Automatic selects 1m through 1h charts, 5m through 4h, 15m through 1D, 60m through 1W, and daily above 1W. The source must be strictly smaller than the chart timeframe; charts of 1m or faster require a supported finer custom source.
// 🔸 Missing/invalid source bars receive no fabricated volume. Bars above 3,000 intrabars are excluded. Profile caching uses at most 120 consecutive groups per chart bar, preserving side totals while reducing price detail. Index-based drawings are limited to roughly 4,900 bars back, within an explicitly reserved time buffer.
// 🔸 Marks/ML commit at confirmed closes; open candles retain the previous closed ribbon. Learning is chronological and viewport-independent. Intrabar history requests cap at 100,000, subject to account/feed availability; returned counts do not prove completeness. Reloads, more history, settings and feed corrections can alter reconstruction.
//
// Built in Pine Script v6 under Mozilla Public License 2.0. This original implementation applies established price-based volume classification, weighted distributions and nearest-neighbour learning. It makes no claim of unique invention, guaranteed performance or Editor’s Choice selection.
// END PUBLICATION DESCRIPTION