本页源码来自 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/
// © Thomas-Sterling
//@version=6
indicator("Market Flow PRO [Thomas]", overlay=true, max_lines_count=100, max_labels_count=150, max_boxes_count=30)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
groupTrend = "Trend Settings"
fastLen = input.int(21, "Fast EMA", minval=1, group=groupTrend)
slowLen = input.int(50, "Slow EMA", minval=1, group=groupTrend)
majorLen = input.int(200, "Major EMA", minval=1, group=groupTrend)
showEMAs = input.bool(true, "Show EMA Lines", group=groupTrend)
groupSignal = "Signal Confirmation"
rsiLen = input.int(14, "RSI Length", group=groupSignal)
adxLen = input.int(14, "ADX Length", group=groupSignal)
adxSmooth = input.int(14, "ADX Smoothing", group=groupSignal)
minADX = input.float(18, "Minimum ADX", step=0.5, group=groupSignal)
useVolume = input.bool(false, "Use Volume Filter", group=groupSignal)
volLen = input.int(20, "Volume Average Length", group=groupSignal)
cooldown = input.int(10, "Signal Cooldown", minval=0, group=groupSignal)
groupStructure = "Market Structure"
pivotLen = input.int(5, "Pivot Length", minval=2, group=groupStructure)
showBOS = input.bool(true, "Show BOS", group=groupStructure)
showCHoCH = input.bool(true, "Show CHoCH", group=groupStructure)
showSweeps = input.bool(true, "Show Liquidity Sweeps", group=groupStructure)
showTrendlines = input.bool(true, "Show Automatic Trendlines", group=groupStructure)
groupFVG = "Fair Value Gaps"
showFVG = input.bool(true, "Show FVG Zones", group=groupFVG)
fvgExtend = input.int(20, "FVG Extension", minval=1, group=groupFVG)
groupTargets = "Automatic Targets"
atrLen = input.int(14, "ATR Length", group=groupTargets)
tp1Mult = input.float(1.0, "TP1 ATR Multiplier", minval=0.1, step=0.1, group=groupTargets)
tp2Mult = input.float(2.0, "TP2 ATR Multiplier", minval=0.1, step=0.1, group=groupTargets)
tp3Mult = input.float(3.0, "TP3 ATR Multiplier", minval=0.1, step=0.1, group=groupTargets)
rightBars = input.int(15, "Right Label Distance", minval=3, maxval=100, group=groupTargets)
showTargets = input.bool(true, "Show Entry & Targets", group=groupTargets)
groupDisplay = "Display"
showDashboard = input.bool(true, "Show Dashboard", group=groupDisplay)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CORE CALCULATIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
emaFast = ta.ema(close, fastLen)
emaSlow = ta.ema(close, slowLen)
emaMajor = ta.ema(close, majorLen)
rsiValue = ta.rsi(close, rsiLen)
[diPlus, diMinus, adxValue] = ta.dmi(adxLen, adxSmooth)
atrValue = ta.atr(atrLen)
volAverage = ta.sma(volume, volLen)
plot(showEMAs ? emaFast : na, "Fast EMA", color=color.aqua, linewidth=2)
plot(showEMAs ? emaSlow : na, "Slow EMA", color=color.orange, linewidth=2)
plot(showEMAs ? emaMajor : na, "Major EMA", color=color.gray, linewidth=2)
bullTrend = emaFast > emaSlow and emaSlow > emaMajor
bearTrend = emaFast < emaSlow and emaSlow < emaMajor
volumeOK = not useVolume or volume > volAverage
strengthOK = adxValue >= minADX
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// MARKET STRUCTURE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
pHigh = ta.pivothigh(high, pivotLen, pivotLen)
pLow = ta.pivotlow(low, pivotLen, pivotLen)
var float lastHigh = na
var float lastLow = na
var float previousHigh = na
var float previousLow = na
var bool highBroken = false
var bool lowBroken = false
var int structureTrend = 0
if not na(pHigh)
previousHigh := lastHigh
lastHigh := pHigh
highBroken := false
if not na(pLow)
previousLow := lastLow
lastLow := pLow
lowBroken := false
bullBOS = not na(lastHigh) and ta.crossover(close, lastHigh) and not highBroken
bearBOS = not na(lastLow) and ta.crossunder(close, lastLow) and not lowBroken
bullCHoCH = bullBOS and structureTrend == -1
bearCHoCH = bearBOS and structureTrend == 1
if bullBOS
highBroken := true
structureTrend := 1
if bearBOS
lowBroken := true
structureTrend := -1
if showBOS and bullBOS and not bullCHoCH
label.new(bar_index, high, "BOS", style=label.style_label_down, color=color.green, textcolor=color.white, size=size.tiny)
if showBOS and bearBOS and not bearCHoCH
label.new(bar_index, low, "BOS", style=label.style_label_up, color=color.red, textcolor=color.white, size=size.tiny)
if showCHoCH and bullCHoCH
label.new(bar_index, high, "CHoCH", style=label.style_label_down, color=color.aqua, textcolor=color.black, size=size.tiny)
if showCHoCH and bearCHoCH
label.new(bar_index, low, "CHoCH", style=label.style_label_up, color=color.orange, textcolor=color.black, size=size.tiny)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// AUTOMATIC PIVOT TRENDLINES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var int lastHighBar = na
var int previousHighBar = na
var float trendHigh1 = na
var float trendHigh2 = na
var int lastLowBar = na
var int previousLowBar = na
var float trendLow1 = na
var float trendLow2 = na
var line resistanceTrendline = na
var line supportTrendline = na
if not na(pHigh)
previousHighBar := lastHighBar
trendHigh2 := trendHigh1
lastHighBar := bar_index - pivotLen
trendHigh1 := pHigh
if showTrendlines and not na(previousHighBar) and not na(trendHigh2)
if not na(resistanceTrendline)
line.delete(resistanceTrendline)
resistanceTrendline := line.new(previousHighBar, trendHigh2, lastHighBar, trendHigh1, extend=extend.right, color=color.new(color.red, 25), width=2)
if not na(pLow)
previousLowBar := lastLowBar
trendLow2 := trendLow1
lastLowBar := bar_index - pivotLen
trendLow1 := pLow
if showTrendlines and not na(previousLowBar) and not na(trendLow2)
if not na(supportTrendline)
line.delete(supportTrendline)
supportTrendline := line.new(previousLowBar, trendLow2, lastLowBar, trendLow1, extend=extend.right, color=color.new(color.green, 25), width=2)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LIQUIDITY SWEEPS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullSweep = not na(lastLow) and low < lastLow and close > lastLow
bearSweep = not na(lastHigh) and high > lastHigh and close < lastHigh
var int bullSweepBar = na
var int bearSweepBar = na
if bullSweep
bullSweepBar := bar_index
if bearSweep
bearSweepBar := bar_index
bullSweepRecent = not na(bullSweepBar) and bar_index - bullSweepBar <= 10
bearSweepRecent = not na(bearSweepBar) and bar_index - bearSweepBar <= 10
if showSweeps and bullSweep
label.new(bar_index, low, "Liquidity Sweep", style=label.style_label_up, color=color.teal, textcolor=color.white, size=size.tiny)
if showSweeps and bearSweep
label.new(bar_index, high, "Liquidity Sweep", style=label.style_label_down, color=color.maroon, textcolor=color.white, size=size.tiny)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// FAIR VALUE GAPS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullFVG = bar_index >= 2 and low > high[2]
bearFVG = bar_index >= 2 and high < low[2]
var box bullGapBox = na
var box bearGapBox = na
if showFVG and bullFVG
if not na(bullGapBox)
box.delete(bullGapBox)
bullGapBox := box.new(bar_index - 2, low, bar_index + fvgExtend, high[2], bgcolor=color.new(color.green, 88), border_color=color.new(color.green, 40), text="Bullish FVG", text_color=color.green, text_size=size.tiny)
if showFVG and bearFVG
if not na(bearGapBox)
box.delete(bearGapBox)
bearGapBox := box.new(bar_index - 2, low[2], bar_index + fvgExtend, high, bgcolor=color.new(color.red, 88), border_color=color.new(color.red, 40), text="Bearish FVG", text_color=color.red, text_size=size.tiny)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SIGNAL ENGINE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullMomentum = close > open and rsiValue > 50 and diPlus > diMinus
bearMomentum = close < open and rsiValue < 50 and diMinus > diPlus
bullPullback = low <= emaFast and close > emaFast
bearPullback = high >= emaFast and close < emaFast
bullSetup = bullTrend and bullPullback and bullMomentum
bearSetup = bearTrend and bearPullback and bearMomentum
var int lastSignalBar = na
cooldownOK = na(lastSignalBar) or bar_index - lastSignalBar > cooldown
buySignal = barstate.isconfirmed and bullSetup and strengthOK and volumeOK and cooldownOK
sellSignal = barstate.isconfirmed and bearSetup and strengthOK and volumeOK and cooldownOK
if buySignal or sellSignal
lastSignalBar := bar_index
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ENTRY & TARGETS — NO STOP LOSS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var line entryLine = na
var line target1Line = na
var line target2Line = na
var line target3Line = na
var label entryLabel = na
var label target1Label = na
var label target2Label = na
var label target3Label = na
var string latestSignal = "Waiting"
var float latestEntry = na
if buySignal or sellSignal
float entryPrice = close
float direction = buySignal ? 1.0 : -1.0
float target1 = entryPrice + direction * atrValue * tp1Mult
float target2 = entryPrice + direction * atrValue * tp2Mult
float target3 = entryPrice + direction * atrValue * tp3Mult
latestSignal := buySignal ? "BUY" : "SELL"
latestEntry := entryPrice
if showTargets
if not na(entryLine)
line.delete(entryLine)
if not na(target1Line)
line.delete(target1Line)
if not na(target2Line)
line.delete(target2Line)
if not na(target3Line)
line.delete(target3Line)
if not na(entryLabel)
label.delete(entryLabel)
if not na(target1Label)
label.delete(target1Label)
if not na(target2Label)
label.delete(target2Label)
if not na(target3Label)
label.delete(target3Label)
color signalColor = buySignal ? color.aqua : color.red
entryLine := line.new(bar_index, entryPrice, bar_index + rightBars, entryPrice, color=signalColor, width=2)
target1Line := line.new(bar_index, target1, bar_index + rightBars, target1, color=color.green, style=line.style_dashed)
target2Line := line.new(bar_index, target2, bar_index + rightBars, target2, color=color.green, style=line.style_dashed)
target3Line := line.new(bar_index, target3, bar_index + rightBars, target3, color=color.green, style=line.style_dashed)
entryLabel := label.new(bar_index + rightBars, entryPrice, (buySignal ? "BUY ENTRY " : "SELL ENTRY ") + str.tostring(entryPrice, format.mintick), style=label.style_label_left, color=signalColor, textcolor=color.black)
target1Label := label.new(bar_index + rightBars, target1, "TP1 " + str.tostring(target1, format.mintick), style=label.style_label_left, color=color.green, textcolor=color.white)
target2Label := label.new(bar_index + rightBars, target2, "TP2 " + str.tostring(target2, format.mintick), style=label.style_label_left, color=color.green, textcolor=color.white)
target3Label := label.new(bar_index + rightBars, target3, "TP3 " + str.tostring(target3, format.mintick), style=label.style_label_left, color=color.green, textcolor=color.white)
plotshape(buySignal, title="BUY", style=shape.labelup, location=location.belowbar, color=color.aqua, text="BUY", textcolor=color.black, size=size.small)
plotshape(sellSignal, title="SELL", style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL", textcolor=color.white, size=size.small)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DASHBOARD
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var table panel = table.new(position.top_right, 2, 5, border_width=1)
if barstate.islast and showDashboard
string trendText = bullTrend ? "BULLISH" : bearTrend ? "BEARISH" : "NEUTRAL"
color trendColor = bullTrend ? color.green : bearTrend ? color.red : color.gray
string structureText = structureTrend == 1 ? "Bullish" : structureTrend == -1 ? "Bearish" : "Neutral"
string liquidityText = bullSweepRecent ? "Sell-side swept" : bearSweepRecent ? "Buy-side swept" : "Waiting"
table.cell(panel, 0, 0, "THOMAS MARKET FLOW", bgcolor=color.black, text_color=color.white)
table.cell(panel, 1, 0, "LIVE STATUS", bgcolor=color.black, text_color=color.white)
table.cell(panel, 0, 1, "Trend", text_color=color.white)
table.cell(panel, 1, 1, trendText, bgcolor=trendColor, text_color=color.white)
table.cell(panel, 0, 2, "Structure", text_color=color.white)
table.cell(panel, 1, 2, structureText, text_color=color.white)
table.cell(panel, 0, 3, "Liquidity", text_color=color.white)
table.cell(panel, 1, 3, liquidityText, text_color=color.yellow)
table.cell(panel, 0, 4, "Last Signal", text_color=color.white)
table.cell(panel, 1, 4, latestSignal, text_color=latestSignal == "BUY" ? color.aqua : latestSignal == "SELL" ? color.red : color.gray)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(buySignal, "Thomas BUY Signal", "Thomas Market Flow PRO: BUY signal")
alertcondition(sellSignal, "Thomas SELL Signal", "Thomas Market Flow PRO: SELL signal")
alertcondition(bullBOS, "Bullish BOS", "Bullish BOS detected")
alertcondition(bearBOS, "Bearish BOS", "Bearish BOS detected")
alertcondition(bullCHoCH, "Bullish CHoCH", "Bullish CHoCH detected")
alertcondition(bearCHoCH, "Bearish CHoCH", "Bearish CHoCH detected")
alertcondition(bullSweep, "Bullish Liquidity Sweep", "Bullish liquidity sweep detected")
alertcondition(bearSweep, "Bearish Liquidity Sweep", "Bearish liquidity sweep detected")