在 Pine Script 中未正确绘制历史最高值
All-time-high not plotting correctly in Pine Script
我正在尝试创建一个脚本,该脚本将给定交易品种的历史最高价显示为交易视图中的水平线。我已经弄清楚如何搜索历史最高点,但是当我尝试显示它时,它似乎显示的是之前的历史最高点。我已经添加了诊断输出,它似乎显示了我期望绘制的线的价格,但在实际图表上它绘制得更低。我做错了什么?
// © dickymoore
//@version=4
study("All-time-high", overlay=true)
// input
Athlw = input(title="All-time-high line widths", type=input.integer, defval=4, minval=0, maxval=4)
Athlc = input(title="All-time-high line color", type=input.color, defval=color.new(color.fuchsia,50))
years = input(title="Years back to search for an ATH", type=input.integer, defval=6,minval=0, maxval=100)
seriesForAth=security(syminfo.tickerid,"M",high,barmerge.gaps_off,barmerge.lookahead_off)
highestHigh = highest(seriesForAth,(12*years))
// Draw lines from the previous highs and lows
newSession = change(time('D'))
count = barssince(newSession)
var line AllTimeHigh = na
if (newSession)
AllTimeHigh := line.new(x1=bar_index[1],y1=highestHigh,
x2=bar_index,y2=highestHigh, extend=extend.both, color=Athlc,width=Athlw)
line.delete(id = AllTimeHigh[1])
//diagnostics
f_print(_text) =>
var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_left)
label.set_xy(_label, bar_index, highest(10)[1])
label.set_text(_label, _text)
f_print("Diagnostics \n" + "\nHigh = " + tostring(highestHigh))
这样更简单也更紧凑。
//@version=4
study("All-time-high", "ATH", overlay=true)
// input
Athlw = input(title="All-time-high line widths", type=input.integer, defval=4, minval=0, maxval=4)
Athlc = input(title="All-time-high line color", type=input.color, defval=color.new(color.fuchsia,50))
years = input(title="Years back to search for an ATH", type=input.integer, defval=6,minval=0, maxval=100)
var float highestHigh = 0
var line allTimeHigh = line.new(na, na, na, na, extend=extend.both, color=Athlc, width=Athlw)
if high > highestHigh
highestHigh := high
line.set_xy1(allTimeHigh, bar_index-1, highestHigh)
line.set_xy2(allTimeHigh, bar_index, highestHigh)
编辑 1
想想看,我们不需要在每个小节都移动线条。
我们可以只移动最后一个柱上的线。
//@version=4
study("All-time-high", "ATH", overlay=true)
// input
Athlw = input(title="All-time-high line widths", type=input.integer, defval=4, minval=0, maxval=4)
Athlc = input(title="All-time-high line color", type=input.color, defval=color.new(color.fuchsia,50))
years = input(title="Years back to search for an ATH", type=input.integer, defval=6,minval=0, maxval=100)
var float highestHigh = 0
var line allTimeHigh = line.new(na, na, na, na, extend=extend.both, color=Athlc, width=Athlw)
if high > highestHigh
highestHigh := high
if barstate.islast
line.set_xy1(allTimeHigh, bar_index-1, highestHigh)
line.set_xy2(allTimeHigh, bar_index, highestHigh)
我正在尝试创建一个脚本,该脚本将给定交易品种的历史最高价显示为交易视图中的水平线。我已经弄清楚如何搜索历史最高点,但是当我尝试显示它时,它似乎显示的是之前的历史最高点。我已经添加了诊断输出,它似乎显示了我期望绘制的线的价格,但在实际图表上它绘制得更低。我做错了什么?
// © dickymoore
//@version=4
study("All-time-high", overlay=true)
// input
Athlw = input(title="All-time-high line widths", type=input.integer, defval=4, minval=0, maxval=4)
Athlc = input(title="All-time-high line color", type=input.color, defval=color.new(color.fuchsia,50))
years = input(title="Years back to search for an ATH", type=input.integer, defval=6,minval=0, maxval=100)
seriesForAth=security(syminfo.tickerid,"M",high,barmerge.gaps_off,barmerge.lookahead_off)
highestHigh = highest(seriesForAth,(12*years))
// Draw lines from the previous highs and lows
newSession = change(time('D'))
count = barssince(newSession)
var line AllTimeHigh = na
if (newSession)
AllTimeHigh := line.new(x1=bar_index[1],y1=highestHigh,
x2=bar_index,y2=highestHigh, extend=extend.both, color=Athlc,width=Athlw)
line.delete(id = AllTimeHigh[1])
//diagnostics
f_print(_text) =>
var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_left)
label.set_xy(_label, bar_index, highest(10)[1])
label.set_text(_label, _text)
f_print("Diagnostics \n" + "\nHigh = " + tostring(highestHigh))
这样更简单也更紧凑。
//@version=4
study("All-time-high", "ATH", overlay=true)
// input
Athlw = input(title="All-time-high line widths", type=input.integer, defval=4, minval=0, maxval=4)
Athlc = input(title="All-time-high line color", type=input.color, defval=color.new(color.fuchsia,50))
years = input(title="Years back to search for an ATH", type=input.integer, defval=6,minval=0, maxval=100)
var float highestHigh = 0
var line allTimeHigh = line.new(na, na, na, na, extend=extend.both, color=Athlc, width=Athlw)
if high > highestHigh
highestHigh := high
line.set_xy1(allTimeHigh, bar_index-1, highestHigh)
line.set_xy2(allTimeHigh, bar_index, highestHigh)
编辑 1
想想看,我们不需要在每个小节都移动线条。
我们可以只移动最后一个柱上的线。
//@version=4
study("All-time-high", "ATH", overlay=true)
// input
Athlw = input(title="All-time-high line widths", type=input.integer, defval=4, minval=0, maxval=4)
Athlc = input(title="All-time-high line color", type=input.color, defval=color.new(color.fuchsia,50))
years = input(title="Years back to search for an ATH", type=input.integer, defval=6,minval=0, maxval=100)
var float highestHigh = 0
var line allTimeHigh = line.new(na, na, na, na, extend=extend.both, color=Athlc, width=Athlw)
if high > highestHigh
highestHigh := high
if barstate.islast
line.set_xy1(allTimeHigh, bar_index-1, highestHigh)
line.set_xy2(allTimeHigh, bar_index, highestHigh)