0

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?

Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42

1 Answers1

1

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.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Thank you for your reply. Unfortunatly, I don't think this will work for what I need. I have data like this entered every trading day. So what I'm really looking for is a way to use a function that updates the same `lo` and `hi` set each day. I'm trying to have only two plot commands at the end of the script. One plot for `lo` and one plot for `hi` – Bjorn Mistiaen Apr 15 '20 at 07:59
  • 1
    ) That's another problem altogether. If that is what you needed, should've made that clear in your question. See [this post](https://stackoverflow.com/questions/61060015/edit-series-only-on-certain-dates). – PineCoders-LucF Apr 15 '20 at 14:24
  • Sorry if my question was unclear. The post you mention is a previous question of me, and it does what I want but only for 1 variable. I guess it cannot be done for 2 `var` variables at once then. Seems a bit odd that you can return 2 values from a function and assign them to `[lo,hi]` with a `=` but you cannot assign them with a `:=` if `lo` and `hi` are declared as `var`. – Bjorn Mistiaen Apr 15 '20 at 20:27