Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6905

Set 2 series at once

I'm trying to set Highs and Lows in 2 series at the same time, but can't seem to get the code right.
It should plot the entered high and low values at every intraday bar, for the specified date.
The code is designed to work with ticker SPX.

//@version=4
study("SPX 5", overlay=true)

// === FUNCTIONS === 
isDate(y,m,d) => year==y and month==m and dayofmonth==d

float lo = na
float hi = na

drawHiLo(y,m,d,l,h) => 
    float ret1 = na
    float ret2 = na
    if isDate(y,m,d) and timeframe.isintraday
        ret1 := l
        ret2 := h 
    else
        ret1 = lo[1]
        ret2 = hi[1]
    [ret1,ret2]

// === MAIN ===
[lo,hi] = drawHiLo(2020,04,13,2700,2770)
[lo,hi] = drawHiLo(2020,04,14,2800,2860)

plot(lo, style=plot.style_circles, color=color.lime)
plot(hi, style=plot.style_circles, color=color.lime)

The above script gives the following error: line 22: 'lo' is already defined.

I've tried several different approaches, but none seem to work.
Does someone have an idea on how to accomplish this?

Upvotes: 0

Views: 549

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8789

You can now change the values through the script's inputs. Added a few comments in code. Feel free to ask if what's going isn't clear:

//@version=4
study("SPX 5", overlay=true)
// Get values through Inputs.
spHi1 = input(2770)
spLo1 = input(2700)
spHi2 = input(2860)
spLo2 = input(2800)

// === FUNCTIONS === 
isDate(y,m,d) => year==y and month==m and dayofmonth==d

drawHiLo(y,m,d,l,h) => 
    // Vars are initialized to na, so we only need to assign them a value when our conditions becomes true.
    float ret1 = na
    float ret2 = na
    if isDate(y,m,d) and timeframe.isintraday
        ret1 := l
        ret2 := h 
    [ret1, ret2]

// === MAIN ===
// Make 2 separate call to function, but need to store results in different variables for each call.
[lo1,hi1] = drawHiLo(2020,04,13,spLo1,spHi1)
[lo2,hi2] = drawHiLo(2020,04,14,spLo2,spHi2)

plot(hi1, style=plot.style_circles, color=color.lime)
plot(lo1, style=plot.style_circles, color=color.lime)
plot(hi2, style=plot.style_circles, color=color.green)
plot(lo2, style=plot.style_circles, color=color.green)

Nice concept with the function, btw. Congrats.

Upvotes: 1

Related Questions