Ilja
Ilja

Reputation: 63

Project latest price back and check where previous price intersects it

Working with Trading View's Pine Script - How can I take the latest high price (from the latest bar) and plot a constant horizontal line back to check when historically the highs were above this latest high?

The following code projects the latest high, but I am struggling to find a way to test where previous highs were above this line. Is there maybe a way to make this last value a proper constant and to project it back or are there other alternatives?

//@version=4
study("My Script", overlay=true)

var line _lpLine = line.new(0, 0, 0, 0, extend=extend.left, style=line.style_dashed, color=color.yellow)

_lastTradedPrice = high
line.set_xy1(_lpLine, bar_index-1, _lastTradedPrice)
line.set_xy2(_lpLine, bar_index, _lastTradedPrice)

test = high > _lpLine ? 1 : 0

plot(test, color=color.green)

Upvotes: 1

Views: 365

Answers (2)

AnyDozer
AnyDozer

Reputation: 2568

It is not good to use the operator for, but in this case it is necessary.

//@version=4
study("Help (My Script)", overlay=true)

lb = input(100, 'look back', minval=1)

var line  _lpLine = line.new(0, 0, 0, 0, extend=extend.left, style=line.style_dashed, color=color.yellow)
var label  lbl    = label.new(na, na, style=label.style_xcross, color=color.red, size=size.tiny)
var        index  = lb
var        flag   = false

_lastTradedPrice = high
line.set_xy1(_lpLine, bar_index-1, _lastTradedPrice)
line.set_xy2(_lpLine, bar_index, _lastTradedPrice)

// test = high > _lpLine ? 1 : 0

// plot(test, color=color.green)

for i=1 to lb
    if high[0] < high[i]
        index := i
        flag  := true
        break

if flag
    label.set_x(lbl, bar_index - index)
    label.set_y(lbl, high)
    flag := false
    index := lb
else
    label.set_x(lbl, na)
    label.set_y(lbl, na)
    index := lb

enter image description here

ADDED a second version of the program that marks all values above the last closing line and finds the highest value among them.

//@version=4
study("Help (My Script) v2", overlay=true, max_labels_count=100)

lb = input(100, 'look back', minval=1)

var line  _lpLine = line.new(0, 0, 0, 0, extend=extend.left, style=line.style_dashed, color=color.yellow, width=2)
label  lbl    = label.new(na, na, style=label.style_xcross, color=color.red, size=size.tiny)
var        capture = float(na)

_lastTradedPrice = high
line.set_xy1(_lpLine, bar_index-1, _lastTradedPrice)
line.set_xy2(_lpLine, bar_index, _lastTradedPrice)

for i = 1 to lb
    if not na(lbl[i])
        label.delete(lbl[i])

capture := 0
for i=1 to lb
    if high[0] < high[i]
        lbl := label.new(bar_index - i, high[i], style=label.style_xcross, color=color.red, size=size.auto)
    if capture < high[i]
        capture := high[i]

plot(capture, color=color.blue, style=plot.style_cross, linewidth=3, show_last=1)

enter image description here

Upvotes: 1

Ilja
Ilja

Reputation: 63

Tried the following based on AnyDozer's approach to mark ALL values with high > last high with a cross, but this only marks the first 3 or so and not all. What would be wrong here? Additionally, I am trying to capture all these high values in the variable "capture" and to plot just them. All other values of "capture" should be just na.

//@version=4
study("Help (My Script)", overlay=true, max_labels_count=500)

lb = input(10)

var line  _lpLine = line.new(0, 0, 0, 0, extend=extend.left, style=line.style_dashed, color=color.yellow)
lbl    = label.new(na, na, style=label.style_xcross, color=color.red, size=size.tiny)
var        index  = lb
var        flag   = false
capture = float(na)

_lastTradedPrice = high
line.set_xy1(_lpLine, bar_index-1, _lastTradedPrice)
line.set_xy2(_lpLine, bar_index, _lastTradedPrice)

for k=0 to lb
    for i=0 to lb
        if high[0] < high[i]
            index := i
            flag  := true
            label.set_x(lbl, bar_index - index)
            label.set_y(lbl, high[i])
            capture := high[i]
            break
            
plot(capture)

enter image description here

Upvotes: 0

Related Questions