Pine Script技术问询:仅显示最新2条枢轴线(计数器实现方案)
How to Keep Only the Latest 1-2 Pivot Lines with a Counter
Yep, using a counter (plus array tracking for line objects) is a perfect solution to avoid cluttering your chart with old pivot lines. Below’s a modified version of your Pine Script code that lets you specify how many recent pivot line sets to keep (1 or 2, as you requested):
Modified Full Code
study("Pivot Points Standard", shorttitle="Pivots Std", overlay=true) // Let user specify how many latest line sets to keep (1-2, or more if needed) keepLines = input.int(2, minval=1, maxval=5, title="Number of Latest Pivot Line Sets to Keep") // Determine higher timeframe logic (unchanged from original) higherTF = iff(timeframe.isminutes,iff(timeframe.period == "15", "D", "W"), iff(timeframe.isdaily, "M", "12M")) prevCloseHTF = security(syminfo.tickerid, higherTF, close[1], lookahead=true) prevOpenHTF = security(syminfo.tickerid, higherTF, open[1], lookahead=true) prevHighHTF = security(syminfo.tickerid, higherTF, high[1], lookahead=true) prevLowHTF = security(syminfo.tickerid, higherTF, low[1], lookahead=true) // Calculate pivot levels (unchanged from original) pLevel = (prevHighHTF + prevLowHTF + prevCloseHTF) / 3 r1Level = pLevel * 2 - prevLowHTF s1Level = pLevel * 2 - prevHighHTF // Arrays to track all created line objects (one array per line type) var line[] r1Lines = array.new_line() var line[] pLines = array.new_line() var line[] s1Lines = array.new_line() var int lineCount = 0 // Counter for number of line sets created // Only create new lines when pivot levels change (prevents duplicate lines) if pLevel[1] != pLevel // If we've exceeded the number of lines to keep, delete the oldest set if lineCount >= keepLines // Remove and delete the oldest R1, P, S1 lines line.delete(array.shift(r1Lines)) line.delete(array.shift(pLines)) line.delete(array.shift(s1Lines)) lineCount := lineCount - 1 // Create new line set for current pivot levels newR1 = line.new(bar_index, r1Level, bar_index, r1Level, extend=extend.none) newP = line.new(bar_index, pLevel, bar_index, pLevel, width=3, extend=extend.none, color=color.blue) newS1 = line.new(bar_index, s1Level, bar_index, s1Level, extend=extend.none) // Add new lines to our arrays and increment counter array.push(r1Lines, newR1) array.push(pLines, newP) array.push(s1Lines, newS1) lineCount := lineCount + 1 // Add labels for the new pivot levels (optional) label.new(bar_index, r1Level, "R1", style=label.style_none) label.new(bar_index, pLevel, "P", style=label.style_none) label.new(bar_index, s1Level, "S1", style=label.style_none) // Extend all active lines to the current bar (keeps lines updated as new bars form) if barstate.islast for i = 0 to array.size(r1Lines)-1 line.set_x2(array.get(r1Lines, i), bar_index) line.set_x2(array.get(pLines, i), bar_index) line.set_x2(array.get(s1Lines, i), bar_index)
Key Changes Explained
- User-Controlled Line Count: The
keepLinesinput lets you easily switch between keeping 1 or 2 (or more) recent pivot line sets—just adjust the slider in the settings panel. - Array Tracking: We use arrays (
r1Lines,pLines,s1Lines) to store every line object we create. This lets us precisely target and delete the oldest lines when we hit our limit. - Counter Logic: The
lineCountvariable tracks how many line sets we’ve created. When it exceedskeepLines, we usearray.shift()to remove the oldest line from each array andline.delete()to erase it from the chart. - Clean Line Extension: The final loop ensures all active lines extend to the latest bar, so your pivot levels stay visible as new price action occurs.
内容的提问来源于stack exchange,提问作者Erwin Blumenhagen
相关产品推荐
相关产品推荐

