本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Quantum-Edge-Capital
//@version=6
indicator("Quantum Flow Scanner", overlay=true, max_labels_count=300, max_lines_count=50)
// ================= Inputs =================
grpTrend = "Trend Detection"
fastLen = input.int(9, "Fast Period", minval=1, maxval=200, group=grpTrend)
slowLen = input.int(21, "Slow Period", minval=1, maxval=500, group=grpTrend)
patternLen = input.int(14, "Pattern Recognition Length", minval=5, maxval=50, group=grpTrend)
grpBand = "Dynamic Bands"
atrLen = input.int(14, "ATR Length", minval=1, maxval=200, group=grpBand)
distMult = input.float(2.5, "Distance Multiplier", minval=1.0, maxval=10.0, step=0.1, group=grpBand)
clusterSize = input.int(5, "Cluster Size", minval=1, maxval=15, group=grpBand)
showMid = input.bool(true, "Show Midline", group=grpBand)
showUpper = input.bool(true, "Show Upper Band", group=grpBand)
showLower = input.bool(true, "Show Lower Band", group=grpBand)
showFill = input.bool(true, "Show Channel Fill", group=grpBand)
showBg = input.bool(true, "Background Trend Color", group=grpBand)
grpDisp = "Display"
bullColor = input.color(color.new(color.teal, 0), "Bull Color", group=grpDisp)
bearColor = input.color(color.new(color.orange, 0), "Bear Color", group=grpDisp)
showMarkers = input.bool(true, "Show Detection Markers", group=grpDisp)
showPct = input.bool(true, "Show Strength %", group=grpDisp)
grpHtf = "Higher Timeframe"
htfInput = input.timeframe("", "Higher Timeframe (blank = auto)", group=grpHtf)
grpPerf = "Performance Tracking"
slPoints = input.float(10.0, "Stop Loss (points)", minval=0.01, group=grpPerf)
tpPoints = input.float(20.0, "Take Profit (points)", minval=0.01, group=grpPerf)
showTable = input.bool(true, "Show Performance Table", group=grpPerf)
// ================= Core calculations =================
fastMA = ta.ema(close, fastLen)
slowMA = ta.ema(close, slowLen)
atrVal = ta.atr(atrLen)
midline = ta.ema(hlc3, slowLen)
bandWidth = atrVal * distMult
upperBand = midline + bandWidth
lowerBand = midline - bandWidth
trendUp = fastMA > slowMA
bullCross = ta.crossover(fastMA, slowMA)
bearCross = ta.crossunder(fastMA, slowMA)
// Pattern recognition: directional consistency over recent bars
upCnt = 0
for i = 1 to patternLen
if close[i - 1] > close[i]
upCnt += 1
patternScore = upCnt / patternLen
// Momentum cluster: how many of the last bars had rising fast MA
clusterUp = 0
for i = 0 to (clusterSize - 1)
if fastMA[i] > fastMA[i + 1]
clusterUp += 1
clusterFactor = clusterUp / clusterSize
// Distance from midline factor
distFactor = bandWidth != 0 ? math.min(math.abs(close - midline) / bandWidth, 1.0) : 0.0
// Trend duration factor
barsSinceCross = trendUp ? ta.barssince(bullCross) : ta.barssince(bearCross)
trendDurFactor = math.min(nz(barsSinceCross, 0) / patternLen, 1.0)
// Volatility factor
atrAvg = ta.sma(atrVal, 50)
volFactor = atrAvg != 0 ? math.min(atrVal / atrAvg, 1.5) / 1.5 : 0.5
// Higher timeframe alignment
htf = htfInput != "" ? htfInput : (timeframe.isseconds ? "5" : timeframe.isminutes ? str.tostring(math.min(timeframe.multiplier * 4, 240)) : timeframe.isdaily ? "W" : "M")
[htfFast, htfSlow] = request.security(syminfo.tickerid, htf, [ta.ema(close, fastLen), ta.ema(close, slowLen)], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
htfBull = htfFast > htfSlow
htfFactorBull = htfBull ? 1.0 : 0.0
htfFactorBear = htfBull ? 0.0 : 1.0
// Volume profile
volAvg = ta.sma(volume, 20)
volFactorVolume = volAvg != 0 ? math.min(volume / volAvg, 2.0) / 2.0 : 0.5
// Candle strength
rangeVal = high - low
bodyVal = math.abs(close - open)
candleFactor = rangeVal != 0 ? bodyVal / rangeVal : 0.0
// Swing position context
hh = ta.highest(high, patternLen)
ll = ta.lowest(low, patternLen)
swingRange = hh - ll
swingPosBull = swingRange != 0 ? (close - ll) / swingRange : 0.5
swingPosBear = swingRange != 0 ? (hh - close) / swingRange : 0.5
// ================= Detection strength (25-92%) =================
bullFactors = (volFactor + clusterFactor + distFactor + trendDurFactor + htfFactorBull + volFactorVolume + candleFactor + swingPosBull) / 8.0
bearFactors = (volFactor + (1 - clusterFactor) + distFactor + trendDurFactor + htfFactorBear + volFactorVolume + candleFactor + swingPosBear) / 8.0
bullStrength = 25 + bullFactors * (92 - 25)
bearStrength = 25 + bearFactors * (92 - 25)
bullDetect = bullCross and close > midline
bearDetect = bearCross and close < midline
// ================= Plots =================
plot(showMid ? midline : na, "Midline", color=color.new(color.gray, 20))
upperPlot = plot(showUpper ? upperBand : na, "Upper Band", color=color.new(bullColor, 40))
lowerPlot = plot(showLower ? lowerBand : na, "Lower Band", color=color.new(bearColor, 40))
fillColor = trendUp ? color.new(bullColor, 90) : color.new(bearColor, 90)
fill(upperPlot, lowerPlot, color = showFill ? fillColor : na)
bgColor = trendUp ? color.new(bullColor, 92) : color.new(bearColor, 92)
bgcolor(showBg ? bgColor : na)
plotshape(showMarkers and bullDetect, title="Bull Detection", style=shape.triangleup, location=location.belowbar, color=bullColor, size=size.small)
plotshape(showMarkers and bearDetect, title="Bear Detection", style=shape.triangledown, location=location.abovebar, color=bearColor, size=size.small)
if showPct and bullDetect
label.new(bar_index, low - atrVal * 0.5, str.tostring(math.round(bullStrength)) + "%", style=label.style_label_up, color=color.new(bullColor, 0), textcolor=color.white, size=size.tiny)
if showPct and bearDetect
label.new(bar_index, high + atrVal * 0.5, str.tostring(math.round(bearStrength)) + "%", style=label.style_label_down, color=color.new(bearColor, 0), textcolor=color.white, size=size.tiny)
alertcondition(bullDetect, "Bullish Detection", "Quantum Flow Scanner: Bullish detection")
alertcondition(bearDetect, "Bearish Detection", "Quantum Flow Scanner: Bearish detection")
// ================= Performance tracking =================
var array<int> pendType = array.new<int>()
var array<float> pendEntry = array.new<float>()
var array<float> pendSL = array.new<float>()
var array<float> pendTP = array.new<float>()
var int totalDetections = 0
var int successCount = 0
var int failCount = 0
var float sumStrength = 0.0
idx = array.size(pendType) - 1
while idx >= 0
t = array.get(pendType, idx)
eSL = array.get(pendSL, idx)
eTP = array.get(pendTP, idx)
resolved = false
if t == 1
if low <= eSL
failCount += 1
resolved := true
else if high >= eTP
successCount += 1
resolved := true
else
if high >= eSL
failCount += 1
resolved := true
else if low <= eTP
successCount += 1
resolved := true
if resolved
array.remove(pendType, idx)
array.remove(pendEntry, idx)
array.remove(pendSL, idx)
array.remove(pendTP, idx)
idx := idx - 1
if bullDetect
array.push(pendType, 1)
array.push(pendEntry, close)
array.push(pendSL, close - slPoints)
array.push(pendTP, close + tpPoints)
totalDetections += 1
sumStrength += bullStrength
if bearDetect
array.push(pendType, -1)
array.push(pendEntry, close)
array.push(pendSL, close + slPoints)
array.push(pendTP, close - tpPoints)
totalDetections += 1
sumStrength += bearStrength
resolvedTotal = successCount + failCount
successRate = resolvedTotal > 0 ? (successCount / resolvedTotal) * 100 : 0.0
avgStrength = totalDetections > 0 ? sumStrength / totalDetections : 0.0
var table perfTable = table.new(position.top_right, 2, 6, bgcolor=color.new(color.gray, 85), border_width=1)
if showTable and barstate.islast
table.cell(perfTable, 0, 0, "Quantum Flow", text_color=color.white, text_size=size.small, bgcolor=color.new(color.gray, 70))
table.cell(perfTable, 1, 0, "Stats", text_color=color.white, text_size=size.small, bgcolor=color.new(color.gray, 70))
table.cell(perfTable, 0, 1, "Total Detections", text_color=color.gray, text_size=size.small)
table.cell(perfTable, 1, 1, str.tostring(totalDetections), text_color=color.white, text_size=size.small)
table.cell(perfTable, 0, 2, "Successful", text_color=color.gray, text_size=size.small)
table.cell(perfTable, 1, 2, str.tostring(successCount), text_color=bullColor, text_size=size.small)
table.cell(perfTable, 0, 3, "Unsuccessful", text_color=color.gray, text_size=size.small)
table.cell(perfTable, 1, 3, str.tostring(failCount), text_color=bearColor, text_size=size.small)
table.cell(perfTable, 0, 4, "Success Rate", text_color=color.gray, text_size=size.small)
table.cell(perfTable, 1, 4, str.tostring(math.round(successRate)) + "%", text_color=color.white, text_size=size.small)
table.cell(perfTable, 0, 5, "Avg Strength", text_color=color.gray, text_size=size.small)
table.cell(perfTable, 1, 5, str.tostring(math.round(avgStrength)) + "%", text_color=color.white, text_size=size.small)