user12847070
user12847070

Reputation: 31

Draw A Line From This Years Highest High To Today PineScript

I am learning pinescript and getting compile errors in my if statement "expecting end of line without line continuation."

What I want the code to do is create 2 lines

  1. From the highest high this year to the closing candle
  2. From the lowest low this year to the closing candle
@version=4
    strategy("Time Range", overlay=true)

    i_startTime=input(defval=timestamp("01 Jan 2021 13:30 +0000"), title="Start Time", type=input.time)
    i_endTime=input(defval=timestamp("01 Jul 2021 13:30 +0000"), title="End Time", type=input.time)
    i_length=input(defval=20, title= "Length", type=input.integer)
    
    inDateRange = time >= i_startTime and time <= i_endTime
    inCondition = not na(close[i_length])
    
    hh=highest(high,i_length)
    ll=lowest(low,i_length)
    
    if(inCondition and inDateRange)
        // Make both trend lines just once
    highLine = line.new(x1=bar_index[hh], y1=close[hh],
         x2=bar_index, y2=close, color=color.green,
         extend=extend.both)
    
    lowLine = line.new(x1=bar_index[ll], y1=close[ll],
         x2=bar_index, y2=close, color=color.red,
         extend=extend.both)

Upvotes: 0

Views: 521

Answers (1)

rumpypumpydumpy
rumpypumpydumpy

Reputation: 3803

First, it should be //@version=4 and you've unnecessarily indented all your code.

newYear = change(time("12M")) != 0

var float hh = na
var float ll = na
var int hhIndex = na
var int llIndex = na

var line hhLine = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.red)
var line llLine = line.new(x1 = na, y1 = na, x2 = na, y2 = na, color = color.green)


if newYear
    hh := high
    ll := low
    hhIndex := bar_index
    llIndex := bar_index
if not newYear
    if high > hh
        hh := high
        hhIndex := bar_index
        line.set_xy1(hhLine, x = hhIndex, y = hh)
        line.set_xy2(hhLine, x = hhIndex + 1, y = hh)
    if low < ll
        ll := low
        llIndex := bar_index
        line.set_xy1(llLine, x = llIndex, y = ll)
        line.set_xy2(llLine, x = llIndex + 1, y = ll)

if barstate.isconfirmed
    line.set_x2(hhLine, x = bar_index + 1)
    line.set_x2(llLine, x = bar_index + 1)

Be aware that lines don't load unless their origin coordinates are within a chunk of chart that has been loaded. Depending on your timeframe, this may mean they don't appear unless you pan back in time and new chunks are retrieved. In this case, it may be better to just use plot instead :

plot(hh)
plot(ll)

Upvotes: 1

Related Questions