Problem with Pine Scripts plotshapes offset

前提是你 提交于 2019-12-11 07:51:25

问题


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)

//@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)

回答1:


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



来源:https://stackoverflow.com/questions/54190749/problem-with-pine-scripts-plotshapes-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!