Reputation: 996
I'm trying to create a script which shows the all-time-high of a given symbol as a horizontal line on Trading View. I have worked out how to search for the all-time-high but when I try to display it, it seems to display a previous all-time-high. I've added diagnostic output, which seems to show the price I would expect the line to be drawn, but on the actual chart it draws it lower. What am I doing wrong?
// © 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))
Upvotes: 0
Views: 622
Reputation: 6865
This is easier and more compact.
//@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)
Edit 1
Come to think of it, we don't need to move the line at every bar.
We can just move the line on the last bar.
//@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)
Upvotes: 2