本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Auto HTF High Low + Trading Sessions", overlay=true, max_lines_count=500, max_labels_count=500)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 1. AUTO TIME FRAMES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
autoMode = input.bool(true, "Auto", group="AUTO TIME FRAMES")
tfUnder1m = input.timeframe("1", "If current TF < 1m", group="AUTO TIME FRAMES")
tfUnder5m = input.timeframe("15", "If current TF < 5m", group="AUTO TIME FRAMES")
tfUnder15m = input.timeframe("60", "If current TF < 15m", group="AUTO TIME FRAMES")
tfUnder1H = input.timeframe("240", "If current TF < 1H", group="AUTO TIME FRAMES")
tfUnder4H = input.timeframe("D", "If current TF < 4H", group="AUTO TIME FRAMES")
tfUnder1D = input.timeframe("W", "If current TF < 1D", group="AUTO TIME FRAMES")
tfUnder1W = input.timeframe("M", "If current TF < 1W", group="AUTO TIME FRAMES")
tfUnder1M = input.timeframe("3M", "If current TF < 1M", group="AUTO TIME FRAMES")
tfUnder3M = input.timeframe("12M", "If current TF < 3M", group="AUTO TIME FRAMES")
tfYearly = input.timeframe("12M", "Yearly / Higher TF", group="AUTO TIME FRAMES")
manualTF = input.timeframe("D", "Manual Period", group="MANUAL")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 2. TIMEFRAME ENGINE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
chartSeconds = timeframe.in_seconds()
string autoTF = switch
chartSeconds < 60 => tfUnder1m
chartSeconds < 300 => tfUnder5m
chartSeconds < 900 => tfUnder15m
chartSeconds < 3600 => tfUnder1H
chartSeconds < 14400 => tfUnder4H
chartSeconds < 86400 => tfUnder1D
chartSeconds < 604800 => tfUnder1W
chartSeconds < 2592000 => tfUnder1M
chartSeconds < 7776000 => tfUnder3M
=> tfYearly
string periodTF = autoMode ? autoTF : manualTF
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 3. DISPLAY
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
showHigh = input.bool(true, "Show High", group="DISPLAY")
showLow = input.bool(true, "Show Low", group="DISPLAY")
showSeparators = input.bool(true, "Show Separators", group="DISPLAY")
extendLevels = input.bool(false, "Extend H/L Right", group="DISPLAY")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 4. HIGH STYLE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
highColor = input.color(color.green, "Color", group="HIGH STYLE")
highTypeInput = input.string("Dashed", "Line Type",
options=["Solid", "Dashed", "Dotted"], group="HIGH STYLE")
highWidth = input.int(1, "Thickness", minval=1, maxval=5, group="HIGH STYLE")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 5. LOW STYLE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
lowColor = input.color(color.green, "Color", group="LOW STYLE")
lowTypeInput = input.string("Dashed", "Line Type",
options=["Solid", "Dashed", "Dotted"], group="LOW STYLE")
lowWidth = input.int(1, "Thickness", minval=1, maxval=5, group="LOW STYLE")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 6. SEPARATOR STYLE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
separatorColor = input.color(color.gray, "Color", group="SEPARATOR STYLE")
separatorTypeInput = input.string("Dotted", "Line Type",
options=["Solid", "Dashed", "Dotted"], group="SEPARATOR STYLE")
separatorWidth = input.int(1, "Thickness", minval=1, maxval=5, group="SEPARATOR STYLE")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 7. TOP TEXT SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
showSessionText = input.bool(true, "Show Day / Week Text", group="TOP TEXT")
textColor = input.color(color.silver, "Text Color", group="TOP TEXT")
shortDayNames = input.bool(false, "Short Day Names", group="TOP TEXT")
textSizeInput = input.string("Small", "Text Size",
options=["Tiny", "Small", "Normal"],
group="TOP TEXT")
// Default: text appears one-third into the period.
textFraction = input.float(0.3333, "Text Position Within Period",
minval=0.05, maxval=0.95, step=0.05,
group="TOP TEXT")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 8. TRADING SESSION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
string sessionTZ = "America/New_York"
sessionHour = input.int(18, "Trading Day Start Hour ET",
minval=0, maxval=23, group="TRADING SESSION")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 9. LINE STYLE FUNCTIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
getLineStyle(string selection) =>
switch selection
"Solid" => line.style_solid
"Dotted" => line.style_dotted
=> line.style_dashed
highStyle = getLineStyle(highTypeInput)
lowStyle = getLineStyle(lowTypeInput)
separatorStyle = getLineStyle(separatorTypeInput)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 10. HIGH / LOW ENGINE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
newPeriod = timeframe.change(periodTF)
var float periodHigh = na
var float periodLow = na
var int periodStart = na
var line highLine = na
var line lowLine = na
if na(periodHigh)
periodHigh := high
periodLow := low
periodStart := bar_index
if showHigh
highLine := line.new(
periodStart, periodHigh,
bar_index, periodHigh,
color=highColor,
style=highStyle,
width=highWidth)
if showLow
lowLine := line.new(
periodStart, periodLow,
bar_index, periodLow,
color=lowColor,
style=lowStyle,
width=lowWidth)
if newPeriod
if not na(highLine)
line.set_x2(highLine, bar_index - 1)
if not na(lowLine)
line.set_x2(lowLine, bar_index - 1)
periodHigh := high
periodLow := low
periodStart := bar_index
if showSeparators
line.new(
bar_index, low,
bar_index, high,
extend=extend.both,
color=separatorColor,
style=separatorStyle,
width=separatorWidth)
if showHigh
highLine := line.new(
periodStart, periodHigh,
bar_index, periodHigh,
color=highColor,
style=highStyle,
width=highWidth)
if showLow
lowLine := line.new(
periodStart, periodLow,
bar_index, periodLow,
color=lowColor,
style=lowStyle,
width=lowWidth)
else
periodHigh := math.max(periodHigh, high)
periodLow := math.min(periodLow, low)
if showHigh and not na(highLine)
line.set_x1(highLine, periodStart)
line.set_x2(highLine, bar_index)
line.set_y1(highLine, periodHigh)
line.set_y2(highLine, periodHigh)
line.set_color(highLine, highColor)
line.set_style(highLine, highStyle)
line.set_width(highLine, highWidth)
if showLow and not na(lowLine)
line.set_x1(lowLine, periodStart)
line.set_x2(lowLine, bar_index)
line.set_y1(lowLine, periodLow)
line.set_y2(lowLine, periodLow)
line.set_color(lowLine, lowColor)
line.set_style(lowLine, lowStyle)
line.set_width(lowLine, lowWidth)
if not na(highLine)
line.set_extend(highLine, extendLevels ? extend.right : extend.none)
if not na(lowLine)
line.set_extend(lowLine, extendLevels ? extend.right : extend.none)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 11. NEW YORK SESSION ENGINE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
nyYear = year(time, sessionTZ)
nyMonth = month(time, sessionTZ)
nyDate = dayofmonth(time, sessionTZ)
todayBoundary = timestamp(
sessionTZ, nyYear, nyMonth, nyDate, sessionHour, 0)
previousBoundary = timestamp(
sessionTZ, nyYear, nyMonth, nyDate - 1, sessionHour, 0)
sessionStart = time >= todayBoundary ? todayBoundary : previousBoundary
newTradingDay = not na(sessionStart[1]) and sessionStart != sessionStart[1]
sessionDOW = dayofweek(sessionStart, sessionTZ)
tradingDOW = sessionDOW == dayofweek.saturday ?
dayofweek.sunday : sessionDOW + 1
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 12. TEXT TIMING
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Daily text appears at 1/3 of a 24-hour period.
// With an 18:00 start, that is around 02:00 ET.
dayTextTarget = sessionStart +
int(24 * 60 * 60 * 1000 * textFraction)
dayTextNow = time >= dayTextTarget and
(na(time[1]) or time[1] < dayTextTarget)
// Weekly text appears at 1/3 of a 7-day period.
// The week starts Sunday at 18:00 ET.
isMondaySession = tradingDOW == dayofweek.monday
var int weekStart = na
var int weekNumber = 0
var int weekMonthKey = na
newTradingWeek = newTradingDay and isMondaySession
if na(weekStart)
weekStart := sessionStart
if newTradingWeek
weekStart := sessionStart
// Assign week numbers by Monday's month.
mondayTimestamp = timestamp(
sessionTZ,
year(sessionStart, sessionTZ),
month(sessionStart, sessionTZ),
dayofmonth(sessionStart, sessionTZ) + 1,
12, 0)
currentMonthKey = year(mondayTimestamp, sessionTZ) * 100 +
month(mondayTimestamp, sessionTZ)
if currentMonthKey != weekMonthKey
weekNumber := 1
weekMonthKey := currentMonthKey
else
weekNumber += 1
weekTextTarget = weekStart +
int(7 * 24 * 60 * 60 * 1000 * textFraction)
weekTextNow = time >= weekTextTarget and
(na(time[1]) or time[1] < weekTextTarget)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 13. DAY TEXT AT CHART TOP
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
showDays = showSessionText and chartSeconds < 14400
monday = showDays and dayTextNow and tradingDOW == dayofweek.monday
tuesday = showDays and dayTextNow and tradingDOW == dayofweek.tuesday
wednesday = showDays and dayTextNow and tradingDOW == dayofweek.wednesday
thursday = showDays and dayTextNow and tradingDOW == dayofweek.thursday
friday = showDays and dayTextNow and tradingDOW == dayofweek.friday
// Fully transparent shape, visible text only.
shapeColor = color.new(color.white, 100)
// Full names
plotshape(monday and not shortDayNames, title="Monday",
text="Monday", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
plotshape(tuesday and not shortDayNames, title="Tuesday",
text="Tuesday", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
plotshape(wednesday and not shortDayNames, title="Wednesday",
text="Wednesday", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
plotshape(thursday and not shortDayNames, title="Thursday",
text="Thursday", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
plotshape(friday and not shortDayNames, title="Friday",
text="Friday", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
// Short names
plotshape(monday and shortDayNames, title="Mon",
text="Mon", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
plotshape(tuesday and shortDayNames, title="Tue",
text="Tue", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
plotshape(wednesday and shortDayNames, title="Wed",
text="Wed", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
plotshape(thursday and shortDayNames, title="Thu",
text="Thu", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
plotshape(friday and shortDayNames, title="Fri",
text="Fri", style=shape.circle,
location=location.top, color=shapeColor,
textcolor=textColor, size=size.tiny)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 14. WEEK TEXT AT CHART TOP — 4H
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
showWeeks = showSessionText and chartSeconds == 14400
plotshape(showWeeks and weekTextNow and weekNumber == 1,
title="Week 1", text="Week 1",
style=shape.circle, location=location.top,
color=shapeColor, textcolor=textColor,
size=size.tiny)
plotshape(showWeeks and weekTextNow and weekNumber == 2,
title="Week 2", text="Week 2",
style=shape.circle, location=location.top,
color=shapeColor, textcolor=textColor,
size=size.tiny)
plotshape(showWeeks and weekTextNow and weekNumber == 3,
title="Week 3", text="Week 3",
style=shape.circle, location=location.top,
color=shapeColor, textcolor=textColor,
size=size.tiny)
plotshape(showWeeks and weekTextNow and weekNumber == 4,
title="Week 4", text="Week 4",
style=shape.circle, location=location.top,
color=shapeColor, textcolor=textColor,
size=size.tiny)
plotshape(showWeeks and weekTextNow and weekNumber == 5,
title="Week 5", text="Week 5",
style=shape.circle, location=location.top,
color=shapeColor, textcolor=textColor,
size=size.tiny)
plotshape(showWeeks and weekTextNow and weekNumber == 6,
title="Week 6", text="Week 6",
style=shape.circle, location=location.top,
color=shapeColor, textcolor=textColor,
size=size.tiny)