本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator("Previous Day and Current Day High/Low", overlay=true)
// Input group for Previous Day High/Low settings
prevGroupName = "Previous Day High/Low"
showPrevLines = input.bool(true, title="Show Lines", group=prevGroupName)
prevHighLineColor = input.color(color.blue, title="High Line Color", group=prevGroupName, inline="Colors")
prevLowLineColor = input.color(color.red, title="Low Line Color", group=prevGroupName, inline="Colors")
prevLineExtend = input.int(5, title="Extend Lines (Candles)", minval=1, group=prevGroupName)
prevLineStyle = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], group=prevGroupName, inline="StyleWidth") // Line style input
prevLineWidth = input.int(1, title="Width", minval=1, maxval=4, group=prevGroupName, inline="StyleWidth") // Line width input
// Input group for Current Day High/Low settings
currGroupName = "Current Day High/Low"
showCurrLines = input.bool(true, title="Show Lines", group=currGroupName)
currHighLineColor = input.color(color.green, title="High Line Color", group=currGroupName, inline="Colors")
currLowLineColor = input.color(color.orange, title="Low Line Color", group=currGroupName, inline="Colors")
currLineExtend = input.int(5, title="Extend Lines (Candles)", minval=1, group=currGroupName)
currLineStyle = input.string("Solid", title="Style", options=["Solid", "Dashed", "Dotted"], group=currGroupName, inline="StyleWidth") // Line style input
currLineWidth = input.int(1, title="Width", minval=1, maxval=4, group=currGroupName, inline="StyleWidth") // Line width input
// Helper function to map line style string to Pine Script line style
lineStyleMapping(style) =>
style == "Dashed" ? line.style_dashed : style == "Dotted" ? line.style_dotted : line.style_solid
// Variables for Previous Day line style and width
prevLineActualStyle = lineStyleMapping(prevLineStyle)
// Variables for Current Day line style and width
currLineActualStyle = lineStyleMapping(currLineStyle)
// Define the regular trading session
regularSession = input.session("0930-1600", title="Regular Trading Hours Session")
// Variables to store high and low values for RTH
var float prevDayRTHHigh = na
var float prevDayRTHLow = na
var float currDayRTHHigh = na
var float currDayRTHLow = na
var int prevDayStartBar = na
// Variables for previous day lines and labels
var line prevHighLine = na
var line prevLowLine = na
var label prevHighLabel = na
var label prevLowLabel = na
// Variables for current day lines and labels
var line currHighLine = na
var line currLowLine = na
var label currHighLabel = na
var label currLowLabel = na
// Check if the bar is in the regular trading session
inRegularSession = not na(time(timeframe.period, regularSession))
// Detect the start of a new day
newDay = ta.change(time("D")) != 0
// Variables to store current day's high and low
var float currDayHigh = na
var float currDayLow = na
var int currDayStartBar = na
if newDay
// Update previous day's RTH high and low
prevDayRTHHigh := currDayRTHHigh
prevDayRTHLow := currDayRTHLow
prevDayStartBar := currDayStartBar
// Reset current day's RTH high and low
currDayRTHHigh := na
currDayRTHLow := na
// Reset current day's high and low
currDayHigh := na
currDayLow := na
currDayStartBar := bar_index
// Delete old lines and labels for previous day
if not na(prevHighLine)
line.delete(prevHighLine)
prevHighLine := na
if not na(prevLowLine)
line.delete(prevLowLine)
prevLowLine := na
if not na(prevHighLabel)
label.delete(prevHighLabel)
prevHighLabel := na
if not na(prevLowLabel)
label.delete(prevLowLabel)
prevLowLabel := na
// Create new lines and labels for previous day's RTH if enabled
if showPrevLines and not na(prevDayStartBar)
prevHighLine := line.new(prevDayStartBar, prevDayRTHHigh, bar_index + prevLineExtend, prevDayRTHHigh, color=prevHighLineColor, style=prevLineActualStyle, width=prevLineWidth)
prevLowLine := line.new(prevDayStartBar, prevDayRTHLow, bar_index + prevLineExtend, prevDayRTHLow, color=prevLowLineColor, style=prevLineActualStyle, width=prevLineWidth)
prevHighLabel := label.new(bar_index + prevLineExtend, prevDayRTHHigh, "PDH", textcolor=prevHighLineColor, style=label.style_none)
prevLowLabel := label.new(bar_index + prevLineExtend, prevDayRTHLow, "PDL", textcolor=prevLowLineColor, style=label.style_none)
// Delete old lines and labels for current day
if not na(currHighLine)
line.delete(currHighLine)
currHighLine := na
if not na(currLowLine)
line.delete(currLowLine)
currLowLine := na
if not na(currHighLabel)
label.delete(currHighLabel)
currHighLabel := na
if not na(currLowLabel)
label.delete(currLowLabel)
currLowLabel := na
else
// Accumulate current day's RTH high and low during regular session
if inRegularSession
currDayRTHHigh := na(currDayRTHHigh) ? high : math.max(currDayRTHHigh, high)
currDayRTHLow := na(currDayRTHLow) ? low : math.min(currDayRTHLow, low)
// Update current day's high and low
currDayHigh := na(currDayHigh) ? high : math.max(currDayHigh, high)
currDayLow := na(currDayLow) ? low : math.min(currDayLow, low)
// Update current day lines and labels
if showCurrLines
if not na(currHighLine)
line.set_x2(currHighLine, bar_index + currLineExtend)
line.set_y1(currHighLine, currDayRTHHigh)
line.set_y2(currHighLine, currDayRTHHigh)
else
currHighLine := line.new(currDayStartBar, currDayRTHHigh, bar_index + currLineExtend, currDayRTHHigh, color=currHighLineColor, style=currLineActualStyle, width=currLineWidth)
if not na(currLowLine)
line.set_x2(currLowLine, bar_index + currLineExtend)
line.set_y1(currLowLine, currDayRTHLow)
line.set_y2(currLowLine, currDayRTHLow)
else
currLowLine := line.new(currDayStartBar, currDayRTHLow, bar_index + currLineExtend, currDayRTHLow, color=currLowLineColor, style=currLineActualStyle, width=currLineWidth)
if not na(currHighLabel)
label.set_x(currHighLabel, bar_index + currLineExtend)
label.set_y(currHighLabel, currDayRTHHigh)
else
currHighLabel := label.new(bar_index + currLineExtend, currDayRTHHigh, "CDH", textcolor=currHighLineColor, style=label.style_none)
if not na(currLowLabel)
label.set_x(currLowLabel, bar_index + currLineExtend)
label.set_y(currLowLabel, currDayRTHLow)
else
currLowLabel := label.new(bar_index + currLineExtend, currDayRTHLow, "CDL", textcolor=currLowLineColor, style=label.style_none)
// Extend previous day's RTH lines and labels
if showPrevLines
if not na(prevHighLine)
line.set_x2(prevHighLine, bar_index + prevLineExtend)
if not na(prevLowLine)
line.set_x2(prevLowLine, bar_index + prevLineExtend)
if not na(prevHighLabel)
label.set_x(prevHighLabel, bar_index + prevLineExtend)
if not na(prevLowLabel)
label.set_x(prevLowLabel, bar_index + prevLineExtend)