Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

pine script - How to get the highest value between 2 points?

I want to get the highest point between 2 entries (ex. between 2 ema crosses, like the image example). It's over my head, I tried with barssince() without luck. Any help would be welcomed!

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

ema1 = ema(close, 20)
ema2 = ema(close, 50)
plot(ema1)
plot(ema2, color = color.yellow)

buy = crossover(ema1, ema2)
sell = crossunder(ema1, ema2)

highest_buy = highestbars(high, barssince(buy))

plot(highest_buy)

enter image description here

question from:https://stackoverflow.com/questions/65939868/how-to-get-the-highest-value-between-2-points

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When sell signal appears highest_buy is equal na and the label is not displayed. It is necessary to take the values on the previous bar.

//@version=4

study("Help prof (My Script)", overlay = true)

var bool    track       = false
var float   highest_buy = na

ema1 = ema(close, 20)
ema2 = ema(close, 50)

buy  = crossover(ema1, ema2)
sell = crossunder(ema1, ema2)

if buy
    track := true
else if sell
    track := false

highest_buy := track ? max(nz(highest_buy), high) : na

plot(ema1)
plot(ema2,        color=color.yellow)
plot(highest_buy, color=color.purple, style=plot.style_linebr)

buy_entry = valuewhen(buy, close, 0)
plot(buy_entry, color = buy_entry == buy_entry[1] and ema1 > ema2? color.lime :na)

if sell 
    label1 = label.new(bar_index, highest_buy[1], text=tostring(highest_buy[1] - buy_entry[1]) , style=label.style_label_down, color=color.black, textcolor=color.white)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...