本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(
title = "Previous Day Range Map [Trade Tribe HQ]",
shorttitle = "Prev Day Range Map",
overlay = true,
max_labels_count = 50
)
// ---------------------------------------------
// TRADING DAY DEFINITION
// ---------------------------------------------
grpDay = "Trading Day Definition"
dayPreset = input.string(
"Forex - 5 PM New York",
title = "Trading Day Resets At",
options = ["Forex - 5 PM New York", "CME Futures - 6 PM New York", "Midnight UTC", "Custom"],
group = grpDay,
tooltip = "Defines where one trading day ends and the next begins. " +
"Forex convention closes the day at 5 PM New York. " +
"CME Globex reopens at 6 PM New York.")
customHour = input.int(
17,
title = "Custom Reset Hour (0-23)",
minval = 0,
maxval = 23,
group = grpDay)
customTz = input.string(
"America/New_York",
title = "Custom Time Zone",
group = grpDay,
tooltip = "Only used when 'Custom' is selected. Examples: America/New_York, Europe/London, Asia/Tokyo, UTC")
// ---------------------------------------------
// LEVELS
// ---------------------------------------------
grpLvl = "Levels"
showPDH = input.bool(true, "Show Previous Day High", group = grpLvl)
showPDL = input.bool(true, "Show Previous Day Low", group = grpLvl)
showMid = input.bool(true, "Show Previous Day Midpoint", group = grpLvl,
tooltip = "50% of the previous day's range. Useful as a bias line.")
pdhColor = input.color(#388ce1, "PDH Color", group = grpLvl)
pdlColor = input.color(#388ce1, "PDL Color", group = grpLvl)
midColor = input.color(color.new(#388ce1, 40), "Midpoint Color", group = grpLvl)
lineWidth = input.int(1, "Line Width", minval = 1, maxval = 5, group = grpLvl)
historyDays = input.int(7, "Days of History to Draw (calendar days)", minval = 1, maxval = 365, group = grpLvl)
showLabels = input.bool(true, "Show Price Labels (current day)", group = grpLvl)
// ---------------------------------------------
// REACTION TRACKING
// ---------------------------------------------
grpRx = "Sweep vs. Acceptance"
showReactions = input.bool(true, "Mark First Sweep / Break Each Day", group = grpRx,
tooltip = "SWEEP = trades beyond the level, closes back inside. " +
"BREAK = closes beyond the level. " +
"First event of each type per day, on candle close (no repaint).")
sweepColor = input.color(#f5a623, "Sweep Color", group = grpRx)
breakColor = input.color(#26a69a, "Break Color", group = grpRx)
// ---------------------------------------------
// RANGE CONTEXT
// ---------------------------------------------
grpCtx = "Range Context Panel"
showTable = input.bool(true, "Show Context Panel", group = grpCtx)
avgLen = input.int(10, "Average Range Lookback (days)", minval = 2, maxval = 60, group = grpCtx)
compressPct = input.float(70, "Compressed Below (% of average)", minval = 1, group = grpCtx)
expandPct = input.float(130, "Expanded Above (% of average)", minval = 1, group = grpCtx)
tablePosIn = input.string("Top Right", "Panel Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpCtx)
// ---------------------------------------------
// RESOLVE TRADING DAY BOUNDARY
// ---------------------------------------------
resetHour = switch dayPreset
"Forex - 5 PM New York" => 17
"CME Futures - 6 PM New York" => 18
"Midnight UTC" => 0
=> customHour
resetTz = switch dayPreset
"Midnight UTC" => "UTC"
"Custom" => customTz
=> "America/New_York"
// Shift each bar's timestamp back by the reset hour, then read the date
// in the chosen time zone. This makes the reset hour behave like midnight,
// and it follows daylight saving changes automatically.
shiftedTime = time - resetHour * 3600000
dayKey = year(shiftedTime, resetTz) * 10000 + month(shiftedTime, resetTz) * 100 + dayofmonth(shiftedTime, resetTz)
newDay = dayKey != nz(dayKey[1], dayKey)
isIntra = timeframe.isintraday
// ---------------------------------------------
// BUILD THE PREVIOUS DAY FROM CHART BARS
// ---------------------------------------------
var float runHigh = na
var float runLow = na
var float pdh = na
var float pdl = na
var float pdMid = na
var float pdRange = na
var float avgRange = na
var float rangePct = na
var int daysStarted = 0
var array<float> rangeHistory = array.new<float>()
// 0 = untouched, 1 = swept (wick beyond, close inside), 2 = closed beyond
var int highState = 0
var int lowState = 0
if newDay
// The first day on the chart is partial, so it is skipped.
if daysStarted > 0
pdh := runHigh
pdl := runLow
pdMid := (runHigh + runLow) / 2
pdRange := runHigh - runLow
// Compare the completed day to the days BEFORE it
avgRange := rangeHistory.size() > 0 ? rangeHistory.avg() : na
rangePct := not na(avgRange) and avgRange > 0 ? pdRange / avgRange * 100 : na
rangeHistory.push(pdRange)
if rangeHistory.size() > avgLen
rangeHistory.shift()
daysStarted += 1
runHigh := high
runLow := low
highState := 0
lowState := 0
else
runHigh := na(runHigh) ? high : math.max(runHigh, high)
runLow := na(runLow) ? low : math.min(runLow, low)
// ---------------------------------------------
// SWEEP vs. ACCEPTANCE (confirmed bars only)
// ---------------------------------------------
bool sweptHigh = false
bool brokeHigh = false
bool sweptLow = false
bool brokeLow = false
if not na(pdh) and barstate.isconfirmed
if highState < 2 and close > pdh
highState := 2
brokeHigh := true
else if highState == 0 and high > pdh
highState := 1
sweptHigh := true
if lowState < 2 and close < pdl
lowState := 2
brokeLow := true
else if lowState == 0 and low < pdl
lowState := 1
sweptLow := true
plotshape(isIntra and showReactions and sweptHigh, title = "PDH Sweep",
style = shape.triangledown, location = location.abovebar, color = sweepColor,
text = "Sweep", textcolor = sweepColor, size = size.tiny)
plotshape(isIntra and showReactions and brokeHigh, title = "PDH Break",
style = shape.triangleup, location = location.abovebar, color = breakColor,
text = "Break", textcolor = breakColor, size = size.tiny)
plotshape(isIntra and showReactions and sweptLow, title = "PDL Sweep",
style = shape.triangleup, location = location.belowbar, color = sweepColor,
text = "Sweep", textcolor = sweepColor, size = size.tiny)
plotshape(isIntra and showReactions and brokeLow, title = "PDL Break",
style = shape.triangledown, location = location.belowbar, color = breakColor,
text = "Break", textcolor = breakColor, size = size.tiny)
// ---------------------------------------------
// LINES (one segment per trading day)
// ---------------------------------------------
// Only draw the most recent N calendar days. The first bar of each
// day is left blank so each day's level is its own clean segment.
inWindow = time >= last_bar_time - historyDays * 86400000
drawBar = isIntra and inWindow and not newDay
plot(showPDH and drawBar ? pdh : na,
title = "Previous Day High", color = pdhColor, linewidth = lineWidth, style = plot.style_linebr)
plot(showPDL and drawBar ? pdl : na,
title = "Previous Day Low", color = pdlColor, linewidth = lineWidth, style = plot.style_linebr)
plot(showMid and drawBar ? pdMid : na,
title = "Previous Day Midpoint", color = midColor, linewidth = 1, style = plot.style_linebr)
// ---------------------------------------------
// CURRENT-DAY PRICE LABELS
// ---------------------------------------------
var label hiLbl = na
var label loLbl = na
var label midLbl = na
if barstate.islast
label.delete(hiLbl)
label.delete(loLbl)
label.delete(midLbl)
if isIntra and showLabels and not na(pdh)
if showPDH
hiLbl := label.new(bar_index + 3, pdh, "PDH " + str.tostring(pdh, format.mintick),
style = label.style_label_left, color = color.new(pdhColor, 85), textcolor = pdhColor, size = size.small)
if showPDL
loLbl := label.new(bar_index + 3, pdl, "PDL " + str.tostring(pdl, format.mintick),
style = label.style_label_left, color = color.new(pdlColor, 85), textcolor = pdlColor, size = size.small)
if showMid
midLbl := label.new(bar_index + 3, pdMid, "MID " + str.tostring(pdMid, format.mintick),
style = label.style_label_left, color = color.new(midColor, 85), textcolor = midColor, size = size.small)
// ---------------------------------------------
// CONTEXT PANEL
// ---------------------------------------------
tablePos = switch tablePosIn
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
var table panel = table.new(tablePos, 2, 6, bgcolor = color.new(color.gray, 88), frame_color = color.new(color.gray, 60), frame_width = 1, border_width = 0)
stateText(int s, bool isHigh) =>
s == 0 ? "Untouched" : s == 1 ? "Swept, closed back inside" : isHigh ? "Accepted above" : "Accepted below"
if barstate.islast and showTable
txt = chart.fg_color
if not isIntra
table.cell(panel, 0, 0, "PD Range Map runs on intraday charts", text_color = txt, text_size = size.small)
else
rangeTag = na(rangePct) ? "Building history" : rangePct < compressPct ? "Compressed" : rangePct > expandPct ? "Expanded" : "Normal"
rangeVal = na(rangePct) ? rangeTag : str.tostring(rangePct, "#") + "% | " + rangeTag
pricePos = na(pdh) ? "-" : close > pdh ? "Above PDH" : close < pdl ? "Below PDL" : close >= pdMid ? "Inside | upper half" : "Inside | lower half"
table.cell(panel, 0, 0, "Day resets", text_color = txt, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 0, str.tostring(resetHour, "00") + ":00 " + resetTz, text_color = txt, text_size = size.small, text_halign = text.align_right)
table.cell(panel, 0, 1, "Prev day range", text_color = txt, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 1, na(pdRange) ? "-" : str.tostring(pdRange, format.mintick), text_color = txt, text_size = size.small, text_halign = text.align_right)
table.cell(panel, 0, 2, "vs " + str.tostring(avgLen) + "-day avg", text_color = txt, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 2, rangeVal, text_color = txt, text_size = size.small, text_halign = text.align_right)
table.cell(panel, 0, 3, "PDH", text_color = txt, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 3, stateText(highState, true), text_color = highState == 0 ? txt : highState == 1 ? sweepColor : breakColor, text_size = size.small, text_halign = text.align_right)
table.cell(panel, 0, 4, "PDL", text_color = txt, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 4, stateText(lowState, false), text_color = lowState == 0 ? txt : lowState == 1 ? sweepColor : breakColor, text_size = size.small, text_halign = text.align_right)
table.cell(panel, 0, 5, "Price", text_color = txt, text_size = size.small, text_halign = text.align_left)
table.cell(panel, 1, 5, pricePos, text_color = txt, text_size = size.small, text_halign = text.align_right)
// ---------------------------------------------
// ALERTS (fire on candle close)
// ---------------------------------------------
alertcondition(sweptHigh, "PDH Sweep", "{{ticker}}: traded above the previous day high and closed back inside the range")
alertcondition(brokeHigh, "PDH Break", "{{ticker}}: closed above the previous day high")
alertcondition(sweptLow, "PDL Sweep", "{{ticker}}: traded below the previous day low and closed back inside the range")
alertcondition(brokeLow, "PDL Break", "{{ticker}}: closed below the previous day low")