本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gdad
//Developed with the aid of ChatGPT
//Keep important price levels visible with descriptive labels that identify what each level represents and when it originated
//This indicator combines six manually configured levels, automatic previous-day levels, and optional EMA reference levels from the 5-minute, 15-minute, and 60-minute timeframes.
//FEATURES
//• Six manual levels with independent visibility, color, label text, price, and start date/time.
//• Automatic previous-day high, low, and close, each independently selectable.
//• Optional EMA 20, 100, and 200 levels from each of three timeframes: 5 minutes, 15 minutes, and 60 minutes.
//• Adjustable line width and solid, dashed, or dotted styling for manual and EMA levels. Previous-day levels use dashed lines.
//• Adjustable line extension and label spacing.
//• Optional timestamps identifying manual level start times and EMA source-bar closing times.
//MANUAL LEVELS
//Enter a price, select its start date/time, and give the level a descriptive label. These levels can mark swing highs and lows, support and resistance, or other prices you want to monitor.
//Manual levels remain at the entered prices until you change their settings. Swing points are selected by the user; the indicator does not detect them automatically.
//PREVIOUS-DAY LEVELS
//Previous-day high, low, and close come from the symbol’s daily data feed. For futures, these values may differ from levels calculated using only regular trading hours.
//CURRENT EMA LEVELS
//EMA references are horizontal lines showing the value from the previous completed source-timeframe candle. They update as new source-timeframe periods begin and remain fixed while that source candle develops.
//The “Start lines this many chart bars back” setting controls their displayed length. It does not select a historical EMA value or freeze the level.
//When any EMA reference is enabled, use a time-based chart of 5 minutes or lower.
//DISPLAY NOTES
//Labels appear to the right of the latest chart bar. Allow sufficient space on the right side of the chart to display them.
//Label timestamps use the symbol’s exchange timezone, which may differ from the chart’s selected timezone.
//This indicator organizes reference levels for visual analysis. It does not generate trade signals.
//Developed with assistance from ChatGPT.
//@version=6
indicator("Swing and Previous Day Levels", overlay = true, max_lines_count = 18, max_labels_count = 18, max_bars_back = 5000)
const int defaultStartDate = timestamp("1 Oct 2026 09:30 -0400")
// Shared display settings
extendBars = input.int(2, "Bars past current bar", minval = 0, maxval = 50, group = "Display")
labelGapBars = input.int(10, "Label gap in bars", minval = 0, maxval = 20, group = "Display")
appendDateTime = input.bool(true, "Append start date/time to labels", group = "Display", tooltip = "Dates and times in labels use the symbol exchange timezone. EMA timestamps identify the source bar close.")
lineWidth = input.int(2, "Line width", minval = 1, maxval = 5, group = "Display")
styleChoice = input.string("Solid", "Line style", options = ["Solid", "Dashed", "Dotted"], group = "Display")
selectedLineStyle = styleChoice == "Dashed" ? line.style_dashed : styleChoice == "Dotted" ? line.style_dotted : line.style_solid
// Manual level 1
color1 = input.color(color.aqua, "", group = "Level 1", inline = "1A")
show1 = input.bool(false, "Show", group = "Level 1", inline = "1A")
text1 = input.string("Swing High", "Label", group = "Level 1", inline = "1A")
price1 = input.float(0.00, "Price", step = 0.25, group = "Level 1", inline = "1A")
time1 = input.time(defaultStartDate, "Start date/time", group = "Level 1", inline = "1B")
// Manual level 2
color2 = input.color(color.orange, "", group = "Level 2", inline = "2A")
show2 = input.bool(false, "Show", group = "Level 2", inline = "2A")
text2 = input.string("Swing Low", "Label", group = "Level 2", inline = "2A")
price2 = input.float(0.00, "Price", step = 0.25, group = "Level 2", inline = "2A")
time2 = input.time(defaultStartDate, "Start date/time", group = "Level 2", inline = "2B")
// Manual level 3
color3 = input.color(color.lime, "", group = "Level 3", inline = "3A")
show3 = input.bool(false, "Show", group = "Level 3", inline = "3A")
text3 = input.string("Swing High", "Label", group = "Level 3", inline = "3A")
price3 = input.float(0.00, "Price", step = 0.25, group = "Level 3", inline = "3A")
time3 = input.time(defaultStartDate, "Start date/time", group = "Level 3", inline = "3B")
// Manual level 4
color4 = input.color(color.red, "", group = "Level 4", inline = "4A")
show4 = input.bool(false, "Show", group = "Level 4", inline = "4A")
text4 = input.string("Swing Low", "Label", group = "Level 4", inline = "4A")
price4 = input.float(0.00, "Price", step = 0.25, group = "Level 4", inline = "4A")
time4 = input.time(defaultStartDate, "Start date/time", group = "Level 4", inline = "4B")
// Manual level 5
color5 = input.color(color.fuchsia, "", group = "Level 5", inline = "5A")
show5 = input.bool(false, "Show", group = "Level 5", inline = "5A")
text5 = input.string("Swing High", "Label", group = "Level 5", inline = "5A")
price5 = input.float(0.00, "Price", step = 0.25, group = "Level 5", inline = "5A")
time5 = input.time(defaultStartDate, "Start date/time", group = "Level 5", inline = "5B")
// Manual level 6
color6 = input.color(color.yellow, "", group = "Level 6", inline = "6A")
show6 = input.bool(false, "Show", group = "Level 6", inline = "6A")
text6 = input.string("Swing Low", "Label", group = "Level 6", inline = "6A")
price6 = input.float(0.00, "Price", step = 0.25, group = "Level 6", inline = "6A")
time6 = input.time(defaultStartDate, "Start date/time", group = "Level 6", inline = "6B")
// The daily candle follows the symbol's TradingView session settings.
colorPDH = input.color(color.green, "", group = "Previous Day", inline = "PH")
showPDH = input.bool(true, "High", group = "Previous Day", inline = "PH")
colorPDL = input.color(color.red, "", group = "Previous Day", inline = "PL")
showPDL = input.bool(true, "Low", group = "Previous Day", inline = "PL")
colorPDC = input.color(color.orange, "", group = "Previous Day", inline = "PC")
showPDC = input.bool(true, "Close", group = "Previous Day", inline = "PC")
[prevDayHigh, prevDayLow, prevDayClose] = request.security(syminfo.tickerid, "D", [high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
// EMA reference levels follow the latest completed source-timeframe bar.
// Values remain fixed while the next source-timeframe candle develops.
emaStartBars = input.int(20, "Start lines this many chart bars back", minval = 0, maxval = 4999, group = "Current EMA Levels", tooltip = "Controls line length only. EMA prices update automatically after each source-timeframe bar closes.")
min5group = "5 min EMA levels"
colorEMA5_20 = input.color(color.teal, "", group = min5group, inline = "EMA5_20")
showEMA5_20 = input.bool(false, "EMA 20", group = min5group, inline = "EMA5_20")
colorEMA5_100 = input.color(color.lime, "", group = min5group, inline = "EMA5_100")
showEMA5_100 = input.bool(false, "EMA 100", group = min5group, inline = "EMA5_100")
colorEMA5_200 = input.color(color.green, "", group = min5group, inline = "EMA5_200")
showEMA5_200 = input.bool(false, "EMA 200", group = min5group, inline = "EMA5_200")
min15group = "15 min EMA levels"
colorEMA15_20 = input.color(color.navy, "", group = min15group, inline = "EMA5_20")
showEMA15_20 = input.bool(false, "EMA 20", group = min15group, inline = "EMA5_20")
colorEMA15_100 = input.color(color.aqua, "", group = min15group, inline = "EMA15_100")
showEMA15_100 = input.bool(false, "EMA 100", group = min15group, inline = "EMA15_100")
colorEMA15_200 = input.color(color.blue, "", group = min15group, inline = "EMA15_200")
showEMA15_200 = input.bool(false, "EMA 200", group = min15group, inline = "EMA15_200")
min60group = "60 min EMA levels"
colorEMA60_20 = input.color(color.orange, "", group = min60group, inline = "EMA20_100")
showEMA60_20 = input.bool(false, "60m EMA 20", group = min60group, inline = "EMA20_100")
colorEMA60_100 = input.color(color.orange, "", group = min60group, inline = "EMA60_100")
showEMA60_100 = input.bool(false, "60m EMA 100", group = min60group, inline = "EMA60_100")
colorEMA60_200 = input.color(color.fuchsia, "", group = min60group, inline = "EMA60_200")
showEMA60_200 = input.bool(false, "60m EMA 200", group = min60group, inline = "EMA60_200")
anyEMAEnabled = showEMA5_100 or showEMA5_200 or showEMA15_100 or showEMA15_200 or showEMA60_100 or showEMA60_200 or showEMA5_20 or showEMA15_20 or showEMA60_20
if anyEMAEnabled and timeframe.in_seconds() > timeframe.in_seconds("5")
runtime.error("Current EMA levels require a time-based chart of 5 minutes or lower. Use your 1-minute chart.")
[ema5_20, ema5_100, ema5_200, ema5CloseTime] = request.security(syminfo.tickerid, "5", [ta.ema(close, 20)[1], ta.ema(close, 100)[1], ta.ema(close, 200)[1], time_close[1]], lookahead = barmerge.lookahead_on)
[ema15_20, ema15_100, ema15_200, ema15CloseTime] = request.security(syminfo.tickerid, "15", [ta.ema(close, 20)[1], ta.ema(close, 100)[1], ta.ema(close, 200)[1], time_close[1]], lookahead = barmerge.lookahead_on)
[ema60_20, ema60_100, ema60_200, ema60CloseTime] = request.security(syminfo.tickerid, "60", [ta.ema(close, 20)[1], ta.ema(close, 100)[1], ta.ema(close, 200)[1], time_close[1]], lookahead = barmerge.lookahead_on)
var line[] levelLines = array.new_line(18, na)
var label[] levelLabels = array.new_label(18, na)
getLabelText(string levelText, int startTime) =>
appendDateTime ? levelText + " - " + str.format_time(startTime, "MM/dd HH:mm", syminfo.timezone) : levelText
updateLevel(int index, bool show, int startTime, float levelPrice, string levelText, color levelColor, int endTime, int labelBar, line_style) =>
line levelLine = array.get(levelLines, index)
label levelLabel = array.get(levelLabels, index)
if show and not na(levelPrice)
if na(levelLine)
levelLine := line.new(startTime, levelPrice, endTime, levelPrice, xloc = xloc.bar_time, color = levelColor, style = line_style, width = lineWidth)
array.set(levelLines, index, levelLine)
else
line.set_xy1(levelLine, startTime, levelPrice)
line.set_xy2(levelLine, endTime, levelPrice)
line.set_color(levelLine, levelColor)
line.set_style(levelLine, line_style)
line.set_width(levelLine, lineWidth)
if na(levelLabel)
levelLabel := label.new(labelBar, levelPrice, levelText, xloc = xloc.bar_index, style = label.style_label_left, color = color.new(levelColor, 100), textcolor = levelColor, textalign = text.align_left, size = size.small)
array.set(levelLabels, index, levelLabel)
else
label.set_xy(levelLabel, labelBar, levelPrice)
label.set_text(levelLabel, levelText)
label.set_textcolor(levelLabel, levelColor)
else
if not na(levelLine)
line.delete(levelLine)
array.set(levelLines, index, na)
if not na(levelLabel)
label.delete(levelLabel)
array.set(levelLabels, index, na)
if barstate.islast
barMilliseconds = int(timeframe.in_seconds() * 1000)
rightEdgeTime = time_close + extendBars * barMilliseconds
labelTime = bar_index + 1 + extendBars + labelGapBars
updateLevel(0, show1, time1, price1, getLabelText(text1, time1), color1, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(1, show2, time2, price2, getLabelText(text2, time2), color2, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(2, show3, time3, price3, getLabelText(text3, time3), color3, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(3, show4, time4, price4, getLabelText(text4, time4), color4, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(4, show5, time5, price5, getLabelText(text5, time5), color5, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(5, show6, time6, price6, getLabelText(text6, time6), color6, rightEdgeTime, labelTime, selectedLineStyle)
// Previous-day lines begin at the last bar, matching the original indicator.
updateLevel(6, showPDH, time, prevDayHigh, "Prev Day High", colorPDH, rightEdgeTime, labelTime, line.style_dashed)
updateLevel(7, showPDL, time, prevDayLow, "Prev Day Low", colorPDL, rightEdgeTime, labelTime, line.style_dashed)
updateLevel(8, showPDC, time, prevDayClose, "Prev Day Close", colorPDC, rightEdgeTime, labelTime, line.style_dashed)
// Line starts follow the chart; prices follow completed source-timeframe bars.
emaLineStart = time[math.min(emaStartBars, bar_index)]
updateLevel(9, showEMA5_100, emaLineStart, ema5_100, getLabelText("5m EMA 100", ema5CloseTime), colorEMA5_100, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(10, showEMA5_200, emaLineStart, ema5_200, getLabelText("5m EMA 200", ema5CloseTime), colorEMA5_200, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(11, showEMA15_100, emaLineStart, ema15_100, getLabelText("15m EMA 100", ema15CloseTime), colorEMA15_100, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(12, showEMA15_200, emaLineStart, ema15_200, getLabelText("15m EMA 200", ema15CloseTime), colorEMA15_200, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(13, showEMA60_100, emaLineStart, ema60_100, getLabelText("60m EMA 100", ema60CloseTime), colorEMA60_100, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(14, showEMA60_200, emaLineStart, ema60_200, getLabelText("60m EMA 200", ema60CloseTime), colorEMA60_200, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(15, showEMA5_20, emaLineStart, ema5_20, getLabelText("5m EMA 20", ema5CloseTime), colorEMA5_20, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(16, showEMA15_20, emaLineStart, ema15_20, getLabelText("15m EMA 20", ema15CloseTime), colorEMA15_20, rightEdgeTime, labelTime, selectedLineStyle)
updateLevel(17, showEMA60_20, emaLineStart, ema60_20, getLabelText("60m EMA 20", ema60CloseTime), colorEMA60_20, rightEdgeTime, labelTime, selectedLineStyle)
// Report unavailable enabled EMA values while source history loads.
var table emaStatus = table.new(position.bottom_right, 1, 1)
if barstate.islast
missingEMA = (showEMA5_20 and na(ema5_20)) or (showEMA5_100 and na(ema5_100)) or (showEMA5_200 and na(ema5_200)) or (showEMA15_20 and na(ema15_20)) or (showEMA15_100 and na(ema15_100)) or (showEMA15_200 and na(ema15_200)) or (showEMA60_20 and na(ema60_20)) or (showEMA60_100 and na(ema60_100)) or (showEMA60_200 and na(ema60_200))
statusText = missingEMA ? "EMA values unavailable: source history is insufficient." : ""
table.cell(emaStatus, 0, 0, statusText, text_color = color.orange, bgcolor = missingEMA ? color.new(color.black, 15) : na, text_size = size.small)