在 Pine Script 中画一条一定长度的线
Draw a line of certain length in 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)
此外,下面是我期望的行为 - 请注意较长的行:
这不是一件简单的事情,至少不是这里的做法)也许有更好的方法,但还没有想出任何:
- 您需要与
xloc = xloc.bar_time
合作,以便将来需要时能够画一条线。
- 使用 PineCoders MTF Selection Framework 中的
f_avgDilationOf()
了解较高 TF 扩张中图表柱的平均数量(在 24/7 市场中,日线图上为 7如果 HTF=1W).
- 确定当前图表柱(当前 是脚本正在执行的柱)是否包含在 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)
警告:尚未对此进行前向测试,因此不确定它在这些条件下的表现如何。
我正在尝试在 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)
此外,下面是我期望的行为 - 请注意较长的行:
这不是一件简单的事情,至少不是这里的做法)也许有更好的方法,但还没有想出任何:
- 您需要与
xloc = xloc.bar_time
合作,以便将来需要时能够画一条线。 - 使用 PineCoders MTF Selection Framework 中的
f_avgDilationOf()
了解较高 TF 扩张中图表柱的平均数量(在 24/7 市场中,日线图上为 7如果 HTF=1W). - 确定当前图表柱(当前 是脚本正在执行的柱)是否包含在 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)
警告:尚未对此进行前向测试,因此不确定它在这些条件下的表现如何。