本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Hourly + M45 Vertical Time V1.0", shorttitle="H1 + M45 Time V1.0",
overlay=true, max_lines_count=500, max_labels_count=500)
// ============================================================================
// HOURLY + M45 VERTICAL TIME — V1.0
//
// FEATURES
// - H1 (:00) vertical time markers
// - M45 (:45) 15-minute warning markers
// - Eastern / Central DST-aware timezone selection
// - 12-hour / 24-hour time display
// - Bottom / Center / Top timestamp positioning
// - Independent H1 and M45 line styling
// - M45 labels: Full Time / :45 / 15m / None
// - H1 and M45 alerts
// - Pre-H1 confirmed swing high / swing low identification
// - Swing search windows: Current Hour / Last 90 Minutes / Last 2 Hours
// - Adjustable swing strength
// - Circle or triangle swing markers
// - Independent high/low colors and sizes
// ============================================================================
// ============================================================================
// 1. TIME SETTINGS
// ============================================================================
groupTime = "1. Time Settings"
showH1 = input.bool(true, "Show Hourly (:00) Markers", group=groupTime)
showM45 = input.bool(true, "Show M45 (:45) Markers", group=groupTime)
timezoneChoice = input.string(
"Chicago (Central)",
"Time Zone",
options=["New York (Eastern)", "Chicago (Central)"],
group=groupTime)
timeFormat = input.string(
"12 Hour",
"Time Format",
options=["12 Hour", "24 Hour"],
group=groupTime)
labelPosition = input.string(
"Bottom",
"Timestamp Position",
options=["Bottom", "Center", "Top"],
group=groupTime)
string selectedTZ =
timezoneChoice == "New York (Eastern)" ?
"America/New_York" :
"America/Chicago"
// ============================================================================
// 2. H1 (:00) STYLE
// ============================================================================
groupH1 = "2. H1 (:00) Style"
h1Color = input.color(
color.new(color.gray, 25),
"H1 Line Color",
group=groupH1)
h1StyleInput = input.string(
"Dashed",
"H1 Line Style",
options=["Solid", "Dashed", "Dotted"],
group=groupH1)
h1Width = input.int(
1,
"H1 Line Width",
minval=1,
maxval=4,
group=groupH1)
showH1Label = input.bool(
true,
"Show H1 Timestamp",
group=groupH1)
h1LabelColor = input.color(
color.black,
"H1 Timestamp Color",
group=groupH1)
h1LabelSizeInput = input.string(
"Normal",
"H1 Timestamp Size",
options=["Tiny", "Small", "Normal", "Large"],
group=groupH1)
// ============================================================================
// 3. M45 (:45) STYLE
// ============================================================================
groupM45 = "3. M45 (:45) Style"
m45Color = input.color(
color.new(color.gray, 65),
"M45 Line Color",
group=groupM45)
m45StyleInput = input.string(
"Dotted",
"M45 Line Style",
options=["Solid", "Dashed", "Dotted"],
group=groupM45)
m45Width = input.int(
1,
"M45 Line Width",
minval=1,
maxval=4,
group=groupM45)
m45LabelStyle = input.string(
":45",
"M45 Label Style",
options=["Full Time", ":45", "15m", "None"],
group=groupM45)
m45LabelColor = input.color(
color.new(color.gray, 10),
"M45 Label Color",
group=groupM45)
m45LabelSizeInput = input.string(
"Small",
"M45 Label Size",
options=["Tiny", "Small", "Normal", "Large"],
group=groupM45)
// ============================================================================
// 4. PRE-H1 SWING MARKERS
// ============================================================================
groupSwing = "4. Pre-H1 Swing Markers"
showSwings = input.bool(
true,
"Show Pre-H1 Swings",
group=groupSwing)
swingDisplay = input.string(
"Both High + Low",
"Show Swing Markers",
options=["Both High + Low", "High Only", "Low Only"],
group=groupSwing)
swingStrength = input.int(
2,
"Swing Strength",
minval=1,
maxval=10,
tooltip="Number of candles required on each side of a pivot. Higher values identify stronger, less frequent swings.",
group=groupSwing)
swingSearchWindow = input.string(
"Current Hour",
"Swing Search Window",
options=["Current Hour", "Last 90 Minutes", "Last 2 Hours"],
tooltip="Controls how far before each H1 open the indicator may search for the nearest confirmed swing high and low.",
group=groupSwing)
swingShape = input.string(
"Triangle",
"Marker Shape",
options=["Triangle", "Circle"],
group=groupSwing)
swingHighColor = input.color(
color.red,
"Swing High Color",
group=groupSwing)
swingLowColor = input.color(
color.lime,
"Swing Low Color",
group=groupSwing)
swingHighSizeInput = input.string(
"Small",
"Swing High Size",
options=["Tiny", "Small", "Normal", "Large"],
group=groupSwing)
swingLowSizeInput = input.string(
"Small",
"Swing Low Size",
options=["Tiny", "Small", "Normal", "Large"],
group=groupSwing)
// ============================================================================
// 5. DISPLAY
// ============================================================================
groupDisplay = "5. Display"
historyDays = input.int(
5,
"Historical Days To Display",
minval=1,
maxval=10,
tooltip="Limits how far back H1, M45 and finalized swing markers are drawn.",
group=groupDisplay)
// ============================================================================
// 6. ALERTS
// ============================================================================
groupAlerts = "6. Alerts"
enableH1Alert = input.bool(
true,
"Enable H1 Alert Condition",
group=groupAlerts)
enableM45Alert = input.bool(
true,
"Enable M45 / 15-Minute Warning",
group=groupAlerts)
// ============================================================================
// 7. HELPER FUNCTIONS
// ============================================================================
f_lineStyle(string styleInput) =>
switch styleInput
"Solid" => line.style_solid
"Dashed" => line.style_dashed
=> line.style_dotted
f_labelSize(string sizeInput) =>
switch sizeInput
"Tiny" => size.tiny
"Small" => size.small
"Large" => size.large
=> size.normal
f_markerSize(string sizeInput) =>
switch sizeInput
"Tiny" => size.tiny
"Small" => size.small
"Large" => size.large
=> size.normal
f_hourText(int hr) =>
string result = ""
if timeFormat == "24 Hour"
result := str.tostring(hr, "00") + ":00"
else
int displayHour = hr % 12
displayHour := displayHour == 0 ? 12 : displayHour
string suffix = hr < 12 ? "am" : "pm"
result := str.tostring(displayHour) + suffix
result
f_m45FullText(int hr) =>
string result = ""
if timeFormat == "24 Hour"
result := str.tostring(hr, "00") + ":45"
else
int displayHour = hr % 12
displayHour := displayHour == 0 ? 12 : displayHour
string suffix = hr < 12 ? "am" : "pm"
result := str.tostring(displayHour) + ":45" + suffix
result
f_m45Text(int hr) =>
switch m45LabelStyle
"Full Time" => f_m45FullText(hr)
":45" => ":45"
"15m" => "15m"
=> ""
f_timestampLabelStyle() =>
switch labelPosition
"Top" => label.style_label_down
"Center" => label.style_none
=> label.style_label_up
// Search window in milliseconds.
f_searchWindowMs() =>
switch swingSearchWindow
"Last 90 Minutes" => 90 * 60 * 1000
"Last 2 Hours" => 120 * 60 * 1000
=> 60 * 60 * 1000
// ============================================================================
// 8. CONVERT INPUT SETTINGS
// ============================================================================
h1LineStyle = f_lineStyle(h1StyleInput)
m45LineStyle = f_lineStyle(m45StyleInput)
h1LabelSize = f_labelSize(h1LabelSizeInput)
m45LabelSize = f_labelSize(m45LabelSizeInput)
swingHighSize = f_markerSize(swingHighSizeInput)
swingLowSize = f_markerSize(swingLowSizeInput)
int swingWindowMs = f_searchWindowMs()
// ============================================================================
// 9. TIME / EVENT DETECTION
// ============================================================================
int localHour = hour(time, selectedTZ)
int currentYear = year(time, selectedTZ)
int currentMonth = month(time, selectedTZ)
int currentDay = dayofmonth(time, selectedTZ)
int thisHourTimestamp = timestamp(
selectedTZ,
currentYear,
currentMonth,
currentDay,
localHour,
0)
int thisM45Timestamp = timestamp(
selectedTZ,
currentYear,
currentMonth,
currentDay,
localHour,
45)
// Detect when target timestamp occurs inside the chart bar.
bool crossesH1 =
time <= thisHourTimestamp and
time_close > thisHourTimestamp
bool crossesM45 =
time <= thisM45Timestamp and
time_close > thisM45Timestamp
bool newH1Event =
crossesH1 and not crossesH1[1]
bool newM45Event =
crossesM45 and not crossesM45[1]
// ============================================================================
// 10. HISTORY FILTER
// ============================================================================
int historyMilliseconds =
historyDays * 24 * 60 * 60 * 1000
bool insideHistory =
time >= timenow - historyMilliseconds
// ============================================================================
// 11. TIMESTAMP LABEL Y POSITION
// ============================================================================
float barRange =
math.max(high - low, syminfo.mintick * 10)
f_timestampY() =>
switch labelPosition
"Top" => high + barRange * 2.0
"Center" => hl2
=> low - barRange * 2.0
// ============================================================================
// 12. SWING DETECTION
// ============================================================================
//
// A swing is confirmed using ta.pivothigh / ta.pivotlow.
//
// IMPORTANT:
// Pine confirms the swing AFTER swingStrength right-side candles exist.
// We therefore store the timestamp and price of the ORIGINAL pivot candle,
// not the later confirmation candle.
//
// The actual swing markers are finalized when the next H1 arrives.
// ============================================================================
float confirmedPivotHigh =
ta.pivothigh(high, swingStrength, swingStrength)
float confirmedPivotLow =
ta.pivotlow(low, swingStrength, swingStrength)
// Candidate nearest swing high.
var float candidateHighPrice = na
var int candidateHighTime = na
// Candidate nearest swing low.
var float candidateLowPrice = na
var int candidateLowTime = na
// Timestamp of actual pivot candle.
int pivotCandleTime =
time[swingStrength]
// Store newest confirmed swing high.
if not na(confirmedPivotHigh)
candidateHighPrice := confirmedPivotHigh
candidateHighTime := pivotCandleTime
// Store newest confirmed swing low.
if not na(confirmedPivotLow)
candidateLowPrice := confirmedPivotLow
candidateLowTime := pivotCandleTime
// ============================================================================
// 13. DRAW H1 + FINALIZE PRE-H1 SWINGS
// ============================================================================
if showH1 and newH1Event and insideHistory
// ------------------------------------------------------------------------
// H1 VERTICAL LINE
// ------------------------------------------------------------------------
line.new(
x1=thisHourTimestamp,
y1=low,
x2=thisHourTimestamp,
y2=high,
xloc=xloc.bar_time,
extend=extend.both,
color=h1Color,
style=h1LineStyle,
width=h1Width)
// ------------------------------------------------------------------------
// H1 TIMESTAMP
// ------------------------------------------------------------------------
if showH1Label
label.new(
x=thisHourTimestamp,
y=f_timestampY(),
text=f_hourText(localHour),
xloc=xloc.bar_time,
yloc=yloc.price,
style=f_timestampLabelStyle(),
textcolor=h1LabelColor,
color=color.new(color.white, 100),
size=h1LabelSize,
textalign=text.align_center)
// ------------------------------------------------------------------------
// DETERMINE VALID PRE-H1 SEARCH WINDOW
// ------------------------------------------------------------------------
int searchStart =
thisHourTimestamp - swingWindowMs
bool validHigh =
showSwings and
not na(candidateHighTime) and
candidateHighTime >= searchStart and
candidateHighTime < thisHourTimestamp
bool validLow =
showSwings and
not na(candidateLowTime) and
candidateLowTime >= searchStart and
candidateLowTime < thisHourTimestamp
bool allowHigh =
swingDisplay == "Both High + Low" or
swingDisplay == "High Only"
bool allowLow =
swingDisplay == "Both High + Low" or
swingDisplay == "Low Only"
// ------------------------------------------------------------------------
// FINAL SWING HIGH MARKER
//
// Triangle points DOWN toward the actual swing-high price.
// ------------------------------------------------------------------------
if validHigh and allowHigh
if swingShape == "Triangle"
label.new(
x=candidateHighTime,
y=candidateHighPrice,
text="▼",
xloc=xloc.bar_time,
yloc=yloc.price,
style=label.style_none,
textcolor=swingHighColor,
size=swingHighSize)
else
label.new(
x=candidateHighTime,
y=candidateHighPrice,
text="●",
xloc=xloc.bar_time,
yloc=yloc.price,
style=label.style_none,
textcolor=swingHighColor,
size=swingHighSize)
// ------------------------------------------------------------------------
// FINAL SWING LOW MARKER
//
// Triangle points UP toward the actual swing-low price.
// ------------------------------------------------------------------------
if validLow and allowLow
if swingShape == "Triangle"
label.new(
x=candidateLowTime,
y=candidateLowPrice,
text="▲",
xloc=xloc.bar_time,
yloc=yloc.price,
style=label.style_none,
textcolor=swingLowColor,
size=swingLowSize)
else
label.new(
x=candidateLowTime,
y=candidateLowPrice,
text="●",
xloc=xloc.bar_time,
yloc=yloc.price,
style=label.style_none,
textcolor=swingLowColor,
size=swingLowSize)
// ============================================================================
// 14. DRAW M45
// ============================================================================
if showM45 and newM45Event and insideHistory
line.new(
x1=thisM45Timestamp,
y1=low,
x2=thisM45Timestamp,
y2=high,
xloc=xloc.bar_time,
extend=extend.both,
color=m45Color,
style=m45LineStyle,
width=m45Width)
if m45LabelStyle != "None"
label.new(
x=thisM45Timestamp,
y=f_timestampY(),
text=f_m45Text(localHour),
xloc=xloc.bar_time,
yloc=yloc.price,
style=f_timestampLabelStyle(),
textcolor=m45LabelColor,
color=color.new(color.white, 100),
size=m45LabelSize,
textalign=text.align_center)
// ============================================================================
// 15. ALERT CONDITIONS
// ============================================================================
bool h1AlertSignal =
enableH1Alert and newH1Event
bool m45AlertSignal =
enableM45Alert and newM45Event
alertcondition(
h1AlertSignal,
title="H1 Hourly Open",
message="H1 hourly open has occurred.")
alertcondition(
m45AlertSignal,
title="M45 - 15 Minutes Until Next H1",
message="M45 reached. 15 minutes until the next H1 open.")