本页源码来自 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/
// © jijabi6625
//@version=6
indicator("EMMA SETUPS | Supply & Demand Volume Profile Pro", overlay=true, max_boxes_count=500, max_lines_count=100, max_labels_count=100)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// EMMA SETUPS | INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
grpZone = "Supply & Demand Settings"
pivotLen = input.int(8, "Pivot Length", minval=2, maxval=30, group=grpZone)
atrLen = input.int(14, "ATR Length", group=grpZone)
zoneAtr = input.float(0.6, "Zone Thickness (ATR)", minval=0.1, step=0.1, group=grpZone)
maxZones = input.int(5, "Maximum Zones", minval=1, maxval=10, group=grpZone)
showZones = input.bool(true, "Show Supply & Demand", group=grpZone)
grpVP = "Volume Profile Settings"
showProfile = input.bool(true, "Show Volume Profile", group=grpVP)
profileLength = input.int(150, "Profile Lookback", minval=30, maxval=500, group=grpVP)
profileBins = input.int(24, "Profile Rows", minval=10, maxval=50, group=grpVP)
profileWidth = input.int(18, "Profile Width", minval=5, maxval=40, group=grpVP)
valuePercent = input.float(70, "Value Area %", minval=50, maxval=90, group=grpVP)
grpTrend = "Trend & Confirmation"
emaFastLen = input.int(50, "Fast EMA", group=grpTrend)
emaSlowLen = input.int(200, "Slow EMA", group=grpTrend)
volumeLen = input.int(20, "Volume Average", group=grpTrend)
volumeMultiplier = input.float(1.2, "Volume Confirmation", minval=0.5, step=0.1, group=grpTrend)
showEMAs = input.bool(false, "Show EMAs", group=grpTrend)
grpStructure = "Market Structure"
showStructure = input.bool(true, "Show BOS / CHoCH", group=grpStructure)
grpDashboard = "Dashboard"
showDashboard = input.bool(true, "Show Dashboard", group=grpDashboard)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CALCULATIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
atr = ta.atr(atrLen)
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
avgVolume = ta.sma(volume, volumeLen)
volumeRatio = avgVolume > 0 ? volume / avgVolume : 0.0
strongVolume = volumeRatio >= volumeMultiplier
bullTrend = emaFast > emaSlow
bearTrend = emaFast < emaSlow
marketDirection = bullTrend ? "BULLISH" : bearTrend ? "BEARISH" : "SIDEWAYS"
plot(showEMAs ? emaFast : na, "Fast EMA", color=color.aqua)
plot(showEMAs ? emaSlow : na, "Slow EMA", color=color.orange)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SUPPLY & DEMAND ZONES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
pivotHigh = ta.pivothigh(high, pivotLen, pivotLen)
pivotLow = ta.pivotlow(low, pivotLen, pivotLen)
var box[] supplyBoxes = array.new_box()
var box[] demandBoxes = array.new_box()
var float supplyTop = na
var float supplyBottom = na
var float demandTop = na
var float demandBottom = na
var bool supplyTested = false
var bool demandTested = false
var float supplyVolume = na
var float demandVolume = na
if not na(pivotHigh)
supplyTop := pivotHigh
supplyBottom := pivotHigh - atr[pivotLen] * zoneAtr
supplyTested := false
supplyVolume := volume[pivotLen]
if showZones
newSupply = box.new(bar_index - pivotLen, supplyTop, bar_index, supplyBottom, border_color=color.red, bgcolor=color.new(color.red, 86), extend=extend.right, text="SUPPLY", text_color=color.red, text_size=size.tiny)
array.push(supplyBoxes, newSupply)
if array.size(supplyBoxes) > maxZones
box.delete(array.shift(supplyBoxes))
if not na(pivotLow)
demandTop := pivotLow + atr[pivotLen] * zoneAtr
demandBottom := pivotLow
demandTested := false
demandVolume := volume[pivotLen]
if showZones
newDemand = box.new(bar_index - pivotLen, demandTop, bar_index, demandBottom, border_color=color.lime, bgcolor=color.new(color.green, 86), extend=extend.right, text="DEMAND", text_color=color.lime, text_size=size.tiny)
array.push(demandBoxes, newDemand)
if array.size(demandBoxes) > maxZones
box.delete(array.shift(demandBoxes))
supplyTouch = not na(supplyTop) and high >= supplyBottom and low <= supplyTop
demandTouch = not na(demandBottom) and low <= demandTop and high >= demandBottom
if supplyTouch
supplyTested := true
if demandTouch
demandTested := true
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// MARKET STRUCTURE: BOS / CHoCH
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var float lastSwingHigh = na
var float lastSwingLow = na
var int structureDirection = 0
if not na(pivotHigh)
lastSwingHigh := pivotHigh
if not na(pivotLow)
lastSwingLow := pivotLow
bullBreak = not na(lastSwingHigh) and ta.crossover(close, lastSwingHigh)
bearBreak = not na(lastSwingLow) and ta.crossunder(close, lastSwingLow)
bullCHoCH = bullBreak and structureDirection == -1
bearCHoCH = bearBreak and structureDirection == 1
bullBOS = bullBreak and structureDirection != -1
bearBOS = bearBreak and structureDirection != 1
if bullBreak
structureDirection := 1
if bearBreak
structureDirection := -1
var string structureText = "WAITING"
if bullBreak
structureText := bullCHoCH ? "BULLISH CHoCH" : "BULLISH BOS"
if bearBreak
structureText := bearCHoCH ? "BEARISH CHoCH" : "BEARISH BOS"
if showStructure and bullBreak
label.new(bar_index, low, bullCHoCH ? "CHoCH ↑" : "BOS ↑", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.tiny)
if showStructure and bearBreak
label.new(bar_index, high, bearCHoCH ? "CHoCH ↓" : "BOS ↓", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.tiny)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUY / SELL CONFIRMATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullCandle = close > open and close > close[1]
bearCandle = close < open and close < close[1]
buySignal = demandTouch and bullCandle and strongVolume and bullTrend
sellSignal = supplyTouch and bearCandle and strongVolume and bearTrend
plotshape(buySignal, title="EMMA BUY", text="BUY", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.small)
plotshape(sellSignal, title="EMMA SELL", text="SELL", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.small)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// VOLUME PROFILE CALCULATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var float poc = na
var float vah = na
var float val = na
var float totalProfileVolume = na
var box[] profileBoxes = array.new_box()
var line pocLine = na
var line vahLine = na
var line valLine = na
if barstate.islast and showProfile
if array.size(profileBoxes) > 0
for i = 0 to array.size(profileBoxes) - 1
box.delete(array.get(profileBoxes, i))
array.clear(profileBoxes)
if not na(pocLine)
line.delete(pocLine)
if not na(vahLine)
line.delete(vahLine)
if not na(valLine)
line.delete(valLine)
float rangeHigh = ta.highest(high, profileLength)
float rangeLow = ta.lowest(low, profileLength)
float binSize = math.max((rangeHigh - rangeLow) / profileBins, syminfo.mintick)
float[] binVolumes = array.new_float(profileBins, 0.0)
for i = 0 to profileLength - 1
float candleHigh = high[i]
float candleLow = low[i]
float candleRange = math.max(candleHigh - candleLow, syminfo.mintick)
for j = 0 to profileBins - 1
float binLow = rangeLow + j * binSize
float binHigh = binLow + binSize
float overlap = math.max(0.0, math.min(candleHigh, binHigh) - math.max(candleLow, binLow))
if overlap > 0
float allocatedVolume = nz(volume[i], 0) * overlap / candleRange
array.set(binVolumes, j, array.get(binVolumes, j) + allocatedVolume)
float maxBinVolume = 0.0
int pocIndex = 0
float totalVol = 0.0
for j = 0 to profileBins - 1
float currentVol = array.get(binVolumes, j)
totalVol += currentVol
if currentVol > maxBinVolume
maxBinVolume := currentVol
pocIndex := j
totalProfileVolume := totalVol
float targetVolume = totalVol * valuePercent / 100.0
float accumulatedVolume = array.get(binVolumes, pocIndex)
int upperIndex = pocIndex
int lowerIndex = pocIndex
while accumulatedVolume < targetVolume and (upperIndex < profileBins - 1 or lowerIndex > 0)
float nextUpper = upperIndex < profileBins - 1 ? array.get(binVolumes, upperIndex + 1) : -1.0
float nextLower = lowerIndex > 0 ? array.get(binVolumes, lowerIndex - 1) : -1.0
if nextUpper >= nextLower and upperIndex < profileBins - 1
upperIndex += 1
accumulatedVolume += nextUpper
else if lowerIndex > 0
lowerIndex -= 1
accumulatedVolume += nextLower
else
break
poc := rangeLow + (pocIndex + 0.5) * binSize
vah := rangeLow + (upperIndex + 1) * binSize
val := rangeLow + lowerIndex * binSize
if maxBinVolume > 0
for j = 0 to profileBins - 1
float currentVol = array.get(binVolumes, j)
int barWidth = int(math.round(currentVol / maxBinVolume * profileWidth))
float binLow = rangeLow + j * binSize
float binHigh = binLow + binSize
color profileColor = j == pocIndex ? color.yellow : j >= lowerIndex and j <= upperIndex ? color.new(color.aqua, 45) : color.new(color.gray, 75)
if barWidth > 0
profileBox = box.new(bar_index + 3, binHigh, bar_index + 3 + barWidth, binLow, border_color=color.new(profileColor, 100), bgcolor=profileColor)
array.push(profileBoxes, profileBox)
pocLine := line.new(bar_index - profileLength, poc, bar_index + profileWidth + 5, poc, color=color.yellow, width=2)
vahLine := line.new(bar_index - profileLength, vah, bar_index + profileWidth + 5, vah, color=color.aqua, style=line.style_dashed)
valLine := line.new(bar_index - profileLength, val, bar_index + profileWidth + 5, val, color=color.aqua, style=line.style_dashed)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DASHBOARD
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var table dash = table.new(position.top_right, 2, 12, bgcolor=color.new(color.black, 10), frame_color=color.gray, frame_width=1, border_width=1)
if barstate.islast and showDashboard
color directionColor = bullTrend ? color.green : bearTrend ? color.red : color.gray
string signalText = buySignal ? "BUY" : sellSignal ? "SELL" : "WAIT"
color signalColor = buySignal ? color.green : sellSignal ? color.red : color.gray
string supplyStatus = na(supplyTop) ? "N/A" : supplyTested ? "TESTED" : "FRESH"
string demandStatus = na(demandBottom) ? "N/A" : demandTested ? "TESTED" : "FRESH"
string volumeStatus = volumeRatio >= volumeMultiplier ? "HIGH" : volumeRatio >= 0.8 ? "NORMAL" : "LOW"
table.cell(dash, 0, 0, "EMMA SETUPS", bgcolor=color.new(color.blue, 15), text_color=color.white)
table.cell(dash, 1, 0, "PRO VOLUME", bgcolor=color.new(color.blue, 15), text_color=color.white)
table.cell(dash, 0, 1, "MARKET")
table.cell(dash, 1, 1, syminfo.ticker, text_color=color.white)
table.cell(dash, 0, 2, "TIMEFRAME")
table.cell(dash, 1, 2, timeframe.period, text_color=color.white)
table.cell(dash, 0, 3, "DIRECTION")
table.cell(dash, 1, 3, marketDirection, text_color=directionColor)
table.cell(dash, 0, 4, "SIGNAL")
table.cell(dash, 1, 4, signalText, text_color=signalColor)
table.cell(dash, 0, 5, "SUPPLY")
table.cell(dash, 1, 5, na(supplyTop) ? "N/A" : str.tostring(supplyBottom, format.mintick) + " - " + str.tostring(supplyTop, format.mintick), text_color=color.red)
table.cell(dash, 0, 6, "SUPPLY STATUS")
table.cell(dash, 1, 6, supplyStatus, text_color=supplyTested ? color.orange : color.red)
table.cell(dash, 0, 7, "DEMAND")
table.cell(dash, 1, 7, na(demandBottom) ? "N/A" : str.tostring(demandBottom, format.mintick) + " - " + str.tostring(demandTop, format.mintick), text_color=color.lime)
table.cell(dash, 0, 8, "DEMAND STATUS")
table.cell(dash, 1, 8, demandStatus, text_color=demandTested ? color.orange : color.lime)
table.cell(dash, 0, 9, "VOLUME")
table.cell(dash, 1, 9, volumeStatus + " (" + str.tostring(volumeRatio, "#.##") + "x)", text_color=strongVolume ? color.lime : color.white)
table.cell(dash, 0, 10, "POC / VAH / VAL")
table.cell(dash, 1, 10, na(poc) ? "N/A" : str.tostring(poc, format.mintick) + " / " + str.tostring(vah, format.mintick) + " / " + str.tostring(val, format.mintick), text_color=color.yellow)
table.cell(dash, 0, 11, "STRUCTURE")
table.cell(dash, 1, 11, structureText, text_color=structureDirection == 1 ? color.lime : structureDirection == -1 ? color.red : color.gray)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(buySignal, "EMMA BUY Signal", "EMMA SETUPS: Bullish Supply & Demand confirmation")
alertcondition(sellSignal, "EMMA SELL Signal", "EMMA SETUPS: Bearish Supply & Demand confirmation")
alertcondition(bullBreak, "Bullish BOS / CHoCH", "EMMA SETUPS: Bullish market structure break")
alertcondition(bearBreak, "Bearish BOS / CHoCH", "EMMA SETUPS: Bearish market structure break")
alertcondition(not na(pivotHigh), "New Supply Zone", "EMMA SETUPS: New Supply Zone detected")
alertcondition(not na(pivotLow), "New Demand Zone", "EMMA SETUPS: New Demand Zone detected")