本页源码来自 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/
// © pine_astro
//@version=6
// ═══════════════════════════════════════════════════════════════════════
// DRAWING MEMORY TEST • DATE FILTER (fully commented edition)
// ═══════════════════════════════════════════════════════════════════════
//
// WHAT THIS SCRIPT DOES
// ---------------------
// It detects pivot highs and pivot lows and draws a box, a horizontal
// line and a label for each one. The special part is the DATE GATE:
// every pivot must pass a date check BEFORE anything is drawn.
//
// Show Previous = OFF -> draw only pivots born ON/AFTER the start date
// Show Previous = ON -> draw only pivots born BEFORE the start date
//
// WHY IT MATTERS
// --------------
// TradingView keeps at most 500 boxes, 500 lines and 500 labels per
// script. When the limit is reached, the OLDEST object is deleted.
// The date gate stops the script from wasting those 500 slots on old
// history you don't care about, so they are spent on your chosen period.
//
// IMPORTANT HONESTY NOTE
// ----------------------
// The gate controls DRAWING only. It does not raise the 500 limit and
// it does not reduce calculations (the script still runs on every bar).
//
// HOW TO READ THE COMMENTS
// ------------------------
// "//" starts a comment. Comments are ignored by TradingView, so you can
// delete any of them without changing how the script behaves.
// ═══════════════════════════════════════════════════════════════════════
// ───────────────────────────────────────────────────────────────────────
// indicator() declares this script as an indicator (not a strategy).
//
// "Drawing Memory Test • Date Filter" : the name shown on the chart.
// overlay=true : draw ON TOP of the price chart
// (false = separate pane below).
// max_boxes_count=500 : keep up to 500 boxes (default 50,
// 500 is the maximum allowed).
// max_lines_count=500 : same idea for lines.
// max_labels_count=500 : same idea for labels.
//
// Each object type has its OWN limit, so the total possible is 1500.
// If you forget these three parameters the limit stays at 50 and older
// drawings disappear very quickly.
// ───────────────────────────────────────────────────────────────────────
indicator("Drawing Memory Test Date Filter", overlay=true,
max_boxes_count=500,
max_lines_count=500,
max_labels_count=500)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DRAWING CONTROL
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// A string used as a "group" name. Every input that uses this variable
// appears under the same heading in the settings window.
groupDrawing = "Drawing Control"
// ── Input 1: the start date ────────────────────────────────────────────
// input.time() creates a date/time picker in the settings window.
// The value is stored as a number: milliseconds since 1 Jan 1970 (UTC),
// the same unit as the built-in `time` variable, so the two can be
// compared directly with <, >=, etc.
//
// timestamp("01 Sep 2026 00:00 +0000") builds the default value:
// 1 September 2026, 00:00, UTC offset +0000.
//
// This date is the foundation of the whole gate. Set it to the beginning
// of the period you want to study.
startDate = input.time(
timestamp("01 Sep 2026 00:00 +0000"),
"Drawing Start Date",
group=groupDrawing)
// ── Input 2: the direction switch ──────────────────────────────────────
// input.bool() creates a checkbox.
// false (OFF, default) -> draw from startDate FORWARD
// true (ON) -> draw BEFORE startDate only
// Useful for reviewing the past or comparing "before" vs "after".
showPrevious = input.bool(
false,
"Show Previous Drawings",
group=groupDrawing)
// ── Inputs 3-5: per-type visibility switches ───────────────────────────
// Each switch turns one drawing type on or off. When a type is OFF, no
// objects of that type are created at all, so none of its 500-slot
// limit is consumed.
showBoxes = input.bool(
true,
"Show Boxes",
group=groupDrawing)
showLines = input.bool(
true,
"Show Lines",
group=groupDrawing)
showLabels = input.bool(
true,
"Show Labels",
group=groupDrawing)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PIVOT SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
groupPivot = "Pivot Detection"
// ── Input 6: Left Bars ─────────────────────────────────────────────────
// input.int() creates a whole-number field. minval=1 prevents values < 1.
// A pivot HIGH must be higher than this many bars on its LEFT side
// (a pivot LOW must be lower than this many bars on its left).
leftBars = input.int(
5,
"Pivot Left Bars",
minval=1,
group=groupPivot)
// ── Input 7: Right Bars ────────────────────────────────────────────────
// Same idea on the RIGHT side, and this is also the CONFIRMATION delay:
// a pivot is only confirmed after this many bars have closed after it.
// So the drawing appears `rightBars` bars late. That is normal.
//
// Tuning guide (for both Left and Right):
// 2-3 -> many sensitive points, lots of noise, eats the limit fast
// 5-8 -> balanced for most timeframes
// 10-15 -> fewer but stronger points, slower confirmation
rightBars = input.int(
5,
"Pivot Right Bars",
minval=1,
group=groupPivot)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DRAWING FILTER
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// OFF = draw from selected date forward
// ON = draw before selected date
// NOTE: `canDraw` is declared but NEVER USED anywhere below. The real
// filtering happens through the `allowed` variable inside each pivot
// block (see further down). You can safely delete this variable.
//
// How the ternary operator works here: condition ? value_if_true : value_if_false
// showPrevious ON -> canDraw = (current bar time < startDate)
// showPrevious OFF -> canDraw = (current bar time >= startDate)
// It uses the CURRENT bar's time, which is exactly why it is not good
// enough for pivots (see the explanation in the PIVOT HIGH section).
bool canDraw = showPrevious
? time < startDate
: time >= startDate
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PIVOT HIGH
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ta.pivothigh(source, leftBars, rightBars)
//
// A bar is a pivot high when its `high` is higher than the highs of
// `leftBars` bars before it AND `rightBars` bars after it.
//
// The function runs on EVERY bar but returns:
// - the pivot's PRICE on the bar where a pivot gets confirmed
// - na ("no value") on all other bars
//
// KEY POINT: the confirmation bar is `rightBars` bars AFTER the real
// pivot bar. So when a value is returned, the real pivot candle is
// located at bar_index[rightBars], not at the current bar.
float pivotHigh = ta.pivothigh(high, leftBars, rightBars)
// na() checks whether a value is "no value". `not na(...)` therefore
// means "a pivot high was just confirmed on this bar".
if not na(pivotHigh)
// ── THE TIME GATE ──────────────────────────────────────────────────
// time[rightBars] is the time of the REAL pivot candle (its "birth
// time"), not the time of the confirmation candle.
//
// Why this matters (example): start date = 1 Sep.
// The pivot formed on 31 Aug and was confirmed on 1 Sep.
// Filtering with `time` -> accepted (WRONG, it is August)
// Filtering with `time[rightBars]` -> rejected (CORRECT)
//
// Golden rule: always filter by when the object was BORN.
int pivotTime = time[rightBars]
// The gate itself:
// Show Previous OFF -> allowed only if the pivot was born ON/AFTER startDate
// Show Previous ON -> allowed only if the pivot was born BEFORE startDate
bool allowed = showPrevious
? pivotTime < startDate
: pivotTime >= startDate
// Nothing below is executed unless the pivot passed the gate.
// Objects that fail the gate are never created, so they use no slots.
if allowed
// ── BOX ────────────────────────────────────────────────────────
// Parameters:
// left : left edge = the real pivot candle
// top : upper edge = the pivot's price
// right : right edge = current bar (the confirmation bar),
// so the box is `rightBars` bars wide and does
// NOT extend into the future
// bottom : lower edge = pivot price minus 20 ticks.
// syminfo.mintick is the smallest price step of
// the symbol, so the height scales with the symbol
// border_color : outline color (red = resistance side)
// bgcolor : fill color. color.new(color, 85) sets the
// TRANSPARENCY: 0 = solid, 100 = invisible,
// 85 = very light so candles stay visible
if showBoxes
box.new(
left=bar_index[rightBars],
top=pivotHigh,
right=bar_index,
bottom=pivotHigh - syminfo.mintick * 20,
border_color=color.red,
bgcolor=color.new(color.red, 85))
// ── LINE ───────────────────────────────────────────────────────
// Parameters:
// x1, y1 : start point (pivot candle, pivot price)
// x2, y2 : end point (current bar, SAME price -> horizontal line)
// extend : extend.right pushes the line to the right forever,
// so it works as a resistance / liquidity level
// color : line color
// width : thickness in pixels
if showLines
line.new(
x1=bar_index[rightBars],
y1=pivotHigh,
x2=bar_index,
y2=pivotHigh,
extend=extend.right,
color=color.red,
width=1)
// ── LABEL ──────────────────────────────────────────────────────
// Parameters:
// x, y : position (pivot candle, pivot price)
// text : the text to show
// style : label.style_label_down = pointer faces DOWN, so
// the label sits ABOVE the high
// textcolor : text color
// color : label background color
if showLabels
label.new(
x=bar_index[rightBars],
y=pivotHigh,
text="HIGH",
style=label.style_label_down,
textcolor=color.white,
color=color.red)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PIVOT LOW
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Exact mirror of the pivot high section, using lows instead of highs.
// A pivot low is a bar whose `low` is lower than `leftBars` bars before
// it and `rightBars` bars after it. Returns the price on the confirmation
// bar and na on every other bar.
float pivotLow = ta.pivotlow(low, leftBars, rightBars)
if not na(pivotLow)
// Birth time of the real pivot candle (same logic as the pivot high).
// `pivotTime` and `allowed` are declared again here without any
// conflict, because each one lives inside its own separate `if` block.
int pivotTime = time[rightBars]
// Same gate as above.
bool allowed = showPrevious
? pivotTime < startDate
: pivotTime >= startDate
if allowed
// ── BOX ────────────────────────────────────────────────────────
// Mirrored: the box sits ABOVE the low.
// top = pivot price + 20 ticks
// bottom = pivot price
// Green = support side.
if showBoxes
box.new(
left=bar_index[rightBars],
top=pivotLow + syminfo.mintick * 20,
right=bar_index,
bottom=pivotLow,
border_color=color.green,
bgcolor=color.new(color.green, 85))
// ── LINE ───────────────────────────────────────────────────────
// Horizontal line at the low's price, extended to the right so
// it works as a support / liquidity level.
if showLines
line.new(
x1=bar_index[rightBars],
y1=pivotLow,
x2=bar_index,
y2=pivotLow,
extend=extend.right,
color=color.green,
width=1)
// ── LABEL ──────────────────────────────────────────────────────
// label.style_label_up = pointer faces UP, so the label sits
// BELOW the low.
if showLabels
label.new(
x=bar_index[rightBars],
y=pivotLow,
text="LOW",
style=label.style_label_up,
textcolor=color.white,
color=color.green)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// START DATE MARKER
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Becomes TRUE only on the FIRST bar that crosses the start date:
// time >= startDate -> this bar is on/after the start date
// time[1] < startDate -> the previous bar was still before it
// Both must be true, so it fires exactly once.
//
// Purpose: a visual check that the filter is anchored where you expect.
// If you can't see the diamond, your date is outside the loaded history
// (too old for the timeframe, or in the future).
bool isStartDate = time >= startDate and time[1] < startDate
// plotshape() draws a symbol on the bars where the condition is true.
// isStartDate : the condition (true on exactly one bar)
// title : name shown in the settings / data window
// style=shape.diamond: diamond-shaped symbol
// location : location.abovebar places it above the candle
// color : yellow
// size : small
// text : the word printed next to the symbol
plotshape(
isStartDate,
title="Start Date",
style=shape.diamond,
location=location.abovebar,
color=color.yellow,
size=size.small,
text="START")