pine-script - Pine Scripts plotshapes偏移问题
问题描述
该脚本将表示高点,左侧栏较低,右侧栏较低。我还希望这个脚本给我 HighofHighs,左高和右高较低。我让它工作,但我无法让标签显示在正确的栏上。
如果我使用 offset=-1,它将把它放在最近的高点上,如果我使用 offset=-high_bars_back 它根本不会抵消它。
(代码的默认值在最新高点上显示“HighofHighs”(使用偏移量=-1),但我需要它显示在第二个到最新高点上)
//@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)
解决方案
不幸的是,这不能通过'plotshape'+'offset'来实现。这样做的原因是偏移会在给定的条数处移动整个形状系列。但是在您的任务中,每个 HIGHofHIGHs 都需要不同的偏移值。
好消息是这个功能或多或少很快就会推出。它包含在 Pine Script 第 4 版公共草案中。该功能称为“标签”。阅读更多https://docs.google.com/document/d/12ogvjzasBJSXerSOql4b9KwkE3wUlsc5HgA5qGhoYXk/edit#heading=h.uz6ftgjlvspe