本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Multi-Timeframe Volumized Order Blocks", overlay=true, max_boxes_count=500, max_labels_count=500)
// --- Inputs & Timeframe Dropdown ---
tfSelection = input.string("4 Hours", title="Higher Timeframe", options=["Monthly", "Weekly", "Daily", "4 Hours", "1 Hour"], tooltip="Select the higher timeframe to extract order blocks from.")
volThreshold = input.float(1.5, title="Volume Threshold Multiplier (vs SMA)", minval=0.5)
showBullish = input.bool(true, title="Show Bullish Order Blocks")
showBearish = input.bool(true, title="Show Bearish Order Blocks")
bullColor = input.color(color.new(color.green, 80), title="Bullish Box Color")
bearColor = input.color(color.new(color.red, 80), title="Bearish Box Color")
// Map human-readable selections to TradingView timeframe codes
tfCode = switch tfSelection
"Monthly" => "M"
"Weekly" => "W"
"Daily" => "D"
"4 Hours" => "240"
"1 Hour" => "60"
=> "240"
// --- Fetch Higher Timeframe Data ---
htf_open = request.security(syminfo.tickerid, tfCode, open[1], barmerge.gaps_off, barmerge.lookahead_on)
htf_close = request.security(syminfo.tickerid, tfCode, close[1], barmerge.gaps_off, barmerge.lookahead_on)
htf_high = request.security(syminfo.tickerid, tfCode, high[1], barmerge.gaps_off, barmerge.lookahead_on)
htf_low = request.security(syminfo.tickerid, tfCode, low[1], barmerge.gaps_off, barmerge.lookahead_on)
htf_volume = request.security(syminfo.tickerid, tfCode, volume[1], barmerge.gaps_off, barmerge.lookahead_on)
htf_volSMA = request.security(syminfo.tickerid, tfCode, ta.sma(volume, 20)[1], barmerge.gaps_off, barmerge.lookahead_on)
// Detect when a higher timeframe bar changes
htf_time = request.security(syminfo.tickerid, tfCode, time, barmerge.gaps_off, barmerge.lookahead_on)
isNewHtfBar = ta.change(htf_time) != 0
// --- Order Block Logic on HTF ---
isBullishOB = (htf_close < htf_open) and (htf_volume > htf_volSMA * volThreshold)
isBearishOB = (htf_close > htf_open) and (htf_volume > htf_volSMA * volThreshold)
// Calculate volume percentage strength representation
volRatio = math.round((htf_volume / htf_volSMA) * 100)
// --- Plotting Boxes & Labels ---
if isNewHtfBar
// Plot Bullish Order Block Zone
if showBullish and isBullishOB
box.new(left=bar_index[1], top=htf_high, right=bar_index, bottom=htf_low,
bgcolor=bullColor, border_color=color.green, border_style=line.style_solid,
text=tfSelection + " Bull OB (" + str.tostring(volRatio) + "%)",
text_color=color.white, text_size=size.small)
// Plot Bearish Order Block Zone
if showBearish and isBearishOB
box.new(left=bar_index[1], top=htf_high, right=bar_index, bottom=htf_low,
bgcolor=bearColor, border_color=color.red, border_style=line.style_solid,
text=tfSelection + " Bear OB (" + str.tostring(volRatio) + "%)",
text_color=color.white, text_size=size.small)