简体   繁体   中英

how can i put the highest value since a condition in a label at the bar it occurs (pinescript)

I'm using this code to get the "peak" value, but I don't know how to put a label in the the highest bar with it.

like this: image

I don´t know how to set the correct "x, y" parameters.

GetHighestSince(condition, series) =>
    var float highestValueSince = na
    if condition or series > highestValueSince
        highestValueSince := series
        highestValueSince
    highestValueSince

GetLowestSince(condition, series) =>
    var float lowestValueSincemc = na
    if condition or series < lowestValueSincemc
        lowestValueSincemc := series
        lowestValueSincemc
    lowestValueSincemc

mhh = GetHighestSince(enterLong, high)
mll = GetLowestSince(enterShort, low)

mpeakg = 0.00
if bullishRule
//    mpeakg := round(mhh/pricelong, 2)
    mpeakg := math.round((mhh - pricelong) * 100 / pricelong, 2)
    mpeakg
else
    mpeakg := math.round((priceshort - mll) * 100 / priceshort, 2)
    mpeakg

barsLabel = label.new(x=na, y=na, style=label.style_label_center, color=color.teal, textcolor=color.white, size=size.large)


//if barstate.islast
//label.delete (barsLabel)

labelText = 'Peak Profit\n' + str.tostring(mpeakg)
label.set_text(id=barsLabel, text=labelText)
//label.set_xy(id=barsLabel, x=bar_index +1 , y=close)
//label.set_xy(id=barsLabel, x=barssince(bullishRule ? enterLong : enterShort), y=close)


//***end peak profit

I'm providing a general solution to the question. If you want something more specific please share your entire code next time.

//@version=5
indicator("Test offset", overlay=true)

var int latest = 0 // helper variable to not print so much
highestOffset = math.abs(ta.highestbars(high, 30)) // get the offset to the bar with the highest high in the last 30 days
offsetTime = time[highestOffset]
condition = dayofmonth %10 == 0 // your whatever condition you didn't share
conditionOffset = math.abs(ta.barssince(condition)) // offset to the bar the condition happened on

if condition or offsetTime != latest
    latest := offsetTime
    currentOffset = condition ? conditionOffset : highestOffset
    label.new(bar_index[currentOffset], high[currentOffset], 'H30')```

Thanks for your answer!

My condition is

bullishRule = close >= upValue1

I changed your code to this:

var int latest = 0 // helper variable to not print so much
highestOffset = math.abs(ta.highestbars(high, 30)) // get the offset to the bar with the highest high in the last 30 days
offsetTime = time[highestOffset]
// condition = dayofmonth %10 == 0 // your whatever condition you didn't share
conditionOffset = math.abs(ta.barssince(bullishRule)) // offset to the bar the condition happened on

if bullishRule or offsetTime != latest
    latest := offsetTime
    currentOffset = bullishRule ? conditionOffset : highestOffset
    label.new(bar_index[currentOffset], high[currentOffset], 'H30')

And the result was this:

screen

I want to keep the label on the highest bar only after the condition.

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