简体   繁体   中英

Pine Script Pivot just the newest Pivot lines

study("Pivot Points Standard", shorttitle="Pivots Std", overlay=true) 

//var higherTF = input("D", type=input.resolution)

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)

pLevel = (prevHighHTF + prevLowHTF + prevCloseHTF) / 3
r1Level = pLevel * 2 - prevLowHTF
s1Level = pLevel * 2 - prevHighHTF

var line r1Line = na
var line pLine = na
var line s1Line = na


if not na(pLine) and line.get_x2(pLine) != bar_index
    line.set_color(pLine,color.green)
    line.set_x2(r1Line, bar_index)
    line.set_x2(pLine, bar_index)
    line.set_x2(s1Line, bar_index)


if pLevel[1] != pLevel
    line.set_color(pLine,color.blue)
    line.set_x2(r1Line, bar_index)
    line.set_x2(pLine, bar_index)
    line.set_x2(s1Line, bar_index)
    r1Line := line.new(bar_index, r1Level, bar_index, r1Level, extend=extend.none)
    pLine := line.new(bar_index, pLevel, bar_index, pLevel, width=3, extend=extend.none)
    s1Line := line.new(bar_index, s1Level, bar_index, s1Level, extend=extend.none)
    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)

My Code is the Blue one. But i want only the latest 2 lines like the brown one any Ideas how to do that?

If it is possible with a counter to decide one two lines back

enter image description here

This code uses the technique explained here: http://www.pinecoders.com/faq_and_code/#how-can-i-keep-only-the-last-x-labels-or-lines

You can change the number of last lines you want to keep:

//@version=4
maxBarsBack = 2000
study("Pivot Points Standard", "Pivots Std", true, max_bars_back = maxBarsBack)

//var higherTF = input("D", type=input.resolution)
keepLastLines = input(2)

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)

pLevel = (prevHighHTF + prevLowHTF + prevCloseHTF) / 3
r1Level = pLevel * 2 - prevLowHTF
s1Level = pLevel * 2 - prevHighHTF

var line r1Line = na
var line pLine = na
var line s1Line = na
// Create series which will hold line and label ids as they are created.
line r1Line2 = na
line pLine2 = na
line s1Line2 = na
label r1Lbl2 = na
label pLbl2 = na
label s1Lbl2 = na


if not na(pLine) //and line.get_x2(pLine) != bar_index
    line.set_color(pLine,color.green)
    line.set_x2(r1Line, bar_index)
    line.set_x2(pLine, bar_index)
    line.set_x2(s1Line, bar_index)


if pLevel[1] != pLevel
    line.set_color(pLine,color.blue)
    line.set_x2(r1Line, bar_index)
    line.set_x2(pLine, bar_index)
    line.set_x2(s1Line, bar_index)
    r1Line := line.new(bar_index, r1Level, bar_index, r1Level, extend=extend.none)
    pLine := line.new(bar_index, pLevel, bar_index, pLevel, width=3, extend=extend.none)
    s1Line := line.new(bar_index, s1Level, bar_index, s1Level, extend=extend.none)
    // Save line and label ids as they are created.
    r1Line2 := r1Line
    pLine2 := pLine
    s1Line2 := s1Line
    r1Lbl2 := label.new(bar_index, r1Level, "R1", style=label.style_none)
    pLbl2 := label.new(bar_index, pLevel, "P", style=label.style_none)
    s1Lbl2 := label.new(bar_index, s1Level, "S1", style=label.style_none)

// ————— Delete all required lines.
// Loop from previous bar into the past, looking for bars where a line was created.
// Delete all lines found in last "maxBarsBack" bars after the required count has been left intact.
lineCount = 0
for i = 1 to maxBarsBack
    if not na(pLine2[i])
        lineCount := lineCount + 1
        if lineCount > keepLastLines
            line.delete(r1Line2[i])
            line.delete(pLine2[i])
            line.delete(s1Line2[i])
            label.delete(r1Lbl2[i])
            label.delete(pLbl2[i])
            label.delete(s1Lbl2[i])

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM