首页 > 解决方案 > 用 Pine Script 画一条一定长度的线

问题描述

我正在尝试在 Trading View v4.0 上开发一个枢轴点指标,我面临的一个问题是枢轴线仅绘制到当前柱。我想要的行为是我希望这条线在这段时间内延长。例如,即使今天是星期一,每周轴心线也应该从星期一延伸到星期日。

以下是我正在使用的代码以及结果:

// Function outputs 1 when it's the first bar of the D/W/M/Y
is_newbar(res) =>
    ch = 0
    if(res == 'Y')
        t  = year(time('D'))
        ch := change(t) != 0 ? 1 : 0
    else
        t = time(res)
        ch := change(t) != 0 ? 1 : 0
    ch

bars_since_week = 0
bars_since_week := is_newbar(pp_res_week) ? 0 : bars_since_week[1] + 1

vpp_p_week = line.new(bar_index[min(bars_since_week, 300)], PP_week, bar_index, PP_week, color=color.black, width = 2, style =  line.style_solid, extend = extend.none)
vs1_p_week = line.new(bar_index[min(bars_since_week, 300)], S1_week, bar_index, S1_week, color=color.black, width = 2, style =  line.style_solid, extend = extend.none)
vs2_p_week = line.new(bar_index[min(bars_since_week, 300)], S2_week, bar_index, S2_week, color=color.black, width = 2, style =  line.style_solid, extend = extend.none)
vs3_p_week = line.new(bar_index[min(bars_since_week, 300)], S3_week, bar_index, S3_week, color=color.black, width = 2, style =  line.style_solid, extend = extend.none)
vr1_p_week = line.new(bar_index[min(bars_since_week, 300)], R1_week, bar_index, R1_week, color=color.black, width = 2, style =  line.style_solid, extend = extend.none)
vr2_p_week = line.new(bar_index[min(bars_since_week, 300)], R2_week, bar_index, R2_week, color=color.black, width = 2, style =  line.style_solid, extend = extend.none)
vr3_p_week = line.new(bar_index[min(bars_since_week, 300)], R3_week, bar_index, R3_week, color=color.black, width = 2, style =  line.style_solid, extend = extend.none)

在此处输入图像描述

此外,以下是我所期望的行为 - 请注意较长的行:

在此处输入图像描述

标签: pivotpine-script

解决方案


这不是一件简单的事情,至少不是这里的方式)也许有更好的方法,但还没有想出:

  1. 您需要合作xloc = xloc.bar_time才能在将来需要时画一条线。
  2. 使用f_avgDilationOf()PineCoders MTF 选择框架中的 来了解较高 TF 膨胀中的图表柱的平均数量(在 24/7 市场上,如果 HTF=1W,则在日线图上这将是 7)。
  3. 确定当前图表柱(当前是脚本正在执行的柱)是否包含在 HTF 的最后一次膨胀中。我们需要这些信息,因为当这是真的时,我们将在未来投影这条线。
//@version=4
study("Periodic lines", "", true)
htf = input("W", type = input.resolution)

// Returns the average number of current chart bars in the given target HTF resolution (this reflects the dataset's history).
f_avgDilationOf(_res) =>
    // _res: resolution of any TF (in "timeframe.period" string format).
    b = barssince(change(time(_res)))
    cumTotal = cum(b == 0 ? b[1] + 1 : 0)
    cumCount = cum(b == 0 ? 1 : 0)
    cumTotal / cumCount

// Period change detection.
pChange(res) =>
    change(time(res == 'Y' ? 'D' : res))

// Get some previous value from last HTF period.
pHi = security(syminfo.tickerid, htf, high[1], lookahead = barmerge.lookahead_on)
// Verify if current charts bars are part of the last dilation of HTF.
lastPBar = security(syminfo.tickerid, htf, barstate.islast, lookahead = barmerge.lookahead_on)
// Get avg no of chart bars in one dilation of HTF.
dilation = round(f_avgDilationOf(htf))
timeDelta = time - time[1]

var line pHiLine = na
// Holds bar index where a new line is created.
var pHiBar = 0
if pChange(htf)
    // Extend old line for the last bar before creating a new one.
    line.set_xy2(pHiLine, time, pHi[1])
    // Save bar index on transition.
    pHiBar := bar_index
    // Create new line.
    pHiLine := line.new(time, pHi, time + timeDelta, pHi, xloc.bar_time, color = color.black, width = 2)
    // Make type of the 2 `if` blocks the same.
    float(na)
else
    // We are not on a transition; prolong line until next transition.
    line.set_xy2(pHiLine, time, pHi)
    float(na)

// If we are in the last bars of the HTF resolution's dilation, project line into the future with remaining bars in average no of bars in dilation.
if lastPBar
    line.set_xy2(pHiLine, time + (timeDelta * (dilation - (bar_index - pHiBar))), pHi)

在此处输入图像描述

警告:尚未对此进行前向测试,因此不确定它在这些条件下的表现如何。


推荐阅读