Problem with Pine Scripts plotshapes offset

Viewed 3362

This script will denote highs, with the left bar being lower and the right bar being lower. I also want this script to give me the HighofHighs, with the left high and right high being lower. I have it working but I can't get the label to display on the correct bar.

If I use offset=-1, it will put it over the most recent high, if I use offset=-high_bars_back it doesn't offset it at all.

(the default of the code is showing "HighofHighs" on the most current high (using offset=-1), but I need it to display on the second to most current high)

enter image description here

//@version=3

strategy(title = "Trend_v1", shorttitle = "Thrend_v1", overlay = true, 
  pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, 
  calc_on_every_tick=true, initial_capital=100000)//, calc_on_order_fills=true)


//Window of time
start     = timestamp(1000, 01, 01, 00, 00)  // backtest start window
finish    = timestamp(3000, 10, 01, 00, 00)        // backtest finish window
window()  => time >= start and time <= finish ? true : false // create function "within window of time"    

//Input
showLocalTrendHighLables = input(title="Show Local Trend High Labels", type=bool, defval=true)
showRealTrendHighLables = input(title="Show Real Trend High Labels", type=bool, defval=true)

//Initialize Variables
first_high = na
second_high = na
third_high = na
local_trend_highs = na
real_trend_highs = na

//Local Trend Highs
if(nz(high[2]) < nz(high[1]) and nz(high[1]) > high)
    local_trend_highs := nz(high[1])
    third_high := second_high[1]
    second_high := first_high[1]
    first_high := nz(high[1])
else
    local_trend_highs := nz(local_trend_highs[1])
    third_high := third_high[1]
    second_high := second_high[1]
    first_high := first_high[1]

//Real Trend Highs
if (third_high < second_high and second_high > first_high)
    real_trend_highs := nz(local_trend_highs)
else
    real_trend_highs := nz(real_trend_highs[1])


//Calculate how many high bars back to display HighofHighs
high_bars_back = 0
for i = 0 to 999
    high_bars_back = i
    if(high[i] == second_high)
        break
    else
        continue


//Plots
plotshape((not (local_trend_highs == local_trend_highs[1])) and showLocalTrendHighLables, style=shape.arrowdown, location=location.abovebar, color=green, text='high', offset=-1)

//For some reason, offset=-high_bars_back doesn't shift at all
plotshape((not (real_trend_highs == real_trend_highs[1])) and showRealTrendHighLables, style=shape.arrowdown, location=location.top, color=green, text='HIGHofHIGHs', offset=-1)//offset=-high_bars_back)

plot(high_bars_back, color=blue, style=columns)
2 Answers

Unfortunately this cannot be achieved with 'plotshape'+'offset'. The reason for this is that offset would shift the whole series of shapes at the given number of bars. But in your task each of HIGHofHIGHs requires a different offset value.

The good news is that this functionality will be available more or less soon. It's included in Pine Script version 4 public draft. The feature is called "labels". Read more https://docs.google.com/document/d/12ogvjzasBJSXerSOql4b9KwkE3wUlsc5HgA5qGhoYXk/edit#heading=h.uz6ftgjlvspe

I have the same question about pivot of pivots.And I tested over and over a day.After I saw vitvlkv's point that use label to replace plotshape then I solved it.share my partial code..

if h2>h1 and h2>h3 and h2>h4 and h2>h5
    h1_1:=h2
    ph2_1:=ph1_1
    ph1_1:=ph2
else
    h1_1:=h1_1[1]
    ph1_1:=ph1_1[1]
    ph2_1:=ph2_1[1]
var label pop=na
if h1_1!=h1_1[1]
    pop:=label.new(ph1_1,h1_1,yloc=yloc.belowbar,style=label.style_diamond,color=color.lime,size=size.tiny)
Related