'Variable not becoming a series of values. Why?
I created this script to detect Swing Highs and Lows and then to determine the High and Low values of these Swings. The code works for the last bar, but I would like these values to be a series I can refer back to, but it's not. It's just retaining the last value. Not sure why. I included a picture of the indicator that includes this codeand a table in the corner with the last 5 swing high values....as you can see there are different swing highs but the same value in the table. Here is the code:
//Swing Highs and Lows
swing_detection(index)=>
swing_high = false
swing_low = false
start = (index*2) - 1 // -1 so we have an even number of
swing_point_high = high[index]
swing_point_low = low[index]
//Swing Highs
for i = 0 to start
swing_high := true
if i < index
if high[i] > swing_point_high
swing_high := false
break
// Have to do checks before pivot and after seperately because we can get
// two highs of the same value in a row. Notice the > and >= difference
if i > index
if high[i] >= swing_point_high
swing_high := false
break
//Swing lows
for i = 0 to start
swing_low := true
if i < index
if low[i] < swing_point_low
swing_low := false
break
// Have to do checks before pivot and after seperately because we can get
// two lows of the same value in a row. Notice the > and >= difference
if i > index
if low[i] <= swing_point_low
swing_low := false
break
[swing_high, swing_low]
// Check for a swing
[swing_high, swing_low] = swing_detection(barsback)
//Determine Swing High and Low Values
var float swing_high_price = na
var float swing_low_price = na
if swing_high
swing_high_price := high[barsback]
if swing_low
swing_low_price := low[barsback]
Solution 1:[1]
I don't see any code that explicitly builds a series/array so I suppose you expect swing_high_price
to be the series as you add value bar-by-bar (built by the Pine engine automatically).
However Variables defined with var
needs to be handled with care or you will put gaps into their series, on which Pine usually barfs.
Instead of the IF structure at the end, try this:
swing_high_price := swing_high ? high[barsback] : swing_high_price
That is, swing_high_price
will be always set on every bar. This will result in a continuous series, which keeps Pine happy.
Solution 2:[2]
Thank you. I got it working creating a function using valuewhen().
//Swing Highs and Lows
swing_detection(index)=>
swing_high = false
swing_low = false
start = (index*2) - 1 // -1 so we have an even number of
swing_point_high = high[index]
swing_point_low = low[index]
//Swing Highs
for i = 0 to start
swing_high := true
if i < index
if high[i] > swing_point_high
swing_high := false
break
// Have to do checks before pivot and after seperately because we can get
// two highs of the same value in a row. Notice the > and >= difference
if i > index
if high[i] >= swing_point_high
swing_high := false
break
//Swing lows
for i = 0 to start
swing_low := true
if i < index
if low[i] < swing_point_low
swing_low := false
break
// Have to do checks before pivot and after seperately because we can get
// two lows of the same value in a row. Notice the > and >= difference
if i > index
if low[i] <= swing_point_low
swing_low := false
break
[swing_high, swing_low]
// Check for a swing
[swing_high, swing_low] = swing_detection(barsback)
////////////////////////////////////////////////////////////////////////////////
swing_high_price(_vw) =>
_return=valuewhen(conf_swing_high, high[barsback],_vw)
swing_low_price(_vw) =>
_return=valuewhen(conf_swing_low, low[barsback],_vw)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | karatedog |
Solution 2 |